[seed] N-Oscillator: Rewrite, with just one global pipeline.



commit 375666499901018c3ffd679626f9b36c591f97ac
Author: Tim Horton <hortont svn gnome org>
Date:   Tue May 12 02:49:42 2009 -0400

    N-Oscillator: Rewrite, with just one global pipeline.
---
 examples/n-oscillator.js |  160 +++++++++++++++++++++++++--------------------
 1 files changed, 89 insertions(+), 71 deletions(-)

diff --git a/examples/n-oscillator.js b/examples/n-oscillator.js
index 4da1fbf..812963f 100755
--- a/examples/n-oscillator.js
+++ b/examples/n-oscillator.js
@@ -1,96 +1,114 @@
 #!/usr/bin/env seed
+
 Gtk = imports.gi.Gtk;
 Gst = imports.gi.Gst;
+GObject = imports.gi.GObject;
 
 Gst.init(Seed.argv);
 Gtk.init(Seed.argv);
 
-// This is a really ugly program. Please fix it.
-
-function oscillator(freq){
-    this.vbox = new Gtk.VBox();
-    var hbox = new Gtk.HBox();
-    var vscale = new Gtk.VScale();
-    var volscale = new Gtk.VScale();
-    var button = new Gtk.Button({label: "Toggle"});
-
-    var pipeline = new Gst.Pipeline({name: "test"});
-    // No actual introspection data for audiotestsrc, so can not
-    // instantiate one with a constructor, have to use element_factory,
-    // likewise for the others.
-    var audiosrc = Gst.ElementFactory.make("audiotestsrc", "audio");
-    var audiosink = Gst.ElementFactory.make("alsasink", "sink");
-    var volume = Gst.ElementFactory.make("volume", "vcontrol");
-    audiosrc.freq = freq;
-
-    pipeline.add(audiosrc);
-    pipeline.add(audiosink);
-    pipeline.add(volume);
-    audiosrc.link(volume);
-    volume.link(audiosink);
-
-    var playing = false;
-
-    vscale.adjustment.upper = 3000;
-    vscale.adjustment.value = freq;
-
-    volscale.adjustment.upper = 10;
-    volscale.adjustment.value = volume.volume;
-
-    hbox.pack_start(vscale, true, true, 10);
-    hbox.pack_start(volscale, true, true, 10);
-    this.vbox.pack_start(hbox, true, true, 10);
-    this.vbox.pack_start(button, false, false, 10);
-
-    var toggle = function(button, that){
-	if (playing === false){
-	    pipeline.set_state(Gst.State.PLAYING);
-	    playing = true;
+var pipeline = new Gst.Pipeline({name: "test"});
+var unique_id = 0;
+
+OscillatorWidget = new GType({
+	parent: Gtk.VBox.type,
+	name: "OscillatorWidget",
+	class_init: function(klass, prototype)
+	{
+		var flags = (GObject.ParamFlags.CONSTRUCT | 
+					 GObject.ParamFlags.READABLE |
+					 GObject.ParamFlags.WRITABLE);
+		
+		var ps = GObject.param_spec_int("frequency",
+										"Oscillator Frequency",
+										"The frequency of the audiotestsrc.",
+										0, 3000, 1000, flags);
+		
+		klass.c_install_property(ps);
+	},
+	init: function(klass)
+	{
+		// Private
+		
+		var hbox = new Gtk.HBox();
+		var frequency_slider = new Gtk.VScale();
+		var volume_slider = new Gtk.VScale();
+		var button = new Gtk.ToggleButton({label: "Enabled"});
+		
+		// No actual introspection data for audiotestsrc, so can not
+		// instantiate one with a constructor, have to use element_factory,
+		// likewise for the others.
+		var audiosrc = Gst.ElementFactory.make("audiotestsrc", "source" + unique_id);
+		var audiosink = Gst.ElementFactory.make("alsasink", "sink" + unique_id);
+		var volume = Gst.ElementFactory.make("volume", "volume" + unique_id);
+		
+		unique_id++;
+		
+		var toggle_enabled = function(button, that)
+		{
+			pipeline.set_state(Gst.State.PLAYING);
+			volume.volume = button.active ? volume_slider.get_value() : 0;
+		};
+		
+		var set_frequency = function(range)
+		{
+			this.frequency = audiosrc.freq = range.get_value();
+		};
+
+		var set_volume = function(range)
+		{
+			if(button.active)
+				volume.volume = range.get_value();
+		};
+		
+		// Implementation
+		
+		frequency_slider.adjustment.upper = 3000;
+		audiosrc.freq = frequency_slider.adjustment.value = this.frequency;
+		
+		volume_slider.adjustment.upper = 10;
+		volume_slider.adjustment.value = 1;
+		volume.volume = 0;
+		
+		pipeline.add(audiosrc);
+		pipeline.add(audiosink);
+		pipeline.add(volume);
+		audiosrc.link(volume);
+		volume.link(audiosink);
+		
+		button.signal.toggled.connect(toggle_enabled);
+		frequency_slider.signal.value_changed.connect(set_frequency);
+		volume_slider.signal.value_changed.connect(set_volume);
+
+		hbox.pack_start(frequency_slider, true, true, 10);
+		hbox.pack_start(volume_slider, true, true, 10);
+		this.pack_start(hbox, true, true, 10);
+		this.pack_start(button, false, false, 10);
 	}
-	else{
-	    pipeline.set_state(Gst.State.PAUSED);
-	    playing = false;
-	}
-    };
-
-    var update_freq = function(range){
-	audiosrc.freq = range.get_value();
-    };
-
-    var update_vol = function(range){
-	volume.volume = range.get_value();
-    };
-
-    button.signal.clicked.connect(toggle);
-    vscale.signal.value_changed.connect(update_freq);
-    volscale.signal.value_changed.connect(update_vol);
-}
+});
 
 var window = new Gtk.Window();
 var button = new Gtk.Button({label: "Add Oscillator"});
 
 window.signal.hide.connect(Gtk.main_quit);
 window.resize(600,300);
-var hbox = new Gtk.HBox();
 
-var os1 = new oscillator(523.25);
-var os2 = new oscillator(659.26);
-var os3 = new oscillator(783.99);
+var hbox = new Gtk.HBox();
 
-function add_oscillator(button){
-    var os = new oscillator(300);
-    hbox.pack_start(os.vbox, true, true, 10);
-    os.vbox.show_all();
+function add_oscillator(button)
+{
+    var os = new OscillatorWidget();
+    hbox.pack_start(os, true, true, 10);
+    os.show_all();
 }
 button.signal.clicked.connect(add_oscillator);
 
 window.add(hbox);
 hbox.pack_start(button, true, true, 10);
-hbox.pack_start(os1.vbox, true, true, 10);
-hbox.pack_start(os2.vbox, true, true, 10);
-hbox.pack_start(os3.vbox, true, true, 10);
+hbox.pack_start(new OscillatorWidget({frequency:523.25}), true, true, 10);
+hbox.pack_start(new OscillatorWidget({frequency:659.26}), true, true, 10);
+hbox.pack_start(new OscillatorWidget({frequency:783.99}), true, true, 10);
 window.show_all();
 
-
 Gtk.main();
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]