1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 */
30 #if defined(LOCAL_DSP)
31 /* This is for writing to the DSP directly from the Simulator */
34 #include <linux/soundcard.h>
35 #include <sys/ioctl.h>
38 #include "../../firmware/export/system.h"
40 #include "../../plugin.h"
42 /*#include "../codecs/lib/xxx2wav.h" */
46 int numberOfSamples IDATA_ATTR
;
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"
59 int fd
=-1; /* File descriptor where the output is written */
61 extern long tempo
; /* The sequencer keeps track of this */
64 struct plugin_api
* rb
;
70 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
77 rb
->splash(HZ
*2, "Play .MID file");
81 rb
->splash(HZ
, parameter
);
82 if(midimain(parameter
) == -1)
86 rb
->splash(HZ
*3, "FINISHED PLAYING");
87 /* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
88 return PLUGIN_USB_CONNECTED
;
91 signed char outputBuffer
[3000] IDATA_ATTR
; /* signed char.. gonna run out of iram ... ! */
94 int currentSample IDATA_ATTR
;
95 int outputBufferPosition IDATA_ATTR
;
96 int outputSampleOne IDATA_ATTR
;
97 int outputSampleTwo IDATA_ATTR
;
100 int midimain(void * filename
)
103 printf("\nHello.\n");
105 rb
->splash(HZ
/5, "LOADING MIDI");
107 struct MIDIfile
* mf
= loadFile(filename
);
109 rb
->splash(HZ
/5, "LOADING PATCHES");
110 if (initSynth(mf
, ROCKBOX_DIR
"/patchset/patchset.cfg",
111 ROCKBOX_DIR
"/patchset/drums.cfg") == -1)
117 * This lets you hear the music through the sound card if you are on Simulator
118 * Make a symlink, archos/dsp.raw and make it point to /dev/dsp or whatever
119 * your sound device is.
122 #if defined(LOCAL_DSP)
123 fd
=rb
->open("/dsp.raw", O_WRONLY
);
127 arg
= 16; /* sample size */
128 status
= ioctl(fd
, SOUND_PCM_WRITE_BITS
, &arg
);
129 status
= ioctl(fd
, SOUND_PCM_READ_BITS
, &arg
);
133 arg
= 2; /* Number of channels, 1=mono */
134 status
= ioctl(fd
, SOUND_PCM_WRITE_CHANNELS
, &arg
);
135 status
= ioctl(fd
, SOUND_PCM_READ_CHANNELS
, &arg
);
138 arg
= SAMPLE_RATE
; /* Yeah. sampling rate */
139 status
= ioctl(fd
, SOUND_PCM_WRITE_RATE
, &arg
);
140 status
= ioctl(fd
, SOUND_PCM_READ_RATE
, &arg
);
144 /* xxx2wav stuff, removed for now, will move to the real way of outputting sound soon */
146 file_info_struct file_info;
147 file_info.samplerate = SAMPLE_RATE;
148 file_info.infile = fd;
149 file_info.channels = 2;
150 file_info.bitspersample = 16;
151 local_init("/miditest.tmp", "/miditest.wav", &file_info, rb);
152 fd = file_info.outfile;
157 rb
->splash(HZ
/5, "I hope this works...");
163 * tick() will do one MIDI clock tick. Then, there's a loop here that
164 * will generate the right number of samples per MIDI tick. The whole
165 * MIDI playback is timed in terms of this value.. there are no forced
166 * delays or anything. It just produces enough samples for each tick, and
167 * the playback of these samples is what makes the timings right.
169 * This seems to work quite well.
172 printf("\nOkay, starting sequencing");
175 currentSample
=0; /* Sample counting variable */
176 outputBufferPosition
= 0;
179 bpm
=mf
->div
*1000000/tempo
;
180 numberOfSamples
=SAMPLE_RATE
/bpm
;
184 /* Tick() will return 0 if there are no more events left to play */
188 * Tempo recalculation moved to sequencer.c to be done on a tempo event only
191 for(currentSample
=0; currentSample
<numberOfSamples
; currentSample
++)
194 synthSample(&outputSampleOne
, &outputSampleTwo
);
198 * 16-bit audio because, well, it's better
199 * But really because ALSA's OSS emulation sounds extremely
200 * noisy and distorted when in 8-bit mode. I still do not know
204 outputBuffer
[outputBufferPosition
]=outputSampleOne
&0XFF; /* Low byte first */
205 outputBufferPosition
++;
206 outputBuffer
[outputBufferPosition
]=outputSampleOne
>>8; /*High byte second */
207 outputBufferPosition
++;
209 outputBuffer
[outputBufferPosition
]=outputSampleTwo
&0XFF; /* Low byte first */
210 outputBufferPosition
++;
211 outputBuffer
[outputBufferPosition
]=outputSampleTwo
>>8; /*High byte second */
212 outputBufferPosition
++;
216 * As soon as we produce 2000 bytes of sound,
217 * write it to the sound card. Why 2000? I have
218 * no idea. It's 1 AM and I am dead tired.
220 if(outputBufferPosition
>=2000)
222 rb
->write(fd
, outputBuffer
, 2000);
223 outputBufferPosition
=0;
230 #if !defined(LOCAL_DSP)
232 /* again, xxx2wav stuff, removed for now */
234 /* close_wav(&file_info); */