spek

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

commit 81373dd7cc0dced4157e5e9ce0ae373c40f9b12b
parent 761287eb7affe19883aeb0860cecd90d9f685329
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Tue,  4 May 2010 19:33:03 +1000

Factor out gst-specific code

Diffstat:
Msrc/Makefile.am | 1+
Asrc/spek-source.vala | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/spek-window.vala | 70+++-------------------------------------------------------------------
3 files changed, 80 insertions(+), 67 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am @@ -2,6 +2,7 @@ bin_PROGRAMS = spek spek_SOURCES = \ spek.vala \ + spek-source.vala \ spek-window.vala INCLUDES = \ diff --git a/src/spek-source.vala b/src/spek-source.vala @@ -0,0 +1,75 @@ +using Gst; + +namespace Spek { + public class Source { + + private Pipeline pipeline; + + public Source (string file_name) { + pipeline = new Pipeline ("pipeline"); + var filesrc = ElementFactory.make ("filesrc", null); + var decodebin = ElementFactory.make ("decodebin", null); + pipeline.add_many (filesrc, decodebin); + filesrc.link (decodebin); + filesrc.set ("location", file_name); + + Signal.connect (decodebin, "new-decoded-pad", (GLib.Callback) on_new_decoded_pad, pipeline); + + pipeline.get_bus ().add_watch (on_bus_watch); + + if (StateChangeReturn.ASYNC == pipeline.set_state (State.PLAYING)) { + pipeline.get_state (null, null, -1); + } + + /* + unowned Iterator it = decodebin.iterate_src_pads (); + void *data; + while (it.next (out data) == IteratorResult.OK) { + var pad = (Pad) data; + var caps = pad.get_caps (); + var structure = caps.get_structure (0); + stdout.printf ("structure=%s\n", structure.to_string ()); + } + */ + } + + private static void on_new_decoded_pad ( + Element decodebin, Pad new_pad, bool last, Pipeline pipeline) { + var spectrum = ElementFactory.make ("spectrum", null); + pipeline.add (spectrum); + var sinkpad = spectrum.get_static_pad ("sink"); + new_pad.link (sinkpad); + spectrum.set ("bands", 10); + spectrum.set ("interval", 1000000000); // 1 sec + spectrum.set ("threshold", -99); + spectrum.set ("message-magnitude", true); + spectrum.set ("post-messages", true); + spectrum.set_state (State.PAUSED); + + var fakesink = ElementFactory.make ("fakesink", null); + pipeline.add (fakesink); + spectrum.link (fakesink); + fakesink.set_state (State.PAUSED); + } + + private static bool on_bus_watch (Bus bus, Message message) { + var structure = message.get_structure (); + if (message.type == MessageType.ELEMENT && + structure.get_name () == "spectrum") { + + ClockTime endtime = 0; + // TODO: binding must be fixed: `out endtime` + structure.get_clock_time ("endtime", (ClockTime) (void *) (&endtime)); + stdout.printf ("%d:", endtime / 1000000000); + + var magnitudes = structure.get_value ("magnitude"); + for (int i=0; i<10; i++) { + var mag = magnitudes.list_get_value (i); + stdout.printf (" %.2f", mag.get_float ()); + } + stdout.printf ("\n"); + } + return true; + } + } +} +\ No newline at end of file diff --git a/src/spek-window.vala b/src/spek-window.vala @@ -1,9 +1,10 @@ -using Gst; using Gtk; namespace Spek { public class Window : Gtk.Window { + private Source source; + public Window () { this.title = Config.PACKAGE_STRING; this.set_default_size (300, 200); @@ -29,74 +30,9 @@ namespace Spek { STOCK_CANCEL, ResponseType.CANCEL, STOCK_OPEN, ResponseType.ACCEPT, null); if (chooser.run () == ResponseType.ACCEPT) { - open (chooser.get_filename ()); + source = new Source (chooser.get_filename ()); } chooser.destroy (); } - - private void open (string name) { - var pipeline = new Pipeline ("pipeline"); - var filesrc = ElementFactory.make ("filesrc", null); - var decodebin = ElementFactory.make ("decodebin", null); - pipeline.add_many (filesrc, decodebin); - filesrc.link (decodebin); - filesrc.set ("location", name); - - Signal.connect (decodebin, "new-decoded-pad", (GLib.Callback) on_new_decoded_pad, pipeline); - - pipeline.get_bus ().add_watch (on_watch); - - if (StateChangeReturn.ASYNC == pipeline.set_state (State.PLAYING)) { - pipeline.get_state (null, null, -1); - } - Thread.usleep (1000000); - - unowned Iterator it = decodebin.iterate_src_pads (); - void *data; - while (it.next (out data) == IteratorResult.OK) { - var pad = (Pad) data; - var caps = pad.get_caps (); - var structure = caps.get_structure (0); - stdout.printf ("structure=%s\n", structure.to_string ()); - } - } - - private static bool on_watch (Bus bus, Message message) { - var structure = message.get_structure (); - if (message.type == Gst.MessageType.ELEMENT && - structure.get_name () == "spectrum") { - // TODO: binding must be fixed: `out endtime` - ClockTime endtime = 0; - void *p = &endtime; - structure.get_clock_time ("endtime", (ClockTime) p); - stdout.printf ("%d:", endtime / 1000000000); - - var magnitudes = structure.get_value ("magnitude"); - for (int i=0; i<10; i++) { - var mag = magnitudes.list_get_value (i); - stdout.printf (" %.2f", mag.get_float ()); - } - stdout.printf ("\n"); - } - return true; - } - - private static void on_new_decoded_pad ( - Element decodebin, Pad new_pad, bool last, Pipeline pipeline) { - var spectrum = ElementFactory.make ("spectrum", null); - pipeline.add (spectrum); - var sinkpad = spectrum.get_static_pad ("sink"); - new_pad.link (sinkpad); - spectrum.set ("bands", 10); - spectrum.set ("interval", 1000000000); // 1 sec - spectrum.set ("message-magnitude", true); - spectrum.set ("post-messages", true); - spectrum.set_state (State.PAUSED); - - var fakesink = ElementFactory.make ("fakesink", null); - pipeline.add (fakesink); - spectrum.link (fakesink); - fakesink.set_state (State.PAUSED); - } } }