1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Björn Stenberg
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 ****************************************************************************/
34 #include "statusbar.h"
39 #include "pitchscreen.h"
41 #define PITCH_MODE_ABSOLUTE 1
42 #define PITCH_MODE_SEMITONE -PITCH_MODE_ABSOLUTE
43 #define ICON_BORDER 12 /* icons are currently 7x8, so add ~2 pixels */
44 /* on both sides when drawing */
47 #define PITCH_MAX 2000
49 #define PITCH_SMALL_DELTA 1
50 #define PITCH_BIG_DELTA 10
51 #define PITCH_NUDGE_DELTA 20
54 static int pitch_mode
= PITCH_MODE_ABSOLUTE
; /* 1 - absolute, -1 - semitone */
64 static void pitchscreen_fix_viewports(struct viewport
*parent
,
65 struct viewport pitch_viewports
[PITCH_ITEM_COUNT
])
68 height
= font_get(parent
->font
)->height
;
69 for (i
= 0; i
< PITCH_ITEM_COUNT
; i
++)
71 pitch_viewports
[i
] = *parent
;
72 pitch_viewports
[i
].height
= height
;
74 pitch_viewports
[PITCH_TOP
].y
+= ICON_BORDER
;
76 pitch_viewports
[PITCH_MID
].x
+= ICON_BORDER
;
77 pitch_viewports
[PITCH_MID
].width
= parent
->width
- ICON_BORDER
*2;
78 pitch_viewports
[PITCH_MID
].height
= height
* 2;
79 pitch_viewports
[PITCH_MID
].y
+= parent
->height
/ 2 -
80 pitch_viewports
[PITCH_MID
].height
/ 2;
82 pitch_viewports
[PITCH_BOTTOM
].y
+= parent
->height
- height
- ICON_BORDER
;
85 /* must be called before pitchscreen_draw, or within
86 * since it neither clears nor updates the display */
87 static void pitchscreen_draw_icons (struct screen
*display
,
88 struct viewport
*parent
)
90 display
->set_viewport(parent
);
91 display
->mono_bitmap(bitmap_icons_7x8
[Icon_UpArrow
],
94 display
->mono_bitmap(bitmap_icons_7x8
[Icon_DownArrow
],
96 parent
->height
- 10, 7, 8);
97 display
->mono_bitmap(bitmap_icons_7x8
[Icon_FastForward
],
99 parent
->height
/2 - 4, 7, 8);
100 display
->mono_bitmap(bitmap_icons_7x8
[Icon_FastBackward
],
102 parent
->height
/2 - 4, 7, 8);
103 display
->update_viewport();
106 static void pitchscreen_draw (struct screen
*display
, int max_lines
,
107 struct viewport pitch_viewports
[PITCH_ITEM_COUNT
], int pitch
)
110 unsigned char buf
[32];
112 bool show_lang_pitch
;
114 /* Hide "Pitch up/Pitch down" for a small screen */
118 display
->set_viewport(&pitch_viewports
[PITCH_TOP
]);
119 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
120 ptr
= str(LANG_PITCH_UP
);
122 ptr
= str(LANG_PITCH_UP_SEMITONE
);
124 display
->getstringsize(ptr
,&w
,&h
);
125 display
->clear_viewport();
127 display
->putsxy((pitch_viewports
[PITCH_TOP
].width
/ 2) -
129 display
->update_viewport();
131 /* DOWN: Pitch Down */
132 display
->set_viewport(&pitch_viewports
[PITCH_BOTTOM
]);
133 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
134 ptr
= str(LANG_PITCH_DOWN
);
136 ptr
= str(LANG_PITCH_DOWN_SEMITONE
);
138 display
->getstringsize(ptr
,&w
,&h
);
139 display
->clear_viewport();
141 display
->putsxy((pitch_viewports
[PITCH_BOTTOM
].width
/ 2) -
143 display
->update_viewport();
145 display
->set_viewport(&pitch_viewports
[PITCH_MID
]);
147 snprintf((char *)buf
, sizeof(buf
), "%s", str(LANG_PITCH
));
148 display
->getstringsize(buf
,&w
,&h
);
149 /* lets hide LANG_PITCH for smaller screens */
150 display
->clear_viewport();
151 if ((show_lang_pitch
= (max_lines
>= 3)))
152 display
->putsxy((pitch_viewports
[PITCH_MID
].width
/ 2) - (w
/ 2),
156 snprintf((char *)buf
, sizeof(buf
), "%d.%d%%",
157 pitch
/ 10, pitch
% 10 );
158 display
->getstringsize(buf
,&width_val
,&h
);
159 display
->putsxy((pitch_viewports
[PITCH_MID
].width
/ 2) - (width_val
/ 2),
160 (show_lang_pitch
? h
: h
/2), buf
);
162 /* What's wider? LANG_PITCH or the value?
163 * Only interesting if LANG_PITCH is actually drawn */
164 if (show_lang_pitch
&& width_val
> w
)
167 /* Let's treat '+' and '-' as equally wide
168 * This saves a getstringsize call
169 * Also, it wouldn't look nice if -2% shows up, but +2% not */
170 display
->getstringsize("+2%",&width_val
,&h
);
172 /* hide +2%/-2% for a narrow screens */
173 if (w
<= pitch_viewports
[PITCH_MID
].width
)
176 display
->putsxy(pitch_viewports
[PITCH_MID
].width
- width_val
, h
/2, "+2%");
178 display
->putsxy(0, h
/ 2, "-2%");
180 display
->update_viewport();
181 display
->set_viewport(NULL
);
184 static int pitch_increase(int pitch
, int delta
, bool allow_cutoff
)
189 if (pitch
+ delta
>= PITCH_MIN
) {
190 new_pitch
= pitch
+ delta
;
195 new_pitch
= PITCH_MIN
;
197 } else if (delta
> 0) {
198 if (pitch
+ delta
<= PITCH_MAX
) {
199 new_pitch
= pitch
+ delta
;
204 new_pitch
= PITCH_MAX
;
207 /* delta == 0 -> no real change */
210 sound_set_pitch(new_pitch
);
215 /* Factor for changing the pitch one half tone up.
216 The exact value is 2^(1/12) = 1.05946309436
217 But we use only integer arithmetics, so take
218 rounded factor multiplied by 10^5=100,000. This is
219 enough to get the same promille values as if we
220 had used floating point (checked with a spread
223 #define PITCH_SEMITONE_FACTOR 105946L
225 /* Some helpful constants. K is the scaling factor for SEMITONE.
226 N is for more accurate rounding
229 #define PITCH_K_FCT 100000UL
230 #define PITCH_N_FCT 10
231 #define PITCH_KN_FCT 1000000UL
233 static int pitch_increase_semitone(int pitch
, bool up
)
236 uint32_t round_fct
; /* How much to scale down at the end */
239 tmp
= tmp
* PITCH_SEMITONE_FACTOR
;
240 round_fct
= PITCH_K_FCT
;
242 tmp
= (tmp
* PITCH_KN_FCT
) / PITCH_SEMITONE_FACTOR
;
243 round_fct
= PITCH_N_FCT
;
245 /* Scaling down with rounding */
246 tmp
= (tmp
+ round_fct
/ 2) / round_fct
;
247 return pitch_increase(pitch
, tmp
- pitch
, false);
253 1 if USB was connected
256 int gui_syncpitchscreen_run(void)
259 int pitch
= sound_get_pitch();
260 int new_pitch
, delta
= 0;
263 /* should maybe be passed per parameter later, not needed for now */
264 struct viewport parent
[NB_SCREENS
];
265 struct viewport pitch_viewports
[NB_SCREENS
][PITCH_ITEM_COUNT
];
266 int max_lines
[NB_SCREENS
];
268 /* initialize pitchscreen vps */
271 screens
[i
].clear_display();
272 viewport_set_defaults(&parent
[i
], i
);
273 max_lines
[i
] = viewport_get_nb_lines(&parent
[i
]);
274 pitchscreen_fix_viewports(&parent
[i
], pitch_viewports
[i
]);
276 /* also, draw the icons now, it's only needed once */
277 pitchscreen_draw_icons(&screens
[i
], &parent
[i
]);
279 #if CONFIG_CODEC == SWCODEC
280 pcmbuf_set_low_latency(true);
286 pitchscreen_draw(&screens
[i
], max_lines
[i
],
287 pitch_viewports
[i
], pitch
);
288 button
= get_action(CONTEXT_PITCHSCREEN
,HZ
);
290 case ACTION_PS_INC_SMALL
:
291 delta
= PITCH_SMALL_DELTA
;
294 case ACTION_PS_INC_BIG
:
295 delta
= PITCH_BIG_DELTA
;
298 case ACTION_PS_DEC_SMALL
:
299 delta
= -PITCH_SMALL_DELTA
;
302 case ACTION_PS_DEC_BIG
:
303 delta
= -PITCH_BIG_DELTA
;
306 case ACTION_PS_NUDGE_RIGHT
:
307 new_pitch
= pitch_increase(pitch
, PITCH_NUDGE_DELTA
, false);
308 nudged
= (new_pitch
!= pitch
);
312 case ACTION_PS_NUDGE_RIGHTOFF
:
314 pitch
= pitch_increase(pitch
, -PITCH_NUDGE_DELTA
, false);
319 case ACTION_PS_NUDGE_LEFT
:
320 new_pitch
= pitch_increase(pitch
, -PITCH_NUDGE_DELTA
, false);
321 nudged
= (new_pitch
!= pitch
);
325 case ACTION_PS_NUDGE_LEFTOFF
:
327 pitch
= pitch_increase(pitch
, PITCH_NUDGE_DELTA
, false);
332 case ACTION_PS_RESET
:
334 sound_set_pitch( pitch
);
337 case ACTION_PS_TOGGLE_MODE
:
338 pitch_mode
= -pitch_mode
;
346 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
352 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
353 pitch
= pitch_increase(pitch
, delta
, true);
355 pitch
= pitch_increase_semitone(pitch
, delta
> 0 ? true:false);
361 #if CONFIG_CODEC == SWCODEC
362 pcmbuf_set_low_latency(false);