Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / midi / midiplay.c
blob2c685f2e4bf5539140fd71f4b4fb5e864ebe4c54
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "guspat.h"
23 #include "midiutil.h"
24 #include "synth.h"
25 #include "sequencer.h"
26 #include "midifile.h"
28 PLUGIN_HEADER
29 PLUGIN_IRAM_DECLARE
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
206 #else
207 #error No keymap defined!
208 #endif
210 #ifdef HAVE_TOUCHSCREEN
211 #ifndef BTN_QUIT
212 #define BTN_QUIT BUTTON_TOPLEFT
213 #endif
214 #ifndef BTN_RIGHT
215 #define BTN_RIGHT BUTTON_MIDRIGHT
216 #endif
217 #ifndef BTN_LEFT
218 #define BTN_LEFT BUTTON_MIDLEFT
219 #endif
220 #ifndef BTN_UP
221 #define BTN_UP BUTTON_TOPMIDDLE
222 #endif
223 #ifndef BTN_DOWN
224 #define BTN_DOWN BUTTON_BOTTOMMIDDLE
225 #endif
226 #ifndef BTN_PLAY
227 #define BTN_PLAY BUTTON_CENTER
228 #endif
229 #endif
231 #undef SYNC
233 #ifdef SIMULATOR
234 #define SYNC
235 #endif
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? */
242 long bpm IBSS_ATTR;
244 int32_t gmbuf[BUF_SIZE*NBUF];
245 static unsigned int samples_in_buf;
247 bool quit = false;
248 bool swap = false;
249 bool lastswap = true;
251 static inline void synthbuf(void)
253 int32_t *outptr;
254 int i = BUF_SIZE;
256 #ifndef SYNC
257 if (lastswap == swap)
258 return;
259 lastswap = swap;
261 outptr = (swap ? gmbuf : gmbuf+BUF_SIZE);
262 #else
263 outptr = gmbuf;
264 #endif
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;
271 #ifndef SYNC
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 */
274 rb->yield();
275 #endif
276 if (tick() == 0)
277 quit = true;
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)
286 #ifndef SYNC
287 if(lastswap != swap)
289 printf("Buffer miss!"); /* Comment out the printf to make missses less noticable. */
292 #else
293 synthbuf(); /* For some reason midiplayer crashes when an update is forced */
294 #endif
296 *size = samples_in_buf*sizeof(int32_t);
297 #ifndef SYNC
298 *start = (unsigned char*)((swap ? gmbuf : gmbuf + BUF_SIZE));
299 swap = !swap;
300 #else
301 *start = (unsigned char*)(gmbuf);
302 #endif
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);
313 if (mf == NULL)
315 printf("Error loading file.");
316 return -1;
319 if (initSynth(mf, ROCKBOX_DIR "/patchset/patchset.cfg",
320 ROCKBOX_DIR "/patchset/drums.cfg") == -1)
321 return -1;
323 rb->pcm_play_stop();
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);
328 #endif
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 */
350 notes_used = 0;
351 for (a = 0; a < MAX_VOICES; a++)
352 if (voices[a].isUsed)
353 notes_used++;
354 tick();
355 } while (notes_used == 0);
357 playing_time = 0;
358 samples_this_second = 0;
360 synthbuf();
361 rb->pcm_play_data(&get_more, NULL, 0);
363 while (!quit)
365 #ifndef SYNC
366 synthbuf();
367 #endif
368 rb->yield();
370 /* Prevent idle poweroff */
371 rb->reset_poweroff_timer();
373 /* Code taken from Oscilloscope plugin */
374 switch (rb->button_get(false))
376 case BTN_UP:
377 case BTN_UP | BUTTON_REPEAT:
379 vol = rb->global_settings->volume;
380 if (vol < rb->sound_max(SOUND_VOLUME))
382 vol++;
383 rb->sound_set(SOUND_VOLUME, vol);
384 rb->global_settings->volume = vol;
386 break;
389 case BTN_DOWN:
390 case BTN_DOWN | BUTTON_REPEAT:
392 vol = rb->global_settings->volume;
393 if (vol > rb->sound_min(SOUND_VOLUME))
395 vol--;
396 rb->sound_set(SOUND_VOLUME, vol);
397 rb->global_settings->volume = vol;
399 break;
402 case BTN_LEFT:
404 /* Rewinding is tricky. Basically start the file over */
405 /* but run through the tracks without the synth running */
406 rb->pcm_play_stop();
407 seekBackward(5);
408 printf("Rewind to %d:%02d\n", playing_time/60, playing_time%60);
409 if (is_playing)
410 rb->pcm_play_data(&get_more, NULL, 0);
411 break;
414 case BTN_RIGHT:
416 rb->pcm_play_stop();
417 seekForward(5);
418 printf("Skip to %d:%02d\n", playing_time/60, playing_time%60);
419 if (is_playing)
420 rb->pcm_play_data(&get_more, NULL, 0);
421 break;
424 case BTN_PLAY:
426 if (is_playing)
428 printf("Paused at %d:%02d\n", playing_time/60, playing_time%60);
429 is_playing = false;
430 rb->pcm_play_stop();
431 } else
433 printf("Playing from %d:%02d\n", playing_time/60, playing_time%60);
434 is_playing = true;
435 rb->pcm_play_data(&get_more, NULL, 0);
437 break;
440 #ifdef BTN_RC_QUIT
441 case BTN_RC_QUIT:
442 #endif
443 case BTN_QUIT:
444 quit = true;
447 return 0;
450 enum plugin_status plugin_start(const void* parameter)
452 int retval;
453 PLUGINLIB_EXIT_INIT;
455 PLUGIN_IRAM_INIT(rb)
457 if (parameter == NULL)
459 rb->splash(HZ*2, " Play .MID file ");
460 return PLUGIN_OK;
462 rb->lcd_setfont(0);
464 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
465 rb->cpu_boost(true);
466 #endif
468 printf("%s", parameter);
469 /* rb->splash(HZ, true, parameter); */
471 #ifdef RB_PROFILE
472 rb->profile_thread();
473 #endif
475 retval = midimain(parameter);
477 #ifdef RB_PROFILE
478 rb->profstop();
479 #endif
481 rb->pcm_play_stop();
482 rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
484 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
485 rb->cpu_boost(false);
486 #endif
487 rb->splash(HZ, "FINISHED PLAYING");
489 if (retval == -1)
490 return PLUGIN_ERROR;
491 return PLUGIN_OK;