Fix yellow
[Rockbox.git] / apps / plugins / midi2wav.c
blob6e4e7d1bc159b89f07366500ee1b4df2870c2e8b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2005 Stepan Moskovchenko
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #define SAMPLE_RATE 22050
20 #define MAX_VOICES 100
23 /* Only define LOCAL_DSP on Simulator or else we're asking for trouble */
24 #if defined(SIMULATOR)
25 /*Enable this to write to the soundcard via a /dsv/dsp symlink in */
26 /*#define LOCAL_DSP */
27 #endif
30 #if defined(LOCAL_DSP)
31 /* This is for writing to the DSP directly from the Simulator */
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <linux/soundcard.h>
35 #include <sys/ioctl.h>
36 #endif
38 #include "../../firmware/export/system.h"
40 #include "../../plugin.h"
42 /*#include "../codecs/lib/xxx2wav.h" */
44 PLUGIN_HEADER
46 int numberOfSamples IDATA_ATTR;
47 long bpm;
49 #include "midi/midiutil.c"
50 #include "midi/guspat.h"
51 #include "midi/guspat.c"
52 #include "midi/sequencer.c"
53 #include "midi/midifile.c"
54 #include "midi/synth.c"
57 int fd=-1; /* File descriptor where the output is written */
59 extern long tempo; /* The sequencer keeps track of this */
61 const struct plugin_api * rb;
64 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
66 (void)parameter;
67 rb = api;
69 if(parameter == NULL)
71 rb->splash(HZ*2, "Play .MID file");
72 return PLUGIN_OK;
75 rb->splash(HZ, parameter);
76 if(midimain(parameter) == -1)
78 return PLUGIN_ERROR;
80 rb->splash(HZ*3, "FINISHED PLAYING");
81 /* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
82 return PLUGIN_USB_CONNECTED;
85 signed char outputBuffer[3000] IDATA_ATTR; /* signed char.. gonna run out of iram ... ! */
88 int currentSample IDATA_ATTR;
89 int outputBufferPosition IDATA_ATTR;
90 int outputSampleOne IDATA_ATTR;
91 int outputSampleTwo IDATA_ATTR;
94 int midimain(void * filename)
97 printf("\nHello.\n");
99 rb->splash(HZ/5, "LOADING MIDI");
101 struct MIDIfile * mf = loadFile(filename);
103 rb->splash(HZ/5, "LOADING PATCHES");
104 if (initSynth(mf, ROCKBOX_DIR "/patchset/patchset.cfg",
105 ROCKBOX_DIR "/patchset/drums.cfg") == -1)
107 return -1;
111 * This lets you hear the music through the sound card if you are on Simulator
112 * Make a symlink, archos/dsp.raw and make it point to /dev/dsp or whatever
113 * your sound device is.
116 #if defined(LOCAL_DSP)
117 fd=rb->open("/dsp.raw", O_WRONLY);
118 int arg, status;
119 int bit, samp, ch;
121 arg = 16; /* sample size */
122 status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
123 status = ioctl(fd, SOUND_PCM_READ_BITS, &arg);
124 bit=arg;
127 arg = 2; /* Number of channels, 1=mono */
128 status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
129 status = ioctl(fd, SOUND_PCM_READ_CHANNELS, &arg);
130 ch=arg;
132 arg = SAMPLE_RATE; /* Yeah. sampling rate */
133 status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
134 status = ioctl(fd, SOUND_PCM_READ_RATE, &arg);
135 samp=arg;
136 #else
138 /* xxx2wav stuff, removed for now, will move to the real way of outputting sound soon */
140 file_info_struct file_info;
141 file_info.samplerate = SAMPLE_RATE;
142 file_info.infile = fd;
143 file_info.channels = 2;
144 file_info.bitspersample = 16;
145 local_init("/miditest.tmp", "/miditest.wav", &file_info, rb);
146 fd = file_info.outfile;
148 #endif
151 rb->splash(HZ/5, "I hope this works...");
157 * tick() will do one MIDI clock tick. Then, there's a loop here that
158 * will generate the right number of samples per MIDI tick. The whole
159 * MIDI playback is timed in terms of this value.. there are no forced
160 * delays or anything. It just produces enough samples for each tick, and
161 * the playback of these samples is what makes the timings right.
163 * This seems to work quite well.
166 printf("\nOkay, starting sequencing");
169 currentSample=0; /* Sample counting variable */
170 outputBufferPosition = 0;
173 bpm=mf->div*1000000/tempo;
174 numberOfSamples=SAMPLE_RATE/bpm;
178 /* Tick() will return 0 if there are no more events left to play */
179 while(tick(mf))
182 * Tempo recalculation moved to sequencer.c to be done on a tempo event only
185 for(currentSample=0; currentSample<numberOfSamples; currentSample++)
188 synthSample(&outputSampleOne, &outputSampleTwo);
192 * 16-bit audio because, well, it's better
193 * But really because ALSA's OSS emulation sounds extremely
194 * noisy and distorted when in 8-bit mode. I still do not know
195 * why this happens.
198 outputBuffer[outputBufferPosition]=outputSampleOne&0XFF; /* Low byte first */
199 outputBufferPosition++;
200 outputBuffer[outputBufferPosition]=outputSampleOne>>8; /*High byte second */
201 outputBufferPosition++;
203 outputBuffer[outputBufferPosition]=outputSampleTwo&0XFF; /* Low byte first */
204 outputBufferPosition++;
205 outputBuffer[outputBufferPosition]=outputSampleTwo>>8; /*High byte second */
206 outputBufferPosition++;
210 * As soon as we produce 2000 bytes of sound,
211 * write it to the sound card. Why 2000? I have
212 * no idea. It's 1 AM and I am dead tired.
214 if(outputBufferPosition>=2000)
216 rb->write(fd, outputBuffer, 2000);
217 outputBufferPosition=0;
222 printf("\n");
224 #if !defined(LOCAL_DSP)
226 /* again, xxx2wav stuff, removed for now */
228 /* close_wav(&file_info); */
229 #else
230 rb->close(fd);
231 #endif
232 return 0;