Use the AMS_LOWMEM define to indicate memory size as the .lds files do in config...
[kugel-rb.git] / apps / gui / pitchscreen.c
blob485eb7861cb4f301f4a3df0893124a433a1534da
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdbool.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "config.h"
26 #include "sprintf.h"
27 #include "action.h"
28 #include "dsp.h"
29 #include "sound.h"
30 #include "pcmbuf.h"
31 #include "lang.h"
32 #include "icons.h"
33 #include "screens.h"
34 #include "viewport.h"
35 #include "font.h"
36 #include "system.h"
37 #include "misc.h"
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
47 #define PITCH_MIN 500
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 */
55 enum
57 PITCH_TOP = 0,
58 PITCH_MID,
59 PITCH_BOTTOM,
60 PITCH_ITEM_COUNT,
63 static void pitchscreen_fix_viewports(struct viewport *parent,
64 struct viewport pitch_viewports[PITCH_ITEM_COUNT])
66 int i, height;
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],
91 parent->width/2 - 3,
92 2, 7, 8);
93 display->mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
94 parent->width /2 - 3,
95 parent->height - 10, 7, 8);
96 display->mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
97 parent->width - 10,
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)
108 unsigned char* ptr;
109 unsigned char buf[32];
110 int width_val, w, h;
111 bool show_lang_pitch;
113 /* Hide "Pitch up/Pitch down" for a small screen */
114 if (max_lines >= 5)
116 /* UP: Pitch Up */
117 display->set_viewport(&pitch_viewports[PITCH_TOP]);
118 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
119 ptr = str(LANG_PITCH_UP);
120 } else {
121 ptr = str(LANG_PITCH_UP_SEMITONE);
123 display->getstringsize(ptr,&w,&h);
124 display->clear_viewport();
125 /* draw text */
126 display->putsxy((pitch_viewports[PITCH_TOP].width / 2) -
127 (w / 2), 0, ptr);
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);
134 } else {
135 ptr = str(LANG_PITCH_DOWN_SEMITONE);
137 display->getstringsize(ptr,&w,&h);
138 display->clear_viewport();
139 /* draw text */
140 display->putsxy((pitch_viewports[PITCH_BOTTOM].width / 2) -
141 (w / 2), 0, ptr);
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),
152 0, buf);
154 /* "XXX.X%" */
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)
164 w = width_val;
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);
170 w += width_val*2;
171 /* hide +2%/-2% for a narrow screens */
172 if (w <= pitch_viewports[PITCH_MID].width)
174 /* RIGHT: +2% */
175 display->putsxy(pitch_viewports[PITCH_MID].width - width_val, h /2, "+2%");
176 /* LEFT: -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)
185 int new_pitch;
187 if (delta < 0) {
188 if (pitch + delta >= PITCH_MIN) {
189 new_pitch = pitch + delta;
190 } else {
191 if (!allow_cutoff) {
192 return pitch;
194 new_pitch = PITCH_MIN;
196 } else if (delta > 0) {
197 if (pitch + delta <= PITCH_MAX) {
198 new_pitch = pitch + delta;
199 } else {
200 if (!allow_cutoff) {
201 return pitch;
203 new_pitch = PITCH_MAX;
205 } else {
206 /* delta == 0 -> no real change */
207 return pitch;
209 sound_set_pitch(new_pitch);
211 return 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
220 sheet).
222 #define PITCH_SEMITONE_FACTOR 105946L
224 /* Some helpful constants. K is the scaling factor for SEMITONE.
225 N is for more accurate rounding
226 KN is K * N
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)
234 uint32_t tmp;
235 uint32_t round_fct; /* How much to scale down at the end */
236 tmp = pitch;
237 if (up) {
238 tmp = tmp * PITCH_SEMITONE_FACTOR;
239 round_fct = PITCH_K_FCT;
240 } else {
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);
250 returns:
251 0 on exit
252 1 if USB was connected
255 int gui_syncpitchscreen_run(void)
257 int button, i;
258 int pitch = sound_get_pitch();
259 int new_pitch, delta = 0;
260 bool nudged = false;
261 bool exit = false;
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 */
268 FOR_NB_SCREENS(i)
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);
280 #endif
282 while (!exit)
284 FOR_NB_SCREENS(i)
285 pitchscreen_draw(&screens[i], max_lines[i],
286 pitch_viewports[i], pitch);
287 button = get_action(CONTEXT_PITCHSCREEN,HZ);
288 switch (button) {
289 case ACTION_PS_INC_SMALL:
290 delta = PITCH_SMALL_DELTA;
291 break;
293 case ACTION_PS_INC_BIG:
294 delta = PITCH_BIG_DELTA;
295 break;
297 case ACTION_PS_DEC_SMALL:
298 delta = -PITCH_SMALL_DELTA;
299 break;
301 case ACTION_PS_DEC_BIG:
302 delta = -PITCH_BIG_DELTA;
303 break;
305 case ACTION_PS_NUDGE_RIGHT:
306 new_pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false);
307 nudged = (new_pitch != pitch);
308 pitch = new_pitch;
309 break;
311 case ACTION_PS_NUDGE_RIGHTOFF:
312 if (nudged) {
313 pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false);
315 nudged = false;
316 break;
318 case ACTION_PS_NUDGE_LEFT:
319 new_pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false);
320 nudged = (new_pitch != pitch);
321 pitch = new_pitch;
322 break;
324 case ACTION_PS_NUDGE_LEFTOFF:
325 if (nudged) {
326 pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false);
328 nudged = false;
329 break;
331 case ACTION_PS_RESET:
332 pitch = 1000;
333 sound_set_pitch( pitch );
334 break;
336 case ACTION_PS_TOGGLE_MODE:
337 pitch_mode = -pitch_mode;
338 break;
340 case ACTION_PS_EXIT:
341 exit = true;
342 break;
344 default:
345 if(default_event_handler(button) == SYS_USB_CONNECTED)
346 return 1;
347 break;
349 if(delta)
351 if (pitch_mode == PITCH_MODE_ABSOLUTE) {
352 pitch = pitch_increase(pitch, delta, true);
353 } else {
354 pitch = pitch_increase_semitone(pitch, delta > 0);
357 delta = 0;
360 #if CONFIG_CODEC == SWCODEC
361 pcmbuf_set_low_latency(false);
362 #endif
363 return 0;