Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / menus / eq_menu.c
blob066787a8968b4d7cc63a242943c519fcf32ba560
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 "screens.h"
35 #include "icons.h"
36 #include "font.h"
37 #include "lang.h"
38 #include "talk.h"
39 #include "misc.h"
40 #include "sound.h"
41 #include "dsp.h"
42 #include "tree.h"
43 #include "screen_access.h"
44 #include "keyboard.h"
45 #include "gui/scrollbar.h"
46 #include "menu_common.h"
47 #include "viewport.h"
50 * Utility functions
53 const char* eq_q_format(char* buffer, size_t buffer_size, int value, const char* unit)
55 snprintf(buffer, buffer_size, "%d.%d %s", value / EQ_USER_DIVISOR,
56 value % EQ_USER_DIVISOR, unit);
57 return buffer;
60 const char* 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);
64 return buffer;
68 * Settings functions
70 static void eq_apply(void)
72 int i;
73 dsp_set_eq(global_settings.eq_enabled);
74 dsp_set_eq_precut(global_settings.eq_precut);
75 /* Update all bands */
76 for(i = 0; i < 5; i++) {
77 dsp_set_eq_coefs(i);
81 static int eq_setting_callback(int action, const struct menu_item_ex *this_item)
83 switch (action)
85 case ACTION_ENTER_MENUITEM:
86 action = lowlatency_callback(action, this_item);
87 break;
88 case ACTION_EXIT_MENUITEM:
89 eq_apply();
90 action = lowlatency_callback(action, this_item);
91 break;
94 return action;
96 MENUITEM_SETTING(eq_enable, &global_settings.eq_enabled, eq_setting_callback);
97 MENUITEM_SETTING(eq_precut, &global_settings.eq_precut, eq_setting_callback);
99 MENUITEM_SETTING(cutoff_0, &global_settings.eq_band0_cutoff, eq_setting_callback);
100 MENUITEM_SETTING(cutoff_1, &global_settings.eq_band1_cutoff, eq_setting_callback);
101 MENUITEM_SETTING(cutoff_2, &global_settings.eq_band2_cutoff, eq_setting_callback);
102 MENUITEM_SETTING(cutoff_3, &global_settings.eq_band3_cutoff, eq_setting_callback);
103 MENUITEM_SETTING(cutoff_4, &global_settings.eq_band4_cutoff, eq_setting_callback);
105 MENUITEM_SETTING(q_0, &global_settings.eq_band0_q, eq_setting_callback);
106 MENUITEM_SETTING(q_1, &global_settings.eq_band1_q, eq_setting_callback);
107 MENUITEM_SETTING(q_2, &global_settings.eq_band2_q, eq_setting_callback);
108 MENUITEM_SETTING(q_3, &global_settings.eq_band3_q, eq_setting_callback);
109 MENUITEM_SETTING(q_4, &global_settings.eq_band4_q, eq_setting_callback);
111 MENUITEM_SETTING(gain_0, &global_settings.eq_band0_gain, eq_setting_callback);
112 MENUITEM_SETTING(gain_1, &global_settings.eq_band1_gain, eq_setting_callback);
113 MENUITEM_SETTING(gain_2, &global_settings.eq_band2_gain, eq_setting_callback);
114 MENUITEM_SETTING(gain_3, &global_settings.eq_band3_gain, eq_setting_callback);
115 MENUITEM_SETTING(gain_4, &global_settings.eq_band4_gain, eq_setting_callback);
117 static char* gainitem_get_name(int selected_item, void * data, char *buffer)
119 (void)selected_item;
120 int *setting = (int*)data;
121 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_GAIN_ITEM), *setting);
122 return buffer;
125 static int gainitem_speak_item(int selected_item, void * data)
127 (void)selected_item;
128 int *setting = (int*)data;
129 talk_number(*setting, false);
130 talk_id(LANG_EQUALIZER_GAIN_ITEM, true);
131 return 0;
134 static int do_option(void * param)
136 const struct menu_item_ex *setting = (const struct menu_item_ex*)param;
137 lowlatency_callback(ACTION_ENTER_MENUITEM, setting);
138 do_setting_from_menu(setting, NULL);
139 eq_apply();
140 lowlatency_callback(ACTION_EXIT_MENUITEM, setting);
141 return 0;
144 MENUITEM_FUNCTION_DYNTEXT(gain_item_0, MENU_FUNC_USEPARAM,
145 do_option, (void*)&gain_0,
146 gainitem_get_name, gainitem_speak_item,
147 &global_settings.eq_band0_cutoff, NULL, Icon_NOICON);
148 MENUITEM_FUNCTION_DYNTEXT(gain_item_1, MENU_FUNC_USEPARAM,
149 do_option, (void*)&gain_1,
150 gainitem_get_name, gainitem_speak_item,
151 &global_settings.eq_band1_cutoff, NULL, Icon_NOICON);
152 MENUITEM_FUNCTION_DYNTEXT(gain_item_2, MENU_FUNC_USEPARAM,
153 do_option, (void*)&gain_2,
154 gainitem_get_name, gainitem_speak_item,
155 &global_settings.eq_band2_cutoff, NULL, Icon_NOICON);
156 MENUITEM_FUNCTION_DYNTEXT(gain_item_3, MENU_FUNC_USEPARAM,
157 do_option, (void*)&gain_3,
158 gainitem_get_name, gainitem_speak_item,
159 &global_settings.eq_band3_cutoff, NULL, Icon_NOICON);
160 MENUITEM_FUNCTION_DYNTEXT(gain_item_4, MENU_FUNC_USEPARAM,
161 do_option, (void*)&gain_4,
162 gainitem_get_name, gainitem_speak_item,
163 &global_settings.eq_band4_cutoff, NULL, Icon_NOICON);
165 MAKE_MENU(gain_menu, ID2P(LANG_EQUALIZER_GAIN), NULL, Icon_NOICON, &gain_item_0,
166 &gain_item_1, &gain_item_2, &gain_item_3, &gain_item_4);
168 static const struct menu_item_ex *band_items[3][3] = {
169 { &cutoff_1, &q_1, &gain_1 },
170 { &cutoff_2, &q_2, &gain_2 },
171 { &cutoff_3, &q_3, &gain_3 }
174 static char* centerband_get_name(int selected_item, void * data, char *buffer)
176 (void)selected_item;
177 int band = (intptr_t)data;
178 snprintf(buffer, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
179 return buffer;
182 static int centerband_speak_item(int selected_item, void * data)
184 (void)selected_item;
185 int band = (intptr_t)data;
186 talk_id(LANG_EQUALIZER_BAND_PEAK, false);
187 talk_number(band, true);
188 return 0;
191 static int do_center_band_menu(void* param)
193 int band = (intptr_t)param;
194 struct menu_item_ex menu;
195 struct menu_callback_with_desc cb_and_desc;
196 char desc[MAX_PATH];
198 cb_and_desc.menu_callback = NULL;
199 snprintf(desc, MAX_PATH, str(LANG_EQUALIZER_BAND_PEAK), band);
200 cb_and_desc.desc = desc;
201 cb_and_desc.icon_id = Icon_EQ;
202 menu.flags = MT_MENU|(3<<MENU_COUNT_SHIFT)|MENU_HAS_DESC;
203 menu.submenus = band_items[band-1];
204 menu.callback_and_desc = &cb_and_desc;
205 do_menu(&menu, NULL, NULL, false);
206 return 0;
209 MAKE_MENU(band_0_menu, ID2P(LANG_EQUALIZER_BAND_LOW_SHELF), NULL,
210 Icon_EQ, &cutoff_0, &q_0, &gain_0);
211 MENUITEM_FUNCTION_DYNTEXT(band_1_menu, MENU_FUNC_USEPARAM,
212 do_center_band_menu, (void*)1,
213 centerband_get_name, centerband_speak_item,
214 (void*)1, NULL, Icon_EQ);
215 MENUITEM_FUNCTION_DYNTEXT(band_2_menu, MENU_FUNC_USEPARAM,
216 do_center_band_menu, (void*)2,
217 centerband_get_name, centerband_speak_item,
218 (void*)2, NULL, Icon_EQ);
219 MENUITEM_FUNCTION_DYNTEXT(band_3_menu, MENU_FUNC_USEPARAM,
220 do_center_band_menu, (void*)3,
221 centerband_get_name, centerband_speak_item,
222 (void*)3, NULL, Icon_EQ);
223 MAKE_MENU(band_4_menu, ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF), NULL,
224 Icon_EQ, &cutoff_4, &q_4, &gain_4);
226 MAKE_MENU(advanced_eq_menu_, ID2P(LANG_EQUALIZER_ADVANCED), NULL, Icon_EQ,
227 &band_0_menu, &band_1_menu, &band_2_menu, &band_3_menu, &band_4_menu);
230 enum eq_slider_mode {
231 GAIN,
232 CUTOFF,
236 enum eq_type {
237 LOW_SHELF,
238 PEAK,
239 HIGH_SHELF
242 /* Size of just the slider/srollbar */
243 #define SCROLLBAR_SIZE 6
245 /* Draw the UI for a whole EQ band */
246 static int draw_eq_slider(struct screen * screen, int x, int y,
247 int width, int cutoff, int q, int gain, bool selected,
248 enum eq_slider_mode mode, int band)
250 char buf[26];
251 int steps, min_item, max_item;
252 int abs_gain = abs(gain);
253 int x1, x2, y1, total_height;
254 int w, h;
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 x1 = x + 2;
277 y1 = y + 2;
279 /* Print out the band label */
280 if (band == 0) {
281 screen->putsxy(x1, y1, "LS: ");
282 screen->getstringsize("LS:", &w, &h);
283 } else if (band == 4) {
284 screen->putsxy(x1, y1, "HS: ");
285 screen->getstringsize("HS:", &w, &h);
286 } else {
287 snprintf(buf, sizeof(buf), "PK%d:", band);
288 screen->putsxy(x1, y1, buf);
289 screen->getstringsize(buf, &w, &h);
292 screen->getstringsize("A", &w, &h);
293 x1 += 5*w; /* 4 chars for label + 1 space = 5 */
295 /* Print out gain part of status line (left justify after label) */
296 if (mode == GAIN && selected)
297 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
298 else
299 screen->set_drawmode(DRMODE_SOLID);
301 snprintf(buf, sizeof(buf), "%s%2d.%d%s", gain < 0 ? "-" : " ",
302 abs_gain / EQ_USER_DIVISOR, abs_gain % EQ_USER_DIVISOR,
303 screen->lcdwidth >= 160 ? "dB" : "");
304 screen->putsxy(x1, y1, buf);
305 screen->getstringsize(buf, &w, &h);
306 x1 += w;
308 /* Print out Q part of status line (right justify) */
309 if (mode == Q && selected)
310 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
311 else
312 screen->set_drawmode(DRMODE_SOLID);
314 snprintf(buf, sizeof(buf), "%d.%d%s", q / EQ_USER_DIVISOR,
315 q % EQ_USER_DIVISOR, screen->lcdwidth >= 160 ? " Q" : "");
316 screen->getstringsize(buf, &w, &h);
317 x2 = x + width - w - 2;
318 screen->putsxy(x2, y1, buf);
320 /* Print out cutoff part of status line (center between gain & Q) */
321 if (mode == CUTOFF && selected)
322 screen->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
323 else
324 screen->set_drawmode(DRMODE_SOLID);
326 snprintf(buf, sizeof(buf), "%5d%s", cutoff,
327 screen->lcdwidth >= 160 ? "Hz" : "");
328 screen->getstringsize(buf, &w, &h);
329 x1 = x1 + (x2 - x1 - w)/2;
330 screen->putsxy(x1, y1, buf);
332 /* Draw selection box */
333 total_height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
334 screen->set_drawmode(DRMODE_SOLID);
335 if (selected) {
336 screen->drawrect(x, y, width, total_height);
339 /* Draw horizontal slider. Reuse scrollbar for this */
340 gui_scrollbar_draw(screen, x + 3, y1 + h + 1, width - 6, SCROLLBAR_SIZE,
341 steps, min_item, max_item, HORIZONTAL);
343 return total_height;
346 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
347 static void draw_eq_sliders(struct screen * screen, int x, int y,
348 int nb_eq_sliders, int start_item,
349 int current_band, enum eq_slider_mode mode)
351 int i, gain, q, cutoff;
352 int height = y;
353 int *setting = &global_settings.eq_band0_cutoff;
355 start_item = MIN(start_item, 5-nb_eq_sliders);
356 for (i=0; i<5; i++) {
357 cutoff = *setting++;
358 q = *setting++;
359 gain = *setting++;
361 if (i == start_item + nb_eq_sliders)
362 break;
364 if (i >= start_item) {
365 height += draw_eq_slider(screen, x, height, screen->lcdwidth - x - 1,
366 cutoff, q, gain, i == current_band, mode,
368 /* add a margin */
369 height++;
372 if (nb_eq_sliders != 5)
373 gui_scrollbar_draw(screen, 0, y, SCROLLBAR_SIZE - 1,
374 screen->lcdheight - y, 5,
375 start_item, start_item + nb_eq_sliders,
376 VERTICAL);
377 return;
380 /* Provides a graphical means of editing the EQ settings */
381 bool eq_menu_graphical(void)
383 bool exit_request = false;
384 bool result = true;
385 bool has_changed = false;
386 int button;
387 int *setting;
388 int current_band, x, y, step, fast_step, min, max, voice_unit;
389 enum eq_slider_mode mode;
390 enum eq_type current_type;
391 char buf[24];
392 int i, w, h, height, start_item, nb_eq_sliders[NB_SCREENS];
393 FOR_NB_SCREENS(i)
394 viewportmanager_theme_enable(i, false, NULL);
397 FOR_NB_SCREENS(i) {
398 screens[i].set_viewport(NULL);
399 screens[i].setfont(FONT_SYSFIXED);
400 screens[i].clear_display();
402 /* Figure out how many sliders can be drawn on the screen */
403 screens[i].getstringsize("A", &w, &h);
405 /* Total height includes margins (1), text, slider, and line selector (1) */
406 height = 3 + h + 1 + SCROLLBAR_SIZE + 3;
407 nb_eq_sliders[i] = screens[i].lcdheight / height;
409 /* Make sure the "Edit Mode" text fits too */
410 height = nb_eq_sliders[i]*height + h + 2;
411 if (height > screens[i].lcdheight)
412 nb_eq_sliders[i]--;
414 if (nb_eq_sliders[i] > 5)
415 nb_eq_sliders[i] = 5;
418 y = h + 1;
420 /* Start off editing gain on the first band */
421 mode = GAIN;
422 current_type = LOW_SHELF;
423 current_band = 0;
425 while (!exit_request) {
426 FOR_NB_SCREENS(i)
428 screens[i].clear_display();
430 /* Set pointer to the band data currently editable */
431 if (mode == GAIN) {
432 /* gain */
433 setting = &global_settings.eq_band0_gain;
434 setting += current_band * 3;
436 step = EQ_GAIN_STEP;
437 fast_step = EQ_GAIN_FAST_STEP;
438 min = EQ_GAIN_MIN;
439 max = EQ_GAIN_MAX;
440 voice_unit = UNIT_DB;
442 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
443 str(LANG_SYSFONT_GAIN), "(dB)");
445 screens[i].putsxy(0, 0, buf);
446 } else if (mode == CUTOFF) {
447 /* cutoff */
448 setting = &global_settings.eq_band0_cutoff;
449 setting += current_band * 3;
451 step = EQ_CUTOFF_STEP;
452 fast_step = EQ_CUTOFF_FAST_STEP;
453 min = EQ_CUTOFF_MIN;
454 max = EQ_CUTOFF_MAX;
455 voice_unit = UNIT_HERTZ;
457 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
458 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF), "(Hz)");
460 screens[i].putsxy(0, 0, buf);
461 } else {
462 /* Q */
463 setting = &global_settings.eq_band0_q;
464 setting += current_band * 3;
466 step = EQ_Q_STEP;
467 fast_step = EQ_Q_FAST_STEP;
468 min = EQ_Q_MIN;
469 max = EQ_Q_MAX;
470 voice_unit = UNIT_INT;
472 snprintf(buf, sizeof(buf), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE),
473 str(LANG_EQUALIZER_BAND_Q), "");
475 screens[i].putsxy(0, 0, buf);
478 /* Draw scrollbar if needed */
479 if (nb_eq_sliders[i] != 5)
481 if (current_band == 0) {
482 start_item = 0;
483 } else if (current_band == 4) {
484 start_item = 5 - nb_eq_sliders[i];
485 } else {
486 start_item = current_band - 1;
488 x = SCROLLBAR_SIZE;
489 } else {
490 x = 1;
491 start_item = 0;
493 /* Draw equalizer band details */
494 draw_eq_sliders(&screens[i], x, y, nb_eq_sliders[i], start_item,
495 current_band, mode);
497 screens[i].update();
500 button = get_action(CONTEXT_SETTINGS_EQ,TIMEOUT_BLOCK);
502 switch (button) {
503 case ACTION_SETTINGS_DEC:
504 case ACTION_SETTINGS_DECREPEAT:
505 *(setting) -= step;
506 has_changed = true;
507 if (*(setting) < min)
508 *(setting) = min;
509 break;
511 case ACTION_SETTINGS_INC:
512 case ACTION_SETTINGS_INCREPEAT:
513 *(setting) += step;
514 has_changed = true;
515 if (*(setting) > max)
516 *(setting) = max;
517 break;
519 case ACTION_SETTINGS_INCBIGSTEP:
520 *(setting) += fast_step;
521 has_changed = true;
522 if (*(setting) > max)
523 *(setting) = max;
524 break;
526 case ACTION_SETTINGS_DECBIGSTEP:
527 *(setting) -= fast_step;
528 has_changed = true;
529 if (*(setting) < min)
530 *(setting) = min;
531 break;
533 case ACTION_STD_PREV:
534 case ACTION_STD_PREVREPEAT:
535 current_band--;
536 if (current_band < 0)
537 current_band = 4; /* wrap around */
538 break;
540 case ACTION_STD_NEXT:
541 case ACTION_STD_NEXTREPEAT:
542 current_band++;
543 if (current_band > 4)
544 current_band = 0; /* wrap around */
545 break;
547 case ACTION_STD_OK:
548 mode++;
549 if (mode > Q)
550 mode = GAIN; /* wrap around */
551 break;
553 case ACTION_STD_CANCEL:
554 exit_request = true;
555 result = false;
556 break;
557 default:
558 if(default_event_handler(button) == SYS_USB_CONNECTED) {
559 exit_request = true;
560 result = true;
562 break;
565 /* Update the filter if the user changed something */
566 if (has_changed) {
567 dsp_set_eq_coefs(current_band);
568 has_changed = false;
572 /* Reset screen settings */
573 FOR_NB_SCREENS(i)
575 screens[i].setfont(FONT_UI);
576 screens[i].clear_display();
577 screens[i].set_viewport(NULL);
578 viewportmanager_theme_undo(i, false);
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);