mirror of
https://github.com/raysan5/raylib.git
synced 2026-04-09 16:59:09 -04:00
Update audio_stream_callback.c
This commit is contained in:
@ -6,133 +6,53 @@
|
|||||||
*
|
*
|
||||||
* Example originally created with raylib 6.0, last time updated with raylib 6.0
|
* Example originally created with raylib 6.0, last time updated with raylib 6.0
|
||||||
*
|
*
|
||||||
* Example created by Dan Hoang (@dan-hoang) and reviewed by
|
* Example created by Dan Hoang (@dan-hoang) and reviewed by Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
* NOTE: Example sends a wave to the audio device,
|
||||||
|
* user gets the choice of four waves: sine, square, triangle, and sawtooth
|
||||||
|
* A stream is set up to play to the audio device; stream is hooked to a callback that
|
||||||
|
* generates a wave, that is determined by user choice
|
||||||
*
|
*
|
||||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||||
* BSD-like license that allows static linking with closed source software
|
* BSD-like license that allows static linking with closed source software
|
||||||
*
|
*
|
||||||
* Copyright (c) 2015-2026
|
* Copyright (c) 2026 Dan Hoang (@dan-hoang)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#define BUFFER_SIZE 4096
|
#define BUFFER_SIZE 4096
|
||||||
#define SAMPLE_RATE 44100
|
#define SAMPLE_RATE 44100
|
||||||
|
|
||||||
// This example sends a wave to the audio device
|
// Wave type
|
||||||
// The user gets the choice of four waves: sine, square, triangle, and sawtooth
|
typedef enum {
|
||||||
// A stream is set up to play to the audio device
|
|
||||||
// The stream is hooked to a callback that generates a wave
|
|
||||||
// The callback used is determined by user choice
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
SINE,
|
SINE,
|
||||||
SQUARE,
|
SQUARE,
|
||||||
TRIANGLE,
|
TRIANGLE,
|
||||||
SAWTOOTH
|
SAWTOOTH
|
||||||
} WaveType;
|
} WaveType;
|
||||||
|
|
||||||
int waveFrequency = 440;
|
//------------------------------------------------------------------------------------
|
||||||
int newWaveFrequency = 440;
|
// Module Functions Declaration
|
||||||
int waveIndex = 0;
|
//------------------------------------------------------------------------------------
|
||||||
|
static void SineCallback(void *framesOut, unsigned int frameCount);
|
||||||
|
static void SquareCallback(void *framesOut, unsigned int frameCount);
|
||||||
|
static void TriangleCallback(void *framesOut, unsigned int frameCount);
|
||||||
|
static void SawtoothCallback(void *framesOut, unsigned int frameCount);
|
||||||
|
|
||||||
// Buffer for keeping the last second of uploaded audio, part of which will be drawn on the screen
|
static int waveFrequency = 440;
|
||||||
float buffer[SAMPLE_RATE] = {};
|
static int newWaveFrequency = 440;
|
||||||
|
static int waveIndex = 0;
|
||||||
|
|
||||||
void SineCallback(void *framesOut, unsigned int frameCount)
|
// Buffer to keep the last second of uploaded audio,
|
||||||
{
|
// part of which will be drawn on the screen
|
||||||
int wavelength = SAMPLE_RATE/waveFrequency;
|
static float buffer[SAMPLE_RATE] = { 0 };
|
||||||
|
static AudioCallback waveCallbacks[] = { SineCallback, SquareCallback, TriangleCallback, SawtoothCallback };
|
||||||
// Synthesize the sine wave
|
static char *waveTypesAsString[] = { "sine", "square", "triangle", "sawtooth" };
|
||||||
for (int i = 0; i < frameCount; i++)
|
|
||||||
{
|
|
||||||
((float *)framesOut)[i] = sin(2*PI*waveIndex/wavelength);
|
|
||||||
|
|
||||||
waveIndex++;
|
|
||||||
|
|
||||||
if (waveIndex >= wavelength)
|
|
||||||
{
|
|
||||||
waveFrequency = newWaveFrequency;
|
|
||||||
waveIndex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the synthesized samples for later drawing
|
|
||||||
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
|
||||||
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
void SquareCallback(void *framesOut, unsigned int frameCount)
|
|
||||||
{
|
|
||||||
int wavelength = SAMPLE_RATE/waveFrequency;
|
|
||||||
|
|
||||||
// Synthesize the square wave
|
|
||||||
for (int i = 0; i < frameCount; i++)
|
|
||||||
{
|
|
||||||
((float *)framesOut)[i] = (waveIndex < wavelength/2)? 1 : -1;
|
|
||||||
waveIndex++;
|
|
||||||
|
|
||||||
if (waveIndex >= wavelength)
|
|
||||||
{
|
|
||||||
waveFrequency = newWaveFrequency;
|
|
||||||
waveIndex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the synthesized samples for later drawing
|
|
||||||
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
|
||||||
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
void TriangleCallback(void *framesOut, unsigned int frameCount)
|
|
||||||
{
|
|
||||||
int wavelength = SAMPLE_RATE/waveFrequency;
|
|
||||||
|
|
||||||
// Synthesize the triangle wave
|
|
||||||
for (int i = 0; i < frameCount; i++)
|
|
||||||
{
|
|
||||||
((float *)framesOut)[i] = (waveIndex < wavelength/2)? (-1 + 2.0f*waveIndex/(wavelength/2)) : (1 - 2.0f*(waveIndex - wavelength/2)/(wavelength/2));
|
|
||||||
waveIndex++;
|
|
||||||
|
|
||||||
if (waveIndex >= wavelength)
|
|
||||||
{
|
|
||||||
waveFrequency = newWaveFrequency;
|
|
||||||
waveIndex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the synthesized samples for later drawing
|
|
||||||
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
|
||||||
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
void SawtoothCallback(void *framesOut, unsigned int frameCount)
|
|
||||||
{
|
|
||||||
int wavelength = SAMPLE_RATE/waveFrequency;
|
|
||||||
|
|
||||||
// Synthesize the sawtooth wave
|
|
||||||
for (int i = 0; i < frameCount; i++)
|
|
||||||
{
|
|
||||||
((float *)framesOut)[i] = -1 + 2.0f*waveIndex/wavelength;
|
|
||||||
waveIndex++;
|
|
||||||
|
|
||||||
if (waveIndex >= wavelength)
|
|
||||||
{
|
|
||||||
waveFrequency = newWaveFrequency;
|
|
||||||
waveIndex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the synthesized samples for later drawing
|
|
||||||
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
|
||||||
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
AudioCallback waveCallbacks[] = { SineCallback, SquareCallback, TriangleCallback, SawtoothCallback };
|
|
||||||
char *waveTypesAsString[] = { "sine", "square", "triangle", "sawtooth" };
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Program main entry point
|
// Program main entry point
|
||||||
@ -167,7 +87,6 @@ int main(void)
|
|||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
if (IsKeyDown(KEY_UP))
|
if (IsKeyDown(KEY_UP))
|
||||||
{
|
{
|
||||||
newWaveFrequency += 10;
|
newWaveFrequency += 10;
|
||||||
@ -199,7 +118,6 @@ int main(void)
|
|||||||
|
|
||||||
SetAudioStreamCallback(stream, waveCallbacks[waveType]);
|
SetAudioStreamCallback(stream, waveCallbacks[waveType]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
@ -226,7 +144,7 @@ int main(void)
|
|||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
UnloadAudioStream(stream); // Close raw audio stream and delete buffers from RAM
|
UnloadAudioStream(stream); // Close raw audio stream and delete buffers from RAM
|
||||||
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
@ -234,3 +152,95 @@ int main(void)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definition
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
static void SineCallback(void *framesOut, unsigned int frameCount)
|
||||||
|
{
|
||||||
|
int wavelength = SAMPLE_RATE/waveFrequency;
|
||||||
|
|
||||||
|
// Synthesize the sine wave
|
||||||
|
for (int i = 0; i < frameCount; i++)
|
||||||
|
{
|
||||||
|
((float *)framesOut)[i] = sin(2*PI*waveIndex/wavelength);
|
||||||
|
|
||||||
|
waveIndex++;
|
||||||
|
|
||||||
|
if (waveIndex >= wavelength)
|
||||||
|
{
|
||||||
|
waveFrequency = newWaveFrequency;
|
||||||
|
waveIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the synthesized samples for later drawing
|
||||||
|
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
||||||
|
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SquareCallback(void *framesOut, unsigned int frameCount)
|
||||||
|
{
|
||||||
|
int wavelength = SAMPLE_RATE/waveFrequency;
|
||||||
|
|
||||||
|
// Synthesize the square wave
|
||||||
|
for (int i = 0; i < frameCount; i++)
|
||||||
|
{
|
||||||
|
((float *)framesOut)[i] = (waveIndex < wavelength/2)? 1 : -1;
|
||||||
|
waveIndex++;
|
||||||
|
|
||||||
|
if (waveIndex >= wavelength)
|
||||||
|
{
|
||||||
|
waveFrequency = newWaveFrequency;
|
||||||
|
waveIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the synthesized samples for later drawing
|
||||||
|
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
||||||
|
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TriangleCallback(void *framesOut, unsigned int frameCount)
|
||||||
|
{
|
||||||
|
int wavelength = SAMPLE_RATE/waveFrequency;
|
||||||
|
|
||||||
|
// Synthesize the triangle wave
|
||||||
|
for (int i = 0; i < frameCount; i++)
|
||||||
|
{
|
||||||
|
((float *)framesOut)[i] = (waveIndex < wavelength/2)? (-1 + 2.0f*waveIndex/(wavelength/2)) : (1 - 2.0f*(waveIndex - wavelength/2)/(wavelength/2));
|
||||||
|
waveIndex++;
|
||||||
|
|
||||||
|
if (waveIndex >= wavelength)
|
||||||
|
{
|
||||||
|
waveFrequency = newWaveFrequency;
|
||||||
|
waveIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the synthesized samples for later drawing
|
||||||
|
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
||||||
|
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SawtoothCallback(void *framesOut, unsigned int frameCount)
|
||||||
|
{
|
||||||
|
int wavelength = SAMPLE_RATE/waveFrequency;
|
||||||
|
|
||||||
|
// Synthesize the sawtooth wave
|
||||||
|
for (int i = 0; i < frameCount; i++)
|
||||||
|
{
|
||||||
|
((float *)framesOut)[i] = -1 + 2.0f*waveIndex/wavelength;
|
||||||
|
waveIndex++;
|
||||||
|
|
||||||
|
if (waveIndex >= wavelength)
|
||||||
|
{
|
||||||
|
waveFrequency = newWaveFrequency;
|
||||||
|
waveIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the synthesized samples for later drawing
|
||||||
|
for (int i = 0; i < SAMPLE_RATE - frameCount; i++) buffer[i] = buffer[i + frameCount];
|
||||||
|
for (int i = 0; i < frameCount; i++) buffer[SAMPLE_RATE - frameCount + i] = ((float *)framesOut)[i];
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user