Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to record from default microphone. #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ AC_CHECK_LIB(m, log10)
PKG_CHECK_MODULES(AVFORMAT, [libavformat >= 53.17])
PKG_CHECK_MODULES(AVCODEC, [libavcodec >= 55.46])
PKG_CHECK_MODULES(AVUTIL, [libavutil >= 51.17])
PKG_CHECK_MODULES(AVDEVICE, [libavdevice >= 55.0])

AM_OPTIONS_WXCONFIG
reqwx=3.0.0
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ libspek_a_CXXFLAGS = \
$(AVFORMAT_CFLAGS) \
$(AVCODEC_CFLAGS) \
$(AVUTIL_CFLAGS) \
$(AVDEVICE_CFLAGS) \
$(WX_CXXFLAGS_ONLY)

bin_PROGRAMS = spek
Expand Down Expand Up @@ -57,6 +58,7 @@ spek_LDADD = \
$(AVFORMAT_LIBS) \
$(AVCODEC_LIBS) \
$(AVUTIL_LIBS) \
$(AVDEVICE_LIBS) \
$(WX_LIBS)

spek_LDFLAGS = \
Expand Down
32 changes: 29 additions & 3 deletions src/spek-audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/mathematics.h>
#include <libavdevice/avdevice.h>
}

#include "spek-audio.h"
Expand Down Expand Up @@ -72,13 +73,32 @@ Audio::~Audio()
av_lockmgr_register(nullptr);
}

std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, int stream)
std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, const std::string& device_name, int stream)
{
AudioError error = AudioError::OK;

static bool registered;
if (!registered) {
avdevice_register_all();
registered = true;
}

const char* file_or_device = file_name.c_str();
AVInputFormat *file_iformat = nullptr;
if (!device_name.empty()) {
file_or_device = "default";
file_iformat = av_find_input_format("alsa");
if (!file_iformat) {
error = AudioError::CANNOT_OPEN_DEVICE;
} else {
}
}

AVFormatContext *format_context = nullptr;
if (avformat_open_input(&format_context, file_name.c_str(), nullptr, nullptr) != 0) {
error = AudioError::CANNOT_OPEN_FILE;
if (!error) {
if (avformat_open_input(&format_context, file_or_device, file_iformat, nullptr) != 0) {
error = AudioError::CANNOT_OPEN_FILE;
}
}

if (!error && avformat_find_stream_info(format_context, nullptr) < 0) {
Expand Down Expand Up @@ -149,6 +169,8 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, int stream)
duration = avstream->duration * av_q2d(avstream->time_base);
} else if (format_context->duration != AV_NOPTS_VALUE) {
duration = format_context->duration / (double) AV_TIME_BASE;
} else if (!device_name.empty()) {
duration = 60; /* seconds visible */
} else {
error = AudioError::NO_DURATION;
}
Expand All @@ -172,6 +194,10 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, int stream)
}
}

if (format_context) {
av_dump_format(format_context, 0, file_name.c_str(), false);
}

return std::unique_ptr<AudioFile>(new AudioFileImpl(
error, format_context, audio_stream,
codec_name, bit_rate, sample_rate, bits_per_sample,
Expand Down
3 changes: 2 additions & 1 deletion src/spek-audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Audio
Audio();
~Audio();

std::unique_ptr<AudioFile> open(const std::string& file_name, int stream);
std::unique_ptr<AudioFile> open(const std::string& file_name, const std::string& device_name, int stream);
};

class AudioFile
Expand Down Expand Up @@ -42,6 +42,7 @@ enum class AudioError
{
OK,
CANNOT_OPEN_FILE,
CANNOT_OPEN_DEVICE,
NO_STREAMS,
NO_AUDIO,
NO_DECODER,
Expand Down
4 changes: 3 additions & 1 deletion src/spek-pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ std::string spek_pipeline_desc(const struct spek_pipeline *pipeline)
case AudioError::CANNOT_OPEN_FILE:
error = _("Cannot open input file");
break;
case AudioError::CANNOT_OPEN_DEVICE:
error = _("Cannot open input device");
break;
case AudioError::NO_STREAMS:
error = _("Cannot find stream info");
break;
Expand Down Expand Up @@ -453,7 +456,6 @@ static void * worker_func(void *pp)
p->output[i] /= num_fft;
}

if (sample == p->samples) break;
p->cb(p->fft->get_output_size(), sample++, p->output, p->cb_data);

memset(p->output, 0, sizeof(float) * p->fft->get_output_size());
Expand Down
39 changes: 29 additions & 10 deletions src/spek-spectrogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ SpekSpectrogram::~SpekSpectrogram()
this->stop();
}

void SpekSpectrogram::open(const wxString& path)
void SpekSpectrogram::open(const wxString& path, const wxString& device)
{
this->path = path;
this->device = device;
this->stream = 0;
this->channel = 0;
start();
Expand Down Expand Up @@ -192,13 +193,29 @@ void SpekSpectrogram::on_have_sample(SpekHaveSampleEvent& event)
double value = fmin(this->urange, fmax(this->lrange, values[y]));
double level = (value - this->lrange) / range;
uint32_t color = spek_palette(this->palette, level);
this->image.SetRGB(
sample,
bands - y - 1,
color >> 16,
(color >> 8) & 0xFF,
color & 0xFF
);
int draw_sample = sample;
if (!device.IsEmpty()) {
draw_sample = sample % (this->image.GetWidth()-1);
}
if (draw_sample >= 0 && draw_sample < this->image.GetWidth()) {
this->image.SetRGB(
draw_sample,
bands - y - 1,
color >> 16,
(color >> 8) & 0xFF,
color & 0xFF
);
}
if (!device.IsEmpty()) {
draw_sample = (sample+1) % (this->image.GetWidth()-1);
this->image.SetRGB(
draw_sample,
bands - y - 1,
0xFF,
0xFF,
0xFF
);
}
}

// TODO: refresh only one pixel column
Expand Down Expand Up @@ -377,7 +394,8 @@ static void pipeline_cb(int bands, int sample, float *values, void *cb_data)

void SpekSpectrogram::start()
{
if (this->path.IsEmpty()) {
wxLogMessage("SpekSpectrogram::start");
if (this->path.IsEmpty() && this->device.IsEmpty()) {
return;
}

Expand All @@ -391,7 +409,7 @@ void SpekSpectrogram::start()
if (samples > 0) {
this->image.Create(samples, bits_to_bands(this->fft_bits));
this->pipeline = spek_pipeline_open(
this->audio->open(std::string(this->path.utf8_str()), this->stream),
this->audio->open(std::string(this->path.utf8_str()), std::string(this->device.utf8_str()), this->stream),
this->fft->create(this->fft_bits),
this->stream,
this->channel,
Expand All @@ -414,6 +432,7 @@ void SpekSpectrogram::start()

void SpekSpectrogram::stop()
{
wxLogMessage("SpekSpectrogram::stop");
if (this->pipeline) {
spek_pipeline_close(this->pipeline);
this->pipeline = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/spek-spectrogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SpekSpectrogram : public wxWindow
public:
SpekSpectrogram(wxFrame *parent);
~SpekSpectrogram();
void open(const wxString& path);
void open(const wxString& path, const wxString& device);
void save(const wxString& path);

private:
Expand All @@ -41,6 +41,7 @@ class SpekSpectrogram : public wxWindow
int channel;
enum window_function window_function;
wxString path;
wxString device;
wxString desc;
double duration;
int sample_rate;
Expand Down
19 changes: 10 additions & 9 deletions src/spek-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SpekDropTarget : public wxFileDropTarget
protected:
virtual bool OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames){
if (filenames.GetCount() == 1) {
window->open(filenames[0]);
window->open(filenames[0], "");
return true;
}
return false;
Expand All @@ -51,8 +51,8 @@ class SpekDropTarget : public wxFileDropTarget
SpekWindow *window;
};

SpekWindow::SpekWindow(const wxString& path) :
wxFrame(NULL, -1, wxEmptyString, wxDefaultPosition, wxSize(640, 480)), path(path)
SpekWindow::SpekWindow(const wxString& path, const wxString& device) :
wxFrame(NULL, -1, wxEmptyString, wxDefaultPosition, wxSize(640, 480)), path(path), device(device)
{
this->description = _("Spek - Acoustic Spectrum Analyser");
SetTitle(this->description);
Expand Down Expand Up @@ -139,8 +139,8 @@ SpekWindow::SpekWindow(const wxString& path) :

this->cur_dir = wxGetHomeDir();

if (!path.IsEmpty()) {
open(path);
if (!path.IsEmpty() || !device.IsEmpty()) {
open(path, device);
}

SetDropTarget(new SpekDropTarget(this));
Expand All @@ -151,17 +151,18 @@ SpekWindow::SpekWindow(const wxString& path) :
pthread_create(&thread, NULL, &check_version, this);
}

void SpekWindow::open(const wxString& path)
void SpekWindow::open(const wxString& path, const wxString& device)
{
wxFileName file_name(path);
if (file_name.FileExists()) {
if (file_name.FileExists() || !device.IsEmpty()) {
this->path = path;
this->device = device;
wxString full_name = file_name.GetFullName();
// TRANSLATORS: window title, %s is replaced with the file name
wxString title = wxString::Format(_("Spek - %s"), full_name.c_str());
SetTitle(title);

this->spectrogram->open(path);
this->spectrogram->open(path, device);
}
}

Expand Down Expand Up @@ -233,7 +234,7 @@ void SpekWindow::on_open(wxCommandEvent&)
if (dlg->ShowModal() == wxID_OK) {
this->cur_dir = dlg->GetDirectory();
filter_index = dlg->GetFilterIndex();
open(dlg->GetPath());
open(dlg->GetPath(), "");
}

dlg->Destroy();
Expand Down
5 changes: 3 additions & 2 deletions src/spek-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class SpekSpectrogram;
class SpekWindow : public wxFrame
{
public:
SpekWindow(const wxString& path);
void open(const wxString& path);
SpekWindow(const wxString& path, const wxString& device);
void open(const wxString& path, const wxString& device);

private:
void on_open(wxCommandEvent& event);
Expand All @@ -23,6 +23,7 @@ class SpekWindow : public wxFrame

SpekSpectrogram *spectrogram;
wxString path;
wxString device;
wxString cur_dir;
wxString description;

Expand Down
14 changes: 13 additions & 1 deletion src/spek.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class Spek: public wxApp
private:
SpekWindow *window;
wxString path;
wxString device;
bool quit;
};

IMPLEMENT_APP(Spek)

bool Spek::OnInit()
{
delete wxLog::SetActiveTarget(new wxLogStderr()) ;
wxInitAllImageHandlers();
wxSocketBase::Initialize();

Expand All @@ -51,6 +53,13 @@ bool Spek::OnInit()
"Display the version and exit",
wxCMD_LINE_VAL_NONE,
wxCMD_LINE_PARAM_OPTIONAL,
}, {
wxCMD_LINE_SWITCH,
NULL,
"microphone",
"Show continuous graph recorded from mircophone",
wxCMD_LINE_VAL_NONE,
wxCMD_LINE_PARAM_OPTIONAL,
}, {
wxCMD_LINE_PARAM,
NULL,
Expand Down Expand Up @@ -78,11 +87,14 @@ bool Spek::OnInit()
this->quit = true;
return true;
}
if (parser.Found("microphone")) {
this->device = "default";
}
if (parser.GetParamCount()) {
this->path = parser.GetParam();
}

this->window = new SpekWindow(this->path);
this->window = new SpekWindow(this->path, this->device);
this->window->Show(true);
SetTopWindow(this->window);
return true;
Expand Down