spek

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

commit 22c477b4a2423640c2ed1da6bc2cf9f2f6c1cc42
parent 491db7c66360362ead0d5da2a0017547e8cde350
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Thu,  6 May 2010 18:50:14 +1000

Factor out the spectrogram image

Diffstat:
Msrc/Makefile.am | 1+
Asrc/spek-spectrogram.vala | 34++++++++++++++++++++++++++++++++++
Msrc/spek-window.vala | 35++++-------------------------------
3 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am @@ -3,6 +3,7 @@ bin_PROGRAMS = spek spek_SOURCES = \ spek.vala \ spek-source.vala \ + spek-spectrogram.vala \ spek-window.vala INCLUDES = \ diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala @@ -0,0 +1,33 @@ +using Gdk; +using Gtk; + +namespace Spek { + class Spectrogram : Gtk.Image { + + private Source source; + + public Spectrogram () { + } + + public void show_file (string file_name) { + pixbuf = new Pixbuf (Colorspace.RGB, false, 8, allocation.width, allocation.height); + pixbuf.fill (0); + source = new Source (file_name, allocation.height, allocation.width, source_callback); + } + + private void source_callback (int sample, float[] values) { + var x = sample; + var rowstride = pixbuf.rowstride; + unowned uchar[] pixels = pixbuf.get_pixels (); + for (int y = 0; y < values.length; y++) { + var i = (values.length - y - 1) * rowstride + x * 3; + var level = float.min (1f, Math.log10f (101f + values[y]) / 2f); + var value = (uchar) (level * 255f); + pixels[i] = value; + pixels[i + 1] = value; + pixels[i + 2] = value; + } + queue_draw_area (allocation.x + sample, allocation.y, 1, allocation.height); + } + } +} +\ No newline at end of file diff --git a/src/spek-window.vala b/src/spek-window.vala @@ -1,11 +1,9 @@ -using Gdk; using Gtk; namespace Spek { public class Window : Gtk.Window { - private Source source; - private Gtk.Image image; + private Spectrogram spectrogram; public Window () { this.title = Config.PACKAGE_STRING; @@ -20,12 +18,11 @@ namespace Spek { quit.clicked.connect (s => this.destroy()); toolbar.insert (quit, -1); - image = new Gtk.Image (); - image.size_allocate.connect (on_image_size_allocate); + spectrogram = new Spectrogram (); var vbox = new VBox (false, 0); vbox.pack_start (toolbar, false, true, 0); - vbox.pack_start (image, true, true, 0); + vbox.pack_start (spectrogram, true, true, 0); this.add (vbox); this.show_all (); } @@ -36,33 +33,9 @@ namespace Spek { STOCK_CANCEL, ResponseType.CANCEL, STOCK_OPEN, ResponseType.ACCEPT, null); if (chooser.run () == ResponseType.ACCEPT) { - var width = image.allocation.width; - var height = image.allocation.height; - image.pixbuf = new Pixbuf (Colorspace.RGB, false, 8, width, height); - // TODO: clear the pixbuf - source = new Source (chooser.get_filename (), height, width, source_callback); + spectrogram.show_file (chooser.get_filename ()); } chooser.destroy (); } - - private void on_image_size_allocate (Rectangle allocation) { - // TODO: re-create the source - } - - private void source_callback (int sample, float[] values) { - var x = sample; - unowned uchar[] pixels = image.pixbuf.get_pixels (); - var rowstride = image.pixbuf.rowstride; - for (int y = 0; y < values.length; y++) { - var i = (values.length - y - 1) * rowstride + x * 3; - var level = float.min (1f, Math.log10f (101f + values[y]) / 2f); - var value = (uchar) (level * 255); - pixels[i] = value; - pixels[i + 1] = value; - pixels[i + 2] = value; - } - var allocation = image.allocation; - image.queue_draw_area (allocation.x + x, allocation.y, 1, allocation.height); - } } }