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 ****************************************************************************/
38 #include "pitchscreen.h"
40 #define PITCH_MODE_ABSOLUTE 1
41 #define PITCH_MODE_SEMITONE -PITCH_MODE_ABSOLUTE
42 #define ICON_BORDER 12 /* icons are currently 7x8, so add ~2 pixels */
43 /* on both sides when drawing */
46 #define PITCH_MAX 2000
48 #define PITCH_SMALL_DELTA 1
49 #define PITCH_BIG_DELTA 10
50 #define PITCH_NUDGE_DELTA 20
53 static int pitch_mode
= PITCH_MODE_ABSOLUTE
; /* 1 - absolute, -1 - semitone */
63 static void pitchscreen_fix_viewports(struct viewport
*parent
,
64 struct viewport pitch_viewports
[PITCH_ITEM_COUNT
])
67 height
= font_get(parent
->font
)->height
;
68 for (i
= 0; i
< PITCH_ITEM_COUNT
; i
++)
70 pitch_viewports
[i
] = *parent
;
71 pitch_viewports
[i
].height
= height
;
73 pitch_viewports
[PITCH_TOP
].y
+= ICON_BORDER
;
75 pitch_viewports
[PITCH_MID
].x
+= ICON_BORDER
;
76 pitch_viewports
[PITCH_MID
].width
= parent
->width
- ICON_BORDER
*2;
77 pitch_viewports
[PITCH_MID
].height
= height
* 2;
78 pitch_viewports
[PITCH_MID
].y
+= parent
->height
/ 2 -
79 pitch_viewports
[PITCH_MID
].height
/ 2;
81 pitch_viewports
[PITCH_BOTTOM
].y
+= parent
->height
- height
- ICON_BORDER
;
84 /* must be called before pitchscreen_draw, or within
85 * since it neither clears nor updates the display */
86 static void pitchscreen_draw_icons (struct screen
*display
,
87 struct viewport
*parent
)
89 display
->set_viewport(parent
);
90 display
->mono_bitmap(bitmap_icons_7x8
[Icon_UpArrow
],
93 display
->mono_bitmap(bitmap_icons_7x8
[Icon_DownArrow
],
95 parent
->height
- 10, 7, 8);
96 display
->mono_bitmap(bitmap_icons_7x8
[Icon_FastForward
],
98 parent
->height
/2 - 4, 7, 8);
99 display
->mono_bitmap(bitmap_icons_7x8
[Icon_FastBackward
],
101 parent
->height
/2 - 4, 7, 8);
102 display
->update_viewport();
105 static void pitchscreen_draw (struct screen
*display
, int max_lines
,
106 struct viewport pitch_viewports
[PITCH_ITEM_COUNT
], int pitch
)
109 unsigned char buf
[32];
111 bool show_lang_pitch
;
113 /* Hide "Pitch up/Pitch down" for a small screen */
117 display
->set_viewport(&pitch_viewports
[PITCH_TOP
]);
118 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
119 ptr
= str(LANG_PITCH_UP
);
121 ptr
= str(LANG_PITCH_UP_SEMITONE
);
123 display
->getstringsize(ptr
,&w
,&h
);
124 display
->clear_viewport();
126 display
->putsxy((pitch_viewports
[PITCH_TOP
].width
/ 2) -
128 display
->update_viewport();
130 /* DOWN: Pitch Down */
131 display
->set_viewport(&pitch_viewports
[PITCH_BOTTOM
]);
132 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
133 ptr
= str(LANG_PITCH_DOWN
);
135 ptr
= str(LANG_PITCH_DOWN_SEMITONE
);
137 display
->getstringsize(ptr
,&w
,&h
);
138 display
->clear_viewport();
140 display
->putsxy((pitch_viewports
[PITCH_BOTTOM
].width
/ 2) -
142 display
->update_viewport();
144 display
->set_viewport(&pitch_viewports
[PITCH_MID
]);
146 snprintf((char *)buf
, sizeof(buf
), "%s", str(LANG_PITCH
));
147 display
->getstringsize(buf
,&w
,&h
);
148 /* lets hide LANG_PITCH for smaller screens */
149 display
->clear_viewport();
150 if ((show_lang_pitch
= (max_lines
>= 3)))
151 display
->putsxy((pitch_viewports
[PITCH_MID
].width
/ 2) - (w
/ 2),
155 snprintf((char *)buf
, sizeof(buf
), "%d.%d%%",
156 pitch
/ 10, pitch
% 10 );
157 display
->getstringsize(buf
,&width_val
,&h
);
158 display
->putsxy((pitch_viewports
[PITCH_MID
].width
/ 2) - (width_val
/ 2),
159 (show_lang_pitch
? h
: h
/2), buf
);
161 /* What's wider? LANG_PITCH or the value?
162 * Only interesting if LANG_PITCH is actually drawn */
163 if (show_lang_pitch
&& width_val
> w
)
166 /* Let's treat '+' and '-' as equally wide
167 * This saves a getstringsize call
168 * Also, it wouldn't look nice if -2% shows up, but +2% not */
169 display
->getstringsize("+2%",&width_val
,&h
);
171 /* hide +2%/-2% for a narrow screens */
172 if (w
<= pitch_viewports
[PITCH_MID
].width
)
175 display
->putsxy(pitch_viewports
[PITCH_MID
].width
- width_val
, h
/2, "+2%");
177 display
->putsxy(0, h
/ 2, "-2%");
179 display
->update_viewport();
180 display
->set_viewport(NULL
);
183 static int pitch_increase(int pitch
, int delta
, bool allow_cutoff
)
188 if (pitch
+ delta
>= PITCH_MIN
) {
189 new_pitch
= pitch
+ delta
;
194 new_pitch
= PITCH_MIN
;
196 } else if (delta
> 0) {
197 if (pitch
+ delta
<= PITCH_MAX
) {
198 new_pitch
= pitch
+ delta
;
203 new_pitch
= PITCH_MAX
;
206 /* delta == 0 -> no real change */
209 sound_set_pitch(new_pitch
);
214 /* Factor for changing the pitch one half tone up.
215 The exact value is 2^(1/12) = 1.05946309436
216 But we use only integer arithmetics, so take
217 rounded factor multiplied by 10^5=100,000. This is
218 enough to get the same promille values as if we
219 had used floating point (checked with a spread
222 #define PITCH_SEMITONE_FACTOR 105946L
224 /* Some helpful constants. K is the scaling factor for SEMITONE.
225 N is for more accurate rounding
228 #define PITCH_K_FCT 100000UL
229 #define PITCH_N_FCT 10
230 #define PITCH_KN_FCT 1000000UL
232 static int pitch_increase_semitone(int pitch
, bool up
)
235 uint32_t round_fct
; /* How much to scale down at the end */
238 tmp
= tmp
* PITCH_SEMITONE_FACTOR
;
239 round_fct
= PITCH_K_FCT
;
241 tmp
= (tmp
* PITCH_KN_FCT
) / PITCH_SEMITONE_FACTOR
;
242 round_fct
= PITCH_N_FCT
;
244 /* Scaling down with rounding */
245 tmp
= (tmp
+ round_fct
/ 2) / round_fct
;
246 return pitch_increase(pitch
, tmp
- pitch
, false);
252 1 if USB was connected
255 int gui_syncpitchscreen_run(void)
258 int pitch
= sound_get_pitch();
259 int new_pitch
, delta
= 0;
262 /* should maybe be passed per parameter later, not needed for now */
263 struct viewport parent
[NB_SCREENS
];
264 struct viewport pitch_viewports
[NB_SCREENS
][PITCH_ITEM_COUNT
];
265 int max_lines
[NB_SCREENS
];
267 /* initialize pitchscreen vps */
270 screens
[i
].clear_display();
271 viewport_set_defaults(&parent
[i
], i
);
272 max_lines
[i
] = viewport_get_nb_lines(&parent
[i
]);
273 pitchscreen_fix_viewports(&parent
[i
], pitch_viewports
[i
]);
275 /* also, draw the icons now, it's only needed once */
276 pitchscreen_draw_icons(&screens
[i
], &parent
[i
]);
278 #if CONFIG_CODEC == SWCODEC
279 pcmbuf_set_low_latency(true);
285 pitchscreen_draw(&screens
[i
], max_lines
[i
],
286 pitch_viewports
[i
], pitch
);
287 button
= get_action(CONTEXT_PITCHSCREEN
,HZ
);
289 case ACTION_PS_INC_SMALL
:
290 delta
= PITCH_SMALL_DELTA
;
293 case ACTION_PS_INC_BIG
:
294 delta
= PITCH_BIG_DELTA
;
297 case ACTION_PS_DEC_SMALL
:
298 delta
= -PITCH_SMALL_DELTA
;
301 case ACTION_PS_DEC_BIG
:
302 delta
= -PITCH_BIG_DELTA
;
305 case ACTION_PS_NUDGE_RIGHT
:
306 new_pitch
= pitch_increase(pitch
, PITCH_NUDGE_DELTA
, false);
307 nudged
= (new_pitch
!= pitch
);
311 case ACTION_PS_NUDGE_RIGHTOFF
:
313 pitch
= pitch_increase(pitch
, -PITCH_NUDGE_DELTA
, false);
318 case ACTION_PS_NUDGE_LEFT
:
319 new_pitch
= pitch_increase(pitch
, -PITCH_NUDGE_DELTA
, false);
320 nudged
= (new_pitch
!= pitch
);
324 case ACTION_PS_NUDGE_LEFTOFF
:
326 pitch
= pitch_increase(pitch
, PITCH_NUDGE_DELTA
, false);
331 case ACTION_PS_RESET
:
333 sound_set_pitch( pitch
);
336 case ACTION_PS_TOGGLE_MODE
:
337 pitch_mode
= -pitch_mode
;
345 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
351 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
352 pitch
= pitch_increase(pitch
, delta
, true);
354 pitch
= pitch_increase_semitone(pitch
, delta
> 0);
360 #if CONFIG_CODEC == SWCODEC
361 pcmbuf_set_low_latency(false);