1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Karl Kurbjun based on midi2wav by Stepan Moskovchenko
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
23 #include "sequencer.h"
29 /* variable button definitions */
30 #if CONFIG_KEYPAD == RECORDER_PAD
31 #define BTN_QUIT BUTTON_OFF
32 #define BTN_RIGHT BUTTON_RIGHT
33 #define BTN_UP BUTTON_UP
34 #define BTN_DOWN BUTTON_DOWN
36 #elif CONFIG_KEYPAD == ONDIO_PAD
37 #define BTN_QUIT BUTTON_OFF
38 #define BTN_RIGHT BUTTON_RIGHT
39 #define BTN_UP BUTTON_UP
40 #define BTN_DOWN BUTTON_DOWN
42 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
43 #define BTN_QUIT BUTTON_OFF
44 #define BTN_RIGHT BUTTON_RIGHT
45 #define BTN_UP BUTTON_UP
46 #define BTN_DOWN BUTTON_DOWN
48 #define BTN_RC_QUIT BUTTON_RC_STOP
50 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
51 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
52 #define BTN_QUIT (BUTTON_SELECT | BUTTON_MENU)
53 #define BTN_RIGHT BUTTON_RIGHT
54 #define BTN_UP BUTTON_SCROLL_FWD
55 #define BTN_DOWN BUTTON_SCROLL_BACK
57 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
58 #define BTN_QUIT BUTTON_POWER
59 #define BTN_RIGHT BUTTON_RIGHT
60 #define BTN_UP BUTTON_UP
61 #define BTN_DOWN BUTTON_DOWN
63 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
64 (CONFIG_KEYPAD == SANSA_C200_PAD)
65 #define BTN_QUIT BUTTON_POWER
66 #define BTN_RIGHT BUTTON_RIGHT
67 #define BTN_UP BUTTON_UP
68 #define BTN_DOWN BUTTON_DOWN
70 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
71 #define BTN_QUIT BUTTON_POWER
72 #define BTN_RIGHT BUTTON_RIGHT
73 #define BTN_UP BUTTON_UP
74 #define BTN_DOWN BUTTON_DOWN
76 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
77 #define BTN_QUIT BUTTON_POWER
78 #define BTN_RIGHT BUTTON_RIGHT
79 #define BTN_UP BUTTON_SCROLL_UP
80 #define BTN_DOWN BUTTON_SCROLL_DOWN
90 struct MIDIfile
* mf IBSS_ATTR
;
92 int numberOfSamples IBSS_ATTR
;
95 int32_t gmbuf
[BUF_SIZE
*NBUF
];
98 struct plugin_api
* rb
;
100 static int midimain(void * filename
);
102 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
106 PLUGIN_IRAM_INIT(api
)
109 if(parameter
== NULL
)
111 rb
->splash(HZ
*2, " Play .MID file ");
116 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
120 printf("%s", parameter
);
121 /* rb->splash(HZ, true, parameter); */
124 rb
->profile_thread();
127 retval
= midimain(parameter
);
134 rb
->pcm_set_frequency(HW_SAMPR_DEFAULT
);
136 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
137 rb
->cpu_boost(false);
139 rb
->splash(HZ
, "FINISHED PLAYING");
149 static inline void synthbuf(void)
157 if(lastswap
==swap
) return;
160 outptr
=(swap
? gmbuf
: gmbuf
+BUF_SIZE
);
165 for(i
=0; i
<BUF_SIZE
; i
++)
167 synthSample(&synthtemp
[0], &synthtemp
[1]);
169 *outptr
=((synthtemp
[0]&0xFFFF) << 16) | (synthtemp
[1]&0xFFFF);
171 if(currentSample
==numberOfSamples
)
173 if( tick() == 0 ) quit
=1;
179 void get_more(unsigned char** start
, size_t* size
)
184 printf("Buffer miss!"); // Comment out the printf to make missses less noticable.
188 synthbuf(); // For some reason midiplayer crashes when an update is forced
191 *size
= sizeof(gmbuf
)/NBUF
;
193 *start
= (unsigned char*)((swap
? gmbuf
: gmbuf
+ BUF_SIZE
));
196 *start
= (unsigned char*)(gmbuf
);
200 static int midimain(void * filename
)
204 printf("Loading file");
205 mf
= loadFile(filename
);
209 printf("Error loading file.");
213 if (initSynth(mf
, ROCKBOX_DIR
"/patchset/patchset.cfg",
214 ROCKBOX_DIR
"/patchset/drums.cfg") == -1)
218 #if INPUT_SRC_CAPS != 0
219 /* Select playback */
220 rb
->audio_set_input_source(AUDIO_SRC_PLAYBACK
, SRCF_PLAYBACK
);
221 rb
->audio_set_output_source(AUDIO_SRC_PLAYBACK
);
223 rb
->pcm_set_frequency(SAMPLE_RATE
); // 44100 22050 11025
226 * tick() will do one MIDI clock tick. Then, there's a loop here that
227 * will generate the right number of samples per MIDI tick. The whole
228 * MIDI playback is timed in terms of this value.. there are no forced
229 * delays or anything. It just produces enough samples for each tick, and
230 * the playback of these samples is what makes the timings right.
232 * This seems to work quite well. On a laptop, anyway.
235 printf("Okay, starting sequencing");
237 bpm
=mf
->div
*1000000/tempo
;
238 numberOfSamples
=SAMPLE_RATE
/bpm
;
242 /* Skip over any junk in the beginning of the file, so start playing */
243 /* after the first note event */
247 for(a
=0; a
<MAX_VOICES
; a
++)
248 if(voices
[a
].isUsed
== 1)
251 } while(notesUsed
== 0);
254 rb
->pcm_play_data(&get_more
, NULL
, 0);
265 /* Prevent idle poweroff */
266 rb
->reset_poweroff_timer();
268 /* Code taken from Oscilloscope plugin */
269 switch(rb
->button_get(false))
272 case BTN_UP
| BUTTON_REPEAT
:
273 vol
= rb
->global_settings
->volume
;
274 if (vol
< rb
->sound_max(SOUND_VOLUME
))
277 rb
->sound_set(SOUND_VOLUME
, vol
);
278 rb
->global_settings
->volume
= vol
;
283 case BTN_DOWN
| BUTTON_REPEAT
:
284 vol
= rb
->global_settings
->volume
;
285 if (vol
> rb
->sound_min(SOUND_VOLUME
))
288 rb
->sound_set(SOUND_VOLUME
, vol
);
289 rb
->global_settings
->volume
= vol
;
296 /* Should skip length be retrieved from the RB settings? */
297 int samp
= 3*SAMPLE_RATE
;
298 int tickCount
= samp
/ numberOfSamples
;
300 for(a
=0; a
<tickCount
; a
++)