1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 #include "mp3_playback.h"
32 #include "statusbar.h"
45 #include "screen_access.h"
47 #include "gui/scrollbar.h"
50 #if (CONFIG_KEYPAD == IRIVER_H100_PAD || \
51 CONFIG_KEYPAD == IRIVER_H300_PAD)
53 #define EQ_BTN_MODIFIER BUTTON_ON
54 #define EQ_BTN_DECREMENT BUTTON_LEFT
55 #define EQ_BTN_INCREMENT BUTTON_RIGHT
56 #define EQ_BTN_NEXT_BAND BUTTON_DOWN
57 #define EQ_BTN_PREV_BAND BUTTON_UP
58 #define EQ_BTN_CHANGE_MODE BUTTON_SELECT
59 #define EQ_BTN_EXIT BUTTON_OFF
61 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
62 (CONFIG_KEYPAD == IPOD_3G_PAD)
64 #define EQ_BTN_DECREMENT BUTTON_SCROLL_BACK
65 #define EQ_BTN_INCREMENT BUTTON_SCROLL_FWD
66 #define EQ_BTN_NEXT_BAND BUTTON_RIGHT
67 #define EQ_BTN_PREV_BAND BUTTON_LEFT
68 #define EQ_BTN_CHANGE_MODE BUTTON_SELECT
69 #define EQ_BTN_EXIT BUTTON_MENU
71 #elif CONFIG_KEYPAD == IAUDIO_X5_PAD
73 #define EQ_BTN_DECREMENT BUTTON_LEFT
74 #define EQ_BTN_INCREMENT BUTTON_RIGHT
75 #define EQ_BTN_NEXT_BAND BUTTON_DOWN
76 #define EQ_BTN_PREV_BAND BUTTON_UP
77 #define EQ_BTN_CHANGE_MODE BUTTON_REC
78 #define EQ_BTN_EXIT BUTTON_SELECT
80 #elif (CONFIG_KEYPAD == IRIVER_IFP7XX_PAD)
82 #define EQ_BTN_DECREMENT BUTTON_LEFT
83 #define EQ_BTN_INCREMENT BUTTON_RIGHT
84 #define EQ_BTN_NEXT_BAND BUTTON_DOWN
85 #define EQ_BTN_PREV_BAND BUTTON_UP
86 #define EQ_BTN_CHANGE_MODE BUTTON_SELECT
87 #define EQ_BTN_EXIT BUTTON_PLAY
89 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
91 #define EQ_BTN_DECREMENT BUTTON_LEFT
92 #define EQ_BTN_INCREMENT BUTTON_RIGHT
93 #define EQ_BTN_NEXT_BAND BUTTON_DOWN
94 #define EQ_BTN_PREV_BAND BUTTON_UP
95 #define EQ_BTN_CHANGE_MODE BUTTON_SELECT
96 #define EQ_BTN_EXIT BUTTON_A
100 /* Various user interface limits and sizes */
101 #define EQ_CUTOFF_MIN 20
102 #define EQ_CUTOFF_MAX 22040
103 #define EQ_CUTOFF_STEP 10
104 #define EQ_CUTOFF_FAST_STEP 100
105 #define EQ_GAIN_MIN (-240)
106 #define EQ_GAIN_MAX 240
107 #define EQ_GAIN_STEP 5
108 #define EQ_GAIN_FAST_STEP 10
112 #define EQ_Q_FAST_STEP 10
114 #define EQ_USER_DIVISOR 10
120 static void eq_gain_format(char* buffer
, int buffer_size
, int value
, const char* unit
)
124 snprintf(buffer
, buffer_size
, "%s%d.%d %s", value
< 0 ? "-" : "",
125 v
/ EQ_USER_DIVISOR
, v
% EQ_USER_DIVISOR
, unit
);
128 static void eq_q_format(char* buffer
, int buffer_size
, int value
, const char* unit
)
130 snprintf(buffer
, buffer_size
, "%d.%d %s", value
/ EQ_USER_DIVISOR
, value
% EQ_USER_DIVISOR
, unit
);
133 static void eq_precut_format(char* buffer
, int buffer_size
, int value
, const char* unit
)
135 snprintf(buffer
, buffer_size
, "%s%d.%d %s", value
== 0 ? " " : "-",
136 value
/ EQ_USER_DIVISOR
, value
% EQ_USER_DIVISOR
, unit
);
143 static bool eq_enabled(void)
147 bool result
= set_bool(str(LANG_EQUALIZER_ENABLED
),
148 &global_settings
.eq_enabled
);
150 dsp_set_eq(global_settings
.eq_enabled
);
152 dsp_set_eq_precut(global_settings
.eq_precut
);
154 /* Update all bands */
155 for(i
= 0; i
< 5; i
++) {
162 static bool eq_precut(void)
164 bool result
= set_int(str(LANG_EQUALIZER_PRECUT
), str(LANG_UNIT_DB
),
165 UNIT_DB
, &global_settings
.eq_precut
, dsp_set_eq_precut
, 5, 0, 240,
171 /* Possibly dodgy way of simplifying the code a bit. */
172 #define eq_make_gain_label(buf, bufsize, frequency) snprintf((buf), \
173 (bufsize), str(LANG_EQUALIZER_GAIN_ITEM), (frequency))
175 #define eq_set_center(band) \
176 static bool eq_set_band ## band ## _center(void) \
178 bool result = set_int(str(LANG_EQUALIZER_BAND_CENTER), "Hertz", \
179 UNIT_HERTZ, &global_settings.eq_band ## band ## _cutoff, NULL, \
180 EQ_CUTOFF_STEP, EQ_CUTOFF_MIN, EQ_CUTOFF_MAX, NULL); \
181 dsp_set_eq_coefs(band); \
185 #define eq_set_cutoff(band) \
186 static bool eq_set_band ## band ## _cutoff(void) \
188 bool result = set_int(str(LANG_EQUALIZER_BAND_CUTOFF), "Hertz", \
189 UNIT_HERTZ, &global_settings.eq_band ## band ## _cutoff, NULL, \
190 EQ_CUTOFF_STEP, EQ_CUTOFF_MIN, EQ_CUTOFF_MAX, NULL); \
191 dsp_set_eq_coefs(band); \
195 #define eq_set_q(band) \
196 static bool eq_set_band ## band ## _q(void) \
198 bool result = set_int(str(LANG_EQUALIZER_BAND_Q), "Q", UNIT_INT, \
199 &global_settings.eq_band ## band ## _q, NULL, \
200 EQ_Q_STEP, EQ_Q_MIN, EQ_Q_MAX, eq_q_format); \
201 dsp_set_eq_coefs(band); \
205 #define eq_set_gain(band) \
206 static bool eq_set_band ## band ## _gain(void) \
208 bool result = set_int("Band " #band, str(LANG_UNIT_DB), UNIT_DB, \
209 &global_settings.eq_band ## band ## _gain, NULL, \
210 EQ_GAIN_STEP, EQ_GAIN_MIN, EQ_GAIN_MAX, eq_gain_format); \
211 dsp_set_eq_coefs(band); \
233 static bool eq_gain_menu(void)
238 char gain_label
[5][32];
239 static struct menu_item items
[5] = {
240 { NULL
, eq_set_band0_gain
},
241 { NULL
, eq_set_band1_gain
},
242 { NULL
, eq_set_band2_gain
},
243 { NULL
, eq_set_band3_gain
},
244 { NULL
, eq_set_band4_gain
},
247 setting
= &global_settings
.eq_band0_cutoff
;
249 /* Construct menu labels */
250 for(i
= 0; i
< 5; i
++) {
251 eq_make_gain_label(gain_label
[i
], sizeof(gain_label
[i
]),
253 items
[i
].desc
= gain_label
[i
];
255 /* Skip to next band */
259 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
261 result
= menu_run(m
);
267 static bool eq_set_band0(void)
271 static const struct menu_item items
[] = {
272 { ID2P(LANG_EQUALIZER_BAND_CUTOFF
), eq_set_band0_cutoff
},
273 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band0_q
},
274 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band0_gain
},
277 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
279 result
= menu_run(m
);
285 static bool eq_set_band1(void)
289 static const struct menu_item items
[] = {
290 { ID2P(LANG_EQUALIZER_BAND_CENTER
), eq_set_band1_center
},
291 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band1_q
},
292 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band1_gain
},
295 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
297 result
= menu_run(m
);
303 static bool eq_set_band2(void)
307 static const struct menu_item items
[] = {
308 { ID2P(LANG_EQUALIZER_BAND_CENTER
), eq_set_band2_center
},
309 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band2_q
},
310 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band2_gain
},
313 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
315 result
= menu_run(m
);
321 static bool eq_set_band3(void)
325 static const struct menu_item items
[] = {
326 { ID2P(LANG_EQUALIZER_BAND_CENTER
), eq_set_band3_center
},
327 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band3_q
},
328 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band3_gain
},
331 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
333 result
= menu_run(m
);
339 static bool eq_set_band4(void)
343 static const struct menu_item items
[] = {
344 { ID2P(LANG_EQUALIZER_BAND_CUTOFF
), eq_set_band4_cutoff
},
345 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band4_q
},
346 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band4_gain
},
349 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
351 result
= menu_run(m
);
357 static bool eq_advanced_menu(void)
361 char peak_band_label
[3][32];
362 static struct menu_item items
[] = {
363 { ID2P(LANG_EQUALIZER_BAND_LOW_SHELF
), eq_set_band0
},
364 { NULL
, eq_set_band1
},
365 { NULL
, eq_set_band2
},
366 { NULL
, eq_set_band3
},
367 { ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF
), eq_set_band4
},
370 /* Construct menu labels */
371 for(i
= 1; i
< 4; i
++) {
372 snprintf(peak_band_label
[i
-1], sizeof(peak_band_label
[i
-1]),
373 str(LANG_EQUALIZER_BAND_PEAK
), i
);
374 items
[i
].desc
= peak_band_label
[i
-1];
377 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
379 result
= menu_run(m
);
385 enum eq_slider_mode
{
397 /* Draw the UI for a whole EQ band */
398 static int draw_eq_slider(struct screen
* screen
, int x
, int y
,
399 int width
, int cutoff
, int q
, int gain
, bool selected
,
400 enum eq_slider_mode mode
, enum eq_type type
)
403 const char separator
[2] = " ";
404 int steps
, min_item
, max_item
;
405 int abs_gain
= abs(gain
);
406 int current_x
, total_height
, separator_width
, separator_height
;
408 const int slider_height
= 6;
412 steps
= EQ_Q_MAX
- EQ_Q_MIN
;
413 min_item
= q
- EQ_Q_STEP
- EQ_Q_MIN
;
414 max_item
= q
+ EQ_Q_STEP
- EQ_Q_MIN
;
417 steps
= EQ_CUTOFF_MAX
- EQ_CUTOFF_MIN
;
418 min_item
= cutoff
- EQ_CUTOFF_FAST_STEP
* 2;
419 max_item
= cutoff
+ EQ_CUTOFF_FAST_STEP
* 2;
423 steps
= EQ_GAIN_MAX
- EQ_GAIN_MIN
;
424 min_item
= abs(EQ_GAIN_MIN
) + gain
- EQ_GAIN_STEP
* 5;
425 max_item
= abs(EQ_GAIN_MIN
) + gain
+ EQ_GAIN_STEP
* 5;
429 /* Start two pixels in, one for border, one for margin */
432 /* Figure out how large our separator string is */
433 screen
->getstringsize(separator
, &separator_width
, &separator_height
);
435 /* Total height includes margins, text, and line selector */
436 total_height
= separator_height
+ slider_height
+ 2 + 3;
438 /* Print out the band label */
439 if (type
== LOW_SHELF
) {
440 screen
->putsxy(current_x
, y
+ 2, "LS:");
441 screen
->getstringsize("LS:", &w
, &h
);
442 } else if (type
== HIGH_SHELF
) {
443 screen
->putsxy(current_x
, y
+ 2, "HS:");
444 screen
->getstringsize("HS:", &w
, &h
);
446 screen
->putsxy(current_x
, y
+ 2, "PK:");
447 screen
->getstringsize("PK:", &w
, &h
);
451 /* Print separator */
452 screen
->set_drawmode(DRMODE_SOLID
);
453 screen
->putsxy(current_x
, y
+ 2, separator
);
454 current_x
+= separator_width
;
456 /* Print out gain part of status line */
457 snprintf(buf
, sizeof(buf
), "%s%2d.%ddB", gain
< 0 ? "-" : " ",
458 abs_gain
/ EQ_USER_DIVISOR
, abs_gain
% EQ_USER_DIVISOR
);
460 if (mode
== GAIN
&& selected
)
461 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
463 screen
->putsxy(current_x
, y
+ 2, buf
);
464 screen
->getstringsize(buf
, &w
, &h
);
467 /* Print separator */
468 screen
->set_drawmode(DRMODE_SOLID
);
469 screen
->putsxy(current_x
, y
+ 2, separator
);
470 current_x
+= separator_width
;
472 /* Print out cutoff part of status line */
473 snprintf(buf
, sizeof(buf
), "%5dHz", cutoff
);
475 if (mode
== CUTOFF
&& selected
)
476 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
478 screen
->putsxy(current_x
, y
+ 2, buf
);
479 screen
->getstringsize(buf
, &w
, &h
);
482 /* Print separator */
483 screen
->set_drawmode(DRMODE_SOLID
);
484 screen
->putsxy(current_x
, y
+ 2, separator
);
485 current_x
+= separator_width
;
487 /* Print out Q part of status line */
488 snprintf(buf
, sizeof(buf
), "%d.%d Q", q
/ EQ_USER_DIVISOR
,
489 q
% EQ_USER_DIVISOR
);
491 if (mode
== Q
&& selected
)
492 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
494 screen
->putsxy(current_x
, y
+ 2, buf
);
495 screen
->getstringsize(buf
, &w
, &h
);
498 screen
->set_drawmode(DRMODE_SOLID
);
500 /* Draw selection box */
502 screen
->drawrect(x
, y
, width
, total_height
);
505 /* Draw horizontal slider. Reuse scrollbar for this */
506 gui_scrollbar_draw(screen
, x
+ 3, y
+ h
+ 3, width
- 6, slider_height
, steps
,
507 min_item
, max_item
, HORIZONTAL
);
512 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
513 static int draw_eq_sliders(int current_band
, enum eq_slider_mode mode
)
515 int i
, gain
, q
, cutoff
;
516 int height
= 2; /* Two pixel margin */
517 int slider_width
= screens
[SCREEN_MAIN
].width
- 4; /* two pixel margin on each side */
518 int *setting
= &global_settings
.eq_band0_cutoff
;
521 for( i
= 0; i
< 5; ++i
) {
534 height
+= draw_eq_slider(&(screens
[SCREEN_MAIN
]), 2, height
,
535 slider_width
, cutoff
, q
, gain
, i
== current_band
, mode
, type
);
544 /* Provides a graphical means of editing the EQ settings */
545 bool eq_menu_graphical(void)
547 bool exit_request
= false;
549 bool has_changed
= false;
552 int current_band
, y
, step
, fast_step
, min
, max
, voice_unit
;
553 enum eq_slider_mode mode
;
554 enum eq_type current_type
;
557 screens
[SCREEN_MAIN
].setfont(FONT_SYSFIXED
);
558 screens
[SCREEN_MAIN
].clear_display();
560 /* Start off editing gain on the first band */
562 current_type
= LOW_SHELF
;
565 while (!exit_request
) {
566 /* Clear the screen. The drawing routines expect this */
567 screens
[SCREEN_MAIN
].clear_display();
569 /* Draw equalizer band details */
570 y
= draw_eq_sliders(current_band
, mode
);
572 /* Set pointer to the band data currently editable */
575 setting
= &global_settings
.eq_band0_gain
;
576 setting
+= current_band
* 3;
579 fast_step
= EQ_GAIN_FAST_STEP
;
582 voice_unit
= UNIT_DB
;
584 snprintf(buf
, sizeof(buf
), str(LANG_EQUALIZER_EDIT_MODE
),
585 str(LANG_EQUALIZER_BAND_GAIN
));
587 screens
[SCREEN_MAIN
].putsxy(2, y
, buf
);
588 } else if (mode
== CUTOFF
) {
590 setting
= &global_settings
.eq_band0_cutoff
;
591 setting
+= current_band
* 3;
593 step
= EQ_CUTOFF_STEP
;
594 fast_step
= EQ_CUTOFF_FAST_STEP
;
597 voice_unit
= UNIT_HERTZ
;
599 snprintf(buf
, sizeof(buf
), str(LANG_EQUALIZER_EDIT_MODE
),
600 str(LANG_EQUALIZER_BAND_CUTOFF
));
602 screens
[SCREEN_MAIN
].putsxy(2, y
, buf
);
605 setting
= &global_settings
.eq_band0_q
;
606 setting
+= current_band
* 3;
609 fast_step
= EQ_Q_FAST_STEP
;
612 voice_unit
= UNIT_INT
;
614 snprintf(buf
, sizeof(buf
), str(LANG_EQUALIZER_EDIT_MODE
),
615 str(LANG_EQUALIZER_BAND_Q
));
617 screens
[SCREEN_MAIN
].putsxy(2, y
, buf
);
620 screens
[SCREEN_MAIN
].update();
622 button
= button_get(true);
625 case EQ_BTN_DECREMENT
:
626 case EQ_BTN_DECREMENT
| BUTTON_REPEAT
:
629 if (*(setting
) < min
)
633 case EQ_BTN_INCREMENT
:
634 case EQ_BTN_INCREMENT
| BUTTON_REPEAT
:
637 if (*(setting
) > max
)
641 #ifdef EQ_BTN_MODIFIER
642 case EQ_BTN_MODIFIER
| EQ_BTN_INCREMENT
:
643 case EQ_BTN_MODIFIER
| EQ_BTN_INCREMENT
| BUTTON_REPEAT
:
644 *(setting
) += fast_step
;
646 if (*(setting
) > max
)
650 case EQ_BTN_MODIFIER
| EQ_BTN_DECREMENT
:
651 case EQ_BTN_MODIFIER
| EQ_BTN_DECREMENT
| BUTTON_REPEAT
:
652 *(setting
) -= fast_step
;
654 if (*(setting
) < min
)
659 case EQ_BTN_PREV_BAND
:
660 case EQ_BTN_PREV_BAND
| BUTTON_REPEAT
:
662 if (current_band
< 0)
663 current_band
= 4; /* wrap around */
666 case EQ_BTN_NEXT_BAND
:
667 case EQ_BTN_NEXT_BAND
| BUTTON_REPEAT
:
669 if (current_band
> 4)
670 current_band
= 0; /* wrap around */
673 case EQ_BTN_CHANGE_MODE
:
674 case EQ_BTN_CHANGE_MODE
| BUTTON_REPEAT
:
677 mode
= GAIN
; /* wrap around */
681 case EQ_BTN_EXIT
| BUTTON_REPEAT
:
687 if(default_event_handler(button
) == SYS_USB_CONNECTED
) {
694 /* Update the filter if the user changed something */
696 dsp_set_eq_coefs(current_band
);
701 /* Reset screen settings */
702 screens
[SCREEN_MAIN
].setfont(FONT_UI
);
703 screens
[SCREEN_MAIN
].clear_display();
709 * TODO: Can the settings system be used to do this instead?
711 static bool eq_save_preset(void)
714 char filename
[MAX_PATH
];
717 create_numbered_filename(filename
, EQS_DIR
, "eq", ".cfg", 2);
719 /* allow user to modify filename */
721 if (!kbd_input(filename
, sizeof filename
)) {
722 fd
= creat(filename
,0);
724 gui_syncsplash(HZ
, true, str(LANG_FAILED
));
729 gui_syncsplash(HZ
, true, str(LANG_MENU_SETTING_CANCEL
));
734 /* TODO: Should we really do this? */
735 fdprintf(fd
, "eq enabled: on\r\n");
736 fdprintf(fd
, "eq precut: %d\r\n", global_settings
.eq_precut
);
738 setting
= &global_settings
.eq_band0_cutoff
;
740 for(i
= 0; i
< 5; ++i
) {
741 fdprintf(fd
, "eq band %d cutoff: %d\r\n", i
, *setting
++);
742 fdprintf(fd
, "eq band %d q: %d\r\n", i
, *setting
++);
743 fdprintf(fd
, "eq band %d gain: %d\r\n", i
, *setting
++);
748 gui_syncsplash(HZ
, true, str(LANG_SETTINGS_SAVED
));
753 /* Allows browsing of preset files */
754 bool eq_browse_presets(void)
756 return rockbox_browse(EQS_DIR
, SHOW_CFG
);
759 /* Full equalizer menu */
764 static const struct menu_item items
[] = {
765 { ID2P(LANG_EQUALIZER_ENABLED
), eq_enabled
},
766 { ID2P(LANG_EQUALIZER_GRAPHICAL
), eq_menu_graphical
},
767 { ID2P(LANG_EQUALIZER_PRECUT
), eq_precut
},
768 { ID2P(LANG_EQUALIZER_GAIN
), eq_gain_menu
},
769 { ID2P(LANG_EQUALIZER_ADVANCED
), eq_advanced_menu
},
770 { ID2P(LANG_EQUALIZER_SAVE
), eq_save_preset
},
771 { ID2P(LANG_EQUALIZER_BROWSE
), eq_browse_presets
},
774 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
776 result
= menu_run(m
);