Update Beast installation instructions to use beastpatcher and remove instructions...
[kugel-rb.git] / apps / menus / eq_menu.c
blob1b386816c2560f84850d4d197dfd2b881f6c1df5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: eq_menu.c 12179 2007-02-01 23:08:15Z amiconn $
10 * Copyright (C) 2006 Dan Everton
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include "eq_menu.h"
27 #include "system.h"
28 #include "kernel.h"
29 #include "lcd.h"
30 #include "menu.h"
31 #include "action.h"
32 #include "mp3_playback.h"
33 #include "settings.h"
34 #include "statusbar.h"
35 #include "screens.h"
36 #include "icons.h"
37 #include "font.h"
38 #include "lang.h"
39 #include "sprintf.h"
40 #include "talk.h"
41 #include "misc.h"
42 #include "sound.h"
43 #include "splash.h"
44 #include "dsp.h"
45 #include "tree.h"
46 #include "talk.h"
47 #include "screen_access.h"
48 #include "keyboard.h"
49 #include "gui/scrollbar.h"
50 #include "eq_menu.h"
51 #include "menu_common.h"
52 #include "viewport.h"
55 * Utility functions
58 void eq_q_format(char* buffer, size_t buffer_size, int value, const char* unit)
60 snprintf(buffer, buffer_size, "%d.%d %s", value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
63 void eq_precut_format(char* buffer, size_t buffer_size, int value, const char* unit)
65 snprintf(buffer, buffer_size, "%s%d.%d %s", value == 0 ? " " : "-",
66 value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
70 * Settings functions
72 static void eq_apply(void)
74 int i;
75 dsp_set_eq(global_settings.eq_enabled);
76 dsp_set_eq_precut(global_settings.eq_precut);
77 /* Update all bands */
78 for(i = 0; i < 5; i++) {
79 dsp_set_eq_coefs(i);
83 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
85 switch (action)
87 case ACTION_ENTER_MENUITEM:
88 action = lowlatency_callback(action, this_item);
89 break;
90 case ACTION_EXIT_MENUITEM:
91 eq_apply();
92 action = lowlatency_callback(action, this_item);
93 break;
96 return action;
98 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
99 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band0_cutoff, eq_setting_callback);
102 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band1_cutoff, eq_setting_callback);
103 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band2_cutoff, eq_setting_callback);
104 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band3_cutoff, eq_setting_callback);
105 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band4_cutoff, eq_setting_callback);
107 MENUITEM_SETTING(q_0, &global_settings.eq_band0_q, eq_setting_callback);
108 MENUITEM_SETTING(q_1, &global_settings.eq_band1_q, eq_setting_callback);
109 MENUITEM_SETTING(q_2, &global_settings.eq_band2_q, eq_setting_callback);
110 MENUITEM_SETTING(q_3, &global_settings.eq_band3_q, eq_setting_callback);
111 MENUITEM_SETTING(q_4, &global_settings.eq_band4_q, eq_setting_callback);
113 MENUITEM_SETTING(gain_0, &global_settings.eq_band0_gain, eq_setting_callback);
114 MENUITEM_SETTING(gain_1, &global_settings.eq_band1_gain, eq_setting_callback);
115 MENUITEM_SETTING(gain_2, &global_settings.eq_band2_gain, eq_setting_callback);
116 MENUITEM_SETTING(gain_3, &global_settings.eq_band3_gain, eq_setting_callback);
117 MENUITEM_SETTING(gain_4, &global_settings.eq_band4_gain, eq_setting_callback);
119 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
121 (void)selected_item;
122 int *setting = (int*)data;
123 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
124 return buffer;
127 static int gainitem_speak_item(int selected_item, void * data)
129 (void)selected_item;
130 int *setting = (int*)data;
131 talk_number(*setting, false);
132 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
133 return 0;
136 static int do_option(void * param)
138 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
139 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
140 do_setting_from_menu(setting, NULL);
141 eq_apply();
142 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
143 return 0;
146 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
147 do_option, (void*)&gain_0,
148 gainitem_get_name, gainitem_speak_item,
149 &global_settings.eq_band0_cutoff, NULL, Icon_NOICON);
150 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
151 do_option, (void*)&gain_1,
152 gainitem_get_name, gainitem_speak_item,
153 &global_settings.eq_band1_cutoff, NULL, Icon_NOICON);
154 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
155 do_option, (void*)&gain_2,
156 gainitem_get_name, gainitem_speak_item,
157 &global_settings.eq_band2_cutoff, NULL, Icon_NOICON);
158 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
159 do_option, (void*)&gain_3,
160 gainitem_get_name, gainitem_speak_item,
161 &global_settings.eq_band3_cutoff, NULL, Icon_NOICON);
162 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
163 do_option, (void*)&gain_4,
164 gainitem_get_name, gainitem_speak_item,
165 &global_settings.eq_band4_cutoff, NULL, Icon_NOICON);
167 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
168 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
170 static const struct menu_item_ex *band_items[3][3] = {
171 { &cutoff_1, &q_1, &gain_1 },
172 { &cutoff_2, &q_2, &gain_2 },
173 { &cutoff_3, &q_3, &gain_3 }
176 static char* centerband_get_name(int selected_item, void * data, char *buffer)
178 (void)selected_item;
179 int band = (intptr_t)data;
180 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
181 return buffer;
184 static int centerband_speak_item(int selected_item, void * data)
186 (void)selected_item;
187 int band = (intptr_t)data;
188 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
189 talk_number(band, true);
190 return 0;
193 static int do_center_band_menu(void* param)
195 int band = (intptr_t)param;
196 struct menu_item_ex menu;
197 struct menu_callback_with_desc cb_and_desc;
198 char desc[MAX_PATH];
200 cb_and_desc.menu_callback = NULL;
201 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
202 cb_and_desc.desc = desc;
203 cb_and_desc.icon_id = Icon_EQ;
204 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
205 menu.submenus = band_items[band-1];
206 menu.callback_and_desc = &cb_and_desc;
207 do_menu(&menu, NULL, NULL, false);
208 return 0;
211 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
212 Icon_EQ, &cutoff_0, &q_0, &gain_0);
213 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
214 do_center_band_menu, (void*)1,
215 centerband_get_name, centerband_speak_item,
216 (void*)1, NULL, Icon_EQ);
217 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
218 do_center_band_menu, (void*)2,
219 centerband_get_name, centerband_speak_item,
220 (void*)2, NULL, Icon_EQ);
221 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
222 do_center_band_menu, (void*)3,
223 centerband_get_name, centerband_speak_item,
224 (void*)3, NULL, Icon_EQ);
225 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
226 Icon_EQ, &cutoff_4, &q_4, &gain_4);
228 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
229 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
232 enum eq_slider_mode {
233 GAIN,
234 CUTOFF,
238 enum eq_type {
239 LOW_SHELF,
240 PEAK,
241 HIGH_SHELF
244 /* Size of just the slider/srollbar */
245 #define SCROLLBAR_SIZE 6
247 /* Draw the UI for a whole EQ band */
248 static int draw_eq_slider(struct screen * screen, int x, int y,
249 int width, int cutoff, int q, int gain, bool selected,
250 enum eq_slider_mode mode, int band)
252 char buf[26];
253 int steps, min_item, max_item;
254 int abs_gain = abs(gain);
255 int x1, x2, y1, total_height;
256 int w, h;
258 switch(mode) {
259 case Q:
260 steps = EQ_Q_MAX - EQ_Q_MIN;
261 min_item = q - EQ_Q_STEP - EQ_Q_MIN;
262 max_item = q + EQ_Q_STEP - EQ_Q_MIN;
263 break;
264 case CUTOFF:
265 steps = EQ_CUTOFF_MAX - EQ_CUTOFF_MIN;
266 min_item = cutoff - EQ_CUTOFF_FAST_STEP * 2;
267 max_item = cutoff + EQ_CUTOFF_FAST_STEP * 2;
268 break;
269 case GAIN:
270 default:
271 steps = EQ_GAIN_MAX - EQ_GAIN_MIN;
272 min_item = abs(EQ_GAIN_MIN) + gain - EQ_GAIN_STEP * 5;
273 max_item = abs(EQ_GAIN_MIN) + gain + EQ_GAIN_STEP * 5;
274 break;
277 /* Start two pixels in, one for border, one for margin */
278 x1 = x + 2;
279 y1 = y + 2;
281 /* Print out the band label */
282 if (band == 0) {
283 screen->putsxy(x1, y1, "LS: ");
284 screen->getstringsize("LS:", &w, &h);
285 } else if (band == 4) {
286 screen->putsxy(x1, y1, "HS: ");
287 screen->getstringsize("HS:", &w, &h);
288 } else {
289 snprintf(buf, sizeof(buf), "PK%d:", band);
290 screen->putsxy(x1, y1, buf);
291 screen->getstringsize(buf, &w, &h);
294 screen->getstringsize("A", &w, &h);
295 x1 += 5*w; /* 4 chars for label + 1 space = 5 */
297 /* Print out gain part of status line (left justify after label) */
298 if (mode == GAIN && selected)
299 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
300 else
301 screen->set_drawmode(DRMODE_SOLID);
303 snprintf(buf, sizeof(buf), "%s%2d.%d%s", gain < 0 ? "-" : " ",
304 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR,
305 screen->lcdwidth >= 160 ? "dB" : "");
306 screen->putsxy(x1, y1, buf);
307 screen->getstringsize(buf, &w, &h);
308 x1 += w;
310 /* Print out Q part of status line (right justify) */
311 if (mode == Q && selected)
312 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
313 else
314 screen->set_drawmode(DRMODE_SOLID);
316 snprintf(buf, sizeof(buf), "%d.%d%s", q / EQ_USER_DIVISOR,
317 q % EQ_USER_DIVISOR, screen->lcdwidth >= 160 ? " Q" : "");
318 screen->getstringsize(buf, &w, &h);
319 x2 = x + width - w - 2;
320 screen->putsxy(x2, y1, buf);
322 /* Print out cutoff part of status line (center between gain & Q) */
323 if (mode == CUTOFF && selected)
324 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
325 else
326 screen->set_drawmode(DRMODE_SOLID);
328 snprintf(buf, sizeof(buf), "%5d%s", cutoff,
329 screen->lcdwidth >= 160 ? "Hz" : "");
330 screen->getstringsize(buf, &w, &h);
331 x1 = x1 + (x2 - x1 - w)/2;
332 screen->putsxy(x1, y1, buf);
334 /* Draw selection box */
335 total_height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
336 screen->set_drawmode(DRMODE_SOLID);
337 if (selected) {
338 screen->drawrect(x, y, width, total_height);
341 /* Draw horizontal slider. Reuse scrollbar for this */
342 gui_scrollbar_draw(screen, x + 3, y1 + h + 1, width - 6, SCROLLBAR_SIZE,
343 steps, min_item, max_item, HORIZONTAL);
345 return total_height;
348 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
349 static void draw_eq_sliders(struct screen * screen, int x, int y,
350 int nb_eq_sliders, int start_item,
351 int current_band, enum eq_slider_mode mode)
353 int i, gain, q, cutoff;
354 int height = y;
355 int *setting = &global_settings.eq_band0_cutoff;
357 start_item = MIN(start_item, 5-nb_eq_sliders);
358 for (i=0; i<5; i++) {
359 cutoff = *setting++;
360 q = *setting++;
361 gain = *setting++;
363 if (i == start_item + nb_eq_sliders)
364 break;
366 if (i >= start_item) {
367 height += draw_eq_slider(screen, x, height, screen->lcdwidth - x - 1,
368 cutoff, q, gain, i == current_band, mode,
370 /* add a margin */
371 height++;
374 if (nb_eq_sliders != 5)
375 gui_scrollbar_draw(screen, 0, y, SCROLLBAR_SIZE - 1,
376 screen->lcdheight - y, 5,
377 start_item, start_item + nb_eq_sliders,
378 VERTICAL);
379 return;
382 /* Provides a graphical means of editing the EQ settings */
383 bool eq_menu_graphical(void)
385 bool exit_request = false;
386 bool result = true;
387 bool has_changed = false;
388 int button;
389 int *setting;
390 int current_band, x, y, step, fast_step, min, max, voice_unit;
391 enum eq_slider_mode mode;
392 enum eq_type current_type;
393 char buf[24];
394 int i, w, h, height, start_item, nb_eq_sliders[NB_SCREENS];
395 int barsenabled = viewportmanager_set_statusbar(VP_SB_HIDE_ALL);
398 FOR_NB_SCREENS(i) {
399 screens[i].set_viewport(NULL);
400 screens[i].setfont(FONT_SYSFIXED);
401 screens[i].clear_display();
403 /* Figure out how many sliders can be drawn on the screen */
404 screens[i].getstringsize("A", &w, &h);
406 /* Total height includes margins (1), text, slider, and line selector (1) */
407 height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
408 nb_eq_sliders[i] = screens[i].lcdheight / height;
410 /* Make sure the "Edit Mode" text fits too */
411 height = nb_eq_sliders[i]*height + h + 2;
412 if (height > screens[i].lcdheight)
413 nb_eq_sliders[i]--;
415 if (nb_eq_sliders[i] > 5)
416 nb_eq_sliders[i] = 5;
419 y = h + 1;
421 /* Start off editing gain on the first band */
422 mode = GAIN;
423 current_type = LOW_SHELF;
424 current_band = 0;
426 while (!exit_request) {
427 FOR_NB_SCREENS(i)
429 screens[i].clear_display();
431 /* Set pointer to the band data currently editable */
432 if (mode == GAIN) {
433 /* gain */
434 setting = &global_settings.eq_band0_gain;
435 setting += current_band * 3;
437 step = EQ_GAIN_STEP;
438 fast_step = EQ_GAIN_FAST_STEP;
439 min = EQ_GAIN_MIN;
440 max = EQ_GAIN_MAX;
441 voice_unit = UNIT_DB;
443 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
444 str(LANG_SYSFONT_GAIN), "(dB)");
446 screens[i].putsxy(0, 0, buf);
447 } else if (mode == CUTOFF) {
448 /* cutoff */
449 setting = &global_settings.eq_band0_cutoff;
450 setting += current_band * 3;
452 step = EQ_CUTOFF_STEP;
453 fast_step = EQ_CUTOFF_FAST_STEP;
454 min = EQ_CUTOFF_MIN;
455 max = EQ_CUTOFF_MAX;
456 voice_unit = UNIT_HERTZ;
458 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
459 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF), "(Hz)");
461 screens[i].putsxy(0, 0, buf);
462 } else {
463 /* Q */
464 setting = &global_settings.eq_band0_q;
465 setting += current_band * 3;
467 step = EQ_Q_STEP;
468 fast_step = EQ_Q_FAST_STEP;
469 min = EQ_Q_MIN;
470 max = EQ_Q_MAX;
471 voice_unit = UNIT_INT;
473 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
474 str(LANG_EQUALIZER_BAND_Q), "");
476 screens[i].putsxy(0, 0, buf);
479 /* Draw scrollbar if needed */
480 if (nb_eq_sliders[i] != 5)
482 if (current_band == 0) {
483 start_item = 0;
484 } else if (current_band == 4) {
485 start_item = 5 - nb_eq_sliders[i];
486 } else {
487 start_item = current_band - 1;
489 x = SCROLLBAR_SIZE;
490 } else {
491 x = 1;
492 start_item = 0;
494 /* Draw equalizer band details */
495 draw_eq_sliders(&screens[i], x, y, nb_eq_sliders[i], start_item,
496 current_band, mode);
498 screens[i].update();
501 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
503 switch (button) {
504 case ACTION_SETTINGS_DEC:
505 case ACTION_SETTINGS_DECREPEAT:
506 *(setting) -= step;
507 has_changed = true;
508 if (*(setting) < min)
509 *(setting) = min;
510 break;
512 case ACTION_SETTINGS_INC:
513 case ACTION_SETTINGS_INCREPEAT:
514 *(setting) += step;
515 has_changed = true;
516 if (*(setting) > max)
517 *(setting) = max;
518 break;
520 case ACTION_SETTINGS_INCBIGSTEP:
521 *(setting) += fast_step;
522 has_changed = true;
523 if (*(setting) > max)
524 *(setting) = max;
525 break;
527 case ACTION_SETTINGS_DECBIGSTEP:
528 *(setting) -= fast_step;
529 has_changed = true;
530 if (*(setting) < min)
531 *(setting) = min;
532 break;
534 case ACTION_STD_PREV:
535 case ACTION_STD_PREVREPEAT:
536 current_band--;
537 if (current_band < 0)
538 current_band = 4; /* wrap around */
539 break;
541 case ACTION_STD_NEXT:
542 case ACTION_STD_NEXTREPEAT:
543 current_band++;
544 if (current_band > 4)
545 current_band = 0; /* wrap around */
546 break;
548 case ACTION_STD_OK:
549 mode++;
550 if (mode > Q)
551 mode = GAIN; /* wrap around */
552 break;
554 case ACTION_STD_CANCEL:
555 exit_request = true;
556 result = false;
557 break;
558 default:
559 if(default_event_handler(button) == SYS_USB_CONNECTED) {
560 exit_request = true;
561 result = true;
563 break;
566 /* Update the filter if the user changed something */
567 if (has_changed) {
568 dsp_set_eq_coefs(current_band);
569 has_changed = false;
573 /* Reset screen settings */
574 FOR_NB_SCREENS(i) {
575 screens[i].setfont(FONT_UI);
576 screens[i].clear_display();
577 screens[i].set_viewport(NULL);
579 viewportmanager_set_statusbar(barsenabled);
580 return result;
583 static bool eq_save_preset(void)
585 /* make sure that the eq is enabled for setting saving */
586 bool enabled = global_settings.eq_enabled;
587 global_settings.eq_enabled = true;
589 bool result = settings_save_config(SETTINGS_SAVE_EQPRESET);
591 global_settings.eq_enabled = enabled;
593 return result;
596 /* Allows browsing of preset files */
597 bool eq_browse_presets(void)
599 return rockbox_browse(EQS_DIR, SHOW_CFG);
602 MENUITEM_FUNCTION(eq_graphical, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
603 (int(*)(void))eq_menu_graphical, NULL, lowlatency_callback,
604 Icon_EQ);
605 MENUITEM_FUNCTION(eq_save, 0, ID2P(LANG_EQUALIZER_SAVE),
606 (int(*)(void))eq_save_preset, NULL, NULL, Icon_NOICON);
607 MENUITEM_FUNCTION(eq_browse, 0, ID2P(LANG_EQUALIZER_BROWSE),
608 (int(*)(void))eq_browse_presets, NULL, lowlatency_callback,
609 Icon_NOICON);
611 MAKE_MENU(equalizer_menu, ID2P(LANG_EQUALIZER), NULL, Icon_EQ,
612 &eq_enable, &eq_graphical, &eq_precut, &gain_menu,
613 &advanced_eq_menu_, &eq_save, &eq_browse);