Move language resources to a separate file. Only build and include langs when buildin...
[Rockbox.git] / apps / menus / eq_menu.c
blob1afd5b6db7a06b4a5b859c90439d24555cb075dd
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 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include "eq_menu.h"
25 #include "system.h"
26 #include "kernel.h"
27 #include "lcd.h"
28 #include "menu.h"
29 #include "action.h"
30 #include "mp3_playback.h"
31 #include "settings.h"
32 #include "statusbar.h"
33 #include "screens.h"
34 #include "icons.h"
35 #include "font.h"
36 #include "lang.h"
37 #include "sprintf.h"
38 #include "talk.h"
39 #include "misc.h"
40 #include "sound.h"
41 #include "splash.h"
42 #include "dsp.h"
43 #include "tree.h"
44 #include "talk.h"
45 #include "screen_access.h"
46 #include "keyboard.h"
47 #include "gui/scrollbar.h"
48 #include "eq_menu.h"
49 #include "menu_common.h"
52 * Utility functions
55 void eq_q_format(char* buffer, size_t buffer_size, int value, const char* unit)
57 snprintf(buffer, buffer_size, "%d.%d %s", value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
60 void eq_precut_format(char* buffer, size_t buffer_size, int value, const char* unit)
62 snprintf(buffer, buffer_size, "%s%d.%d %s", value == 0 ? " " : "-",
63 value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
67 * Settings functions
69 static void eq_apply(void)
71 int i;
72 dsp_set_eq(global_settings.eq_enabled);
73 dsp_set_eq_precut(global_settings.eq_precut);
74 /* Update all bands */
75 for(i = 0; i < 5; i++) {
76 dsp_set_eq_coefs(i);
80 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
82 switch (action)
84 case ACTION_ENTER_MENUITEM:
85 action = lowlatency_callback(action, this_item);
86 break;
87 case ACTION_EXIT_MENUITEM:
88 eq_apply();
89 action = lowlatency_callback(action, this_item);
90 break;
93 return action;
95 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
96 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
98 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band0_cutoff, eq_setting_callback);
99 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band1_cutoff, eq_setting_callback);
100 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band2_cutoff, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band3_cutoff, eq_setting_callback);
102 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band4_cutoff, eq_setting_callback);
104 MENUITEM_SETTING(q_0, &global_settings.eq_band0_q, eq_setting_callback);
105 MENUITEM_SETTING(q_1, &global_settings.eq_band1_q, eq_setting_callback);
106 MENUITEM_SETTING(q_2, &global_settings.eq_band2_q, eq_setting_callback);
107 MENUITEM_SETTING(q_3, &global_settings.eq_band3_q, eq_setting_callback);
108 MENUITEM_SETTING(q_4, &global_settings.eq_band4_q, eq_setting_callback);
110 MENUITEM_SETTING(gain_0, &global_settings.eq_band0_gain, eq_setting_callback);
111 MENUITEM_SETTING(gain_1, &global_settings.eq_band1_gain, eq_setting_callback);
112 MENUITEM_SETTING(gain_2, &global_settings.eq_band2_gain, eq_setting_callback);
113 MENUITEM_SETTING(gain_3, &global_settings.eq_band3_gain, eq_setting_callback);
114 MENUITEM_SETTING(gain_4, &global_settings.eq_band4_gain, eq_setting_callback);
116 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
118 (void)selected_item;
119 int *setting = (int*)data;
120 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
121 return buffer;
124 static int gainitem_speak_item(int selected_item, void * data)
126 (void)selected_item;
127 int *setting = (int*)data;
128 talk_number(*setting, false);
129 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
130 return 0;
133 static int do_option(void * param)
135 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
136 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
137 do_setting_from_menu(setting, NULL);
138 eq_apply();
139 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
140 return 0;
143 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
144 do_option, (void*)&gain_0,
145 gainitem_get_name, gainitem_speak_item,
146 &global_settings.eq_band0_cutoff, NULL, Icon_NOICON);
147 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
148 do_option, (void*)&gain_1,
149 gainitem_get_name, gainitem_speak_item,
150 &global_settings.eq_band1_cutoff, NULL, Icon_NOICON);
151 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
152 do_option, (void*)&gain_2,
153 gainitem_get_name, gainitem_speak_item,
154 &global_settings.eq_band2_cutoff, NULL, Icon_NOICON);
155 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
156 do_option, (void*)&gain_3,
157 gainitem_get_name, gainitem_speak_item,
158 &global_settings.eq_band3_cutoff, NULL, Icon_NOICON);
159 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
160 do_option, (void*)&gain_4,
161 gainitem_get_name, gainitem_speak_item,
162 &global_settings.eq_band4_cutoff, NULL, Icon_NOICON);
164 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
165 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
167 static const struct menu_item_ex *band_items[3][3] = {
168 { &cutoff_1, &q_1, &gain_1 },
169 { &cutoff_2, &q_2, &gain_2 },
170 { &cutoff_3, &q_3, &gain_3 }
173 static char* centerband_get_name(int selected_item, void * data, char *buffer)
175 (void)selected_item;
176 int band = (intptr_t)data;
177 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
178 return buffer;
181 static int centerband_speak_item(int selected_item, void * data)
183 (void)selected_item;
184 int band = (intptr_t)data;
185 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
186 talk_number(band, true);
187 return 0;
190 static int do_center_band_menu(void* param)
192 int band = (intptr_t)param;
193 struct menu_item_ex menu;
194 struct menu_callback_with_desc cb_and_desc;
195 char desc[MAX_PATH];
197 cb_and_desc.menu_callback = NULL;
198 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
199 cb_and_desc.desc = desc;
200 cb_and_desc.icon_id = Icon_EQ;
201 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
202 menu.submenus = band_items[band-1];
203 menu.callback_and_desc = &cb_and_desc;
204 do_menu(&menu, NULL, NULL, false);
205 return 0;
208 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
209 Icon_EQ, &cutoff_0, &q_0, &gain_0);
210 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
211 do_center_band_menu, (void*)1,
212 centerband_get_name, centerband_speak_item,
213 (void*)1, NULL, Icon_EQ);
214 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
215 do_center_band_menu, (void*)2,
216 centerband_get_name, centerband_speak_item,
217 (void*)2, NULL, Icon_EQ);
218 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
219 do_center_band_menu, (void*)3,
220 centerband_get_name, centerband_speak_item,
221 (void*)3, NULL, Icon_EQ);
222 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
223 Icon_EQ, &cutoff_4, &q_4, &gain_4);
225 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
226 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
229 enum eq_slider_mode {
230 GAIN,
231 CUTOFF,
235 enum eq_type {
236 LOW_SHELF,
237 PEAK,
238 HIGH_SHELF
241 /* Draw the UI for a whole EQ band */
242 static int draw_eq_slider(struct screen * screen, int x, int y,
243 int width, int cutoff, int q, int gain, bool selected,
244 enum eq_slider_mode mode, enum eq_type type)
246 char buf[26];
247 const char separator[2] = " ";
248 int steps, min_item, max_item;
249 int abs_gain = abs(gain);
250 int current_x, total_height, separator_width, separator_height;
251 int w, h;
252 const int slider_height = 6;
254 switch(mode) {
255 case Q:
256 steps = EQ_Q_MAX - EQ_Q_MIN;
257 min_item = q - EQ_Q_STEP - EQ_Q_MIN;
258 max_item = q + EQ_Q_STEP - EQ_Q_MIN;
259 break;
260 case CUTOFF:
261 steps = EQ_CUTOFF_MAX - EQ_CUTOFF_MIN;
262 min_item = cutoff - EQ_CUTOFF_FAST_STEP * 2;
263 max_item = cutoff + EQ_CUTOFF_FAST_STEP * 2;
264 break;
265 case GAIN:
266 default:
267 steps = EQ_GAIN_MAX - EQ_GAIN_MIN;
268 min_item = abs(EQ_GAIN_MIN) + gain - EQ_GAIN_STEP * 5;
269 max_item = abs(EQ_GAIN_MIN) + gain + EQ_GAIN_STEP * 5;
270 break;
273 /* Start two pixels in, one for border, one for margin */
274 current_x = x + 2;
276 /* Figure out how large our separator string is */
277 screen->getstringsize(separator, &separator_width, &separator_height);
279 /* Total height includes margins, text, and line selector */
280 total_height = separator_height + slider_height + 2 + 3;
282 /* Print out the band label */
283 if (type == LOW_SHELF) {
284 screen->putsxy(current_x, y + 2, "LS:");
285 screen->getstringsize("LS:", &w, &h);
286 } else if (type == HIGH_SHELF) {
287 screen->putsxy(current_x, y + 2, "HS:");
288 screen->getstringsize("HS:", &w, &h);
289 } else {
290 screen->putsxy(current_x, y + 2, "PK:");
291 screen->getstringsize("PK:", &w, &h);
293 current_x += w;
295 /* Print separator */
296 screen->set_drawmode(DRMODE_SOLID);
297 screen->putsxy(current_x, y + 2, separator);
298 current_x += separator_width;
299 #if NB_SCREENS > 1
300 if (screen->screen_type == SCREEN_REMOTE) {
301 if (mode == GAIN) {
302 screen->putsxy(current_x, y + 2, str(LANG_GAIN));
303 screen->getstringsize(str(LANG_GAIN), &w, &h);
304 } else if (mode == CUTOFF) {
305 screen->putsxy(current_x, y + 2, str(LANG_EQUALIZER_BAND_CUTOFF));
306 screen->getstringsize(str(LANG_EQUALIZER_BAND_CUTOFF), &w, &h);
307 } else {
308 screen->putsxy(current_x, y + 2, str(LANG_EQUALIZER_BAND_Q));
309 screen->getstringsize(str(LANG_EQUALIZER_BAND_Q), &w, &h);
312 /* Draw horizontal slider. Reuse scrollbar for this */
313 gui_scrollbar_draw(screen, x + 3, y + h + 3, width - 6, slider_height, steps,
314 min_item, max_item, HORIZONTAL);
316 /* Print out cutoff part */
317 snprintf(buf, sizeof(buf), "%sGain %s%2d.%ddB",mode==GAIN?" > ":" ", gain < 0 ? "-" : " ",
318 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR);
319 screen->getstringsize(buf, &w, &h);
320 y = 3*h;
321 screen->putsxy(0, y, buf);
322 /* Print out cutoff part */
323 snprintf(buf, sizeof(buf), "%sCutoff %5dHz",mode==CUTOFF?" > ":" ", cutoff);
324 y += h;
325 screen->putsxy(0, y, buf);
326 snprintf(buf, sizeof(buf), "%sQ setting %d.%d Q",mode==Q?" > ":" ", q / EQ_USER_DIVISOR,
327 q % EQ_USER_DIVISOR);
328 y += h;
329 screen->putsxy(0, y, buf);
330 return y;
332 #endif
334 /* Print out gain part of status line */
335 snprintf(buf, sizeof(buf), "%s%2d.%ddB", gain < 0 ? "-" : " ",
336 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR);
338 if (mode == GAIN && selected)
339 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
341 screen->putsxy(current_x, y + 2, buf);
342 screen->getstringsize(buf, &w, &h);
343 current_x += w;
345 /* Print separator */
346 screen->set_drawmode(DRMODE_SOLID);
347 screen->putsxy(current_x, y + 2, separator);
348 current_x += separator_width;
350 /* Print out cutoff part of status line */
351 snprintf(buf, sizeof(buf), "%5dHz", cutoff);
353 if (mode == CUTOFF && selected)
354 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
356 screen->putsxy(current_x, y + 2, buf);
357 screen->getstringsize(buf, &w, &h);
358 current_x += w;
360 /* Print separator */
361 screen->set_drawmode(DRMODE_SOLID);
362 screen->putsxy(current_x, y + 2, separator);
363 current_x += separator_width;
365 /* Print out Q part of status line */
366 snprintf(buf, sizeof(buf), "%d.%d Q", q / EQ_USER_DIVISOR,
367 q % EQ_USER_DIVISOR);
369 if (mode == Q && selected)
370 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
372 screen->putsxy(current_x, y + 2, buf);
373 screen->getstringsize(buf, &w, &h);
374 current_x += w;
376 screen->set_drawmode(DRMODE_SOLID);
378 /* Draw selection box */
379 if (selected) {
380 screen->drawrect(x, y, width, total_height);
383 /* Draw horizontal slider. Reuse scrollbar for this */
384 gui_scrollbar_draw(screen, x + 3, y + h + 3, width - 6, slider_height, steps,
385 min_item, max_item, HORIZONTAL);
387 return total_height;
390 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
391 static int draw_eq_sliders(int current_band, enum eq_slider_mode mode)
393 int i, gain, q, cutoff;
394 int height = 2; /* Two pixel margin */
395 int slider_width[NB_SCREENS];
396 int *setting = &global_settings.eq_band0_cutoff;
397 enum eq_type type;
399 FOR_NB_SCREENS(i)
400 slider_width[i] = screens[i].width - 4; /* two pixel margin on each side */
402 for (i=0; i<5; i++) {
403 cutoff = *setting++;
404 q = *setting++;
405 gain = *setting++;
407 if (i == 0) {
408 type = LOW_SHELF;
409 } else if (i == 4) {
410 type = HIGH_SHELF;
411 } else {
412 type = PEAK;
414 height += draw_eq_slider(&(screens[SCREEN_MAIN]), 2, height,
415 slider_width[SCREEN_MAIN], cutoff, q, gain,
416 i == current_band, mode, type);
417 #if NB_SCREENS > 1
418 if (i == current_band)
419 draw_eq_slider(&(screens[SCREEN_REMOTE]), 2, 0,
420 slider_width[SCREEN_REMOTE], cutoff, q, gain,1, mode, type);
421 #endif
422 /* add a margin */
423 height += 2;
426 return height;
429 /* Provides a graphical means of editing the EQ settings */
430 bool eq_menu_graphical(void)
432 bool exit_request = false;
433 bool result = true;
434 bool has_changed = false;
435 int button;
436 int *setting;
437 int current_band, y, step, fast_step, min, max, voice_unit;
438 enum eq_slider_mode mode;
439 enum eq_type current_type;
440 char buf[24];
441 int i;
443 FOR_NB_SCREENS(i) {
444 screens[i].setfont(FONT_SYSFIXED);
445 screens[i].clear_display();
448 /* Start off editing gain on the first band */
449 mode = GAIN;
450 current_type = LOW_SHELF;
451 current_band = 0;
453 while (!exit_request) {
455 FOR_NB_SCREENS(i) {
456 /* Clear the screen. The drawing routines expect this */
457 screens[i].clear_display();
458 /* Draw equalizer band details */
459 y = draw_eq_sliders(current_band, mode);
462 /* Set pointer to the band data currently editable */
463 if (mode == GAIN) {
464 /* gain */
465 setting = &global_settings.eq_band0_gain;
466 setting += current_band * 3;
468 step = EQ_GAIN_STEP;
469 fast_step = EQ_GAIN_FAST_STEP;
470 min = EQ_GAIN_MIN;
471 max = EQ_GAIN_MAX;
472 voice_unit = UNIT_DB;
474 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
475 str(LANG_SYSFONT_GAIN));
477 screens[SCREEN_MAIN].putsxy(2, y, buf);
478 } else if (mode == CUTOFF) {
479 /* cutoff */
480 setting = &global_settings.eq_band0_cutoff;
481 setting += current_band * 3;
483 step = EQ_CUTOFF_STEP;
484 fast_step = EQ_CUTOFF_FAST_STEP;
485 min = EQ_CUTOFF_MIN;
486 max = EQ_CUTOFF_MAX;
487 voice_unit = UNIT_HERTZ;
489 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
490 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF));
492 screens[SCREEN_MAIN].putsxy(2, y, buf);
493 } else {
494 /* Q */
495 setting = &global_settings.eq_band0_q;
496 setting += current_band * 3;
498 step = EQ_Q_STEP;
499 fast_step = EQ_Q_FAST_STEP;
500 min = EQ_Q_MIN;
501 max = EQ_Q_MAX;
502 voice_unit = UNIT_INT;
504 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
505 str(LANG_EQUALIZER_BAND_Q));
507 screens[SCREEN_MAIN].putsxy(2, y, buf);
510 FOR_NB_SCREENS(i) {
511 screens[i].update();
514 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
516 switch (button) {
517 case ACTION_SETTINGS_DEC:
518 case ACTION_SETTINGS_DECREPEAT:
519 *(setting) -= step;
520 has_changed = true;
521 if (*(setting) < min)
522 *(setting) = min;
523 break;
525 case ACTION_SETTINGS_INC:
526 case ACTION_SETTINGS_INCREPEAT:
527 *(setting) += step;
528 has_changed = true;
529 if (*(setting) > max)
530 *(setting) = max;
531 break;
533 case ACTION_SETTINGS_INCBIGSTEP:
534 *(setting) += fast_step;
535 has_changed = true;
536 if (*(setting) > max)
537 *(setting) = max;
538 break;
540 case ACTION_SETTINGS_DECBIGSTEP:
541 *(setting) -= fast_step;
542 has_changed = true;
543 if (*(setting) < min)
544 *(setting) = min;
545 break;
547 case ACTION_STD_PREV:
548 case ACTION_STD_PREVREPEAT:
549 current_band--;
550 if (current_band < 0)
551 current_band = 4; /* wrap around */
552 break;
554 case ACTION_STD_NEXT:
555 case ACTION_STD_NEXTREPEAT:
556 current_band++;
557 if (current_band > 4)
558 current_band = 0; /* wrap around */
559 break;
561 case ACTION_STD_OK:
562 mode++;
563 if (mode > Q)
564 mode = GAIN; /* wrap around */
565 break;
567 case ACTION_STD_CANCEL:
568 exit_request = true;
569 result = false;
570 break;
572 default:
573 if(default_event_handler(button) == SYS_USB_CONNECTED) {
574 exit_request = true;
575 result = true;
577 break;
580 /* Update the filter if the user changed something */
581 if (has_changed) {
582 dsp_set_eq_coefs(current_band);
583 has_changed = false;
587 /* Reset screen settings */
588 FOR_NB_SCREENS(i) {
589 screens[i].setfont(FONT_UI);
590 screens[i].clear_display();
593 return result;
596 static bool eq_save_preset(void)
598 /* make sure that the eq is enabled for setting saving */
599 bool enabled = global_settings.eq_enabled;
600 global_settings.eq_enabled = true;
602 bool result = settings_save_config(SETTINGS_SAVE_EQPRESET);
604 global_settings.eq_enabled = enabled;
606 return result;
609 /* Allows browsing of preset files */
610 bool eq_browse_presets(void)
612 return rockbox_browse(EQS_DIR, SHOW_CFG);
615 MENUITEM_FUNCTION(eq_graphical, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
616 (int(*)(void))eq_menu_graphical, NULL, lowlatency_callback,
617 Icon_EQ);
618 MENUITEM_FUNCTION(eq_save, 0, ID2P(LANG_EQUALIZER_SAVE),
619 (int(*)(void))eq_save_preset, NULL, NULL, Icon_NOICON);
620 MENUITEM_FUNCTION(eq_browse, 0, ID2P(LANG_EQUALIZER_BROWSE),
621 (int(*)(void))eq_browse_presets, NULL, lowlatency_callback,
622 Icon_NOICON);
624 MAKE_MENU(equalizer_menu, ID2P(LANG_EQUALIZER), NULL, Icon_EQ,
625 &eq_enable, &eq_graphical, &eq_precut, &gain_menu,
626 &advanced_eq_menu_, &eq_save, &eq_browse);