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
;
389 setting
.lang_id
= LANG_EQUALIZER_BAND_Q
;
390 setting
.default_val
.int_
= eq_defaults
[band
].q
;
391 setting
.int_setting
= &q_int_setting
;
394 setting
.lang_id
= LANG_GAIN
;
395 setting
.default_val
.int_
= eq_defaults
[band
].gain
;
396 setting
.int_setting
= &gain_int_setting
;
399 pcmbuf_set_low_latency(true);
400 setting
.setting
= &global_settings
.eq_band_settings
[band
].gain
;
401 advancedmenu_item_get_name(info
.selection
, &selected_band
, title
, MAX_PATH
);
403 option_screen(&setting
, NULL
, false, title
[0] == '\t' ? &title
[1] : title
);
405 pcmbuf_set_low_latency(false);
409 MENUITEM_FUNCTION(advanced_menu
, 0, ID2P(LANG_EQUALIZER_ADVANCED
),
410 eq_do_advanced_menu
, NULL
, NULL
, Icon_EQ
);
412 enum eq_slider_mode
{
424 /* Size of just the slider/srollbar */
425 #define SCROLLBAR_SIZE 6
427 /* Draw the UI for a whole EQ band */
428 static int draw_eq_slider(struct screen
* screen
, int x
, int y
,
429 int width
, int cutoff
, int q
, int gain
, bool selected
,
430 enum eq_slider_mode mode
, int band
)
433 int steps
, min_item
, max_item
;
434 int abs_gain
= abs(gain
);
435 int x1
, x2
, y1
, total_height
;
440 steps
= EQ_Q_MAX
- EQ_Q_MIN
;
441 min_item
= q
- EQ_Q_STEP
- EQ_Q_MIN
;
442 max_item
= q
+ EQ_Q_STEP
- EQ_Q_MIN
;
445 steps
= EQ_CUTOFF_MAX
- EQ_CUTOFF_MIN
;
446 min_item
= cutoff
- EQ_CUTOFF_FAST_STEP
* 2;
447 max_item
= cutoff
+ EQ_CUTOFF_FAST_STEP
* 2;
451 steps
= EQ_GAIN_MAX
- EQ_GAIN_MIN
;
452 min_item
= abs(EQ_GAIN_MIN
) + gain
- EQ_GAIN_STEP
* 5;
453 max_item
= abs(EQ_GAIN_MIN
) + gain
+ EQ_GAIN_STEP
* 5;
457 /* Start two pixels in, one for border, one for margin */
461 /* Print out the band label */
463 screen
->putsxy(x1
, y1
, "LS: ");
464 screen
->getstringsize("LS:", &w
, &h
);
465 } else if (band
== EQ_NUM_BANDS
- 1) {
466 screen
->putsxy(x1
, y1
, "HS: ");
467 screen
->getstringsize("HS:", &w
, &h
);
469 snprintf(buf
, sizeof(buf
), "PK%d:", band
);
470 screen
->putsxy(x1
, y1
, buf
);
471 screen
->getstringsize(buf
, &w
, &h
);
474 screen
->getstringsize("A", &w
, &h
);
475 x1
+= 5*w
; /* 4 chars for label + 1 space = 5 */
477 /* Print out gain part of status line (left justify after label) */
478 if (mode
== GAIN
&& selected
)
479 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
481 screen
->set_drawmode(DRMODE_SOLID
);
483 snprintf(buf
, sizeof(buf
), "%s%2d.%d%s", gain
< 0 ? "-" : " ",
484 abs_gain
/ EQ_USER_DIVISOR
, abs_gain
% EQ_USER_DIVISOR
,
485 screen
->lcdwidth
>= 160 ? "dB" : "");
486 screen
->putsxy(x1
, y1
, buf
);
487 screen
->getstringsize(buf
, &w
, &h
);
490 /* Print out Q part of status line (right justify) */
491 if (mode
== Q
&& selected
)
492 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
494 screen
->set_drawmode(DRMODE_SOLID
);
496 snprintf(buf
, sizeof(buf
), "%d.%d%s", q
/ EQ_USER_DIVISOR
,
497 q
% EQ_USER_DIVISOR
, screen
->lcdwidth
>= 160 ? " Q" : "");
498 screen
->getstringsize(buf
, &w
, &h
);
499 x2
= x
+ width
- w
- 2;
500 screen
->putsxy(x2
, y1
, buf
);
502 /* Print out cutoff part of status line (center between gain & Q) */
503 if (mode
== CUTOFF
&& selected
)
504 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
506 screen
->set_drawmode(DRMODE_SOLID
);
508 snprintf(buf
, sizeof(buf
), "%5d%s", cutoff
,
509 screen
->lcdwidth
>= 160 ? "Hz" : "");
510 screen
->getstringsize(buf
, &w
, &h
);
511 x1
= x1
+ (x2
- x1
- w
)/2;
512 screen
->putsxy(x1
, y1
, buf
);
514 /* Draw selection box */
515 total_height
= 3 + h
+ 1 + SCROLLBAR_SIZE
+ 3;
516 screen
->set_drawmode(DRMODE_SOLID
);
518 screen
->drawrect(x
, y
, width
, total_height
);
521 /* Draw horizontal slider. Reuse scrollbar for this */
522 gui_scrollbar_draw(screen
, x
+ 3, y1
+ h
+ 1, width
- 6, SCROLLBAR_SIZE
,
523 steps
, min_item
, max_item
, HORIZONTAL
);
528 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
529 static void draw_eq_sliders(struct screen
* screen
, int x
, int y
,
530 int nb_eq_sliders
, int start_item
,
531 int current_band
, enum eq_slider_mode mode
)
535 start_item
= MIN(start_item
, EQ_NUM_BANDS
- nb_eq_sliders
);
537 for (int i
= 0; i
< EQ_NUM_BANDS
; i
++) {
538 struct eq_band_setting
*setting
= &global_settings
.eq_band_settings
[i
];
539 int cutoff
= setting
->cutoff
;
541 int gain
= setting
->gain
;
543 if (i
== start_item
+ nb_eq_sliders
)
546 if (i
>= start_item
) {
547 height
+= draw_eq_slider(screen
, x
, height
, screen
->lcdwidth
- x
- 1,
548 cutoff
, q
, gain
, i
== current_band
, mode
,
555 if (nb_eq_sliders
!= EQ_NUM_BANDS
)
556 gui_scrollbar_draw(screen
, 0, y
, SCROLLBAR_SIZE
- 1,
557 screen
->lcdheight
- y
, EQ_NUM_BANDS
,
558 start_item
, start_item
+ nb_eq_sliders
,
563 /* Provides a graphical means of editing the EQ settings */
564 bool eq_menu_graphical(void)
566 bool exit_request
= false;
568 bool has_changed
= false;
571 int current_band
, x
, y
, step
, fast_step
, min
, max
;
572 enum eq_slider_mode mode
;
574 int w
, h
, height
, start_item
, nb_eq_sliders
[NB_SCREENS
];
576 viewportmanager_theme_enable(i
, false, NULL
);
580 screens
[i
].set_viewport(NULL
);
581 screens
[i
].setfont(FONT_SYSFIXED
);
582 screens
[i
].clear_display();
584 /* Figure out how many sliders can be drawn on the screen */
585 screens
[i
].getstringsize("A", &w
, &h
);
587 /* Total height includes margins (1), text, slider, and line selector (1) */
588 height
= 3 + h
+ 1 + SCROLLBAR_SIZE
+ 3;
589 nb_eq_sliders
[i
] = screens
[i
].lcdheight
/ height
;
591 /* Make sure the "Edit Mode" text fits too */
592 height
= nb_eq_sliders
[i
]*height
+ h
+ 2;
593 if (height
> screens
[i
].lcdheight
)
596 if (nb_eq_sliders
[i
] > EQ_NUM_BANDS
)
597 nb_eq_sliders
[i
] = EQ_NUM_BANDS
;
602 /* Start off editing gain on the first band */
606 while (!exit_request
) {
609 screens
[i
].clear_display();
611 /* Set pointer to the band data currently editable */
614 setting
= &global_settings
.eq_band_settings
[current_band
].gain
;
617 fast_step
= EQ_GAIN_FAST_STEP
;
621 snprintf(buf
, sizeof(buf
), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE
),
622 str(LANG_SYSFONT_GAIN
), "(dB)");
624 screens
[i
].putsxy(0, 0, buf
);
625 } else if (mode
== CUTOFF
) {
627 setting
= &global_settings
.eq_band_settings
[current_band
].cutoff
;
629 step
= EQ_CUTOFF_STEP
;
630 fast_step
= EQ_CUTOFF_FAST_STEP
;
634 snprintf(buf
, sizeof(buf
), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE
),
635 str(LANG_SYSFONT_EQUALIZER_BAND_CUTOFF
), "(Hz)");
637 screens
[i
].putsxy(0, 0, buf
);
640 setting
= &global_settings
.eq_band_settings
[current_band
].q
;
643 fast_step
= EQ_Q_FAST_STEP
;
647 snprintf(buf
, sizeof(buf
), str(LANG_SYSFONT_EQUALIZER_EDIT_MODE
),
648 str(LANG_EQUALIZER_BAND_Q
), "");
650 screens
[i
].putsxy(0, 0, buf
);
653 /* Draw scrollbar if needed */
654 if (nb_eq_sliders
[i
] != EQ_NUM_BANDS
)
656 if (current_band
== 0) {
658 } else if (current_band
== 9) {
659 start_item
= EQ_NUM_BANDS
- nb_eq_sliders
[i
];
661 start_item
= current_band
- 1;
668 /* Draw equalizer band details */
669 draw_eq_sliders(&screens
[i
], x
, y
, nb_eq_sliders
[i
], start_item
,
675 button
= get_action(CONTEXT_SETTINGS_EQ
,TIMEOUT_BLOCK
);
678 case ACTION_SETTINGS_DEC
:
679 case ACTION_SETTINGS_DECREPEAT
:
682 if (*(setting
) < min
)
686 case ACTION_SETTINGS_INC
:
687 case ACTION_SETTINGS_INCREPEAT
:
690 if (*(setting
) > max
)
694 case ACTION_SETTINGS_INCBIGSTEP
:
695 *(setting
) += fast_step
;
697 if (*(setting
) > max
)
701 case ACTION_SETTINGS_DECBIGSTEP
:
702 *(setting
) -= fast_step
;
704 if (*(setting
) < min
)
708 case ACTION_STD_PREV
:
709 case ACTION_STD_PREVREPEAT
:
711 if (current_band
< 0)
712 current_band
= EQ_NUM_BANDS
- 1; /* wrap around */
715 case ACTION_STD_NEXT
:
716 case ACTION_STD_NEXTREPEAT
:
717 current_band
= (current_band
+ 1) % EQ_NUM_BANDS
;
723 mode
= GAIN
; /* wrap around */
726 case ACTION_STD_CANCEL
:
731 if(default_event_handler(button
) == SYS_USB_CONNECTED
) {
738 /* Update the filter if the user changed something */
740 dsp_set_eq_coefs(current_band
,
741 &global_settings
.eq_band_settings
[current_band
]);
746 /* Reset screen settings */
749 screens
[i
].setfont(FONT_UI
);
750 screens
[i
].clear_display();
751 screens
[i
].set_viewport(NULL
);
752 viewportmanager_theme_undo(i
, false);
757 static bool eq_save_preset(void)
759 /* make sure that the eq is enabled for setting saving */
760 bool enabled
= global_settings
.eq_enabled
;
761 global_settings
.eq_enabled
= true;
763 bool result
= settings_save_config(SETTINGS_SAVE_EQPRESET
);
765 global_settings
.eq_enabled
= enabled
;
769 /* Allows browsing of preset files */
770 static struct browse_folder_info eqs
= { EQS_DIR
, SHOW_CFG
};
772 MENUITEM_FUNCTION(eq_graphical
, 0, ID2P(LANG_EQUALIZER_GRAPHICAL
),
773 (int(*)(void))eq_menu_graphical
, NULL
, lowlatency_callback
,
775 MENUITEM_FUNCTION(eq_save
, 0, ID2P(LANG_EQUALIZER_SAVE
),
776 (int(*)(void))eq_save_preset
, NULL
, NULL
, Icon_NOICON
);
777 MENUITEM_FUNCTION(eq_browse
, MENU_FUNC_USEPARAM
, ID2P(LANG_EQUALIZER_BROWSE
),
778 browse_folder
, (void*)&eqs
, lowlatency_callback
,
781 MAKE_MENU(equalizer_menu
, ID2P(LANG_EQUALIZER
), NULL
, Icon_EQ
,
782 &eq_enable
, &eq_graphical
, &eq_precut
, &gain_menu
,
783 &advanced_menu
, &eq_save
, &eq_browse
);