summaryrefslogtreecommitdiffstats
path: root/apps/gui/pitchscreen.c
blob: 50720316526536ace9a5308a18d740532fadabd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Björn Stenberg
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#include "sprintf.h"
#include "action.h"
#include "dsp.h"
#include "sound.h"
#include "pcmbuf.h"
#include "lang.h"
#include "icons.h"
#include "screens.h"
#include "viewport.h"
#include "font.h"
#include "system.h"
#include "misc.h"
#include "pitchscreen.h"
#if CONFIG_CODEC == SWCODEC
#include "tdspeed.h"
#endif


#define ICON_BORDER 12 /* icons are currently 7x8, so add ~2 pixels  */
                       /*   on both sides when drawing */

#define PITCH_MAX         2000
#define PITCH_MIN         500
#define PITCH_SMALL_DELTA 1
#define PITCH_BIG_DELTA   10
#define PITCH_NUDGE_DELTA 20

static enum
{
    PITCH_MODE_ABSOLUTE,
    PITCH_MODE_SEMITONE,
#if CONFIG_CODEC == SWCODEC
    PITCH_MODE_TIMESTRETCH,
#endif
} pitch_mode = PITCH_MODE_ABSOLUTE;

enum
{
    PITCH_TOP = 0,
    PITCH_MID,
    PITCH_BOTTOM,
    PITCH_ITEM_COUNT,
};

static void pitchscreen_fix_viewports(struct viewport *parent,
        struct viewport pitch_viewports[PITCH_ITEM_COUNT])
{
    int i, height;
    height = font_get(parent->font)->height;
    for (i = 0; i < PITCH_ITEM_COUNT; i++)
    {
        pitch_viewports[i] = *parent;
        pitch_viewports[i].height = height;
    }
    pitch_viewports[PITCH_TOP].y += ICON_BORDER;

    pitch_viewports[PITCH_MID].x += ICON_BORDER;
    pitch_viewports[PITCH_MID].width = parent->width - ICON_BORDER*2;
    pitch_viewports[PITCH_MID].height = height * 2;
    pitch_viewports[PITCH_MID].y += parent->height / 2 -
            pitch_viewports[PITCH_MID].height / 2;

    pitch_viewports[PITCH_BOTTOM].y += parent->height - height - ICON_BORDER;
}

/* must be called before pitchscreen_draw, or within
 * since it neither clears nor updates the display */
static void pitchscreen_draw_icons(struct screen *display,
                                   struct viewport *parent)
{
    display->set_viewport(parent);
    display->mono_bitmap(bitmap_icons_7x8[Icon_UpArrow],
            parent->width/2 - 3,
            2, 7, 8);
    display->mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
            parent->width /2 - 3,
            parent->height - 10, 7, 8);
    display->mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
            parent->width - 10,
            parent->height /2 - 4, 7, 8);
    display->mono_bitmap(bitmap_icons_7x8[Icon_FastBackward],
            2,
            parent->height /2 - 4, 7, 8);
    display->update_viewport();
}

static void pitchscreen_draw(struct screen *display, int max_lines,
                             struct viewport pitch_viewports[PITCH_ITEM_COUNT],
                             int pitch
#if CONFIG_CODEC == SWCODEC
                             ,int speed
#endif
                             )
{
    unsigned char* ptr;
    char buf[32];
    int w, h;
    bool show_lang_pitch;

     /* "Pitch up/Pitch down" - hide for a small screen */
    if (max_lines >= 5)
    {
        /* UP: Pitch Up */
        display->set_viewport(&pitch_viewports[PITCH_TOP]);
        if (pitch_mode == PITCH_MODE_SEMITONE)
            ptr = str(LANG_PITCH_UP_SEMITONE);
        else
            ptr = str(LANG_PITCH_UP);
        display->getstringsize(ptr, &w, &h);
        display->clear_viewport();
        /* draw text */
        display->putsxy((pitch_viewports[PITCH_TOP].width / 2) -
                (w / 2), 0, ptr);
        display->update_viewport();

        /* DOWN: Pitch Down */
        display->set_viewport(&pitch_viewports[PITCH_BOTTOM]);
        if (pitch_mode == PITCH_MODE_SEMITONE)
            ptr = str(LANG_PITCH_DOWN_SEMITONE);
        else
            ptr = str(LANG_PITCH_DOWN);
        display->getstringsize(ptr, &w, &h);
        display->clear_viewport();
        /* draw text */
        display->putsxy((pitch_viewports[PITCH_BOTTOM].width / 2) -
                (w / 2), 0, ptr);
        display->update_viewport();
    }

    /* Middle section */
    display->set_viewport(&pitch_viewports[PITCH_MID]);
    display->clear_viewport();
    int width_used = 0;

    /* Middle section upper line - hide for a small screen */
    if ((show_lang_pitch = (max_lines >= 3)))
    {
#if CONFIG_CODEC == SWCODEC
        if (pitch_mode != PITCH_MODE_TIMESTRETCH)
        {
#endif
            /* LANG_PITCH */
            snprintf(buf, sizeof(buf), "%s", str(LANG_PITCH));
#if CONFIG_CODEC == SWCODEC
        }
        else
        {
            /* Pitch:XXX.X% */
            snprintf(buf, sizeof(buf), "%s:%d.%d%%", str(LANG_PITCH),
                     pitch / 10, pitch % 10);
        }
#endif
        display->getstringsize(buf, &w, &h);
        display->putsxy((pitch_viewports[PITCH_MID].width / 2) - (w / 2),
                        0, buf);
        if (w > width_used)
            width_used = w;
    }

    /* Middle section lower line */
#if CONFIG_CODEC == SWCODEC
    if (pitch_mode != PITCH_MODE_TIMESTRETCH)
    {
#endif
        /* "XXX.X%" */
        snprintf(buf, sizeof(buf), "%d.%d%%",
             pitch / 10, pitch % 10);
#if CONFIG_CODEC == SWCODEC
    }
    else
    {
        /* "Speed:XXX%" */
        snprintf(buf, sizeof(buf), "%s:%d%%", str(LANG_SPEED), speed);
    }
#endif
    display->getstringsize(buf, &w, &h);
    display->putsxy((pitch_viewports[PITCH_MID].width / 2) - (w / 2),
        (show_lang_pitch ? h : h/2), buf);
    if (w > width_used)
        width_used = w;

    /* Middle section left/right labels */
    const char *leftlabel = "-2%";
    const char *rightlabel = "+2%";
#if CONFIG_CODEC == SWCODEC
    if (pitch_mode == PITCH_MODE_TIMESTRETCH)
    {
        leftlabel = "<<";
        rightlabel = ">>";
    }
#endif

    /* Only display if they fit */
    display->getstringsize(leftlabel, &w, &h);
    width_used += w;
    display->getstringsize(rightlabel, &w, &h);
    width_used += w;

    if (width_used <= pitch_viewports[PITCH_MID].width)
    {
        display->putsxy(0, h / 2, leftlabel);
        display->putsxy(pitch_viewports[PITCH_MID].width - w, h /2, rightlabel);
    }
    display->update_viewport();
    display->set_viewport(NULL);
}

static int pitch_increase(int pitch, int pitch_delta, bool allow_cutoff)
{
    int new_pitch;

    if (pitch_delta < 0)
    {
        if (pitch + pitch_delta >= PITCH_MIN)
            new_pitch = pitch + pitch_delta;
        else
        {
            if (!allow_cutoff)
                return pitch;
            new_pitch = PITCH_MIN;
        }
    }
    else if (pitch_delta > 0)
    {
        if (pitch + pitch_delta <= PITCH_MAX)
            new_pitch = pitch + pitch_delta;
        else
        {
            if (!allow_cutoff)
                return pitch;
            new_pitch = PITCH_MAX;
        }
    }
    else
    {
        /* pitch_delta == 0 -> no real change */
        return pitch;
    }
    sound_set_pitch(new_pitch);

    return new_pitch;
}

/* Factor for changing the pitch one half tone up.
   The exact value is 2^(1/12) = 1.05946309436
   But we use only integer arithmetics, so take
   rounded factor multiplied by 10^5=100,000. This is
   enough to get the same promille values as if we
   had used floating point (checked with a spread
   sheet).
 */
#define PITCH_SEMITONE_FACTOR 105946L

/* Some helpful constants. K is the scaling factor for SEMITONE.
   N is for more accurate rounding
   KN is K * N
 */
#define PITCH_K_FCT           100000UL
#define PITCH_N_FCT           10
#define PITCH_KN_FCT          1000000UL

static int pitch_increase_semitone(int pitch, bool up)
{
    uint32_t tmp;
    uint32_t round_fct; /* How much to scale down at the end */
    tmp = pitch;
    if (up)
    {
        tmp = tmp * PITCH_SEMITONE_FACTOR;
        round_fct = PITCH_K_FCT;
    }
    else
    {
        tmp = (tmp * PITCH_KN_FCT) / PITCH_SEMITONE_FACTOR;
        round_fct = PITCH_N_FCT;
    }
    /* Scaling down with rounding */
    tmp = (tmp + round_fct / 2) / round_fct;
    return pitch_increase(pitch, tmp - pitch, false);
}

/*
    returns:
    0 on exit
    1 if USB was connected
*/

int gui_syncpitchscreen_run(void)
{
    int button, i;
    int pitch = sound_get_pitch();
#if CONFIG_CODEC == SWCODEC
    int speed = dsp_get_timestretch();
    int maintain_speed_pitch = speed * pitch; /* speed * pitch to maintain */
#endif
    int new_pitch;
    int pitch_delta = 0;
    bool nudged = false;
    bool exit = false;
    /* should maybe be passed per parameter later, not needed for now */
    struct viewport parent[NB_SCREENS];
    struct viewport pitch_viewports[NB_SCREENS][PITCH_ITEM_COUNT];
    int max_lines[NB_SCREENS];

    /* initialize pitchscreen vps */
    FOR_NB_SCREENS(i)
    {
        screens[i].clear_display();
        viewport_set_defaults(&parent[i], i);
        max_lines[i] = viewport_get_nb_lines(&parent[i]);
        pitchscreen_fix_viewports(&parent[i], pitch_viewports[i]);

        /* also, draw the icons now, it's only needed once */
        pitchscreen_draw_icons(&screens[i], &parent[i]);
    }
#if CONFIG_CODEC == SWCODEC
    pcmbuf_set_low_latency(true);
#endif

    while (!exit)
    {
        FOR_NB_SCREENS(i)
            pitchscreen_draw(&screens[i], max_lines[i],
                              pitch_viewports[i], pitch
#if CONFIG_CODEC == SWCODEC
                              , speed
#endif
                              );
        button = get_action(CONTEXT_PITCHSCREEN, HZ);
        switch (button)
        {
            case ACTION_PS_INC_SMALL:
                pitch_delta = PITCH_SMALL_DELTA;
                break;

            case ACTION_PS_INC_BIG:
                pitch_delta = PITCH_BIG_DELTA;
                break;

            case ACTION_PS_DEC_SMALL:
                pitch_delta = -PITCH_SMALL_DELTA;
                break;

            case ACTION_PS_DEC_BIG:
                pitch_delta = -PITCH_BIG_DELTA;
                break;

            case ACTION_PS_NUDGE_RIGHT:
#if CONFIG_CODEC == SWCODEC
                if (pitch_mode != PITCH_MODE_TIMESTRETCH)
                {
#endif
                    new_pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false);
                    nudged = (new_pitch != pitch);
                    pitch = new_pitch;
                    break;
#if CONFIG_CODEC == SWCODEC
                }

            case ACTION_PS_FASTER:
                if (pitch_mode == PITCH_MODE_TIMESTRETCH)
                {
                    if (speed < SPEED_MAX)
                    {
                        speed++;
                        dsp_set_timestretch(speed);
                        maintain_speed_pitch = speed * pitch;
                    }
                }
                break;
#endif

            case ACTION_PS_NUDGE_RIGHTOFF:
                if (nudged)
                {
                    pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false);
                    nudged = false;
                }
                break;

            case ACTION_PS_NUDGE_LEFT:
#if CONFIG_CODEC == SWCODEC
                if (pitch_mode != PITCH_MODE_TIMESTRETCH)
                {
#endif
                    new_pitch = pitch_increase(pitch, -PITCH_NUDGE_DELTA, false);
                    nudged = (new_pitch != pitch);
                    pitch = new_pitch;
                    break;
#if CONFIG_CODEC == SWCODEC
                }

            case ACTION_PS_SLOWER:
                if (pitch_mode == PITCH_MODE_TIMESTRETCH)
                {
                    if (speed > SPEED_MIN)
                    {
                        speed--;
                        dsp_set_timestretch(speed);
                        maintain_speed_pitch = speed * pitch;
                    }
                }
                break;
#endif

            case ACTION_PS_NUDGE_LEFTOFF:
                if (nudged)
                {
                    pitch = pitch_increase(pitch, PITCH_NUDGE_DELTA, false);
                    nudged = false;
                }
                break;

            case ACTION_PS_RESET:
                pitch = 1000;
                sound_set_pitch(pitch);
#if CONFIG_CODEC == SWCODEC
                speed = 100;
                dsp_set_timestretch(speed);
                maintain_speed_pitch = speed * pitch;
#endif
                break;

            case ACTION_PS_TOGGLE_MODE:
                ++pitch_mode;
#if CONFIG_CODEC == SWCODEC
                if (dsp_timestretch_enabled())
                {
                    if (pitch_mode > PITCH_MODE_TIMESTRETCH)
                        pitch_mode = PITCH_MODE_ABSOLUTE;
                    break;
                }
#endif
                if (pitch_mode > PITCH_MODE_SEMITONE)
                    pitch_mode = PITCH_MODE_ABSOLUTE;
                break;

            case ACTION_PS_EXIT:
                exit = true;
                break;

            default:
                if (default_event_handler(button) == SYS_USB_CONNECTED)
                    return 1;
                break;
        }
        if (pitch_delta)
        {
            if (pitch_mode == PITCH_MODE_SEMITONE)
                pitch = pitch_increase_semitone(pitch, pitch_delta > 0);
            else
                pitch = pitch_increase(pitch, pitch_delta, true);
#if CONFIG_CODEC == SWCODEC
            if (pitch_mode == PITCH_MODE_TIMESTRETCH)
            {
                /* Set speed to maintain time dimension */
                /* i.e. increase pitch, slow down speed */
                int new_speed = maintain_speed_pitch / pitch;
                if (new_speed >= SPEED_MIN && new_speed <= SPEED_MAX)
                {
                    speed = new_speed;
                    dsp_set_timestretch(speed);
                }
            }
            else
                maintain_speed_pitch = speed * pitch;
#endif
            pitch_delta = 0;
        }
    }
#if CONFIG_CODEC == SWCODEC
    pcmbuf_set_low_latency(false);
#endif
    return 0;
}