Adding first code drop
[demorecorder] / src / GraphicEqualizer.vala
1 /*  Demo Recorder for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 using Gtk;
19
20 namespace IdWorks {
21   
22   public class GraphicEqualizer : Gtk.VBox {
23     
24     SettingsStructures.EqualizerSettings preset;
25     SettingsStructures.EqualizerSettings preset_prev;
26     SimpleHashMap<SettingsStructures.EqualizerSettings?> presets;
27     VScale band0_slider;
28     VScale band1_slider;
29     VScale band2_slider;
30     VScale band3_slider;
31     VScale band4_slider;
32     VScale band5_slider;
33     VScale band6_slider;
34     VScale band7_slider;
35     VScale band8_slider;
36     VScale band9_slider;
37     Label name_label;
38     Hildon.TouchSelector eq_presets;
39     
40     
41     public signal void eq_updated (int band, double val);
42     public signal void name_updated (string val);
43     
44     public GraphicEqualizer() {
45       preset_prev = SettingsStructures.EqualizerSettings();
46       preset = SettingsStructures.EqualizerSettings();
47       preset.bands = new double[] {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
48       preset.name = "Default";
49       init();
50     }
51     
52     public GraphicEqualizer.with_preset(SettingsStructures.EqualizerSettings gep)
53     requires (null != gep.bands) {
54       preset_prev.bands = null;
55       preset = gep;
56       init();
57     }
58     
59     public GraphicEqualizer.with_values(double[] bands, string name)
60     requires (bands.length == 10)
61     requires (null != name) {
62       preset_prev.bands = null;
63       //preset = new SettingsStructures.EqualizerSettings();
64       preset.bands = bands;
65       preset.name = name;
66       init();
67     }
68     
69     private void init() {
70       load_presets();
71       construct_interface();    
72     }
73     
74     private void load_presets() {
75       presets = new SimpleHashMap<SettingsStructures.EqualizerSettings?>();
76       //SettingsStructures.EqualizerSettings pst = new SettingsStructures.EqualizerSettings();
77       // do hardcoded ones.
78       //pst.name = "Default";
79       //pst.bands = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
80       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}));
81       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}));
82       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}));
83       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}));
84       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}));
85       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}));
86       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}));
87       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}));
88       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}));
89       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}));
90       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}));
91       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}));
92       // TODO - Load users settings from storage
93     }
94     
95     private void save_eq_settings() {
96       // TODO
97       string name = "";
98       // prompt for name
99       Dialog prompt = new Dialog.with_buttons("Save EQ Settings", null, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, STOCK_OK, ResponseType.ACCEPT);
100       var dialog_area = (VBox)prompt.get_content_area();
101       var label_prompt = new Label("Enter a name for this Preset.");
102       dialog_area.pack_start(label_prompt, false, false, 4);
103       label_prompt.show();
104       var text_prompt = new Entry();
105       dialog_area.pack_start(text_prompt, false, false, 4);
106       text_prompt.show();
107       if (ResponseType.ACCEPT == prompt.run()) {
108         name = text_prompt.get_text();
109         if (name == "") name = "Unnamed";
110         set_name(name);
111         // For now temporarily add it to selector
112         // need to check to see if we are doing an overwrite I guess!
113         bool found = false;
114         foreach (var k in presets.keys) found = found || (k == name);
115         if (!found) eq_presets.append_text(preset.name);
116         // this will update if it exists or create new if not
117         presets.set(preset.name, preset);
118         /*stdout.printf("Presets contains:\n");
119         foreach (var p in presets.values)
120         {
121           string tmp = p.serialize_to_string();
122           stdout.printf("%s\n", tmp);
123           if (p.deserialize_from_string(tmp)) {
124             stdout.printf("%s\n", p.serialize_to_string());
125           }
126         }*/
127       }
128       prompt.destroy();
129       // add to presets    
130     }
131     
132     private void set_eq_preset(string name) {
133       preset_prev = preset;
134       preset = get_eq_preset(name);
135       for (int i = 0; i < preset.bands.length; ++i) {
136         set_eq(i, preset.bands[i]);
137       }
138       set_name(preset.name);
139     }
140     
141     private SettingsStructures.EqualizerSettings get_eq_preset(string name) {
142       return presets.get(name);
143     }
144     
145     private void construct_interface() {
146       //var vbox = new VBox(false, 4);
147       this.set_size_request(600, 400);
148       
149       var eq_label_hbox = new HBox(false, 2);
150       
151       eq_presets = new Hildon.TouchSelector.text();
152       foreach (var p in presets.values) {
153         eq_presets.append_text(p.name);
154       }
155       //eq_presets.append_text("Default");
156       //eq_presets.append_text("Flat Response");
157       //eq_presets.append_text("Folk / Acoustic");
158       //eq_presets.append_text("Spoken Word");
159       //eq_presets.append_text("Bass Boost");
160       
161       Hildon.PickerButton eq_presets_picker = new Hildon.PickerButton(Hildon.SizeType.FINGER_HEIGHT, Hildon.ButtonArrangement.VERTICAL);
162       eq_presets_picker.set_title("Slider presets:");
163       eq_presets_picker.set_selector(eq_presets);
164       eq_presets_picker.value_changed.connect( (s) => { set_eq_preset(s.get_selector().get_current_text()); } );
165       eq_label_hbox.pack_start(eq_presets_picker, false, false, 2);
166       
167       var eq_save_btn = new Button.with_label("Save settings");
168       eq_save_btn.clicked.connect(save_eq_settings);
169       eq_label_hbox.pack_start(eq_save_btn, false, false, 2);
170       
171       this.pack_start(eq_label_hbox, false, false, 2);
172       
173       name_label = new Label(preset.name);
174       eq_label_hbox.pack_end(name_label, false, false, 2);
175       
176       
177       var eq_hbox = new HBox(true, 2);
178       
179       band0_slider = new VScale.with_range(-24, 12, 0.5);
180       band0_slider.set_inverted(true);
181       band0_slider.set_value_pos(PositionType.BOTTOM);
182       band0_slider.set_value(preset.bands[0]);
183       band0_slider.value_changed.connect( (s) => { preset.bands[0] = s.get_value(); eq_updated(0, s.get_value()); } );
184       eq_hbox.pack_start(band0_slider, true, true, 2);
185       band1_slider = new VScale.with_range(-24, 12, 0.5);
186       band1_slider.set_inverted(true);
187       band1_slider.set_value_pos(PositionType.BOTTOM);
188       band1_slider.set_value(preset.bands[1]);
189       band1_slider.value_changed.connect( (s) => { preset.bands[1] = s.get_value(); eq_updated(1, s.get_value()); } );
190       eq_hbox.pack_start(band1_slider, true, true, 2);
191       band2_slider = new VScale.with_range(-24, 12, 0.5);
192       band2_slider.set_inverted(true);
193       band2_slider.set_value_pos(PositionType.BOTTOM);
194       band2_slider.set_value(preset.bands[2]);
195       band2_slider.value_changed.connect( (s) => { preset.bands[2] = s.get_value(); eq_updated(2, s.get_value()); } );
196       eq_hbox.pack_start(band2_slider, true, true, 2);
197       band3_slider = new VScale.with_range(-24, 12, 0.5);
198       band3_slider.set_inverted(true);
199       band3_slider.set_value_pos(PositionType.BOTTOM);
200       band3_slider.set_value(preset.bands[3]);
201       band3_slider.value_changed.connect( (s) => { preset.bands[3] = s.get_value(); eq_updated(3, s.get_value()); } );
202       eq_hbox.pack_start(band3_slider, true, true, 2);
203       band4_slider = new VScale.with_range(-24, 12, 0.5);
204       band4_slider.set_inverted(true);
205       band4_slider.set_value_pos(PositionType.BOTTOM);
206       band4_slider.set_value(preset.bands[4]);
207       band4_slider.value_changed.connect( (s) => { preset.bands[4] = s.get_value(); eq_updated(4, s.get_value()); } );
208       eq_hbox.pack_start(band4_slider, true, true, 2);
209       band5_slider = new VScale.with_range(-24, 12, 0.5);
210       band5_slider.set_inverted(true);
211       band5_slider.set_value_pos(PositionType.BOTTOM);
212       band5_slider.set_value(preset.bands[5]);
213       band5_slider.value_changed.connect( (s) => { preset.bands[5] = s.get_value(); eq_updated(5, s.get_value()); } );
214       eq_hbox.pack_start(band5_slider, true, true, 2);
215       band6_slider = new VScale.with_range(-24, 12, 0.5);
216       band6_slider.set_inverted(true);
217       band6_slider.set_value_pos(PositionType.BOTTOM);
218       band6_slider.set_value(preset.bands[6]);
219       band6_slider.value_changed.connect( (s) => { preset.bands[6] = s.get_value(); eq_updated(6, s.get_value()); } );
220       eq_hbox.pack_start(band6_slider, true, true, 2);
221       band7_slider = new VScale.with_range(-24, 12, 0.5);
222       band7_slider.set_inverted(true);
223       band7_slider.set_value_pos(PositionType.BOTTOM);
224       band7_slider.set_value(preset.bands[7]);
225       band7_slider.value_changed.connect( (s) => { preset.bands[7] = s.get_value(); eq_updated(7, s.get_value()); } );
226       eq_hbox.pack_start(band7_slider, true, true, 2);
227       band8_slider = new VScale.with_range(-24, 12, 0.5);
228       band8_slider.set_inverted(true);
229       band8_slider.set_value_pos(PositionType.BOTTOM);
230       band8_slider.set_value(preset.bands[8]);
231       band8_slider.value_changed.connect( (s) => { preset.bands[8] = s.get_value(); eq_updated(8, s.get_value()); } );
232       eq_hbox.pack_start(band8_slider, true, true, 2);
233       band9_slider = new VScale.with_range(-24, 12, 0.5);
234       band9_slider.set_inverted(true);
235       band9_slider.set_value_pos(PositionType.BOTTOM);
236       band9_slider.set_value(preset.bands[9]);
237       band9_slider.value_changed.connect( (s) => { preset.bands[9] = s.get_value(); eq_updated(9, s.get_value()); } );
238       eq_hbox.pack_start(band9_slider, true, true, 2);
239       
240       this.pack_start(eq_hbox, true, true, 0);
241       
242       //this.add(vbox);
243       
244       //this.show_all();
245     }
246     
247     public double get_eq(int band) {
248       return preset.bands[band];
249     }
250     
251     public void set_eq (int band, double val)
252     requires (band > -1 && band < 10)
253     requires (val >= -24.0 && val <= 12.0) {
254       switch (band) {
255         case 0:
256           band0_slider.set_value (val);
257           break;
258         case 1:
259           band1_slider.set_value (val);
260           break;
261         case 2:
262           band2_slider.set_value (val);
263           break;
264         case 3:
265           band3_slider.set_value (val);
266           break;
267         case 4:
268           band4_slider.set_value (val);
269           break;
270         case 5:
271           band5_slider.set_value (val);
272           break;
273         case 6:
274           band6_slider.set_value (val);
275           break;
276         case 7:
277           band7_slider.set_value (val);
278           break;
279         case 8:
280           band8_slider.set_value (val);
281           break;
282         case 9:
283           band9_slider.set_value (val);
284           break;
285         default:
286           break;
287       }
288       preset.bands[band] = val;
289       eq_updated (band, val);
290     }
291     
292     public void set_name(string val) {
293       name_label.set_text(val);
294       name_updated(val);
295       preset.name = val;
296     }
297     
298     public string get_name() {
299       return preset.name;
300     }  
301     
302   }
303   
304 }