Get rid of unnecessary memory acesses in a loop, gives slight speedup
[kugel-rb.git] / apps / plugins / midi / midiplay.c
blobc557433dceeab8b683bc00f40bacd82487fd67cf
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 * 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 ****************************************************************************/
19 #include "plugin.h"
20 #include "guspat.h"
21 #include "midiutil.h"
22 #include "synth.h"
23 #include "sequencer.h"
24 #include "midifile.h"
26 PLUGIN_HEADER
27 PLUGIN_IRAM_DECLARE
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
82 #endif
84 #undef SYNC
86 #ifdef SIMULATOR
87 #define SYNC
88 #endif
90 struct MIDIfile * mf IBSS_ATTR;
92 int numberOfSamples IBSS_ATTR;
93 long bpm IBSS_ATTR;
95 int32_t gmbuf[BUF_SIZE*NBUF];
97 int quit=0;
98 struct plugin_api * rb;
100 static int midimain(void * filename);
102 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
104 int retval = 0;
106 PLUGIN_IRAM_INIT(api)
108 rb = api;
109 if(parameter == NULL)
111 rb->splash(HZ*2, " Play .MID file ");
112 return PLUGIN_OK;
114 rb->lcd_setfont(0);
116 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
117 rb->cpu_boost(true);
118 #endif
120 printf("%s", parameter);
121 /* rb->splash(HZ, true, parameter); */
123 #ifdef RB_PROFILE
124 rb->profile_thread();
125 #endif
127 retval = midimain(parameter);
129 #ifdef RB_PROFILE
130 rb->profstop();
131 #endif
133 rb->pcm_play_stop();
134 rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
136 #if defined(HAVE_ADJUSTABLE_CPU_FREQ)
137 rb->cpu_boost(false);
138 #endif
139 rb->splash(HZ, "FINISHED PLAYING");
141 if(retval == -1)
142 return PLUGIN_ERROR;
143 return PLUGIN_OK;
146 bool swap=0;
147 bool lastswap=1;
149 static inline void synthbuf(void)
151 int32_t *outptr;
152 register int i;
153 int currentSample=0;
154 int synthtemp[2];
156 #ifndef SYNC
157 if(lastswap==swap) return;
158 lastswap=swap;
160 outptr=(swap ? gmbuf : gmbuf+BUF_SIZE);
161 #else
162 outptr=gmbuf;
163 #endif
165 for(i=0; i<BUF_SIZE; i++)
167 synthSample(&synthtemp[0], &synthtemp[1]);
168 currentSample++;
169 *outptr=((synthtemp[0]&0xFFFF) << 16) | (synthtemp[1]&0xFFFF);
170 outptr++;
171 if(currentSample==numberOfSamples)
173 if( tick() == 0 ) quit=1;
174 currentSample=0;
179 void get_more(unsigned char** start, size_t* size)
181 #ifndef SYNC
182 if(lastswap!=swap)
184 printf("Buffer miss!"); // Comment out the printf to make missses less noticable.
187 #else
188 synthbuf(); // For some reason midiplayer crashes when an update is forced
189 #endif
191 *size = sizeof(gmbuf)/NBUF;
192 #ifndef SYNC
193 *start = (unsigned char*)((swap ? gmbuf : gmbuf + BUF_SIZE));
194 swap=!swap;
195 #else
196 *start = (unsigned char*)(gmbuf);
197 #endif
200 static int midimain(void * filename)
202 int notesUsed = 0;
203 int a=0;
204 printf("Loading file");
205 mf= loadFile(filename);
207 if(mf == NULL)
209 printf("Error loading file.");
210 return -1;
213 if (initSynth(mf, ROCKBOX_DIR "/patchset/patchset.cfg",
214 ROCKBOX_DIR "/patchset/drums.cfg") == -1)
215 return -1;
217 rb->pcm_play_stop();
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);
222 #endif
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 */
246 notesUsed = 0;
247 for(a=0; a<MAX_VOICES; a++)
248 if(voices[a].isUsed == 1)
249 notesUsed++;
250 tick();
251 } while(notesUsed == 0);
253 synthbuf();
254 rb->pcm_play_data(&get_more, NULL, 0);
256 int vol=0;
258 while(!quit)
260 #ifndef SYNC
261 synthbuf();
262 #endif
263 rb->yield();
265 /* Prevent idle poweroff */
266 rb->reset_poweroff_timer();
268 /* Code taken from Oscilloscope plugin */
269 switch(rb->button_get(false))
271 case BTN_UP:
272 case BTN_UP | BUTTON_REPEAT:
273 vol = rb->global_settings->volume;
274 if (vol < rb->sound_max(SOUND_VOLUME))
276 vol++;
277 rb->sound_set(SOUND_VOLUME, vol);
278 rb->global_settings->volume = vol;
280 break;
282 case BTN_DOWN:
283 case BTN_DOWN | BUTTON_REPEAT:
284 vol = rb->global_settings->volume;
285 if (vol > rb->sound_min(SOUND_VOLUME))
287 vol--;
288 rb->sound_set(SOUND_VOLUME, vol);
289 rb->global_settings->volume = vol;
291 break;
293 case BTN_RIGHT:
295 /* Skip 3 seconds */
296 /* Should skip length be retrieved from the RB settings? */
297 int samp = 3*SAMPLE_RATE;
298 int tickCount = samp / numberOfSamples;
299 int a=0;
300 for(a=0; a<tickCount; a++)
301 tick();
302 break;
304 #ifdef BTN_RC_QUIT
305 case BTN_RC_QUIT:
306 #endif
307 case BTN_QUIT:
308 quit=1;
314 return 0;