spek

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

commit 00b385d6d76aa5a585668bb0cfc872f40d668af4
parent 2e8355ee84a298472c081759f5503832af432fa2
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Sun,  4 Jul 2010 13:08:37 +1000

Run pipeline in a separate thread

Diffstat:
Msrc/Makefile.am | 1+
Msrc/spek-pipeline.vala | 34++++++++++++++++++++++++++++++++++
Msrc/spek-spectrogram.vala | 4++--
3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am @@ -18,6 +18,7 @@ INCLUDES = \ -DPKGLIBDIR=\""$(pkglibdir)"\" VALAFLAGS = \ + --thread \ --vapidir=$(srcdir)/../vapi \ --pkg config \ --pkg spek-audio \ diff --git a/src/spek-pipeline.vala b/src/spek-pipeline.vala @@ -42,6 +42,9 @@ namespace Spek { private float[] input; private float[] output; + private unowned Thread thread = null; + private bool quit = false; + public Pipeline (string file_name, int bands, int samples, int threshold, Callback cb) { this.cx = new Audio.Context (file_name); this.bands = bands; @@ -85,7 +88,31 @@ namespace Spek { this.cx.start (samples); } + ~Pipeline () { + stop (); + } + public void start () { + stop (); + + try { + thread = Thread.create (thread_func, true); + thread.set_priority (ThreadPriority.LOW); + } catch (ThreadError e) { + assert_not_reached (); + } + } + + public void stop () { + if (thread != null) { + quit = true; + thread.join (); + quit = false; + thread = null; + } + } + + private void * thread_func () { int pos = 0; int sample = 0; int64 frames = 0; @@ -99,6 +126,9 @@ namespace Spek { uint8 *buffer = (uint8 *) this.buffer; var block_size = cx.width * cx.channels / 8; while (size >= block_size) { + if (quit) { + return null; + } input[pos] = average_input (buffer); buffer += block_size; size -= block_size; @@ -139,6 +169,9 @@ namespace Spek { } cb (sample++, output); + if (sample == samples) { + return null; + } Posix.memset (output, 0, sizeof (float) * bands); frames = 0; num_fft = 0; @@ -146,6 +179,7 @@ namespace Spek { } assert (size == 0); } + return null; } private float average_input (uint8 *buffer) { diff --git a/src/spek-spectrogram.vala b/src/spek-spectrogram.vala @@ -74,7 +74,7 @@ namespace Spek { private void start () { if (pipeline != null) { -// pipeline.stop (); + pipeline.stop (); } // The number of samples is the number of pixels available for the image. @@ -108,7 +108,7 @@ namespace Spek { 1.0, Math.log10 (1.0 - THRESHOLD + values[y]) / Math.log10 (-THRESHOLD)); put_pixel (image, sample, y, get_color (level)); } - queue_draw_area (LPAD + sample, TPAD, 1, allocation.height - TPAD - BPAD); + Idle.add (() => { queue_draw (); return false; }); } private override bool expose_event (EventExpose event) {