mirror of
https://github.com/alexkay/spek.git
synced 2025-04-16 16:42:19 +03:00
Add a callback to the source
This commit is contained in:
parent
65fcc182e1
commit
28ade26079
@ -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;
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user