1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2005 Stepan Moskovchenko
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #define SAMPLE_RATE 22050
22 #define MAX_VOICES 100
25 /* Only define LOCAL_DSP on Simulator or else we're asking for trouble */
26 #if defined(SIMULATOR)
27 /*Enable this to write to the soundcard via a /dsv/dsp symlink in */
28 /*#define LOCAL_DSP */
32 #if defined(LOCAL_DSP)
33 /* This is for writing to the DSP directly from the Simulator */
36 #include <linux/soundcard.h>
37 #include <sys/ioctl.h>
40 #include "../../firmware/export/system.h"
42 #include "../../plugin.h"
44 /*#include "../codecs/lib/xxx2wav.h" */
48 int numberOfSamples IDATA_ATTR
;
51 #include "midi/midiutil.c"
52 #include "midi/guspat.h"
53 #include "midi/guspat.c"
54 #include "midi/sequencer.c"
55 #include "midi/midifile.c"
56 #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 */
63 const struct plugin_api
* rb
;
66 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
73 rb
->splash(HZ
*2, "Play .MID file");
77 rb
->splash(HZ
, parameter
);
78 if(midimain(parameter
) == -1)
82 rb
->splash(HZ
*3, "FINISHED PLAYING");
83 /* Return PLUGIN_USB_CONNECTED to force a file-tree refresh */
84 return PLUGIN_USB_CONNECTED
;
87 signed char outputBuffer
[3000] IDATA_ATTR
; /* signed char.. gonna run out of iram ... ! */
90 int currentSample IDATA_ATTR
;
91 int outputBufferPosition IDATA_ATTR
;
92 int outputSampleOne IDATA_ATTR
;
93 int outputSampleTwo IDATA_ATTR
;
96 int midimain(void * filename
)
101 rb
->splash(HZ
/5, "LOADING MIDI");
103 struct MIDIfile
* mf
= loadFile(filename
);
105 rb
->splash(HZ
/5, "LOADING PATCHES");
106 if (initSynth(mf
, ROCKBOX_DIR
"/patchset/patchset.cfg",
107 ROCKBOX_DIR
"/patchset/drums.cfg") == -1)
113 * This lets you hear the music through the sound card if you are on Simulator
114 * Make a symlink, archos/dsp.raw and make it point to /dev/dsp or whatever
115 * your sound device is.
118 #if defined(LOCAL_DSP)
119 fd
=rb
->open("/dsp.raw", O_WRONLY
);
123 arg
= 16; /* sample size */
124 status
= ioctl(fd
, SOUND_PCM_WRITE_BITS
, &arg
);
125 status
= ioctl(fd
, SOUND_PCM_READ_BITS
, &arg
);
129 arg
= 2; /* Number of channels, 1=mono */
130 status
= ioctl(fd
, SOUND_PCM_WRITE_CHANNELS
, &arg
);
131 status
= ioctl(fd
, SOUND_PCM_READ_CHANNELS
, &arg
);
134 arg
= SAMPLE_RATE
; /* Yeah. sampling rate */
135 status
= ioctl(fd
, SOUND_PCM_WRITE_RATE
, &arg
);
136 status
= ioctl(fd
, SOUND_PCM_READ_RATE
, &arg
);
140 /* xxx2wav stuff, removed for now, will move to the real way of outputting sound soon */
142 file_info_struct file_info;
143 file_info.samplerate = SAMPLE_RATE;
144 file_info.infile = fd;
145 file_info.channels = 2;
146 file_info.bitspersample = 16;
147 local_init("/miditest.tmp", "/miditest.wav", &file_info, rb);
148 fd = file_info.outfile;
153 rb
->splash(HZ
/5, "I hope this works...");
159 * tick() will do one MIDI clock tick. Then, there's a loop here that
160 * will generate the right number of samples per MIDI tick. The whole
161 * MIDI playback is timed in terms of this value.. there are no forced
162 * delays or anything. It just produces enough samples for each tick, and
163 * the playback of these samples is what makes the timings right.
165 * This seems to work quite well.
168 printf("\nOkay, starting sequencing");
171 currentSample
=0; /* Sample counting variable */
172 outputBufferPosition
= 0;
175 bpm
=mf
->div
*1000000/tempo
;
176 numberOfSamples
=SAMPLE_RATE
/bpm
;
180 /* Tick() will return 0 if there are no more events left to play */
184 * Tempo recalculation moved to sequencer.c to be done on a tempo event only
187 for(currentSample
=0; currentSample
<numberOfSamples
; currentSample
++)
190 synthSample(&outputSampleOne
, &outputSampleTwo
);
194 * 16-bit audio because, well, it's better
195 * But really because ALSA's OSS emulation sounds extremely
196 * noisy and distorted when in 8-bit mode. I still do not know
200 outputBuffer
[outputBufferPosition
]=outputSampleOne
&0XFF; /* Low byte first */
201 outputBufferPosition
++;
202 outputBuffer
[outputBufferPosition
]=outputSampleOne
>>8; /*High byte second */
203 outputBufferPosition
++;
205 outputBuffer
[outputBufferPosition
]=outputSampleTwo
&0XFF; /* Low byte first */
206 outputBufferPosition
++;
207 outputBuffer
[outputBufferPosition
]=outputSampleTwo
>>8; /*High byte second */
208 outputBufferPosition
++;
212 * As soon as we produce 2000 bytes of sound,
213 * write it to the sound card. Why 2000? I have
214 * no idea. It's 1 AM and I am dead tired.
216 if(outputBufferPosition
>=2000)
218 rb
->write(fd
, outputBuffer
, 2000);
219 outputBufferPosition
=0;
226 #if !defined(LOCAL_DSP)
228 /* again, xxx2wav stuff, removed for now */
230 /* close_wav(&file_info); */