Initial project commit
[decibelmeter] / src / DecibelMeter.vala
diff --git a/src/DecibelMeter.vala b/src/DecibelMeter.vala
new file mode 100644 (file)
index 0000000..d16b406
--- /dev/null
@@ -0,0 +1,193 @@
+/*  Decibel Meter for MAEMO 5
+*   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   This program is distributed in the hope that it will be useful,
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*   GNU General Public License for more details
+*
+*   You should have received a copy of the GNU General Public
+*   License along with this program; if not, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+
+namespace IdWorks {
+
+public class DecibelMeter : Hildon.Program {
+
+  Hildon.StackableWindow window;  
+  Osso.Context osso_context;
+  MeterWidget channel_one;
+  MeterWidget channel_two;
+  AudioCaptureMonitor monitor;
+  
+  Hildon.Button play_button;
+  bool is_playing = false;
+  Gtk.Image play_icon;
+  Gtk.Image stop_icon;
+  Gtk.Image mic_icon;
+  Gtk.Image audio_icon;
+  Gtk.Image bt_icon;
+  Hildon.TouchSelector source_selector;
+  Hildon.PickerButton source_picker;
+  
+  construct {
+    osso_context = new Osso.Context("decibelmeter", "0.1", false, null);
+    window = new Hildon.StackableWindow();
+    window.destroy.connect(Gtk.main_quit);
+    window.set_title("Decibel Meter");
+    
+    play_icon = new Gtk.Image.from_icon_name("camera_playback", Gtk.IconSize.BUTTON);
+    stop_icon = new Gtk.Image.from_icon_name("camera_video_stop", Gtk.IconSize.BUTTON);
+    mic_icon = new Gtk.Image.from_icon_name("camera_volume_unmuted", Gtk.IconSize.BUTTON);
+    audio_icon = new Gtk.Image.from_icon_name("mediaplayer_main_button_music_pressed", Gtk.IconSize.BUTTON);
+    bt_icon = new Gtk.Image.from_icon_name("general_bluetooth", Gtk.IconSize.BUTTON);
+    
+    channel_one = new MeterWidget();
+    channel_one.title = "Channel 1";
+    channel_one.set_layout(DbMonitorWidget.Layout.VERTICAL);
+    channel_two = new MeterWidget();
+    channel_two.title = "Channel 2";
+    
+    Gtk.VBox container = new Gtk.VBox(false, 4);
+    
+    Gtk.HBox meters = new Gtk.HBox(true, 4);
+    meters.pack_start(channel_one, true, true, 4);
+    meters.pack_start(channel_two, true, true, 4);
+    
+    container.add(meters);
+    
+    Gtk.HBox control = new Gtk.HBox(false, 4);
+    
+    
+    play_button = new Hildon.Button(Hildon.SizeType.THUMB_HEIGHT | Hildon.SizeType.AUTO_WIDTH, Hildon.ButtonArrangement.HORIZONTAL);
+    play_button.set_image(play_icon);
+    play_button.clicked.connect(on_play);
+    control.pack_start(play_button, false, true, 4);
+    
+    Gtk.HScale volume_slider = new Gtk.HScale.with_range(0.0, 10.0, 0.05);
+    volume_slider.set_value_pos(Gtk.PositionType.LEFT);
+    volume_slider.set_value(1.0);
+    volume_slider.value_changed.connect( (s) => { monitor.set_volume(s.get_value()); } );
+    control.pack_start(new Gtk.Label("Gain:"), false, true, 4);
+    control.pack_start(volume_slider, true, true, 4);
+    
+    source_selector = new Hildon.TouchSelector.text();
+    source_selector.append_text("Microphone");
+    source_selector.append_text("Monitor");
+    source_selector.append_text("Bluetooth");
+    //source_selector.set_active(0, 0);
+    this.set_source("Microphone");
+    source_picker = new Hildon.PickerButton(Hildon.SizeType.THUMB_HEIGHT | Hildon.SizeType.AUTO_WIDTH, Hildon.ButtonArrangement.VERTICAL);
+    source_picker.set_title("Source:");
+    source_picker.set_selector(source_selector);
+    source_picker.set_alignment(0, 0, 1, 1);
+    source_picker.set_image(mic_icon);
+    source_picker.value_changed.connect( (s) => { set_input_source(s.get_selector().get_current_text()); } );
+    control.pack_start(source_picker, true, true, 4);
+    
+    
+    container.add(control);
+    
+    window.add(container);
+    construct_menu();
+    construct_monitor();
+  }
+  
+  private void construct_menu() {
+  
+  
+  }
+  
+  private void set_source(string name) {
+    switch (name) {
+      case "Microphone":
+        source_selector.set_active(0, 0);
+        source_picker.set_image(mic_icon);
+        source_picker.set_text("Source:", name);
+        break;
+      case "Monitor":
+        source_selector.set_active(0, 1);
+        source_picker.set_image(audio_icon);
+        source_picker.set_text("Source:", name);
+        break;
+      case "Bluetooth":
+        source_selector.set_active(0, 2);
+        source_picker.set_image(bt_icon);
+        source_picker.set_text("Source:", name);
+        break;
+      default:
+        source_selector.set_active(0, 0);
+        source_picker.set_image(mic_icon);
+        source_picker.set_text("Source:", name);
+        break;
+    }
+    if (null != monitor) monitor.set_source(name);
+  }
+  private void set_input_source(string name) {
+    switch (name) {
+      case "Microphone":
+        source_picker.set_image(mic_icon);
+        source_picker.set_text("Source:", name);
+        break;
+      case "Monitor":
+        source_picker.set_image(audio_icon);
+        source_picker.set_text("Source:", name);
+        break;
+      case "Bluetooth":
+        source_picker.set_image(bt_icon);
+        source_picker.set_text("Source:", name);
+        break;
+      default:
+        source_picker.set_image(mic_icon);
+        source_picker.set_text("Source:", name);
+        break;
+    }
+    if (null != monitor) monitor.set_source(name);
+  }
+  
+  private void on_play() {
+    if (!is_playing) {
+      play_button.set_image(stop_icon);
+      monitor.play();
+    }
+    else {
+      monitor.stop();      
+      play_button.set_image(play_icon);
+    }
+    is_playing = !is_playing;
+  }
+  
+  private void construct_monitor() {
+    monitor = new AudioCaptureMonitor.with_settings("Monitor", "Microphone", 1.0);
+    monitor.update_level.connect(level_updated);
+  }
+  
+  private void level_updated(int channels, int channel, double rms_dB, double peak_dB, double decay_dB, double rms) {
+    switch(channel) {
+      case 0:
+        channel_one.set_peak(peak_dB);
+        channel_one.set_decay(decay_dB);
+        break;
+      case 1:
+        channel_two.set_peak(peak_dB);
+        channel_two.set_decay(decay_dB);
+        break;
+      default:
+        break;
+    }
+  }
+
+  public void run() {
+    window.show_all();
+    Gtk.main();
+  }
+
+}
+
+}