static void SDLAudioCallback(void * /*userdata*/, Uint8 *stream, int len) { if (playing_sample) { if (cursnd.sfxr) { uint l = len/2; float *fbuf = new float[l]; memset(fbuf, 0, sizeof(float)*l); SynthSample(l, fbuf, nullptr); while (l--) { float f = fbuf[l]; if (f < -1.0) f = -1.0; if (f > 1.0) f = 1.0; ((Sint16*)stream)[l] = (Sint16)(f * 32767); } delete[] fbuf; } else { size_t amount = min((size_t)len, cursnd.len - (cursndpos - cursnd.buf)); memcpy(stream, cursndpos, amount); memset(stream + amount, 0, len - amount); cursndpos += amount; if (cursndpos == cursnd.buf + cursnd.len) playing_sample = false; } } else { memset(stream, 0, len); SDL_PauseAudioDevice(audioid, 1); } }
bool ExportWAV(char* filename) { FILE* foutput=fopen(filename, "wb"); if(!foutput) return false; // write wav header char string[32]; unsigned int dword=0; unsigned short word=0; fwrite("RIFF", 4, 1, foutput); // "RIFF" dword=0; fwrite(&dword, 1, 4, foutput); // remaining file size fwrite("WAVE", 4, 1, foutput); // "WAVE" fwrite("fmt ", 4, 1, foutput); // "fmt " dword=16; fwrite(&dword, 1, 4, foutput); // chunk size word=1; fwrite(&word, 1, 2, foutput); // compression code word=1; fwrite(&word, 1, 2, foutput); // channels dword=wav_freq; fwrite(&dword, 1, 4, foutput); // sample rate dword=wav_freq*wav_bits/8; fwrite(&dword, 1, 4, foutput); // bytes/sec word=wav_bits/8; fwrite(&word, 1, 2, foutput); // block align word=wav_bits; fwrite(&word, 1, 2, foutput); // bits per sample fwrite("data", 4, 1, foutput); // "data" dword=0; int foutstream_datasize=ftell(foutput); fwrite(&dword, 1, 4, foutput); // chunk size // write sample data mute_stream=true; file_sampleswritten=0; filesample=0.0f; fileacc=0; PlaySample(); while(playing_sample) SynthSample(256, NULL, foutput); mute_stream=false; // seek back to header and write size info fseek(foutput, 4, SEEK_SET); dword=0; dword=foutstream_datasize-4+file_sampleswritten*wav_bits/8; fwrite(&dword, 1, 4, foutput); // remaining file size fseek(foutput, foutstream_datasize, SEEK_SET); dword=file_sampleswritten*wav_bits/8; fwrite(&dword, 1, 4, foutput); // chunk size (data) fclose(foutput); return true; }
//ancient portaudio stuff static int AudioCallback(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData) { float *out=(float*)outputBuffer; float *in=(float*)inputBuffer; (void)outTime; if(playing_sample && !mute_stream) SynthSample(framesPerBuffer, out, NULL); else for(int i=0;i<framesPerBuffer;i++) *out++=0.0f; return 0; }
//lets use SDL in stead static void SDLAudioCallback(void *userdata, Uint8 *stream, int len) { if (playing_sample && !mute_stream) { unsigned int l = len/2; float fbuf[l]; memset(fbuf, 0, sizeof(fbuf)); SynthSample(l, fbuf, NULL); while (l--) { float f = fbuf[l]; if (f < -1.0) f = -1.0; if (f > 1.0) f = 1.0; ((Sint16*)stream)[l] = (Sint16)(f * 32767); } } else memset(stream, 0, len); }
// This is a replacement for the original sfxr callback function. bool sfxr::operator ()(unsigned char* sampleBuffer, int byteCount) { if (playing_sample && !mute_stream) { unsigned int l = byteCount/2; float fbuf[l]; memset(fbuf, 0, sizeof(fbuf)); SynthSample(l, fbuf, NULL); while (l--) { float f = fbuf[l]; if (f < -1.0) f = -1.0; if (f > 1.0) f = 1.0; ((sint16*)sampleBuffer)[l] = (sint16)(f * 32767); } return true; } memset(sampleBuffer, 0, byteCount); return false; }
//lets use SDL in stead void generator::GenerateAudio(int16_t * bufferInterlace, int32_t nbSample, int32_t nbChannels) { if (playing_sample) { float fbuf[nbSample]; memset(fbuf, 0, sizeof(float)); SynthSample(nbSample, fbuf, NULL); while (nbSample--) { float f = fbuf[nbSample]; if (f < -1.0) { f = -1.0; } if (f > 1.0) { f = 1.0; } for (int32_t iii=0; iii<nbChannels; iii++) { bufferInterlace[nbSample*nbChannels+iii] = (int16_t)(f * 32767); } } } }