spek

Acoustic spectrum analyser
git clone http://git.hanabi.in/repos/spek.git
Log | Files | Refs | README

commit 98b2cb0ab868bcae5a8cbce73672b3ef435ca4f5
parent f81ce84826b48f48c655bcf70343d99eb88bac74
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Wed, 15 Aug 2012 09:46:50 -0700

Draw the spectrogram

Diffstat:
Msrc/spek-pipeline.c | 4+++-
Msrc/spek-spectrogram.cc | 29++++++++++++++++++++++++-----
Msrc/spek-spectrogram.hh | 2++
Msrc/spek-spectrogram.vala | 19-------------------
4 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/src/spek-pipeline.c b/src/spek-pipeline.c @@ -115,6 +115,8 @@ struct spek_pipeline * spek_pipeline_open( p->output = malloc(bands * sizeof(float)); spek_audio_start(p->cx, samples); } + + return p; } const struct spek_audio_properties * spek_pipeline_properties(struct spek_pipeline *pipeline) @@ -124,7 +126,7 @@ const struct spek_audio_properties * spek_pipeline_properties(struct spek_pipeli void spek_pipeline_start(struct spek_pipeline *p) { - if (!p->properties->error) return; + if (p->properties->error) return; p->input_pos = 0; p->worker_done = false; diff --git a/src/spek-spectrogram.cc b/src/spek-spectrogram.cc @@ -26,6 +26,7 @@ BEGIN_EVENT_TABLE(SpekSpectrogram, wxPanel) EVT_PAINT(SpekSpectrogram::on_paint) + EVT_SIZE(SpekSpectrogram::on_size) END_EVENT_TABLE() enum @@ -45,7 +46,8 @@ SpekSpectrogram::SpekSpectrogram(wxFrame *parent) : wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE), pipeline(NULL), palette(RULER, BANDS), - image() + image(1, 1), + prev_width(-1) { SetBackgroundStyle(wxBG_STYLE_CUSTOM); @@ -77,6 +79,17 @@ void SpekSpectrogram::on_paint(wxPaintEvent& evt) render(dc); } +void SpekSpectrogram::on_size(wxSizeEvent& evt) +{ + wxSize size = GetClientSize(); + bool width_changed = this->prev_width != size.GetWidth(); + this->prev_width = size.GetWidth(); + + if (!this->path.IsEmpty() && width_changed) { + start(); + } +} + void SpekSpectrogram::render(wxDC& dc) { wxSize size = GetClientSize(); @@ -119,6 +132,12 @@ void SpekSpectrogram::render(wxDC& dc) TPAD - 2 * GAP - 2 * normal_height ); + if (this->image.GetHeight() > 1) { + // Draw the spectrogram. + wxBitmap bmp(this->image.Scale(w - LPAD - RPAD, h - TPAD - BPAD /*TODO:, wxIMAGE_QUALITY_HIGH*/)); + dc.DrawBitmap(bmp, LPAD, TPAD); + } + // Border around the spectrogram. // TODO: check if this uses antialiasing dc.DrawRectangle(LPAD, TPAD, w - LPAD - RPAD, h - TPAD - BPAD); @@ -139,20 +158,20 @@ void SpekSpectrogram::pipeline_cb(int sample, float *values, void *cb_data) uint32_t color = get_color(level); s->image.SetRGB( sample, - y, + BANDS - y - 1, color >> 16, (color >> 8) & 0xFF, color & 0xFF ); } - s->Refresh(false); // TODO: refresh only one pixel column + s->Refresh(); // TODO: refresh only one pixel column } void SpekSpectrogram::start() { - if(this->pipeline) { + if (this->pipeline) { spek_pipeline_close(this->pipeline); this->pipeline = NULL; } @@ -178,7 +197,7 @@ void SpekSpectrogram::start() this->image.Create(1, 1); } - Refresh(false); + Refresh(); } // Modified version of Dan Bruton's algorithm: diff --git a/src/spek-spectrogram.hh b/src/spek-spectrogram.hh @@ -32,6 +32,7 @@ public: private: void on_paint(wxPaintEvent& evt); + void on_size(wxSizeEvent& evt); void render(wxDC& dc); void start(); @@ -43,6 +44,7 @@ private: wxString desc; wxImage palette; wxImage image; + int prev_width; DECLARE_EVENT_TABLE() }; diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala @@ -36,18 +36,6 @@ namespace Spek { surface.write_to_png (file_name); } - private int prev_width = -1; - protected override void size_allocate (Gdk.Rectangle allocation) { - base.size_allocate (allocation); - - bool width_changed = prev_width != allocation.width; - prev_width = allocation.width; - - if (file_name != null && width_changed) { - start (); - } - } - protected override bool expose_event (EventExpose event) { var window = get_window (); var cr = cairo_create (window); @@ -64,13 +52,6 @@ namespace Spek { int text_width, text_height; if (image != null) { - // Draw the spectrogram. - cr.translate (LPAD, h - BPAD); - cr.scale (1, -(h - TPAD - BPAD) / image.get_height ()); - cr.set_source_surface (image, 0, 0); - cr.paint (); - cr.identity_matrix (); - // Prepare to draw the rulers. cr.set_source_rgb (1, 1, 1); cr.set_line_width (1);