Factor out the spectrogram image

This commit is contained in:
Alexander Kojevnikov 2010-05-06 18:50:14 +10:00
parent 491db7c663
commit 22c477b4a2
3 changed files with 38 additions and 31 deletions

@ -3,6 +3,7 @@ bin_PROGRAMS = spek
spek_SOURCES = \
spek.vala \
spek-source.vala \
spek-spectrogram.vala \
spek-window.vala
INCLUDES = \

33
src/spek-spectrogram.vala Normal file

@ -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);
}
}
}

@ -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);
}
}
}