Remove a viewport ambiguity by changing the screens width/heigth members into lcdwidt...
[Rockbox.git] / apps / menus / eq_menu.c
blob279f8255c2a52ef768cb2f57e35c4ccb6cecc40a
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"
54 * Utility functions
57 void eq_q_format(char* buffer, size_t buffer_size, int value, const char* unit)
59 snprintf(buffer, buffer_size, "%d.%d %s", value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
62 void eq_precut_format(char* buffer, size_t buffer_size, int value, const char* unit)
64 snprintf(buffer, buffer_size, "%s%d.%d %s", value == 0 ? " " : "-",
65 value / EQ_USER_DIVISOR, value % EQ_USER_DIVISOR, unit);
69 * Settings functions
71 static void eq_apply(void)
73 int i;
74 dsp_set_eq(global_settings.eq_enabled);
75 dsp_set_eq_precut(global_settings.eq_precut);
76 /* Update all bands */
77 for(i = 0; i < 5; i++) {
78 dsp_set_eq_coefs(i);
82 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
84 switch (action)
86 case ACTION_ENTER_MENUITEM:
87 action = lowlatency_callback(action, this_item);
88 break;
89 case ACTION_EXIT_MENUITEM:
90 eq_apply();
91 action = lowlatency_callback(action, this_item);
92 break;
95 return action;
97 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
98 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
100 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band0_cutoff, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band1_cutoff, eq_setting_callback);
102 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band2_cutoff, eq_setting_callback);
103 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band3_cutoff, eq_setting_callback);
104 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band4_cutoff, eq_setting_callback);
106 MENUITEM_SETTING(q_0, &global_settings.eq_band0_q, eq_setting_callback);
107 MENUITEM_SETTING(q_1, &global_settings.eq_band1_q, eq_setting_callback);
108 MENUITEM_SETTING(q_2, &global_settings.eq_band2_q, eq_setting_callback);
109 MENUITEM_SETTING(q_3, &global_settings.eq_band3_q, eq_setting_callback);
110 MENUITEM_SETTING(q_4, &global_settings.eq_band4_q, eq_setting_callback);
112 MENUITEM_SETTING(gain_0, &global_settings.eq_band0_gain, eq_setting_callback);
113 MENUITEM_SETTING(gain_1, &global_settings.eq_band1_gain, eq_setting_callback);
114 MENUITEM_SETTING(gain_2, &global_settings.eq_band2_gain, eq_setting_callback);
115 MENUITEM_SETTING(gain_3, &global_settings.eq_band3_gain, eq_setting_callback);
116 MENUITEM_SETTING(gain_4, &global_settings.eq_band4_gain, eq_setting_callback);
118 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
120 (void)selected_item;
121 int *setting = (int*)data;
122 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
123 return buffer;
126 static int gainitem_speak_item(int selected_item, void * data)
128 (void)selected_item;
129 int *setting = (int*)data;
130 talk_number(*setting, false);
131 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
132 return 0;
135 static int do_option(void * param)
137 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
138 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
139 do_setting_from_menu(setting, NULL);
140 eq_apply();
141 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
142 return 0;
145 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
146 do_option, (void*)&gain_0,
147 gainitem_get_name, gainitem_speak_item,
148 &global_settings.eq_band0_cutoff, NULL, Icon_NOICON);
149 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
150 do_option, (void*)&gain_1,
151 gainitem_get_name, gainitem_speak_item,
152 &global_settings.eq_band1_cutoff, NULL, Icon_NOICON);
153 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
154 do_option, (void*)&gain_2,
155 gainitem_get_name, gainitem_speak_item,
156 &global_settings.eq_band2_cutoff, NULL, Icon_NOICON);
157 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
158 do_option, (void*)&gain_3,
159 gainitem_get_name, gainitem_speak_item,
160 &global_settings.eq_band3_cutoff, NULL, Icon_NOICON);
161 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
162 do_option, (void*)&gain_4,
163 gainitem_get_name, gainitem_speak_item,
164 &global_settings.eq_band4_cutoff, NULL, Icon_NOICON);
166 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
167 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
169 static const struct menu_item_ex *band_items[3][3] = {
170 { &cutoff_1, &q_1, &gain_1 },
171 { &cutoff_2, &q_2, &gain_2 },
172 { &cutoff_3, &q_3, &gain_3 }
175 static char* centerband_get_name(int selected_item, void * data, char *buffer)
177 (void)selected_item;
178 int band = (intptr_t)data;
179 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
180 return buffer;
183 static int centerband_speak_item(int selected_item, void * data)
185 (void)selected_item;
186 int band = (intptr_t)data;
187 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
188 talk_number(band, true);
189 return 0;
192 static int do_center_band_menu(void* param)
194 int band = (intptr_t)param;
195 struct menu_item_ex menu;
196 struct menu_callback_with_desc cb_and_desc;
197 char desc[MAX_PATH];
199 cb_and_desc.menu_callback = NULL;
200 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
201 cb_and_desc.desc = desc;
202 cb_and_desc.icon_id = Icon_EQ;
203 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
204 menu.submenus = band_items[band-1];
205 menu.callback_and_desc = &cb_and_desc;
206 do_menu(&menu, NULL, NULL, false);
207 return 0;
210 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
211 Icon_EQ, &cutoff_0, &q_0, &gain_0);
212 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
213 do_center_band_menu, (void*)1,
214 centerband_get_name, centerband_speak_item,
215 (void*)1, NULL, Icon_EQ);
216 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
217 do_center_band_menu, (void*)2,
218 centerband_get_name, centerband_speak_item,
219 (void*)2, NULL, Icon_EQ);
220 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
221 do_center_band_menu, (void*)3,
222 centerband_get_name, centerband_speak_item,
223 (void*)3, NULL, Icon_EQ);
224 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
225 Icon_EQ, &cutoff_4, &q_4, &gain_4);
227 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
228 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
231 enum eq_slider_mode {
232 GAIN,
233 CUTOFF,
237 enum eq_type {
238 LOW_SHELF,
239 PEAK,
240 HIGH_SHELF
243 /* Draw the UI for a whole EQ band */
244 static int draw_eq_slider(struct screen * screen, int x, int y,
245 int width, int cutoff, int q, int gain, bool selected,
246 enum eq_slider_mode mode, enum eq_type type)
248 char buf[26];
249 const char separator[2] = " ";
250 int steps, min_item, max_item;
251 int abs_gain = abs(gain);
252 int current_x, total_height, separator_width, separator_height;
253 int w, h;
254 const int slider_height = 6;
256 switch(mode) {
257 case Q:
258 steps = EQ_Q_MAX - EQ_Q_MIN;
259 min_item = q - EQ_Q_STEP - EQ_Q_MIN;
260 max_item = q + EQ_Q_STEP - EQ_Q_MIN;
261 break;
262 case CUTOFF:
263 steps = EQ_CUTOFF_MAX - EQ_CUTOFF_MIN;
264 min_item = cutoff - EQ_CUTOFF_FAST_STEP * 2;
265 max_item = cutoff + EQ_CUTOFF_FAST_STEP * 2;
266 break;
267 case GAIN:
268 default:
269 steps = EQ_GAIN_MAX - EQ_GAIN_MIN;
270 min_item = abs(EQ_GAIN_MIN) + gain - EQ_GAIN_STEP * 5;
271 max_item = abs(EQ_GAIN_MIN) + gain + EQ_GAIN_STEP * 5;
272 break;
275 /* Start two pixels in, one for border, one for margin */
276 current_x = x + 2;
278 /* Figure out how large our separator string is */
279 screen->getstringsize(separator, &separator_width, &separator_height);
281 /* Total height includes margins, text, and line selector */
282 total_height = separator_height + slider_height + 2 + 3;
284 /* Print out the band label */
285 if (type == LOW_SHELF) {
286 screen->putsxy(current_x, y + 2, "LS:");
287 screen->getstringsize("LS:", &w, &h);
288 } else if (type == HIGH_SHELF) {
289 screen->putsxy(current_x, y + 2, "HS:");
290 screen->getstringsize("HS:", &w, &h);
291 } else {
292 screen->putsxy(current_x, y + 2, "PK:");
293 screen->getstringsize("PK:", &w, &h);
295 current_x += w;
297 /* Print separator */
298 screen->set_drawmode(DRMODE_SOLID);
299 screen->putsxy(current_x, y + 2, separator);
300 current_x += separator_width;
301 #if NB_SCREENS > 1
302 if (screen->screen_type == SCREEN_REMOTE) {
303 if (mode == GAIN) {
304 screen->putsxy(current_x, y + 2, str(LANG_GAIN));
305 screen->getstringsize(str(LANG_GAIN), &w, &h);
306 } else if (mode == CUTOFF) {
307 screen->putsxy(current_x, y + 2, str(LANG_EQUALIZER_BAND_CUTOFF));
308 screen->getstringsize(str(LANG_EQUALIZER_BAND_CUTOFF), &w, &h);
309 } else {
310 screen->putsxy(current_x, y + 2, str(LANG_EQUALIZER_BAND_Q));
311 screen->getstringsize(str(LANG_EQUALIZER_BAND_Q), &w, &h);
314 /* Draw horizontal slider. Reuse scrollbar for this */
315 gui_scrollbar_draw(screen, x + 3, y + h + 3, width - 6, slider_height, steps,
316 min_item, max_item, HORIZONTAL);
318 /* Print out cutoff part */
319 snprintf(buf, sizeof(buf), "%sGain %s%2d.%ddB",mode==GAIN?" > ":" ", gain < 0 ? "-" : " ",
320 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR);
321 screen->getstringsize(buf, &w, &h);
322 y = 3*h;
323 screen->putsxy(0, y, buf);
324 /* Print out cutoff part */
325 snprintf(buf, sizeof(buf), "%sCutoff %5dHz",mode==CUTOFF?" > ":" ", cutoff);
326 y += h;
327 screen->putsxy(0, y, buf);
328 snprintf(buf, sizeof(buf), "%sQ setting %d.%d Q",mode==Q?" > ":" ", q / EQ_USER_DIVISOR,
329 q % EQ_USER_DIVISOR);
330 y += h;
331 screen->putsxy(0, y, buf);
332 return y;
334 #endif
336 /* Print out gain part of status line */
337 snprintf(buf, sizeof(buf), "%s%2d.%ddB", gain < 0 ? "-" : " ",
338 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR);
340 if (mode == GAIN && selected)
341 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
343 screen->putsxy(current_x, y + 2, buf);
344 screen->getstringsize(buf, &w, &h);
345 current_x += w;
347 /* Print separator */
348 screen->set_drawmode(DRMODE_SOLID);
349 screen->putsxy(current_x, y + 2, separator);
350 current_x += separator_width;
352 /* Print out cutoff part of status line */
353 snprintf(buf, sizeof(buf), "%5dHz", cutoff);
355 if (mode == CUTOFF && selected)
356 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
358 screen->putsxy(current_x, y + 2, buf);
359 screen->getstringsize(buf, &w, &h);
360 current_x += w;
362 /* Print separator */
363 screen->set_drawmode(DRMODE_SOLID);
364 screen->putsxy(current_x, y + 2, separator);
365 current_x += separator_width;
367 /* Print out Q part of status line */
368 snprintf(buf, sizeof(buf), "%d.%d Q", q / EQ_USER_DIVISOR,
369 q % EQ_USER_DIVISOR);
371 if (mode == Q && selected)
372 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
374 screen->putsxy(current_x, y + 2, buf);
375 screen->getstringsize(buf, &w, &h);
376 current_x += w;
378 screen->set_drawmode(DRMODE_SOLID);
380 /* Draw selection box */
381 if (selected) {
382 screen->drawrect(x, y, width, total_height);
385 /* Draw horizontal slider. Reuse scrollbar for this */
386 gui_scrollbar_draw(screen, x + 3, y + h + 3, width - 6, slider_height, steps,
387 min_item, max_item, HORIZONTAL);
389 return total_height;
392 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
393 static int draw_eq_sliders(int current_band, enum eq_slider_mode mode)
395 int i, gain, q, cutoff;
396 int height = 2; /* Two pixel margin */
397 int slider_width[NB_SCREENS];
398 int *setting = &global_settings.eq_band0_cutoff;
399 enum eq_type type;
401 FOR_NB_SCREENS(i)
402 slider_width[i] = screens[i].getwidth() - 4; /* 2 pixel margin
403 each side */
404 for (i=0; i<5; i++) {
405 cutoff = *setting++;
406 q = *setting++;
407 gain = *setting++;
409 if (i == 0) {
410 type = LOW_SHELF;
411 } else if (i == 4) {
412 type = HIGH_SHELF;
413 } else {
414 type = PEAK;
416 height += draw_eq_slider(&(screens[SCREEN_MAIN]), 2, height,
417 slider_width[SCREEN_MAIN], cutoff, q, gain,
418 i == current_band, mode, type);
419 #if NB_SCREENS > 1
420 if (i == current_band)
421 draw_eq_slider(&(screens[SCREEN_REMOTE]), 2, 0,
422 slider_width[SCREEN_REMOTE], cutoff, q, gain,1, mode, type);
423 #endif
424 /* add a margin */
425 height += 2;
428 return height;
431 /* Provides a graphical means of editing the EQ settings */
432 bool eq_menu_graphical(void)
434 bool exit_request = false;
435 bool result = true;
436 bool has_changed = false;
437 int button;
438 int *setting;
439 int current_band, y, step, fast_step, min, max, voice_unit;
440 enum eq_slider_mode mode;
441 enum eq_type current_type;
442 char buf[24];
443 int i;
445 FOR_NB_SCREENS(i) {
446 screens[i].setfont(FONT_SYSFIXED);
447 screens[i].clear_display();
450 /* Start off editing gain on the first band */
451 mode = GAIN;
452 current_type = LOW_SHELF;
453 current_band = 0;
455 while (!exit_request) {
457 FOR_NB_SCREENS(i) {
458 /* Clear the screen. The drawing routines expect this */
459 screens[i].clear_display();
460 /* Draw equalizer band details */
461 y = draw_eq_sliders(current_band, mode);
464 /* Set pointer to the band data currently editable */
465 if (mode == GAIN) {
466 /* gain */
467 setting = &global_settings.eq_band0_gain;
468 setting += current_band * 3;
470 step = EQ_GAIN_STEP;
471 fast_step = EQ_GAIN_FAST_STEP;
472 min = EQ_GAIN_MIN;
473 max = EQ_GAIN_MAX;
474 voice_unit = UNIT_DB;
476 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
477 str(LANG_SYSFONT_GAIN));
479 screens[SCREEN_MAIN].putsxy(2, y, buf);
480 } else if (mode == CUTOFF) {
481 /* cutoff */
482 setting = &global_settings.eq_band0_cutoff;
483 setting += current_band * 3;
485 step = EQ_CUTOFF_STEP;
486 fast_step = EQ_CUTOFF_FAST_STEP;
487 min = EQ_CUTOFF_MIN;
488 max = EQ_CUTOFF_MAX;
489 voice_unit = UNIT_HERTZ;
491 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
492 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF));
494 screens[SCREEN_MAIN].putsxy(2, y, buf);
495 } else {
496 /* Q */
497 setting = &global_settings.eq_band0_q;
498 setting += current_band * 3;
500 step = EQ_Q_STEP;
501 fast_step = EQ_Q_FAST_STEP;
502 min = EQ_Q_MIN;
503 max = EQ_Q_MAX;
504 voice_unit = UNIT_INT;
506 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
507 str(LANG_EQUALIZER_BAND_Q));
509 screens[SCREEN_MAIN].putsxy(2, y, buf);
512 FOR_NB_SCREENS(i) {
513 screens[i].update();
516 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
518 switch (button) {
519 case ACTION_SETTINGS_DEC:
520 case ACTION_SETTINGS_DECREPEAT:
521 *(setting) -= step;
522 has_changed = true;
523 if (*(setting) < min)
524 *(setting) = min;
525 break;
527 case ACTION_SETTINGS_INC:
528 case ACTION_SETTINGS_INCREPEAT:
529 *(setting) += step;
530 has_changed = true;
531 if (*(setting) > max)
532 *(setting) = max;
533 break;
535 case ACTION_SETTINGS_INCBIGSTEP:
536 *(setting) += fast_step;
537 has_changed = true;
538 if (*(setting) > max)
539 *(setting) = max;
540 break;
542 case ACTION_SETTINGS_DECBIGSTEP:
543 *(setting) -= fast_step;
544 has_changed = true;
545 if (*(setting) < min)
546 *(setting) = min;
547 break;
549 case ACTION_STD_PREV:
550 case ACTION_STD_PREVREPEAT:
551 current_band--;
552 if (current_band < 0)
553 current_band = 4; /* wrap around */
554 break;
556 case ACTION_STD_NEXT:
557 case ACTION_STD_NEXTREPEAT:
558 current_band++;
559 if (current_band > 4)
560 current_band = 0; /* wrap around */
561 break;
563 case ACTION_STD_OK:
564 mode++;
565 if (mode > Q)
566 mode = GAIN; /* wrap around */
567 break;
569 case ACTION_STD_CANCEL:
570 exit_request = true;
571 result = false;
572 break;
574 default:
575 if(default_event_handler(button) == SYS_USB_CONNECTED) {
576 exit_request = true;
577 result = true;
579 break;
582 /* Update the filter if the user changed something */
583 if (has_changed) {
584 dsp_set_eq_coefs(current_band);
585 has_changed = false;
589 /* Reset screen settings */
590 FOR_NB_SCREENS(i) {
591 screens[i].setfont(FONT_UI);
592 screens[i].clear_display();
595 return result;
598 static bool eq_save_preset(void)
600 /* make sure that the eq is enabled for setting saving */
601 bool enabled = global_settings.eq_enabled;
602 global_settings.eq_enabled = true;
604 bool result = settings_save_config(SETTINGS_SAVE_EQPRESET);
606 global_settings.eq_enabled = enabled;
608 return result;
611 /* Allows browsing of preset files */
612 bool eq_browse_presets(void)
614 return rockbox_browse(EQS_DIR, SHOW_CFG);
617 MENUITEM_FUNCTION(eq_graphical, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
618 (int(*)(void))eq_menu_graphical, NULL, lowlatency_callback,
619 Icon_EQ);
620 MENUITEM_FUNCTION(eq_save, 0, ID2P(LANG_EQUALIZER_SAVE),
621 (int(*)(void))eq_save_preset, NULL, NULL, Icon_NOICON);
622 MENUITEM_FUNCTION(eq_browse, 0, ID2P(LANG_EQUALIZER_BROWSE),
623 (int(*)(void))eq_browse_presets, NULL, lowlatency_callback,
624 Icon_NOICON);
626 MAKE_MENU(equalizer_menu, ID2P(LANG_EQUALIZER), NULL, Icon_EQ,
627 &eq_enable, &eq_graphical, &eq_precut, &gain_menu,
628 &advanced_eq_menu_, &eq_save, &eq_browse);