1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Björn Stenberg
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 ****************************************************************************/
35 #include "screen_access.h"
38 #define PITCH_MAX 2000
40 #define PITCH_SMALL_DELTA 1
41 #define PITCH_BIG_DELTA 10
42 #define PITCH_NUDGE_DELTA 20
44 #define PITCH_MODE_ABSOLUTE 1
45 #define PITCH_MODE_SEMITONE -PITCH_MODE_ABSOLUTE
47 static int pitch_mode
= PITCH_MODE_ABSOLUTE
; /* 1 - absolute, -1 - semitone */
50 0 if no key was pressed
51 1 if USB was connected */
53 static void pitch_screen_draw(struct screen
*display
, int pitch
, int pitch_mode
)
56 unsigned char buf
[32];
59 display
->clear_display();
61 if (display
->nb_lines
< 4) /* very small screen, just show the pitch value */
63 w
= snprintf((char *)buf
, sizeof(buf
), "%s: %d.%d%%",str(LANG_PITCH
),
64 pitch
/ 10, pitch
% 10 );
65 display
->putsxy((display
->width
-(w
*display
->char_width
))/2,
66 display
->nb_lines
/2,buf
);
68 else /* bigger screen, show everything... */
72 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
73 ptr
= str(LANG_PITCH_UP
);
75 ptr
= str(LANG_PITCH_UP_SEMITONE
);
77 display
->getstringsize(ptr
,&w
,&h
);
78 display
->putsxy((display
->width
-w
)/2, 0, ptr
);
79 display
->mono_bitmap(bitmap_icons_7x8
[Icon_UpArrow
],
80 display
->width
/2 - 3, h
, 7, 8);
82 /* DOWN: Pitch Down */
83 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
84 ptr
= str(LANG_PITCH_DOWN
);
86 ptr
= str(LANG_PITCH_DOWN_SEMITONE
);
88 display
->getstringsize(ptr
,&w
,&h
);
89 display
->putsxy((display
->width
-w
)/2, display
->height
- h
, ptr
);
90 display
->mono_bitmap(bitmap_icons_7x8
[Icon_DownArrow
],
91 display
->width
/2 - 3, display
->height
- h
*2, 7, 8);
95 display
->getstringsize(ptr
,&w
,&h
);
96 display
->putsxy(display
->width
-w
, (display
->height
-h
)/2, ptr
);
97 display
->mono_bitmap(bitmap_icons_7x8
[Icon_FastForward
],
98 display
->width
-w
-8, (display
->height
-h
)/2, 7, 8);
102 display
->getstringsize(ptr
,&w
,&h
);
103 display
->putsxy(0, (display
->height
-h
)/2, ptr
);
104 display
->mono_bitmap(bitmap_icons_7x8
[Icon_FastBackward
],
105 w
+1, (display
->height
-h
)/2, 7, 8);
108 snprintf((char *)buf
, sizeof(buf
), str(LANG_PITCH
));
109 display
->getstringsize(buf
,&w
,&h
);
110 display
->putsxy((display
->width
-w
)/2, (display
->height
/2)-h
, buf
);
112 snprintf((char *)buf
, sizeof(buf
), "%d.%d%%",
113 pitch
/ 10, pitch
% 10 );
114 display
->getstringsize(buf
,&w
,&h
);
115 display
->putsxy((display
->width
-w
)/2, display
->height
/2, buf
);
121 static int pitch_increase(int pitch
, int delta
, bool allow_cutoff
)
126 if (pitch
+ delta
>= PITCH_MIN
) {
127 new_pitch
= pitch
+ delta
;
132 new_pitch
= PITCH_MIN
;
134 } else if (delta
> 0) {
135 if (pitch
+ delta
<= PITCH_MAX
) {
136 new_pitch
= pitch
+ delta
;
141 new_pitch
= PITCH_MAX
;
144 /* delta == 0 -> no real change */
147 sound_set_pitch(new_pitch
);
152 /* Factor for changing the pitch one half tone up.
153 The exact value is 2^(1/12) = 1.05946309436
154 But we use only integer arithmetics, so take
155 rounded factor multiplied by 10^5=100,000. This is
156 enough to get the same promille values as if we
157 had used floating point (checked with a spread
160 #define PITCH_SEMITONE_FACTOR 105946L
162 /* Some helpful constants. K is the scaling factor for SEMITONE.
163 N is for more accurate rounding
166 #define PITCH_K_FCT 100000UL
167 #define PITCH_N_FCT 10
168 #define PITCH_KN_FCT 1000000UL
170 static int pitch_increase_semitone(int pitch
, bool up
)
173 uint32_t round_fct
; /* How much to scale down at the end */
176 tmp
= tmp
* PITCH_SEMITONE_FACTOR
;
177 round_fct
= PITCH_K_FCT
;
179 tmp
= (tmp
* PITCH_KN_FCT
) / PITCH_SEMITONE_FACTOR
;
180 round_fct
= PITCH_N_FCT
;
182 /* Scaling down with rounding */
183 tmp
= (tmp
+ round_fct
/ 2) / round_fct
;
184 return pitch_increase(pitch
, tmp
- pitch
, false);
187 bool pitch_screen(void)
190 int pitch
= sound_get_pitch();
191 int new_pitch
, delta
= 0;
196 #if CONFIG_CODEC == SWCODEC
197 pcmbuf_set_low_latency(true);
203 pitch_screen_draw(&screens
[i
], pitch
, pitch_mode
);
205 button
= get_action(CONTEXT_PITCHSCREEN
,TIMEOUT_BLOCK
);
207 case ACTION_PS_INC_SMALL
:
208 delta
= PITCH_SMALL_DELTA
;
211 case ACTION_PS_INC_BIG
:
212 delta
= PITCH_BIG_DELTA
;
215 case ACTION_PS_DEC_SMALL
:
216 delta
= -PITCH_SMALL_DELTA
;
219 case ACTION_PS_DEC_BIG
:
220 delta
= -PITCH_BIG_DELTA
;
223 case ACTION_PS_NUDGE_RIGHT
:
224 new_pitch
= pitch_increase(pitch
, PITCH_NUDGE_DELTA
, false);
225 nudged
= (new_pitch
!= pitch
);
229 case ACTION_PS_NUDGE_RIGHTOFF
:
231 pitch
= pitch_increase(pitch
, -PITCH_NUDGE_DELTA
, false);
236 case ACTION_PS_NUDGE_LEFT
:
237 new_pitch
= pitch_increase(pitch
, -PITCH_NUDGE_DELTA
, false);
238 nudged
= (new_pitch
!= pitch
);
242 case ACTION_PS_NUDGE_LEFTOFF
:
244 pitch
= pitch_increase(pitch
, PITCH_NUDGE_DELTA
, false);
249 case ACTION_PS_RESET
:
251 sound_set_pitch( pitch
);
254 case ACTION_PS_TOGGLE_MODE
:
255 pitch_mode
= -pitch_mode
;
263 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
270 if (pitch_mode
== PITCH_MODE_ABSOLUTE
) {
271 pitch
= pitch_increase(pitch
, delta
, true);
273 pitch
= pitch_increase_semitone(pitch
, delta
> 0 ? true:false);
280 #if CONFIG_CODEC == SWCODEC
281 pcmbuf_set_low_latency(false);
283 lcd_setfont(FONT_UI
);