diff options
| author | Ray <[email protected]> | 2022-03-24 20:49:11 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2022-03-24 20:49:11 +0100 |
| commit | 381236051f696f0c2818b1fff1c119302ae99b27 (patch) | |
| tree | 578e79e7dc9138928c91a73e0b945abac38982f1 /examples/audio/audio_raw_stream.c | |
| parent | bcd84cd36d8777d6fa4dff6dfe08bbb8839ea2be (diff) | |
| download | raylib-381236051f696f0c2818b1fff1c119302ae99b27.tar.gz raylib-381236051f696f0c2818b1fff1c119302ae99b27.zip | |
ADDED: Audio stream input callback #2212 -WIP-
WARNING: This addition is based on a PR and it's still under review, not sure if it will be maintained in the future. In general, raylib tries to avoid callbacks usage mechanisms.
Diffstat (limited to 'examples/audio/audio_raw_stream.c')
| -rw-r--r-- | examples/audio/audio_raw_stream.c | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/examples/audio/audio_raw_stream.c b/examples/audio/audio_raw_stream.c index 9b2d1022..ddf4337e 100644 --- a/examples/audio/audio_raw_stream.c +++ b/examples/audio/audio_raw_stream.c @@ -20,6 +20,35 @@ #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 frames) +{ + 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 < frames; 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 +62,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); + // Buffer for the single cycle waveform we are synthesizing short *data = (short *)malloc(sizeof(short)*MAX_SAMPLES); @@ -47,6 +78,7 @@ int main(void) // Position read in to determine next frequency Vector2 mousePosition = { -100.0f, -100.0f }; + /* // Cycles per second (hz) float frequency = 440.0f; @@ -55,6 +87,7 @@ int main(void) // 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; @@ -82,7 +115,7 @@ int main(void) SetAudioStreamPan(stream, pan); } - // Rewrite the sine wave. + // Rewrite the sine wave // Compute two cycles to allow the buffer padding, simplifying any modulation, resampling, etc. if (frequency != oldFrequency) { @@ -92,17 +125,18 @@ int main(void) if (waveLength > MAX_SAMPLES/2) waveLength = MAX_SAMPLES/2; if (waveLength < 1) waveLength = 1; - // Write sine wave. + // Write sine wave for (int i = 0; i < waveLength*2; i++) { 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)); + //readCursor = (int)(readCursor * ((float)waveLength / (float)oldWavelength)); oldFrequency = frequency; } + /* // Refill audio stream if required if (IsAudioStreamProcessed(stream)) { @@ -131,6 +165,7 @@ int main(void) // Copy finished frame to audio stream UpdateAudioStream(stream, writeBuf, MAX_SAMPLES_PER_UPDATE); } + */ //---------------------------------------------------------------------------------- // Draw |
