1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
33 #include "mp3_playback.h"
42 #include "dsp_proc_settings.h"
44 #include "screen_access.h"
46 #include "gui/scrollbar.h"
47 #include "menu_common.h"
49 #include "exported_menus.h"
51 #include "option_select.h"
57 const char* 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
,
60 value
% EQ_USER_DIVISOR
, unit
);
64 const char* eq_precut_format(char* buffer
, size_t buffer_size
, int value
, const char* unit
)
66 snprintf(buffer
, buffer_size
, "%s%d.%d %s", value
== 0 ? " " : "-",
67 value
/ EQ_USER_DIVISOR
, value
% EQ_USER_DIVISOR
, unit
);
74 static void eq_apply(void)
76 dsp_eq_enable(global_settings
.eq_enabled
);
77 dsp_set_eq_precut(global_settings
.eq_precut
);
78 /* Update all bands */
79 for(int i
= 0; i
< EQ_NUM_BANDS
; i
++) {
80 dsp_set_eq_coefs(i
, &global_settings
.eq_band_settings
[i
]);
84 static int eq_setting_callback(int action
, const struct menu_item_ex
*this_item
)
88 case ACTION_ENTER_MENUITEM
:
89 action
= lowlatency_callback(action
, this_item
);
91 case ACTION_EXIT_MENUITEM
:
93 action
= lowlatency_callback(action
, this_item
);
99 MENUITEM_SETTING(eq_enable
, &global_settings
.eq_enabled
, eq_setting_callback
);
100 MENUITEM_SETTING(eq_precut
, &global_settings
.eq_precut
, eq_setting_callback
);
102 static char* gainitem_get_name(int selected_item
, void *data
, char *buffer
, size_t len
)
105 snprintf(buffer
, len
, str(LANG_EQUALIZER_GAIN_ITEM
),
106 global_settings
.eq_band_settings
[selected_item
].cutoff
);
111 static int gainitem_speak_item(int selected_item
, void *data
)
114 talk_number(global_settings
.eq_band_settings
[selected_item
].cutoff
, false);
115 talk_id(LANG_EQUALIZER_GAIN_ITEM
, true);
119 static enum themable_icons
gainitem_get_icon(int selected_item
, void * data
)
124 return Icon_Menu_functioncall
;
127 static const char* db_format(char* buffer
, size_t buffer_size
, int value
,
132 snprintf(buffer
, buffer_size
, "%s%d.%d %s", value
< 0 ? "-" : "",
133 v
/ 10, v
% 10, unit
);
137 static int32_t get_dec_talkid(int value
, int unit
)
139 return TALK_ID_DECIMAL(value
, 1, unit
);
142 static const struct int_setting gain_int_setting
= {
143 .option_callback
= NULL
,
147 .step
= EQ_GAIN_STEP
,
148 .formatter
= db_format
,
149 .get_talk_id
= get_dec_talkid
,
152 static const struct int_setting q_int_setting
= {
153 .option_callback
= NULL
,
158 .formatter
= eq_q_format
,
159 .get_talk_id
= get_dec_talkid
,
162 static const struct int_setting cutoff_int_setting
= {
163 .option_callback
= NULL
,
165 .min
= EQ_CUTOFF_MIN
,
166 .max
= EQ_CUTOFF_MAX
,
167 .step
= EQ_CUTOFF_STEP
,
172 static int simplelist_action_callback(int action
, struct gui_synclist
*lists
)
175 if (action
== ACTION_STD_OK
)
176 return ACTION_STD_CANCEL
;
180 static int eq_do_simple_menu(void * param
)
183 struct simplelist_info info
;
184 struct settings_list setting
;
185 char title
[MAX_PATH
];
187 simplelist_info_init(&info
, str(LANG_EQUALIZER_GAIN
), EQ_NUM_BANDS
, NULL
);
188 info
.get_name
= (list_get_name
*)gainitem_get_name
;
189 info
.get_talk
= gainitem_speak_item
;
190 info
.get_icon
= gainitem_get_icon
;
191 info
.action_callback
= simplelist_action_callback
;
193 info
.title_icon
= Icon_Submenu
;
194 setting
.flags
= F_BANFROMQS
|F_INT_SETTING
|F_T_INT
|F_NO_WRAP
;
195 setting
.lang_id
= LANG_GAIN
;
196 setting
.default_val
.int_
= 0;
197 setting
.int_setting
= &gain_int_setting
;
201 simplelist_show_list(&info
);
202 if (info
.selection
< 0)
204 pcmbuf_set_low_latency(true);
205 setting
.setting
= &global_settings
.eq_band_settings
[info
.selection
].gain
;
206 option_screen(&setting
, NULL
, false,
207 gainitem_get_name(info
.selection
, NULL
, title
, MAX_PATH
));
209 pcmbuf_set_low_latency(false);
213 MENUITEM_FUNCTION(gain_menu
, 0, ID2P(LANG_EQUALIZER_GAIN
), eq_do_simple_menu
,
214 NULL
, NULL
, Icon_Submenu
);
216 static void selection_to_banditem(int selection
, int expanded_band
, int *band
, int *item
)
218 int diff
= selection
- expanded_band
;
220 if (expanded_band
< 0 || diff
< 0)
227 *item
= selection
- expanded_band
;
228 *band
= expanded_band
;
233 *band
= expanded_band
+ diff
- 3;
237 static char *advancedmenu_item_get_name(int selected_item
, void *data
, char *buffer
, size_t len
)
244 selection_to_banditem(selected_item
, *(intptr_t*)data
, &band
, &item
);
246 strcpy(buffer
, "\t");
249 case 0: /* Band title */
251 return str(LANG_EQUALIZER_BAND_LOW_SHELF
);
252 else if (band
== EQ_NUM_BANDS
- 1)
253 return str(LANG_EQUALIZER_BAND_HIGH_SHELF
);
256 snprintf(buffer
, MAX_PATH
, str(LANG_EQUALIZER_BAND_PEAK
), band
);
262 lang
= str(LANG_EQUALIZER_BAND_CUTOFF
);
263 else if (band
== EQ_NUM_BANDS
- 1)
264 lang
= str(LANG_EQUALIZER_BAND_CUTOFF
);
266 lang
= str(LANG_EQUALIZER_BAND_CENTER
);
269 lang
= str(LANG_EQUALIZER_BAND_Q
);
272 lang
= str(LANG_GAIN
);
276 return strcat(buffer
, lang
);;
279 static int advancedmenu_speak_item(int selected_item
, void *data
)
286 selection_to_banditem(selected_item
, *(intptr_t*)data
, &band
, &item
);
290 case 0: /* Band title */
292 lang
= LANG_EQUALIZER_BAND_LOW_SHELF
;
293 else if (band
== EQ_NUM_BANDS
- 1)
294 lang
= LANG_EQUALIZER_BAND_HIGH_SHELF
;
297 talk_id(LANG_EQUALIZER_BAND_PEAK
, false);
298 talk_number(band
, true);
304 lang
= LANG_EQUALIZER_BAND_CUTOFF
;
305 else if (band
== EQ_NUM_BANDS
- 1)
306 lang
= LANG_EQUALIZER_BAND_CUTOFF
;
308 lang
= LANG_EQUALIZER_BAND_CENTER
;
311 lang
= LANG_EQUALIZER_BAND_Q
;
321 static enum themable_icons
advancedmenu_get_icon(int selected_item
, void * data
)
327 selection_to_banditem(selected_item
, *(intptr_t*)data
, &band
, &item
);
332 return Icon_Menu_setting
;
334 extern struct eq_band_setting eq_defaults
[EQ_NUM_BANDS
];
336 static int eq_do_advanced_menu(void * param
)
339 struct simplelist_info info
;
340 struct settings_list setting
;
341 char title
[MAX_PATH
];
343 intptr_t selected_band
= -1;
345 simplelist_info_init(&info
, str(LANG_EQUALIZER_ADVANCED
),
346 EQ_NUM_BANDS
, &selected_band
);
347 info
.get_name
= (list_get_name
*)advancedmenu_item_get_name
;
348 info
.get_talk
= advancedmenu_speak_item
;
349 info
.get_icon
= advancedmenu_get_icon
;
350 info
.action_callback
= simplelist_action_callback
;
352 info
.title_icon
= Icon_EQ
;
353 setting
.flags
= F_BANFROMQS
|F_INT_SETTING
|F_T_INT
|F_NO_WRAP
;
357 simplelist_show_list(&info
);
358 if (info
.selection
< 0)
360 selection_to_banditem(info
.selection
, selected_band
, &band
, &item
);
363 case 0: /* title, do nothing */
366 if (selected_band
== band
)
374 selected_band
= band
;
376 info
.selection
= band
;
377 info
.count
= EQ_NUM_BANDS
+ extra
;
381 if (band
== 0 || band
== EQ_NUM_BANDS
- 1)
382 setting
.lang_id
= LANG_EQUALIZER_BAND_CUTOFF
;
384 setting
.lang_id
= LANG_EQUALIZER_BAND_CENTER
;
385 setting
.default_val
.int_
= eq_defaults
[band
].cutoff
;
386 setting
.int_setting
= &cutoff_int_setting
;
387 setting
.setting
= &global_settings
.eq_band_settings
[band
].cutoff
;
390 setting
.lang_id
= LANG_EQUALIZER_BAND_Q
;
391 setting
.default_val
.int_
= eq_defaults
[band
].q
;
392 setting
.int_setting
= &q_int_setting
;
393 setting
.setting
= &global_settings
.eq_band_settings
[band
].q
;
396 setting
.lang_id
= LANG_GAIN
;
397 setting
.default_val
.int_
= eq_defaults
[band
].gain
;
398 setting
.int_setting
= &gain_int_setting
;
399 setting
.setting
= &global_settings
.eq_band_settings
[band
].gain
;
402 pcmbuf_set_low_latency(true);
403 advancedmenu_item_get_name(info
.selection
, &selected_band
, title
, MAX_PATH
);
405 option_screen(&setting
, NULL
, false, title
[0] == '\t' ? &title
[1] : title
);
407 pcmbuf_set_low_latency(false);
411 MENUITEM_FUNCTION(advanced_menu
, 0, ID2P(LANG_EQUALIZER_ADVANCED
),
412 eq_do_advanced_menu
, NULL
, NULL
, Icon_EQ
);
414 enum eq_slider_mode
{
426 /* Size of just the slider/srollbar */
427 #define SCROLLBAR_SIZE 6
429 /* Draw the UI for a whole EQ band */
430 static int draw_eq_slider(struct screen
* screen
, int x
, int y
,
431 int width
, int cutoff
, int q
, int gain
, bool selected
,
432 enum eq_slider_mode mode
, int band
)
435 int steps
, min_item
, max_item
;
436 int abs_gain
= abs(gain
);
437 int x1
, x2
, y1
, total_height
;
442 steps
= EQ_Q_MAX
- EQ_Q_MIN
;
443 min_item
= q
- EQ_Q_STEP
- EQ_Q_MIN
;
444 max_item
= q
+ EQ_Q_STEP
- EQ_Q_MIN
;
447 steps
= EQ_CUTOFF_MAX
- EQ_CUTOFF_MIN
;
448 min_item
= cutoff
- EQ_CUTOFF_FAST_STEP
* 2;
449 max_item
= cutoff
+ EQ_CUTOFF_FAST_STEP
* 2;
453 steps
= EQ_GAIN_MAX
- EQ_GAIN_MIN
;
454 min_item
= abs(EQ_GAIN_MIN
) + gain
- EQ_GAIN_STEP
* 5;
455 max_item
= abs(EQ_GAIN_MIN
) + gain
+ EQ_GAIN_STEP
* 5;
459 /* Start two pixels in, one for border, one for margin */
463 /* Print out the band label */
465 screen
->putsxy(x1
, y1
, "LS: ");
466 screen
->getstringsize("LS:", &w
, &h
);
467 } else if (band
== EQ_NUM_BANDS
- 1) {
468 screen
->putsxy(x1
, y1
, "HS: ");
469 screen
->getstringsize("HS:", &w
, &h
);
471 snprintf(buf
, sizeof(buf
), "PK%d:", band
);
472 screen
->putsxy(x1
, y1
, buf
);
473 screen
->getstringsize(buf
, &w
, &h
);
476 screen
->getstringsize("A", &w
, &h
);
477 x1
+= 5*w
; /* 4 chars for label + 1 space = 5 */
479 /* Print out gain part of status line (left justify after label) */
480 if (mode
== GAIN
&& selected
)
481 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
483 screen
->set_drawmode(DRMODE_SOLID
);
485 snprintf(buf
, sizeof(buf
), "%s%2d.%d%s", gain
< 0 ? "-" : " ",
486 abs_gain
/ EQ_USER_DIVISOR
, abs_gain
% EQ_USER_DIVISOR
,
487 screen
->lcdwidth
>= 160 ? "dB" : "");
488 screen
->putsxy(x1
, y1
, buf
);
489 screen
->getstringsize(buf
, &w
, &h
);
492 /* Print out Q part of status line (right justify) */
493 if (mode
== Q
&& selected
)
494 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
496 screen
->set_drawmode(DRMODE_SOLID
);
498 snprintf(buf
, sizeof(buf
), "%d.%d%s", q
/ EQ_USER_DIVISOR
,
499 q
% EQ_USER_DIVISOR
, screen
->lcdwidth
>= 160 ? " Q" : "");
500 screen
->getstringsize(buf
, &w
, &h
);
501 x2
= x
+ width
- w
- 2;
502 screen
->putsxy(x2
, y1
, buf
);
504 /* Print out cutoff part of status line (center between gain & Q) */
505 if (mode
== CUTOFF
&& selected
)
506 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
508 screen
->set_drawmode(DRMODE_SOLID
);
510 snprintf(buf
, sizeof(buf
), "%5d%s", cutoff
,
511 screen
->lcdwidth
>= 160 ? "Hz" : "");
512 screen
->getstringsize(buf
, &w
, &h
);
513 x1
= x1
+ (x2
- x1
- w
)/2;
514 screen
->putsxy(x1
, y1
, buf
);
516 /* Draw selection box */
517 total_height
= 3 + h
+ 1 + SCROLLBAR_SIZE
+ 3;
518 screen
->set_drawmode(DRMODE_SOLID
);
520 screen
->drawrect(x
, y
, width
, total_height
);
523 /* Draw horizontal slider. Reuse scrollbar for this */
524 gui_scrollbar_draw(screen
, x
+ 3, y1
+ h
+ 1, width
- 6, SCROLLBAR_SIZE
,
525 steps
, min_item
, max_item
, HORIZONTAL
);
530 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
531 static void draw_eq_sliders(struct screen
* screen
, int x
, int y
,
532 int nb_eq_sliders
, int start_item
,
533 int current_band
, enum eq_slider_mode mode
)
537 start_item
= MIN(start_item
, EQ_NUM_BANDS
- nb_eq_sliders
);
539 for (int i
= 0; i
< EQ_NUM_BANDS
; i
++) {
540 struct eq_band_setting
*setting
= &global_settings
.eq_band_settings
[i
];
541 int cutoff
= setting
->cutoff
;
543 int gain
= setting
->gain
;
545 if (i
== start_item
+ nb_eq_sliders
)
548 if (i
>= start_item
) {
549 height
+= draw_eq_slider(screen
, x
, height
, screen
->lcdwidth
- x
- 1,
550 cutoff
, q
, gain
, i
== current_band
, mode
,
557 if (nb_eq_sliders
!= EQ_NUM_BANDS
)
558 gui_scrollbar_draw(screen
, 0, y
, SCROLLBAR_SIZE
- 1,
559 screen
->lcdheight
- y
, EQ_NUM_BANDS
,
560 start_item
, start_item
+ nb_eq_sliders
,
565 /* Provides a graphical means of editing the EQ settings */
566 bool eq_menu_graphical(void)
568 bool exit_request
= false;
570 bool has_changed
= false;
573 int current_band
, x
, y
, step
, fast_step
, min
, max
;
574 enum eq_slider_mode mode
;
576 int w
, h
, height
, start_item
, nb_eq_sliders
[NB_SCREENS
];
578 viewportmanager_theme_enable(i
, false, NULL
);
582 screens
[i
].set_viewport(NULL
);
583 screens
[i
].setfont(FONT_SYSFIXED
);
584 screens
[i
].clear_display();
586 /* Figure out how many sliders can be drawn on the screen */
587 screens
[i
].getstringsize("A", &w
, &h
);
589 /* Total height includes margins (1), text, slider, and line selector (1) */
590 height
= 3 + h
+ 1 + SCROLLBAR_SIZE
+ 3;
591 nb_eq_sliders
[i
] = screens
[i
].lcdheight
/ height
;
593 /* Make sure the "Edit Mode" text fits too */
594 height
= nb_eq_sliders
[i
]*height
+ h
+ 2;
595 if (height
> screens
[i
].lcdheight
)
598 if (nb_eq_sliders
[i
] > EQ_NUM_BANDS
)
599 nb_eq_sliders
[i
] = EQ_NUM_BANDS
;
604 /* Start off editing gain on the first band */
608 while (!exit_request
) {
611 screens
[i
].clear_display();
613 /* Set pointer to the band data currently editable */
616 setting
= &global_settings
.eq_band_settings
[current_band
].gain
;
619 fast_step
= EQ_GAIN_FAST_STEP
;
623 snprintf(buf
, sizeof(buf
), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE
),
624 str(LANG_SYSFONT_GAIN
), "(dB)");
626 screens
[i
].putsxy(0, 0, buf
);
627 } else if (mode
== CUTOFF
) {
629 setting
= &global_settings
.eq_band_settings
[current_band
].cutoff
;
631 step
= EQ_CUTOFF_STEP
;
632 fast_step
= EQ_CUTOFF_FAST_STEP
;
636 snprintf(buf
, sizeof(buf
), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE
),
637 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF
), "(Hz)");
639 screens
[i
].putsxy(0, 0, buf
);
642 setting
= &global_settings
.eq_band_settings
[current_band
].q
;
645 fast_step
= EQ_Q_FAST_STEP
;
649 snprintf(buf
, sizeof(buf
), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE
),
650 str(LANG_EQUALIZER_BAND_Q
), "");
652 screens
[i
].putsxy(0, 0, buf
);
655 /* Draw scrollbar if needed */
656 if (nb_eq_sliders
[i
] != EQ_NUM_BANDS
)
658 if (current_band
== 0) {
660 } else if (current_band
== 9) {
661 start_item
= EQ_NUM_BANDS
- nb_eq_sliders
[i
];
663 start_item
= current_band
- 1;
670 /* Draw equalizer band details */
671 draw_eq_sliders(&screens
[i
], x
, y
, nb_eq_sliders
[i
], start_item
,
677 button
= get_action(CONTEXT_SETTINGS_EQ
,TIMEOUT_BLOCK
);
680 case ACTION_SETTINGS_DEC
:
681 case ACTION_SETTINGS_DECREPEAT
:
684 if (*(setting
) < min
)
688 case ACTION_SETTINGS_INC
:
689 case ACTION_SETTINGS_INCREPEAT
:
692 if (*(setting
) > max
)
696 case ACTION_SETTINGS_INCBIGSTEP
:
697 *(setting
) += fast_step
;
699 if (*(setting
) > max
)
703 case ACTION_SETTINGS_DECBIGSTEP
:
704 *(setting
) -= fast_step
;
706 if (*(setting
) < min
)
710 case ACTION_STD_PREV
:
711 case ACTION_STD_PREVREPEAT
:
713 if (current_band
< 0)
714 current_band
= EQ_NUM_BANDS
- 1; /* wrap around */
717 case ACTION_STD_NEXT
:
718 case ACTION_STD_NEXTREPEAT
:
719 current_band
= (current_band
+ 1) % EQ_NUM_BANDS
;
725 mode
= GAIN
; /* wrap around */
728 case ACTION_STD_CANCEL
:
733 if(default_event_handler(button
) == SYS_USB_CONNECTED
) {
740 /* Update the filter if the user changed something */
742 dsp_set_eq_coefs(current_band
,
743 &global_settings
.eq_band_settings
[current_band
]);
748 /* Reset screen settings */
751 screens
[i
].setfont(FONT_UI
);
752 screens
[i
].clear_display();
753 screens
[i
].set_viewport(NULL
);
754 viewportmanager_theme_undo(i
, false);
759 static bool eq_save_preset(void)
761 /* make sure that the eq is enabled for setting saving */
762 bool enabled
= global_settings
.eq_enabled
;
763 global_settings
.eq_enabled
= true;
765 bool result
= settings_save_config(SETTINGS_SAVE_EQPRESET
);
767 global_settings
.eq_enabled
= enabled
;
771 /* Allows browsing of preset files */
772 static struct browse_folder_info eqs
= { EQS_DIR
, SHOW_CFG
};
774 MENUITEM_FUNCTION(eq_graphical
, 0, ID2P(LANG_EQUALIZER_GRAPHICAL
),
775 (int(*)(void))eq_menu_graphical
, NULL
, lowlatency_callback
,
777 MENUITEM_FUNCTION(eq_save
, 0, ID2P(LANG_EQUALIZER_SAVE
),
778 (int(*)(void))eq_save_preset
, NULL
, NULL
, Icon_NOICON
);
779 MENUITEM_FUNCTION(eq_browse
, MENU_FUNC_USEPARAM
, ID2P(LANG_EQUALIZER_BROWSE
),
780 browse_folder
, (void*)&eqs
, lowlatency_callback
,
783 MAKE_MENU(equalizer_menu
, ID2P(LANG_EQUALIZER
), NULL
, Icon_EQ
,
784 &eq_enable
, &eq_graphical
, &eq_precut
, &gain_menu
,
785 &advanced_menu
, &eq_save
, &eq_browse
);