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
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 1
108 #define EQ_GAIN_FAST_STEP 10
112 #define EQ_Q_FAST_STEP 10
114 #define EQ_USER_DIVISOR 10
116 static bool eq_enabled(void)
120 bool result
= set_bool(str(LANG_EQUALIZER_ENABLED
),
121 &global_settings
.eq_enabled
);
123 /* Update all bands */
124 for(i
= 0; i
< 5; i
++) {
125 dsp_eq_update_data(global_settings
.eq_enabled
, i
);
131 static void eq_gain_format(char* buffer
, int buffer_size
, int value
, const char* unit
)
135 snprintf(buffer
, buffer_size
, "%s%d.%d %s", value
< 0 ? "-" : "",
136 v
/ EQ_USER_DIVISOR
, v
% EQ_USER_DIVISOR
, unit
);
139 static void eq_q_format(char* buffer
, int buffer_size
, int value
, const char* unit
)
141 snprintf(buffer
, buffer_size
, "%d.%d %s", value
/ EQ_USER_DIVISOR
, value
% EQ_USER_DIVISOR
, unit
);
144 /* Possibly dodgy way of simplifying the code a bit. */
145 #define eq_make_gain_label(buf, bufsize, frequency) snprintf((buf), \
146 (bufsize), str(LANG_EQUALIZER_GAIN_ITEM), (frequency))
148 #define eq_set_center(band) \
149 static bool eq_set_band ## band ## _center(void) \
151 bool result = set_int(str(LANG_EQUALIZER_BAND_CENTER), "Hertz", UNIT_HERTZ, \
152 &global_settings.eq_band ## band ## _cutoff, NULL, \
153 EQ_CUTOFF_STEP, EQ_CUTOFF_MIN, EQ_CUTOFF_MAX, NULL); \
154 dsp_eq_update_data(global_settings.eq_enabled, band); \
158 #define eq_set_cutoff(band) \
159 static bool eq_set_band ## band ## _cutoff(void) \
161 bool result = set_int(str(LANG_EQUALIZER_BAND_CUTOFF), "Hertz", UNIT_HERTZ, \
162 &global_settings.eq_band ## band ## _cutoff, NULL, \
163 EQ_CUTOFF_STEP, EQ_CUTOFF_MIN, EQ_CUTOFF_MAX, NULL); \
164 dsp_eq_update_data(global_settings.eq_enabled, band); \
168 #define eq_set_q(band) \
169 static bool eq_set_band ## band ## _q(void) \
171 bool result = set_int(str(LANG_EQUALIZER_BAND_Q), "Q", UNIT_INT, \
172 &global_settings.eq_band ## band ## _q, NULL, \
173 EQ_Q_STEP, EQ_Q_MIN, EQ_Q_MAX, eq_q_format); \
174 dsp_eq_update_data(global_settings.eq_enabled, band); \
178 #define eq_set_gain(band) \
179 static bool eq_set_band ## band ## _gain(void) \
181 bool result = set_int("Band " #band, str(LANG_UNIT_DB), UNIT_DB, \
182 &global_settings.eq_band ## band ## _gain, NULL, \
183 EQ_GAIN_STEP, EQ_GAIN_MIN, EQ_GAIN_MAX, eq_gain_format); \
184 dsp_eq_update_data(global_settings.eq_enabled, band); \
206 static bool eq_gain_menu(void)
211 char gain_label
[5][24];
212 static struct menu_item items
[5] = {
213 { NULL
, eq_set_band0_gain
},
214 { NULL
, eq_set_band1_gain
},
215 { NULL
, eq_set_band2_gain
},
216 { NULL
, eq_set_band3_gain
},
217 { NULL
, eq_set_band4_gain
},
220 setting
= &global_settings
.eq_band0_cutoff
;
222 /* Construct menu labels */
223 for(i
= 0; i
< 5; i
++) {
224 eq_make_gain_label(gain_label
[i
], sizeof(gain_label
[i
]),
226 items
[i
].desc
= gain_label
[i
];
228 /* Skip to next band */
232 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
234 result
= menu_run(m
);
240 static bool eq_set_band0(void)
244 static const struct menu_item items
[] = {
245 { ID2P(LANG_EQUALIZER_BAND_CUTOFF
), eq_set_band0_cutoff
},
246 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band0_q
},
247 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band0_gain
},
250 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
252 result
= menu_run(m
);
258 static bool eq_set_band1(void)
262 static const struct menu_item items
[] = {
263 { ID2P(LANG_EQUALIZER_BAND_CENTER
), eq_set_band1_center
},
264 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band1_q
},
265 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band1_gain
},
268 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
270 result
= menu_run(m
);
276 static bool eq_set_band2(void)
280 static const struct menu_item items
[] = {
281 { ID2P(LANG_EQUALIZER_BAND_CENTER
), eq_set_band2_center
},
282 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band2_q
},
283 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band2_gain
},
286 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
288 result
= menu_run(m
);
294 static bool eq_set_band3(void)
298 static const struct menu_item items
[] = {
299 { ID2P(LANG_EQUALIZER_BAND_CENTER
), eq_set_band3_center
},
300 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band3_q
},
301 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band3_gain
},
304 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
306 result
= menu_run(m
);
312 static bool eq_set_band4(void)
316 static const struct menu_item items
[] = {
317 { ID2P(LANG_EQUALIZER_BAND_CUTOFF
), eq_set_band4_cutoff
},
318 { ID2P(LANG_EQUALIZER_BAND_Q
), eq_set_band4_q
},
319 { ID2P(LANG_EQUALIZER_BAND_GAIN
), eq_set_band4_gain
},
322 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
324 result
= menu_run(m
);
330 static bool eq_advanced_menu(void)
334 char peak_band_label
[3][32];
335 static struct menu_item items
[] = {
336 { ID2P(LANG_EQUALIZER_BAND_LOW_SHELF
), eq_set_band0
},
337 { NULL
, eq_set_band1
},
338 { NULL
, eq_set_band2
},
339 { NULL
, eq_set_band3
},
340 { ID2P(LANG_EQUALIZER_BAND_HIGH_SHELF
), eq_set_band4
},
343 /* Construct menu labels */
344 for(i
= 1; i
< 4; i
++) {
345 snprintf(peak_band_label
[i
-1], sizeof(peak_band_label
[i
-1]),
346 str(LANG_EQUALIZER_BAND_PEAK
), i
);
347 items
[i
].desc
= peak_band_label
[i
-1];
350 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
352 result
= menu_run(m
);
358 enum eq_slider_mode
{
370 /* Draw the UI for a whole EQ band */
371 static int draw_eq_slider(struct screen
* screen
, int x
, int y
,
372 int width
, int cutoff
, int q
, int gain
, bool selected
,
373 enum eq_slider_mode mode
, enum eq_type type
)
376 const char separator
[2] = " ";
377 int steps
, min_item
, max_item
;
378 int abs_gain
= abs(gain
);
379 int current_x
, total_height
, separator_width
, separator_height
;
381 const int slider_height
= 6;
385 steps
= EQ_Q_MAX
- EQ_Q_MIN
;
386 min_item
= q
- EQ_Q_STEP
- EQ_Q_MIN
;
387 max_item
= q
+ EQ_Q_STEP
- EQ_Q_MIN
;
390 steps
= EQ_CUTOFF_MAX
- EQ_CUTOFF_MIN
;
391 min_item
= cutoff
- EQ_CUTOFF_FAST_STEP
* 2;
392 max_item
= cutoff
+ EQ_CUTOFF_FAST_STEP
* 2;
396 steps
= EQ_GAIN_MAX
- EQ_GAIN_MIN
;
397 min_item
= abs(EQ_GAIN_MIN
) + gain
- EQ_GAIN_STEP
* 5;
398 max_item
= abs(EQ_GAIN_MIN
) + gain
+ EQ_GAIN_STEP
* 5;
402 /* Start two pixels in, one for border, one for margin */
405 /* Figure out how large our separator string is */
406 screen
->getstringsize(separator
, &separator_width
, &separator_height
);
408 /* Total height includes margins, text, and line selector */
409 total_height
= separator_height
+ slider_height
+ 2 + 3;
411 /* Print out the band label */
412 if (type
== LOW_SHELF
) {
413 screen
->putsxy(current_x
, y
+ 2, "LS:");
414 screen
->getstringsize("LS:", &w
, &h
);
415 } else if (type
== HIGH_SHELF
) {
416 screen
->putsxy(current_x
, y
+ 2, "HS:");
417 screen
->getstringsize("HS:", &w
, &h
);
419 screen
->putsxy(current_x
, y
+ 2, "PK:");
420 screen
->getstringsize("PK:", &w
, &h
);
424 /* Print separator */
425 screen
->set_drawmode(DRMODE_SOLID
);
426 screen
->putsxy(current_x
, y
+ 2, separator
);
427 current_x
+= separator_width
;
429 /* Print out gain part of status line */
430 snprintf(buf
, sizeof(buf
), "%s%2d.%ddB", gain
< 0 ? "-" : " ",
431 abs_gain
/ EQ_USER_DIVISOR
, abs_gain
% EQ_USER_DIVISOR
);
433 if (mode
== GAIN
&& selected
)
434 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
436 screen
->putsxy(current_x
, y
+ 2, buf
);
437 screen
->getstringsize(buf
, &w
, &h
);
440 /* Print separator */
441 screen
->set_drawmode(DRMODE_SOLID
);
442 screen
->putsxy(current_x
, y
+ 2, separator
);
443 current_x
+= separator_width
;
445 /* Print out cutoff part of status line */
446 snprintf(buf
, sizeof(buf
), "%5dHz", cutoff
);
448 if (mode
== CUTOFF
&& selected
)
449 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
451 screen
->putsxy(current_x
, y
+ 2, buf
);
452 screen
->getstringsize(buf
, &w
, &h
);
455 /* Print separator */
456 screen
->set_drawmode(DRMODE_SOLID
);
457 screen
->putsxy(current_x
, y
+ 2, separator
);
458 current_x
+= separator_width
;
460 /* Print out Q part of status line */
461 snprintf(buf
, sizeof(buf
), "%d.%d Q", q
/ EQ_USER_DIVISOR
,
462 q
% EQ_USER_DIVISOR
);
464 if (mode
== Q
&& selected
)
465 screen
->set_drawmode(DRMODE_SOLID
| DRMODE_INVERSEVID
);
467 screen
->putsxy(current_x
, y
+ 2, buf
);
468 screen
->getstringsize(buf
, &w
, &h
);
471 screen
->set_drawmode(DRMODE_SOLID
);
473 /* Draw selection box */
475 screen
->drawrect(x
, y
, width
, total_height
);
478 /* Draw horizontal slider. Reuse scrollbar for this */
479 gui_scrollbar_draw(screen
, x
+ 3, y
+ h
+ 3, width
- 6, slider_height
, steps
,
480 min_item
, max_item
, HORIZONTAL
);
485 /* Draw's all the EQ sliders. Returns the total height of the sliders drawn */
486 static int draw_eq_sliders(int current_band
, enum eq_slider_mode mode
)
488 int i
, gain
, q
, cutoff
;
489 int height
= 2; /* Two pixel margin */
490 int slider_width
= screens
[SCREEN_MAIN
].width
- 4; /* two pixel margin on each side */
491 int *setting
= &global_settings
.eq_band0_cutoff
;
494 for( i
= 0; i
< 5; ++i
) {
507 height
+= draw_eq_slider(&(screens
[SCREEN_MAIN
]), 2, height
,
508 slider_width
, cutoff
, q
, gain
, i
== current_band
, mode
, type
);
517 /* Provides a graphical means of editing the EQ settings */
518 bool eq_menu_graphical(void)
520 bool exit_request
= false;
522 bool has_changed
= false;
525 int current_band
, y
, step
, fast_step
, min
, max
, voice_unit
;
526 enum eq_slider_mode mode
;
527 enum eq_type current_type
;
530 screens
[SCREEN_MAIN
].setfont(FONT_SYSFIXED
);
531 screens
[SCREEN_MAIN
].clear_display();
533 /* Start off editing gain on the first band */
535 current_type
= LOW_SHELF
;
538 while (!exit_request
) {
539 /* Clear the screen. The drawing routines expect this */
540 screens
[SCREEN_MAIN
].clear_display();
542 /* Draw equalizer band details */
543 y
= draw_eq_sliders(current_band
, mode
);
545 /* Set pointer to the band data currently editable */
548 setting
= &global_settings
.eq_band0_gain
;
549 setting
+= current_band
* 3;
552 fast_step
= EQ_GAIN_FAST_STEP
;
555 voice_unit
= UNIT_DB
;
557 snprintf(buf
, sizeof(buf
), str(LANG_EQUALIZER_EDIT_MODE
),
558 str(LANG_EQUALIZER_BAND_GAIN
));
560 screens
[SCREEN_MAIN
].putsxy(2, y
, buf
);
561 } else if (mode
== CUTOFF
) {
563 setting
= &global_settings
.eq_band0_cutoff
;
564 setting
+= current_band
* 3;
566 step
= EQ_CUTOFF_STEP
;
567 fast_step
= EQ_CUTOFF_FAST_STEP
;
570 voice_unit
= UNIT_HERTZ
;
572 snprintf(buf
, sizeof(buf
), str(LANG_EQUALIZER_EDIT_MODE
),
573 str(LANG_EQUALIZER_BAND_CUTOFF
));
575 screens
[SCREEN_MAIN
].putsxy(2, y
, buf
);
578 setting
= &global_settings
.eq_band0_q
;
579 setting
+= current_band
* 3;
582 fast_step
= EQ_Q_FAST_STEP
;
585 voice_unit
= UNIT_INT
;
587 snprintf(buf
, sizeof(buf
), str(LANG_EQUALIZER_EDIT_MODE
),
588 str(LANG_EQUALIZER_BAND_Q
));
590 screens
[SCREEN_MAIN
].putsxy(2, y
, buf
);
593 screens
[SCREEN_MAIN
].update();
595 button
= button_get(true);
598 case EQ_BTN_DECREMENT
:
599 case EQ_BTN_DECREMENT
| BUTTON_REPEAT
:
602 if (*(setting
) < min
)
606 case EQ_BTN_INCREMENT
:
607 case EQ_BTN_INCREMENT
| BUTTON_REPEAT
:
610 if (*(setting
) > max
)
614 #ifdef EQ_BTN_MODIFIER
615 case EQ_BTN_MODIFIER
| EQ_BTN_INCREMENT
:
616 case EQ_BTN_MODIFIER
| EQ_BTN_INCREMENT
| BUTTON_REPEAT
:
617 *(setting
) += fast_step
;
619 if (*(setting
) > max
)
623 case EQ_BTN_MODIFIER
| EQ_BTN_DECREMENT
:
624 case EQ_BTN_MODIFIER
| EQ_BTN_DECREMENT
| BUTTON_REPEAT
:
625 *(setting
) -= fast_step
;
627 if (*(setting
) < min
)
632 case EQ_BTN_PREV_BAND
:
633 case EQ_BTN_PREV_BAND
| BUTTON_REPEAT
:
635 if (current_band
< 0)
636 current_band
= 4; /* wrap around */
639 case EQ_BTN_NEXT_BAND
:
640 case EQ_BTN_NEXT_BAND
| BUTTON_REPEAT
:
642 if (current_band
> 4)
643 current_band
= 0; /* wrap around */
646 case EQ_BTN_CHANGE_MODE
:
647 case EQ_BTN_CHANGE_MODE
| BUTTON_REPEAT
:
650 mode
= GAIN
; /* wrap around */
654 case EQ_BTN_EXIT
| BUTTON_REPEAT
:
660 if(default_event_handler(button
) == SYS_USB_CONNECTED
) {
667 /* Update the filter if the user changed something */
669 dsp_eq_update_data(global_settings
.eq_enabled
, current_band
);
674 /* Reset screen settings */
675 screens
[SCREEN_MAIN
].setfont(FONT_UI
);
676 screens
[SCREEN_MAIN
].clear_display();
682 * TODO: Can the settings system be used to do this instead?
684 static bool eq_save_preset(void)
687 char filename
[MAX_PATH
];
690 create_numbered_filename(filename
, EQS_DIR
, "eq", ".cfg", 2);
692 /* allow user to modify filename */
694 if (!kbd_input(filename
, sizeof filename
)) {
695 fd
= creat(filename
,0);
697 gui_syncsplash(HZ
, true, str(LANG_FAILED
));
702 gui_syncsplash(HZ
, true, str(LANG_MENU_SETTING_CANCEL
));
707 /* TODO: Should we really do this? */
708 fdprintf(fd
, "eq enabled: on\r\n");
710 setting
= &global_settings
.eq_band0_cutoff
;
712 for(i
= 0; i
< 5; ++i
) {
713 fdprintf(fd
, "eq band %d cutoff: %d\r\n", i
, *setting
++);
714 fdprintf(fd
, "eq band %d q: %d\r\n", i
, *setting
++);
715 fdprintf(fd
, "eq band %d gain: %d\r\n", i
, *setting
++);
720 gui_syncsplash(HZ
, true, (unsigned char *)"%s %s", str(LANG_SETTINGS_SAVED1
),
721 str(LANG_SETTINGS_SAVED2
));
726 /* Allows browsing of preset files */
727 bool eq_browse_presets(void)
729 return rockbox_browse(EQS_DIR
, SHOW_CFG
);
732 /* Full equalizer menu */
737 static const struct menu_item items
[] = {
738 { ID2P(LANG_EQUALIZER_ENABLED
), eq_enabled
},
739 { ID2P(LANG_EQUALIZER_GRAPHICAL
), eq_menu_graphical
},
740 { ID2P(LANG_EQUALIZER_GAIN
), eq_gain_menu
},
741 { ID2P(LANG_EQUALIZER_ADVANCED
), eq_advanced_menu
},
742 { ID2P(LANG_EQUALIZER_SAVE
), eq_save_preset
},
743 { ID2P(LANG_EQUALIZER_BROWSE
), eq_browse_presets
},
746 m
=menu_init( items
, sizeof(items
) / sizeof(*items
), NULL
,
748 result
= menu_run(m
);