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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2004 Matthias Wientapper
*
* 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 "pluginlib_actions.h"
#include "metronome.h"
PLUGIN_HEADER
#define METRONOME_QUIT PLA_QUIT
#define METRONOME_VOL_UP PLA_INC
#define METRONOME_VOL_DOWN PLA_DEC
#define METRONOME_VOL_UP_REP PLA_INC_REPEAT
#define METRONOME_VOL_DOWN_REP PLA_DEC_REPEAT
#define METRONOME_LEFT PLA_LEFT
#define METRONOME_RIGHT PLA_RIGHT
#define METRONOME_LEFT_REP PLA_LEFT_REPEAT
#define METRONOME_RIGHT_REP PLA_RIGHT_REPEAT
enum {
METRONOME_PLAY_TAP = LAST_PLUGINLIB_ACTION+1,
#if CONFIG_KEYPAD == ONDIO_PAD
METRONOME_PAUSE,
#endif /* ONDIO_PAD */
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
METRONOME_SYNC
#endif /* IRIVER_H100_PAD||IRIVER_H300_PAD */
};
#if CONFIG_KEYPAD == ONDIO_PAD
#define METRONOME_TAP PLA_START
#define METRONOME_MSG_START "start: mode"
#define METRONOME_MSG_STOP "pause: hold mode"
static const struct button_mapping ondio_action[] =
{
{METRONOME_PLAY_TAP, BUTTON_MENU|BUTTON_REL, BUTTON_MENU },
{METRONOME_PAUSE, BUTTON_MENU|BUTTON_REPEAT, BUTTON_NONE },
{CONTEXT_CUSTOM,BUTTON_NONE,BUTTON_NONE}
};
#else /* !ONDIO_PAD */
#define METRONOME_TAP PLA_FIRE
#define METRONOME_PLAYPAUSE PLA_START
#define METRONOME_MSG_START "press play"
#define METRONOME_MSG_STOP "press pause"
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define MET_SYNC
static const struct button_mapping iriver_syncaction[] =
{
{METRONOME_SYNC, BUTTON_REC, BUTTON_NONE },
{CONTEXT_CUSTOM,BUTTON_NONE,BUTTON_NONE}
};
#endif /* IRIVER_H100_PAD||IRIVER_H300_PAD */
#endif /* #if CONFIG_KEYPAD == ONDIO_PAD */
const struct button_mapping *plugin_contexts[]={
generic_increase_decrease,
generic_directions,
#if CONFIG_KEYPAD == ONDIO_PAD
ondio_action,
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
iriver_syncaction,
#endif
generic_actions
};
#define PLA_ARRAY_COUNT sizeof(plugin_contexts)/sizeof(plugin_contexts[0])
static const struct plugin_api* rb;
MEM_FUNCTION_WRAPPERS(rb);
static int bpm = 120;
static int period = 0;
static int minitick = 0;
static bool sound_active = false;
static bool sound_paused = true;
static char buffer[30];
static bool reset_tap = false;
static int tap_count = 0;
static int tap_time = 0;
static int tap_timeout = 0;
int bpm_step_counter = 0;
#if CONFIG_CODEC != SWCODEC
#define MET_IS_PLAYING rb->mp3_is_playing()
#define MET_PLAY_STOP rb->mp3_play_stop()
void callback(unsigned char** start, size_t* size){
(void)start; /* unused parameter, avoid warning */
*size = 0; /* end of data */
sound_active = false;
rb->led(0);
}
void play_tock(void){
sound_active = true;
rb->led(1);
rb->mp3_play_data(sound, sizeof(sound), callback);
rb->mp3_play_pause(true); /* kickoff audio */
}
#else /* CONFIG_CODEC == SWCODEC */
#define MET_IS_PLAYING rb->pcm_is_playing()
#define MET_PLAY_STOP rb->audio_stop()
int tock;
bool need_to_play = false;
short sndbuf[sizeof(sound)*2];
/* Convert the mono "tock" sample to interleaved stereo */
void prepare_tock(void)
{
int i;
for(i = 0;i < (int)sizeof(sound)/2;i++) {
sndbuf[i*2] = sound[i];
sndbuf[i*2+1] = sound[i];
}
}
void play_tock(void) {
rb->pcm_play_data(NULL,(unsigned char *)sndbuf,sizeof(sndbuf));
tock++;
}
#endif /* CONFIG_CODEC != SWCODEC */
void calc_period(void)
{
period = 61440/bpm-1; /* (60*1024)/bpm; */
}
void metronome_draw(struct screen* display)
{
display->clear_display();
#ifdef HAVE_LCD_BITMAP
display->setfont(FONT_SYSFIXED);
display->puts(0, 0, "Metronome");
if(display->screen_type==SCREEN_MAIN)
{
display->puts(0, 5, "Select to TAP");
display->puts(0, 6, "Rec to SYNC");
}
#ifdef HAVE_REMOTE_LCD
else
{
display->puts(0, 5, "Rec to TAP");
display->puts(0, 6, "Mode to SYNC");
}
#endif
#endif /* HAVE_LCD_BITMAP */
rb->snprintf(buffer, sizeof(buffer), "BPM: %d ",bpm);
#ifdef HAVE_LCD_BITMAP
display->puts(0,3, buffer);
#else
display->puts(0,0, buffer);
#endif /* HAVE_LCD_BITMAP */
rb->snprintf(buffer, sizeof(buffer), "Vol: %d",
rb->global_settings->volume);
#ifdef HAVE_LCD_BITMAP
display->puts(10, 3, buffer);
#else
display->puts(0,1, buffer);
#endif /* HAVE_LCD_BITMAP */
#ifdef HAVE_LCD_BITMAP
display->hline(0, 111, 12);
if(sound_paused)
display->puts(0,2,METRONOME_MSG_START);
else
display->puts(0,2,METRONOME_MSG_STOP);
display->setfont(FONT_UI);
#endif /* HAVE_LCD_BITMAP */
display->update();
}
void draw_display(void)
{
int i;
FOR_NB_SCREENS(i)
metronome_draw(rb->screens[i]);
}
/* helper function to change the volume by a certain amount, +/-
ripped from video.c */
void change_volume(int delta){
int minvol = rb->sound_min(SOUND_VOLUME);
int maxvol = rb->sound_max(SOUND_VOLUME);
int vol = rb->global_settings->volume + delta;
if (vol > maxvol) vol = maxvol;
else if (vol < minvol) vol = minvol;
if (vol != rb->global_settings->volume) {
rb->sound_set(SOUND_VOLUME, vol);
rb->global_settings->volume = vol;
draw_display();
}
}
/*function to accelerate bpm change*/
void change_bpm(int direction){
if((bpm_step_counter < 20)
|| (bpm > 389)
|| (bpm < 10))
bpm = bpm + direction;
else if (bpm_step_counter < 60)
bpm = bpm + direction * 2;
else
bpm = bpm + direction * 9;
if (bpm > 400) bpm = 400;
if (bpm < 1) bpm = 1;
calc_period();
draw_display();
bpm_step_counter++;
}
void timer_callback(void)
{
if(minitick >= period){
minitick = 0;
if(!sound_active && !sound_paused && !tap_count) {
#if CONFIG_CODEC == SWCODEC
/* On SWCODEC we can't call play_tock() directly from an ISR. */
need_to_play = true;
#else
play_tock();
#endif
rb->reset_poweroff_timer();
}
}
else {
minitick++;
}
if (tap_count) {
tap_time++;
if (tap_count > 1 && tap_time > tap_timeout)
tap_count = 0;
}
}
void cleanup(void *parameter)
{
(void)parameter;
rb->timer_unregister();
MET_PLAY_STOP; /* stop audio ISR */
rb->led(0);
}
void tap(void)
{
if (tap_count == 0 || tap_time < tap_count) {
tap_time = 0;
}
else {
if (tap_time > 0) {
bpm = 61440/(tap_time/tap_count);
if (bpm > 400)
bpm = 400;
}
calc_period();
draw_display();
tap_timeout = (tap_count+2)*tap_time/tap_count;
}
tap_count++;
minitick = 0; /* sync tock to tapping */
play_tock();
reset_tap = false;
}
enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter){
int button;
enum plugin_status status;
(void)parameter;
rb = api;
if (MET_IS_PLAYING)
MET_PLAY_STOP; /* stop audio IS */
#if CONFIG_CODEC != SWCODEC
rb->bitswap(sound, sizeof(sound));
#else
prepare_tock();
#if INPUT_SRC_CAPS != 0
/* Select playback */
rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
#endif
rb->pcm_set_frequency(SAMPR_44);
#endif /* CONFIG_CODEC != SWCODEC */
calc_period();
rb->timer_register(1, NULL, TIMER_FREQ/1024, 1, timer_callback IF_COP(, CPU));
draw_display();
/* main loop */
while (true){
reset_tap = true;
#if CONFIG_CODEC == SWCODEC
button = pluginlib_getaction(rb,1,plugin_contexts,PLA_ARRAY_COUNT);
if (need_to_play)
{
need_to_play = false;
play_tock();
}
#else
button = pluginlib_getaction(rb,TIMEOUT_BLOCK,
plugin_contexts,PLA_ARRAY_COUNT);
#endif /* SWCODEC */
switch (button) {
case METRONOME_QUIT:
/* get out of here */
cleanup(NULL);
status = PLUGIN_OK;
goto metronome_exit;
#if CONFIG_KEYPAD == ONDIO_PAD
case METRONOME_PLAY_TAP:
if(sound_paused) {
sound_paused = false;
calc_period();
draw_display();
}
else
tap();
break;
case METRONOME_PAUSE:
if(!sound_paused) {
sound_paused = true;
draw_display();
}
break;
#else
case METRONOME_PLAYPAUSE:
if(sound_paused)
sound_paused = false;
else
sound_paused = true;
calc_period();
draw_display();
break;
#endif /* ONDIO_PAD */
case METRONOME_VOL_UP:
case METRONOME_VOL_UP_REP:
change_volume(1);
calc_period();
break;
case METRONOME_VOL_DOWN:
case METRONOME_VOL_DOWN_REP:
change_volume(-1);
calc_period();
break;
case METRONOME_LEFT:
bpm_step_counter = 0;
case METRONOME_LEFT_REP:
change_bpm(-1);
break;
case METRONOME_RIGHT:
bpm_step_counter = 0;
case METRONOME_RIGHT_REP:
change_bpm(1);
break;
#ifdef METRONOME_TAP
case METRONOME_TAP:
tap();
break;
#endif
#ifdef MET_SYNC
case METRONOME_SYNC:
minitick = period;
break;
#endif
default:
if (rb->default_event_handler_ex(button, cleanup, NULL)
== SYS_USB_CONNECTED)
{
status = PLUGIN_USB_CONNECTED;
goto metronome_exit;
}
reset_tap = false;
break;
}
if (reset_tap) {
tap_count = 0;
}
}
metronome_exit:
#if CONFIG_CODEC == SWCODEC
rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
#endif
return status;
}
|