1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Karl Kurbjun based on midi2wav by Stepan Moskovchenko
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 ****************************************************************************/
25 #include "sequencer.h"
31 /* variable button definitions */
32 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
33 #define BTN_QUIT BUTTON_OFF
34 #define BTN_RIGHT BUTTON_RIGHT
35 #define BTN_UP BUTTON_UP
36 #define BTN_DOWN BUTTON_DOWN
37 #define BTN_LEFT BUTTON_LEFT
38 #define BTN_RC_QUIT BUTTON_RC_STOP
39 #define BTN_PLAY BUTTON_ON
41 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
42 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
43 #define BTN_QUIT (BUTTON_SELECT | BUTTON_MENU)
44 #define BTN_RIGHT BUTTON_RIGHT
45 #define BTN_LEFT BUTTON_LEFT
46 #define BTN_UP BUTTON_SCROLL_FWD
47 #define BTN_DOWN BUTTON_SCROLL_BACK
48 #define BTN_PLAY BUTTON_PLAY
51 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
52 #define BTN_QUIT BUTTON_POWER
53 #define BTN_RIGHT BUTTON_RIGHT
54 #define BTN_LEFT BUTTON_LEFT
55 #define BTN_UP BUTTON_UP
56 #define BTN_DOWN BUTTON_DOWN
57 #define BTN_PLAY BUTTON_A
60 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
61 #define BTN_QUIT BUTTON_POWER
62 #define BTN_RIGHT BUTTON_RIGHT
63 #define BTN_LEFT BUTTON_LEFT
64 #define BTN_UP BUTTON_UP
65 #define BTN_DOWN BUTTON_DOWN
66 #define BTN_PLAY BUTTON_PLAY
69 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
70 #define BTN_QUIT BUTTON_POWER
71 #define BTN_RIGHT BUTTON_RIGHT
72 #define BTN_LEFT BUTTON_LEFT
73 #define BTN_UP BUTTON_SCROLL_FWD
74 #define BTN_DOWN BUTTON_SCROLL_BACK
75 #define BTN_PLAY BUTTON_UP
77 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
78 #define BTN_QUIT (BUTTON_HOME|BUTTON_REPEAT)
79 #define BTN_RIGHT BUTTON_RIGHT
80 #define BTN_LEFT BUTTON_LEFT
81 #define BTN_UP BUTTON_SCROLL_FWD
82 #define BTN_DOWN BUTTON_SCROLL_BACK
83 #define BTN_PLAY BUTTON_UP
86 #elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
87 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
88 (CONFIG_KEYPAD == SANSA_M200_PAD)
89 #define BTN_QUIT BUTTON_POWER
90 #define BTN_RIGHT BUTTON_RIGHT
91 #define BTN_LEFT BUTTON_LEFT
92 #define BTN_UP BUTTON_VOL_UP
93 #define BTN_DOWN BUTTON_VOL_DOWN
94 #define BTN_PLAY BUTTON_UP
97 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
98 #define BTN_QUIT BUTTON_POWER
99 #define BTN_RIGHT BUTTON_RIGHT
100 #define BTN_LEFT BUTTON_LEFT
101 #define BTN_UP BUTTON_UP
102 #define BTN_DOWN BUTTON_DOWN
103 #define BTN_PLAY BUTTON_PLAY
106 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
107 #define BTN_QUIT BUTTON_POWER
108 #define BTN_RIGHT BUTTON_RIGHT
109 #define BTN_LEFT BUTTON_LEFT
110 #define BTN_UP BUTTON_SCROLL_UP
111 #define BTN_DOWN BUTTON_SCROLL_DOWN
112 #define BTN_PLAY BUTTON_PLAY
115 #elif CONFIG_KEYPAD == MROBE500_PAD
116 #define BTN_QUIT BUTTON_POWER
117 #define BTN_RIGHT BUTTON_RIGHT
118 #define BTN_LEFT BUTTON_LEFT
119 #define BTN_UP BUTTON_RC_PLAY
120 #define BTN_DOWN BUTTON_RC_DOWN
121 #define BTN_PLAY BUTTON_RC_HEART
124 #elif (CONFIG_KEYPAD == MROBE100_PAD)
125 #define BTN_QUIT BUTTON_POWER
126 #define BTN_RIGHT BUTTON_RIGHT
127 #define BTN_LEFT BUTTON_LEFT
128 #define BTN_UP BUTTON_UP
129 #define BTN_DOWN BUTTON_DOWN
130 #define BTN_PLAY BUTTON_DISPLAY
133 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
134 #define BTN_QUIT BUTTON_RC_REC
135 #define BTN_RIGHT BUTTON_RC_FF
136 #define BTN_LEFT BUTTON_RC_REW
137 #define BTN_UP BUTTON_RC_VOL_UP
138 #define BTN_DOWN BUTTON_RC_VOL_DOWN
139 #define BTN_PLAY BUTTON_RC_PLAY
142 #elif CONFIG_KEYPAD == COWON_D2_PAD
143 #define BTN_QUIT BUTTON_POWER
145 #elif CONFIG_KEYPAD == IAUDIO67_PAD
146 #define BTN_QUIT BUTTON_POWER
147 #define BTN_RIGHT BUTTON_RIGHT
148 #define BTN_LEFT BUTTON_LEFT
149 #define BTN_UP BUTTON_STOP
150 #define BTN_DOWN BUTTON_PLAY
151 #define BTN_PLAY BUTTON_MENU
153 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
154 #define BTN_QUIT BUTTON_BACK
155 #define BTN_RIGHT BUTTON_RIGHT
156 #define BTN_LEFT BUTTON_LEFT
157 #define BTN_UP BUTTON_UP
158 #define BTN_DOWN BUTTON_DOWN
159 #define BTN_PLAY BUTTON_PLAY
161 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
162 #define BTN_QUIT BUTTON_POWER
163 #define BTN_RIGHT BUTTON_RIGHT
164 #define BTN_LEFT BUTTON_LEFT
165 #define BTN_UP BUTTON_UP
166 #define BTN_DOWN BUTTON_DOWN
167 #define BTN_PLAY BUTTON_MENU
169 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
170 #define BTN_QUIT BUTTON_POWER
171 #define BTN_RIGHT BUTTON_NEXT
172 #define BTN_LEFT BUTTON_PREV
173 #define BTN_UP BUTTON_UP
174 #define BTN_DOWN BUTTON_DOWN
175 #define BTN_PLAY BUTTON_MENU
177 #elif CONFIG_KEYPAD == ONDAVX747_PAD
178 #define BTN_QUIT BUTTON_POWER
179 #elif CONFIG_KEYPAD == ONDAVX777_PAD
180 #define BTN_QUIT BUTTON_POWER
182 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
183 #define BTN_QUIT BUTTON_REW
184 #define BTN_RIGHT BUTTON_RIGHT
185 #define BTN_LEFT BUTTON_LEFT
186 #define BTN_UP BUTTON_UP
187 #define BTN_DOWN BUTTON_DOWN
188 #define BTN_PLAY BUTTON_PLAY
190 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
191 #define BTN_QUIT BUTTON_REC
192 #define BTN_RIGHT BUTTON_NEXT
193 #define BTN_LEFT BUTTON_PREV
194 #define BTN_UP BUTTON_UP
195 #define BTN_DOWN BUTTON_DOWN
196 #define BTN_PLAY BUTTON_PLAY
198 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
199 #define BTN_QUIT (BUTTON_REC | BUTTON_PLAY)
200 #define BTN_RIGHT BUTTON_VOL_DOWN
201 #define BTN_LEFT BUTTON_VOL_UP
202 #define BTN_UP BUTTON_PREV
203 #define BTN_DOWN BUTTON_NEXT
204 #define BTN_PLAY BUTTON_PLAY
207 #error No keymap defined!
210 #ifdef HAVE_TOUCHSCREEN
212 #define BTN_QUIT BUTTON_TOPLEFT
215 #define BTN_RIGHT BUTTON_MIDRIGHT
218 #define BTN_LEFT BUTTON_MIDLEFT
221 #define BTN_UP BUTTON_TOPMIDDLE
224 #define BTN_DOWN BUTTON_BOTTOMMIDDLE
227 #define BTN_PLAY BUTTON_CENTER
237 struct MIDIfile
* mf IBSS_ATTR
;
239 int number_of_samples IBSS_ATTR
; /* the number of samples in the current tick */
240 int playing_time IBSS_ATTR
; /* How many seconds into the file have we been playing? */
241 int samples_this_second IBSS_ATTR
; /* How many samples produced during this second so far? */
244 int32_t gmbuf
[BUF_SIZE
*NBUF
];
245 static unsigned int samples_in_buf
;
249 bool lastswap
= true;
251 static inline void synthbuf(void)
257 if (lastswap
== swap
)
261 outptr
= (swap
? gmbuf
: gmbuf
+BUF_SIZE
);
266 /* synth samples for as many whole ticks as we can fit in the buffer */
267 for (; i
>= number_of_samples
; i
-= number_of_samples
)
269 synthSamples((int32_t*)outptr
, number_of_samples
);
270 outptr
+= number_of_samples
;
272 /* synthbuf is called in interrupt context is SYNC is defined so it cannot yield
273 that bug causing the sim to crach when not using SYNC should really be fixed */
280 /* how many samples did we write to the buffer? */
281 samples_in_buf
= BUF_SIZE
-i
;
284 void get_more(unsigned char** start
, size_t* size
)
289 printf("Buffer miss!"); /* Comment out the printf to make missses less noticable. */
293 synthbuf(); /* For some reason midiplayer crashes when an update is forced */
296 *size
= samples_in_buf
*sizeof(int32_t);
298 *start
= (unsigned char*)((swap
? gmbuf
: gmbuf
+ BUF_SIZE
));
301 *start
= (unsigned char*)(gmbuf
);
305 static int midimain(const void * filename
)
307 int a
, notes_used
, vol
;
308 bool is_playing
= true; /* false = paused */
310 printf("Loading file");
311 mf
= loadFile(filename
);
315 printf("Error loading file.");
319 if (initSynth(mf
, ROCKBOX_DIR
"/patchset/patchset.cfg",
320 ROCKBOX_DIR
"/patchset/drums.cfg") == -1)
324 #if INPUT_SRC_CAPS != 0
325 /* Select playback */
326 rb
->audio_set_input_source(AUDIO_SRC_PLAYBACK
, SRCF_PLAYBACK
);
327 rb
->audio_set_output_source(AUDIO_SRC_PLAYBACK
);
329 rb
->pcm_set_frequency(SAMPLE_RATE
); /* 44100 22050 11025 */
332 * tick() will do one MIDI clock tick. Then, there's a loop here that
333 * will generate the right number of samples per MIDI tick. The whole
334 * MIDI playback is timed in terms of this value.. there are no forced
335 * delays or anything. It just produces enough samples for each tick, and
336 * the playback of these samples is what makes the timings right.
338 * This seems to work quite well. On a laptop, anyway.
341 printf("Okay, starting sequencing");
343 bpm
= mf
->div
*1000000/tempo
;
344 number_of_samples
= SAMPLE_RATE
/bpm
;
346 /* Skip over any junk in the beginning of the file, so start playing */
347 /* after the first note event */
351 for (a
= 0; a
< MAX_VOICES
; a
++)
352 if (voices
[a
].isUsed
)
355 } while (notes_used
== 0);
358 samples_this_second
= 0;
361 rb
->pcm_play_data(&get_more
, NULL
, 0);
370 /* Prevent idle poweroff */
371 rb
->reset_poweroff_timer();
373 /* Code taken from Oscilloscope plugin */
374 switch (rb
->button_get(false))
377 case BTN_UP
| BUTTON_REPEAT
:
379 vol
= rb
->global_settings
->volume
;
380 if (vol
< rb
->sound_max(SOUND_VOLUME
))
383 rb
->sound_set(SOUND_VOLUME
, vol
);
384 rb
->global_settings
->volume
= vol
;
390 case BTN_DOWN
| BUTTON_REPEAT
:
392 vol
= rb
->global_settings
->volume
;
393 if (vol
> rb
->sound_min(SOUND_VOLUME
))
396 rb
->sound_set(SOUND_VOLUME
, vol
);
397 rb
->global_settings
->volume
= vol
;
404 /* Rewinding is tricky. Basically start the file over */
405 /* but run through the tracks without the synth running */
408 printf("Rewind to %d:%02d\n", playing_time
/60, playing_time
%60);
410 rb
->pcm_play_data(&get_more
, NULL
, 0);
418 printf("Skip to %d:%02d\n", playing_time
/60, playing_time
%60);
420 rb
->pcm_play_data(&get_more
, NULL
, 0);
428 printf("Paused at %d:%02d\n", playing_time
/60, playing_time
%60);
433 printf("Playing from %d:%02d\n", playing_time
/60, playing_time
%60);
435 rb
->pcm_play_data(&get_more
, NULL
, 0);
450 enum plugin_status
plugin_start(const void* parameter
)
457 if (parameter
== NULL
)
459 rb
->splash(HZ
*2, " Play .MID file ");
464 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
468 printf("%s", parameter
);
469 /* rb->splash(HZ, true, parameter); */
472 rb
->profile_thread();
475 retval
= midimain(parameter
);
482 rb
->pcm_set_frequency(HW_SAMPR_DEFAULT
);
484 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
485 rb
->cpu_boost(false);
487 rb
->splash(HZ
, "FINISHED PLAYING");