commit 28ade26079755807921599ecb47aa9af9c8d6273
parent 65fcc182e136965e4c80dbe3df8bf9c560eead83
Author: Alexander Kojevnikov <alexander@kojevnikov.com>
Date:   Tue,  4 May 2010 23:16:41 +1000
Add a callback to the source
Diffstat:
2 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/src/spek-source.vala b/src/spek-source.vala
@@ -6,14 +6,20 @@ namespace Spek {
 		public string file_name { get; construct; }
 		public int bands { get; construct; }
 		public int samples { get; construct; }
+		// TODO: file a bug, cannot s/set/construct/
+		public Callback callback {get; set; }
+
+		public delegate void Callback (int sample, float[] values);
 
 		private Pipeline pipeline = null;
 		private Element spectrum = null;
 		private Pad pad = null;
-		private static int sample = 0;
+		private int sample;
+		private float[] values;
 
-		public Source (string file_name, int bands, int samples) {
+		public Source (string file_name, int bands, int samples, Callback callback) {
 			GLib.Object (file_name: file_name, bands: bands, samples: samples);
+			this.callback = callback;
 		}
 
 		~Source () {
@@ -21,6 +27,9 @@ namespace Spek {
 		}
 
 		construct {
+			sample = 0;
+			values = new float[bands];
+
 			// TODO: catch errors
 			pipeline = new Pipeline ("pipeline");
 			var filesrc = ElementFactory.make ("filesrc", null);
@@ -75,16 +84,14 @@ namespace Spek {
 			fakesink.set_state (State.PAUSED);
 		}
 
-		private static bool on_bus_watch (Bus bus, Message message) {
+		private bool on_bus_watch (Bus bus, Message message) {
 			var structure = message.get_structure ();
 			if (message.type == MessageType.ELEMENT && structure.get_name () == "spectrum") {
-				stdout.printf ("%d:", sample++);
 				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 ());
+				for (int i = 0; i < bands; i++) {
+					values[i] = magnitudes.list_get_value (i).get_float ();
 				}
-				stdout.printf ("\n");
+				callback (sample++, values);
 			}
 			return true;
 		}
diff --git a/src/spek-window.vala b/src/spek-window.vala
@@ -30,9 +30,17 @@ namespace Spek {
 				STOCK_CANCEL, ResponseType.CANCEL,
 				STOCK_OPEN, ResponseType.ACCEPT, null);
 			if (chooser.run () == ResponseType.ACCEPT) {
-				source = new Source (chooser.get_filename (), 10, 100);
+				source = new Source (chooser.get_filename (), 10, 100, source_callback);
 			}
 			chooser.destroy ();
 		}
+
+		private void source_callback (int sample, float[] values) {
+			stdout.printf ("%d:", sample);
+			foreach (var value in values) {
+				stdout.printf (" %.2f", value);
+			}
+			stdout.printf ("\n");
+		}
 	}
 }