diff --git a/src/AudioGeneratorMOD.cpp b/src/AudioGeneratorMOD.cpp index f42154f..b47b283 100644 --- a/src/AudioGeneratorMOD.cpp +++ b/src/AudioGeneratorMOD.cpp @@ -124,7 +124,7 @@ bool AudioGeneratorMOD::begin(AudioFileSource *source, AudioOutput *out) UpdateAmiga(); for (int i = 0; i < CHANNELS; i++) { - FatBuffer.channels[i] = reinterpret_cast(malloc(fatBufferSize)); + FatBuffer.channels[i] = reinterpret_cast(calloc(fatBufferSize, 1)); if (!FatBuffer.channels[i]) { stop(); return false; @@ -565,7 +565,7 @@ bool AudioGeneratorMOD::ProcessRow() Mixer.channelFrequency[channel] = Player.amiga / Player.lastAmigaPeriod[channel]; if (note != NONOTE) - Mixer.channelSampleOffset[channel] = sampleOffset << DIVIDER; + Mixer.channelSampleOffset[channel] = sampleOffset << FIXED_DIVIDER; if (sampleNumber) Mixer.channelSampleNumber[channel] = Player.lastSampleNumber[channel]; @@ -757,12 +757,12 @@ void AudioGeneratorMOD::GetSample(int16_t sample[2]) if (!Mixer.channelVolume[channel]) continue; samplePointer = Mixer.sampleBegin[Mixer.channelSampleNumber[channel]] + - (Mixer.channelSampleOffset[channel] >> DIVIDER); + (Mixer.channelSampleOffset[channel] >> FIXED_DIVIDER); if (Mixer.sampleLoopLength[Mixer.channelSampleNumber[channel]]) { if (samplePointer >= Mixer.sampleLoopEnd[Mixer.channelSampleNumber[channel]]) { - Mixer.channelSampleOffset[channel] -= Mixer.sampleLoopLength[Mixer.channelSampleNumber[channel]] << DIVIDER; + Mixer.channelSampleOffset[channel] -= Mixer.sampleLoopLength[Mixer.channelSampleNumber[channel]] << FIXED_DIVIDER; samplePointer -= Mixer.sampleLoopLength[Mixer.channelSampleNumber[channel]]; } @@ -801,7 +801,7 @@ void AudioGeneratorMOD::GetSample(int16_t sample[2]) out = current; // Integer linear interpolation - out += (next - current) * (Mixer.channelSampleOffset[channel] & ((1 << DIVIDER) - 1)) >> DIVIDER; + out += (next - current) * (Mixer.channelSampleOffset[channel] & ((1 << FIXED_DIVIDER) - 1)) >> FIXED_DIVIDER; // Upscale to BITDEPTH out <<= BITDEPTH - 8; diff --git a/src/AudioGeneratorMOD.h b/src/AudioGeneratorMOD.h index 87fac18..0600377 100644 --- a/src/AudioGeneratorMOD.h +++ b/src/AudioGeneratorMOD.h @@ -56,14 +56,14 @@ class AudioGeneratorMOD : public AudioGenerator enum {BITDEPTH = 15}; int sampleRate; int fatBufferSize; //(6*1024) // File system buffers per-CHANNEL (i.e. total mem required is 4 * FATBUFFERSIZE) - enum {DIVIDER = 10}; // Fixed-point mantissa used for integer arithmetic + enum {FIXED_DIVIDER = 10}; // Fixed-point mantissa used for integer arithmetic int stereoSeparation; //STEREOSEPARATION = 32; // 0 (max) to 64 (mono) bool usePAL; // Hz = 7093789 / (amigaPeriod * 2) for PAL // Hz = 7159091 / (amigaPeriod * 2) for NTSC int AMIGA; - void UpdateAmiga() { AMIGA = ((usePAL?7159091:7093789) / 2 / sampleRate << DIVIDER); } + void UpdateAmiga() { AMIGA = ((usePAL?7159091:7093789) / 2 / sampleRate << FIXED_DIVIDER); } enum {ROWS = 64, SAMPLES = 31, CHANNELS = 4, NONOTE = 0xFFFF, NONOTE8 = 0xff }; diff --git a/src/AudioOutputSTDIO.cpp b/src/AudioOutputSTDIO.cpp index 33bbd34..f9032bb 100644 --- a/src/AudioOutputSTDIO.cpp +++ b/src/AudioOutputSTDIO.cpp @@ -54,6 +54,11 @@ bool AudioOutputSTDIO::begin() bool AudioOutputSTDIO::ConsumeSample(int16_t sample[2]) { + static int avail = 100; + if (!(--avail)) { + avail = 100; + return false; + } for (int i=0; i +#include "AudioFileSourcePROGMEM.h" +#include "AudioOutputSTDIO.h" +#include "AudioGeneratorMOD.h" + +#include "../../examples/PlayMODFromPROGMEMToDAC/enigma.h" + +int main(int argc, char **argv) +{ + (void) argc; + (void) argv; + + AudioFileSourcePROGMEM *file = new AudioFileSourcePROGMEM(enigma_mod, sizeof(enigma_mod)); + AudioOutputSTDIO *out = new AudioOutputSTDIO(); + out->SetFilename("mod.wav"); + AudioGeneratorMOD *mod = new AudioGeneratorMOD(); + + mod->begin(file, out); + // The MOD plays forever, so only run for ~30 seconds worth + for (int i=0; i<10000; i++) mod->loop(); + mod->stop(); + + delete out; + delete mod; + delete file; +}