Add SetAudioStreamCallback method to fill audio streams on the audio thread.

This commit is contained in:
ptarabbia 2021-12-12 19:37:09 -05:00
commit 9238d8e656

View file

@ -20,6 +20,33 @@
#define MAX_SAMPLES 512
#define MAX_SAMPLES_PER_UPDATE 4096
// Cycles per second (hz)
float frequency = 440.0f;
// Audio frequency, for smoothing
float audioFrequency = 440.0f;
// Previous value, used to test if sine needs to be rewritten, and to smoothly modulate frequency
float oldFrequency = 1.0f;
// index for audio rendering
float sineIdx = 0.0f;
void audioCallback(void* buffer, unsigned int nFrames, void* callbackData)
{
audioFrequency = frequency + (audioFrequency - frequency) * 0.95f;
audioFrequency += 1.0f;
audioFrequency -= 1.0f;
float incr = audioFrequency / 44100.0f;
short* d = (short*)buffer;
for (int i = 0; i < nFrames; i++)
{
d[i] = (short)(32000.0f * sinf(2 * PI * sineIdx));
sineIdx += incr;
if (sineIdx > 1.0f) sineIdx -= 1.0f;
}
}
int main(void)
{
// Initialization
@ -33,9 +60,11 @@ int main(void)
SetAudioStreamBufferSizeDefault(MAX_SAMPLES_PER_UPDATE);
// Init raw audio stream (sample rate: 22050, sample size: 16bit-short, channels: 1-mono)
// Init raw audio stream (sample rate: 44100, sample size: 16bit-short, channels: 1-mono)
AudioStream stream = LoadAudioStream(44100, 16, 1);
SetAudioStreamCallback(stream, &audioCallback, NULL);
// Buffer for the single cycle waveform we are synthesizing
short *data = (short *)malloc(sizeof(short)*MAX_SAMPLES);
@ -47,15 +76,6 @@ int main(void)
// Position read in to determine next frequency
Vector2 mousePosition = { -100.0f, -100.0f };
// Cycles per second (hz)
float frequency = 440.0f;
// Previous value, used to test if sine needs to be rewritten, and to smoothly modulate frequency
float oldFrequency = 1.0f;
// Cursor to read and copy the samples of the sine wave buffer
int readCursor = 0;
// Computed size in samples of the sine wave
int waveLength = 1;
@ -78,56 +98,23 @@ int main(void)
float fp = (float)(mousePosition.y);
frequency = 40.0f + (float)(fp);
}
// Rewrite the sine wave.
// Compute two cycles to allow the buffer padding, simplifying any modulation, resampling, etc.
if (frequency != oldFrequency)
{
// Compute wavelength. Limit size in both directions.
int oldWavelength = waveLength;
waveLength = (int)(22050/frequency);
if (waveLength > MAX_SAMPLES/2) waveLength = MAX_SAMPLES/2;
waveLength = (int)(22050 / frequency);
if (waveLength > MAX_SAMPLES / 2) waveLength = MAX_SAMPLES / 2;
if (waveLength < 1) waveLength = 1;
// Write sine wave.
for (int i = 0; i < waveLength*2; i++)
for (int i = 0; i < waveLength * 2; i++)
{
data[i] = (short)(sinf(((2*PI*(float)i/waveLength)))*32000);
data[i] = (short)(sinf(((2 * PI * (float)i / waveLength))) * 32000);
}
// Scale read cursor's position to minimize transition artifacts
readCursor = (int)(readCursor * ((float)waveLength / (float)oldWavelength));
oldFrequency = frequency;
}
// Refill audio stream if required
if (IsAudioStreamProcessed(stream))
{
// Synthesize a buffer that is exactly the requested size
int writeCursor = 0;
while (writeCursor < MAX_SAMPLES_PER_UPDATE)
{
// Start by trying to write the whole chunk at once
int writeLength = MAX_SAMPLES_PER_UPDATE-writeCursor;
// Limit to the maximum readable size
int readLength = waveLength-readCursor;
if (writeLength > readLength) writeLength = readLength;
// Write the slice
memcpy(writeBuf + writeCursor, data + readCursor, writeLength*sizeof(short));
// Update cursors and loop audio
readCursor = (readCursor + writeLength) % waveLength;
writeCursor += writeLength;
}
// Copy finished frame to audio stream
UpdateAudioStream(stream, writeBuf, MAX_SAMPLES_PER_UPDATE);
}
//----------------------------------------------------------------------------------
// Draw
@ -147,7 +134,6 @@ int main(void)
DrawPixelV(position, RED);
}
EndDrawing();
//----------------------------------------------------------------------------------
}