Adding first code drop
[demorecorder] / src / GraphicEqualizer.vala
diff --git a/src/GraphicEqualizer.vala b/src/GraphicEqualizer.vala
new file mode 100644 (file)
index 0000000..633a22d
--- /dev/null
@@ -0,0 +1,304 @@
+/*  Demo Recorder 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.
+*/
+using Gtk;
+
+namespace IdWorks {
+  
+  public class GraphicEqualizer : Gtk.VBox {
+    
+    SettingsStructures.EqualizerSettings preset;
+    SettingsStructures.EqualizerSettings preset_prev;
+    SimpleHashMap<SettingsStructures.EqualizerSettings?> presets;
+    VScale band0_slider;
+    VScale band1_slider;
+    VScale band2_slider;
+    VScale band3_slider;
+    VScale band4_slider;
+    VScale band5_slider;
+    VScale band6_slider;
+    VScale band7_slider;
+    VScale band8_slider;
+    VScale band9_slider;
+    Label name_label;
+    Hildon.TouchSelector eq_presets;
+    
+    
+    public signal void eq_updated (int band, double val);
+    public signal void name_updated (string val);
+    
+    public GraphicEqualizer() {
+      preset_prev = SettingsStructures.EqualizerSettings();
+      preset = SettingsStructures.EqualizerSettings();
+      preset.bands = new double[] {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
+      preset.name = "Default";
+      init();
+    }
+    
+    public GraphicEqualizer.with_preset(SettingsStructures.EqualizerSettings gep)
+    requires (null != gep.bands) {
+      preset_prev.bands = null;
+      preset = gep;
+      init();
+    }
+    
+    public GraphicEqualizer.with_values(double[] bands, string name)
+    requires (bands.length == 10)
+    requires (null != name) {
+      preset_prev.bands = null;
+      //preset = new SettingsStructures.EqualizerSettings();
+      preset.bands = bands;
+      preset.name = name;
+      init();
+    }
+    
+    private void init() {
+      load_presets();
+      construct_interface();    
+    }
+    
+    private void load_presets() {
+      presets = new SimpleHashMap<SettingsStructures.EqualizerSettings?>();
+      //SettingsStructures.EqualizerSettings pst = new SettingsStructures.EqualizerSettings();
+      // do hardcoded ones.
+      //pst.name = "Default";
+      //pst.bands = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+      presets.set("Default", SettingsStructures.EqualizerSettings.with_values("Default", new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}));
+      presets.set("Bass Boost", SettingsStructures.EqualizerSettings.with_values("Bass Boost", new double[] {-8.0, 9.6, 9.6, 5.6, 1.6, -4.0, -8.0, -10.4, -11.2, -11.2}));
+      presets.set("Classical", SettingsStructures.EqualizerSettings.with_values("Classical", new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -7.2, -7.2, -7.2, -9.6}));
+      presets.set("Club", SettingsStructures.EqualizerSettings.with_values("Club", new double[] {0.0, 0.0, 8.0, 5.6, 5.6, 5.6, 3.2, 0.0, 0.0, 0.0}));
+      presets.set("Dance", SettingsStructures.EqualizerSettings.with_values("Dance", new double[] {9.6, 7.2, 2.4, 0.0, 0.0, -5.6, -7.2, -7.2, 0.0, 0.0}));
+      presets.set("Flat Response", SettingsStructures.EqualizerSettings.with_values("Flat Response", new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}));
+      presets.set("Folk / Acoustic", SettingsStructures.EqualizerSettings.with_values("Folk / Acoustic", new double[] {0.0, 2.0, 4.0, 2.0, 0.0, 0.0, 0.0, -2.0, -4.0, -6.0}));
+      presets.set("Live", SettingsStructures.EqualizerSettings.with_values("Live", new double[] {-4.8, 0.0, 4.0, 5.6, 5.6, 5.6, 4.0, 2.4, 2.4, 2.4}));
+      presets.set("Pop", SettingsStructures.EqualizerSettings.with_values("Pop", new double[] {-1.6, 4.8, 7.2, 8.0, 5.6, 0.0, -2.4, -2.4, -1.6, -1.6}));
+      presets.set("Rock", SettingsStructures.EqualizerSettings.with_values("Rock", new double[] {8.0, 4.8, -5.6, -8.0, -3.2, 4.0, 8.8, 11.2, 11.2, 11.2}));
+      presets.set("Soft Rock", SettingsStructures.EqualizerSettings.with_values("Soft Rock", new double[] {4.0, 4.0, 2.4, 0.0, -4.0, -5.6, -3.2, 0.0, 2.4, 8.8}));
+      presets.set("Spoken Word", SettingsStructures.EqualizerSettings.with_values("Spoken Word", new double[] {-4.0, -2.0, 0.0, 2.0, 4.0, 4.0, 2.0, 0.0, -2.0, -4.0}));
+      // TODO - Load users settings from storage
+    }
+    
+    private void save_eq_settings() {
+      // TODO
+      string name = "";
+      // prompt for name
+      Dialog prompt = new Dialog.with_buttons("Save EQ Settings", null, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, STOCK_OK, ResponseType.ACCEPT);
+      var dialog_area = (VBox)prompt.get_content_area();
+      var label_prompt = new Label("Enter a name for this Preset.");
+      dialog_area.pack_start(label_prompt, false, false, 4);
+      label_prompt.show();
+      var text_prompt = new Entry();
+      dialog_area.pack_start(text_prompt, false, false, 4);
+      text_prompt.show();
+      if (ResponseType.ACCEPT == prompt.run()) {
+        name = text_prompt.get_text();
+        if (name == "") name = "Unnamed";
+        set_name(name);
+        // For now temporarily add it to selector
+        // need to check to see if we are doing an overwrite I guess!
+        bool found = false;
+        foreach (var k in presets.keys) found = found || (k == name);
+        if (!found) eq_presets.append_text(preset.name);
+        // this will update if it exists or create new if not
+        presets.set(preset.name, preset);
+        /*stdout.printf("Presets contains:\n");
+        foreach (var p in presets.values)
+        {
+          string tmp = p.serialize_to_string();
+          stdout.printf("%s\n", tmp);
+          if (p.deserialize_from_string(tmp)) {
+            stdout.printf("%s\n", p.serialize_to_string());
+          }
+        }*/
+      }
+      prompt.destroy();
+      // add to presets    
+    }
+    
+    private void set_eq_preset(string name) {
+      preset_prev = preset;
+      preset = get_eq_preset(name);
+      for (int i = 0; i < preset.bands.length; ++i) {
+        set_eq(i, preset.bands[i]);
+      }
+      set_name(preset.name);
+    }
+    
+    private SettingsStructures.EqualizerSettings get_eq_preset(string name) {
+      return presets.get(name);
+    }
+    
+    private void construct_interface() {
+      //var vbox = new VBox(false, 4);
+      this.set_size_request(600, 400);
+      
+      var eq_label_hbox = new HBox(false, 2);
+      
+      eq_presets = new Hildon.TouchSelector.text();
+      foreach (var p in presets.values) {
+        eq_presets.append_text(p.name);
+      }
+      //eq_presets.append_text("Default");
+      //eq_presets.append_text("Flat Response");
+      //eq_presets.append_text("Folk / Acoustic");
+      //eq_presets.append_text("Spoken Word");
+      //eq_presets.append_text("Bass Boost");
+      
+      Hildon.PickerButton eq_presets_picker = new Hildon.PickerButton(Hildon.SizeType.FINGER_HEIGHT, Hildon.ButtonArrangement.VERTICAL);
+      eq_presets_picker.set_title("Slider presets:");
+      eq_presets_picker.set_selector(eq_presets);
+      eq_presets_picker.value_changed.connect( (s) => { set_eq_preset(s.get_selector().get_current_text()); } );
+      eq_label_hbox.pack_start(eq_presets_picker, false, false, 2);
+      
+      var eq_save_btn = new Button.with_label("Save settings");
+      eq_save_btn.clicked.connect(save_eq_settings);
+      eq_label_hbox.pack_start(eq_save_btn, false, false, 2);
+      
+      this.pack_start(eq_label_hbox, false, false, 2);
+      
+      name_label = new Label(preset.name);
+      eq_label_hbox.pack_end(name_label, false, false, 2);
+      
+      
+      var eq_hbox = new HBox(true, 2);
+      
+      band0_slider = new VScale.with_range(-24, 12, 0.5);
+      band0_slider.set_inverted(true);
+      band0_slider.set_value_pos(PositionType.BOTTOM);
+      band0_slider.set_value(preset.bands[0]);
+      band0_slider.value_changed.connect( (s) => { preset.bands[0] = s.get_value(); eq_updated(0, s.get_value()); } );
+      eq_hbox.pack_start(band0_slider, true, true, 2);
+      band1_slider = new VScale.with_range(-24, 12, 0.5);
+      band1_slider.set_inverted(true);
+      band1_slider.set_value_pos(PositionType.BOTTOM);
+      band1_slider.set_value(preset.bands[1]);
+      band1_slider.value_changed.connect( (s) => { preset.bands[1] = s.get_value(); eq_updated(1, s.get_value()); } );
+      eq_hbox.pack_start(band1_slider, true, true, 2);
+      band2_slider = new VScale.with_range(-24, 12, 0.5);
+      band2_slider.set_inverted(true);
+      band2_slider.set_value_pos(PositionType.BOTTOM);
+      band2_slider.set_value(preset.bands[2]);
+      band2_slider.value_changed.connect( (s) => { preset.bands[2] = s.get_value(); eq_updated(2, s.get_value()); } );
+      eq_hbox.pack_start(band2_slider, true, true, 2);
+      band3_slider = new VScale.with_range(-24, 12, 0.5);
+      band3_slider.set_inverted(true);
+      band3_slider.set_value_pos(PositionType.BOTTOM);
+      band3_slider.set_value(preset.bands[3]);
+      band3_slider.value_changed.connect( (s) => { preset.bands[3] = s.get_value(); eq_updated(3, s.get_value()); } );
+      eq_hbox.pack_start(band3_slider, true, true, 2);
+      band4_slider = new VScale.with_range(-24, 12, 0.5);
+      band4_slider.set_inverted(true);
+      band4_slider.set_value_pos(PositionType.BOTTOM);
+      band4_slider.set_value(preset.bands[4]);
+      band4_slider.value_changed.connect( (s) => { preset.bands[4] = s.get_value(); eq_updated(4, s.get_value()); } );
+      eq_hbox.pack_start(band4_slider, true, true, 2);
+      band5_slider = new VScale.with_range(-24, 12, 0.5);
+      band5_slider.set_inverted(true);
+      band5_slider.set_value_pos(PositionType.BOTTOM);
+      band5_slider.set_value(preset.bands[5]);
+      band5_slider.value_changed.connect( (s) => { preset.bands[5] = s.get_value(); eq_updated(5, s.get_value()); } );
+      eq_hbox.pack_start(band5_slider, true, true, 2);
+      band6_slider = new VScale.with_range(-24, 12, 0.5);
+      band6_slider.set_inverted(true);
+      band6_slider.set_value_pos(PositionType.BOTTOM);
+      band6_slider.set_value(preset.bands[6]);
+      band6_slider.value_changed.connect( (s) => { preset.bands[6] = s.get_value(); eq_updated(6, s.get_value()); } );
+      eq_hbox.pack_start(band6_slider, true, true, 2);
+      band7_slider = new VScale.with_range(-24, 12, 0.5);
+      band7_slider.set_inverted(true);
+      band7_slider.set_value_pos(PositionType.BOTTOM);
+      band7_slider.set_value(preset.bands[7]);
+      band7_slider.value_changed.connect( (s) => { preset.bands[7] = s.get_value(); eq_updated(7, s.get_value()); } );
+      eq_hbox.pack_start(band7_slider, true, true, 2);
+      band8_slider = new VScale.with_range(-24, 12, 0.5);
+      band8_slider.set_inverted(true);
+      band8_slider.set_value_pos(PositionType.BOTTOM);
+      band8_slider.set_value(preset.bands[8]);
+      band8_slider.value_changed.connect( (s) => { preset.bands[8] = s.get_value(); eq_updated(8, s.get_value()); } );
+      eq_hbox.pack_start(band8_slider, true, true, 2);
+      band9_slider = new VScale.with_range(-24, 12, 0.5);
+      band9_slider.set_inverted(true);
+      band9_slider.set_value_pos(PositionType.BOTTOM);
+      band9_slider.set_value(preset.bands[9]);
+      band9_slider.value_changed.connect( (s) => { preset.bands[9] = s.get_value(); eq_updated(9, s.get_value()); } );
+      eq_hbox.pack_start(band9_slider, true, true, 2);
+      
+      this.pack_start(eq_hbox, true, true, 0);
+      
+      //this.add(vbox);
+      
+      //this.show_all();
+    }
+    
+    public double get_eq(int band) {
+      return preset.bands[band];
+    }
+    
+    public void set_eq (int band, double val)
+    requires (band > -1 && band < 10)
+    requires (val >= -24.0 && val <= 12.0) {
+      switch (band) {
+        case 0:
+          band0_slider.set_value (val);
+          break;
+        case 1:
+          band1_slider.set_value (val);
+          break;
+        case 2:
+          band2_slider.set_value (val);
+          break;
+        case 3:
+          band3_slider.set_value (val);
+          break;
+        case 4:
+          band4_slider.set_value (val);
+          break;
+        case 5:
+          band5_slider.set_value (val);
+          break;
+        case 6:
+          band6_slider.set_value (val);
+          break;
+        case 7:
+          band7_slider.set_value (val);
+          break;
+        case 8:
+          band8_slider.set_value (val);
+          break;
+        case 9:
+          band9_slider.set_value (val);
+          break;
+        default:
+          break;
+      }
+      preset.bands[band] = val;
+      eq_updated (band, val);
+    }
+    
+    public void set_name(string val) {
+      name_label.set_text(val);
+      name_updated(val);
+      preset.name = val;
+    }
+    
+    public string get_name() {
+      return preset.name;
+    }  
+    
+  }
+  
+}