feature: Introduced c-style context pointer on AudioCallback
This commit is contained in:
parent
e46b6147fc
commit
48fc229d0c
|
|
@ -21,7 +21,7 @@ static float averageVolume[400] = { 0.0f }; // Average volume history
|
|||
//------------------------------------------------------------------------------------
|
||||
// Audio processing function
|
||||
//------------------------------------------------------------------------------------
|
||||
void ProcessAudio(void *buffer, unsigned int frames)
|
||||
void ProcessAudio(void *buffer, unsigned int frames, void *context)
|
||||
{
|
||||
float *samples = (float *)buffer; // Samples internally stored as <float>s
|
||||
float average = 0.0f; // Temporary average volume
|
||||
|
|
@ -57,7 +57,7 @@ int main(void)
|
|||
|
||||
InitAudioDevice(); // Initialize audio device
|
||||
|
||||
AttachAudioMixedProcessor(ProcessAudio);
|
||||
AttachAudioMixedProcessor(ProcessAudio, 0);
|
||||
|
||||
Music music = LoadMusicStream("resources/country.mp3");
|
||||
Sound sound = LoadSound("resources/coin.wav");
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ float oldFrequency = 1.0f;
|
|||
float sineIdx = 0.0f;
|
||||
|
||||
// Audio input processing callback
|
||||
void AudioInputCallback(void *buffer, unsigned int frames)
|
||||
void AudioInputCallback(void *buffer, unsigned int frames, void *context)
|
||||
{
|
||||
audioFrequency = frequency + (audioFrequency - frequency)*0.95f;
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ int main(void)
|
|||
// Init raw audio stream (sample rate: 44100, sample size: 16bit-short, channels: 1-mono)
|
||||
AudioStream stream = LoadAudioStream(44100, 16, 1);
|
||||
|
||||
SetAudioStreamCallback(stream, AudioInputCallback);
|
||||
SetAudioStreamCallback(stream, AudioInputCallback, 0);
|
||||
|
||||
// Buffer for the single cycle waveform we are synthesizing
|
||||
short *data = (short *)malloc(sizeof(short)*MAX_SAMPLES);
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ static unsigned int delayWriteIndex = 0;
|
|||
//------------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//------------------------------------------------------------------------------------
|
||||
static void AudioProcessEffectLPF(void *buffer, unsigned int frames); // Audio effect: lowpass filter
|
||||
static void AudioProcessEffectDelay(void *buffer, unsigned int frames); // Audio effect: delay
|
||||
static void AudioProcessEffectLPF(void *buffer, unsigned int frames, void *context); // Audio effect: lowpass filter
|
||||
static void AudioProcessEffectDelay(void *buffer, unsigned int frames, void *context); // Audio effect: delay
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
|
|
@ -85,7 +85,7 @@ int main(void)
|
|||
if (IsKeyPressed(KEY_F))
|
||||
{
|
||||
enableEffectLPF = !enableEffectLPF;
|
||||
if (enableEffectLPF) AttachAudioStreamProcessor(music.stream, AudioProcessEffectLPF);
|
||||
if (enableEffectLPF) AttachAudioStreamProcessor(music.stream, AudioProcessEffectLPF, 0);
|
||||
else DetachAudioStreamProcessor(music.stream, AudioProcessEffectLPF);
|
||||
}
|
||||
|
||||
|
|
@ -93,7 +93,7 @@ int main(void)
|
|||
if (IsKeyPressed(KEY_D))
|
||||
{
|
||||
enableEffectDelay = !enableEffectDelay;
|
||||
if (enableEffectDelay) AttachAudioStreamProcessor(music.stream, AudioProcessEffectDelay);
|
||||
if (enableEffectDelay) AttachAudioStreamProcessor(music.stream, AudioProcessEffectDelay, 0);
|
||||
else DetachAudioStreamProcessor(music.stream, AudioProcessEffectDelay);
|
||||
}
|
||||
|
||||
|
|
@ -143,7 +143,7 @@ int main(void)
|
|||
// Module Functions Definition
|
||||
//------------------------------------------------------------------------------------
|
||||
// Audio effect: lowpass filter
|
||||
static void AudioProcessEffectLPF(void *buffer, unsigned int frames)
|
||||
static void AudioProcessEffectLPF(void *buffer, unsigned int frames, void *context)
|
||||
{
|
||||
static float low[2] = { 0.0f, 0.0f };
|
||||
static const float cutoff = 70.0f / 44100.0f; // 70 Hz lowpass filter
|
||||
|
|
@ -164,7 +164,7 @@ static void AudioProcessEffectLPF(void *buffer, unsigned int frames)
|
|||
}
|
||||
|
||||
// Audio effect: delay
|
||||
static void AudioProcessEffectDelay(void *buffer, unsigned int frames)
|
||||
static void AudioProcessEffectDelay(void *buffer, unsigned int frames, void *context)
|
||||
{
|
||||
for (unsigned int i = 0; i < frames*2; i += 2)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -694,11 +694,10 @@ RLAPI void SetAudioStreamVolume(AudioStream stream, float volume); // Set vol
|
|||
RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
|
||||
RLAPI void SetAudioStreamPan(AudioStream stream, float pan); // Set pan for audio stream (0.5 is centered)
|
||||
RLAPI void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
||||
RLAPI void SetAudioStreamCallback(AudioStream stream, AudioCallback callback); // Audio thread callback to request new data
|
||||
RLAPI void SetAudioStreamCallback(AudioStream stream, AudioCallback callback, void *context); // Audio thread callback to request new data
|
||||
|
||||
RLAPI void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Attach audio stream processor to stream, receives the samples as <float>s
|
||||
RLAPI void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor, void *context); // Attach audio stream processor to stream, receives the samples as <float>s
|
||||
RLAPI void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Detach audio stream processor from stream
|
||||
|
||||
RLAPI void AttachAudioMixedProcessor(AudioCallback processor); // Attach audio stream processor to the entire audio pipeline, receives the samples as <float>s
|
||||
RLAPI void AttachAudioMixedProcessor(AudioCallback processor, void *context); // Attach audio stream processor to the entire audio pipeline, receives the samples as <float>s
|
||||
RLAPI void DetachAudioMixedProcessor(AudioCallback processor); // Detach audio stream processor from the entire audio pipeline
|
||||
|
||||
|
|
|
|||
23
src/raudio.c
23
src/raudio.c
|
|
@ -343,6 +343,7 @@ struct rAudioBuffer {
|
|||
ma_data_converter converter; // Audio data converter
|
||||
|
||||
AudioCallback callback; // Audio buffer callback for buffer filling on audio threads
|
||||
void *callbackContext; // Audio buffer callback context to be passed on callback
|
||||
rAudioProcessor *processor; // Audio processor
|
||||
|
||||
float volume; // Audio buffer volume
|
||||
|
|
@ -369,6 +370,7 @@ struct rAudioBuffer {
|
|||
// NOTE: Useful to apply effects to an AudioBuffer
|
||||
struct rAudioProcessor {
|
||||
AudioCallback process; // Processor callback function
|
||||
void *context; // Processor callback context
|
||||
rAudioProcessor *next; // Next audio processor on the list
|
||||
rAudioProcessor *prev; // Previous audio processor on the list
|
||||
};
|
||||
|
|
@ -584,6 +586,7 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
|
|||
audioBuffer->pan = 0.5f;
|
||||
|
||||
audioBuffer->callback = NULL;
|
||||
audioBuffer->callbackContext = NULL;
|
||||
audioBuffer->processor = NULL;
|
||||
|
||||
audioBuffer->playing = false;
|
||||
|
|
@ -2209,20 +2212,25 @@ void SetAudioStreamBufferSizeDefault(int size)
|
|||
}
|
||||
|
||||
// Audio thread callback to request new data
|
||||
void SetAudioStreamCallback(AudioStream stream, AudioCallback callback)
|
||||
void SetAudioStreamCallback(AudioStream stream, AudioCallback callback, void *context)
|
||||
{
|
||||
if (stream.buffer != NULL) stream.buffer->callback = callback;
|
||||
if (stream.buffer != NULL)
|
||||
{
|
||||
stream.buffer->callback = callback;
|
||||
stream.buffer->callbackContext = context;
|
||||
}
|
||||
}
|
||||
|
||||
// Add processor to audio stream. Contrary to buffers, the order of processors is important.
|
||||
// The new processor must be added at the end. As there aren't supposed to be a lot of processors attached to
|
||||
// a given stream, we iterate through the list to find the end. That way we don't need a pointer to the last element.
|
||||
void AttachAudioStreamProcessor(AudioStream stream, AudioCallback process)
|
||||
void AttachAudioStreamProcessor(AudioStream stream, AudioCallback process, void *context)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
|
||||
processor->process = process;
|
||||
processor->context = context;
|
||||
|
||||
rAudioProcessor *last = stream.buffer->processor;
|
||||
|
||||
|
|
@ -2270,12 +2278,13 @@ void DetachAudioStreamProcessor(AudioStream stream, AudioCallback process)
|
|||
// Add processor to audio pipeline. Order of processors is important
|
||||
// Works the same way as {Attach,Detach}AudioStreamProcessor() functions, except
|
||||
// these two work on the already mixed output just before sending it to the sound hardware
|
||||
void AttachAudioMixedProcessor(AudioCallback process)
|
||||
void AttachAudioMixedProcessor(AudioCallback process, void *context)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
|
||||
processor->process = process;
|
||||
processor->context = context;
|
||||
|
||||
rAudioProcessor *last = AUDIO.mixedProcessor;
|
||||
|
||||
|
|
@ -2337,7 +2346,7 @@ static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer,
|
|||
// Using audio buffer callback
|
||||
if (audioBuffer->callback)
|
||||
{
|
||||
audioBuffer->callback(framesOut, frameCount);
|
||||
audioBuffer->callback(framesOut, frameCount, audioBuffer->callbackContext);
|
||||
audioBuffer->framesProcessed += frameCount;
|
||||
|
||||
return frameCount;
|
||||
|
|
@ -2520,7 +2529,7 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
|
|||
rAudioProcessor *processor = audioBuffer->processor;
|
||||
while (processor)
|
||||
{
|
||||
processor->process(framesIn, framesJustRead);
|
||||
processor->process(framesIn, framesJustRead, processor->context);
|
||||
processor = processor->next;
|
||||
}
|
||||
|
||||
|
|
@ -2564,7 +2573,7 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
|
|||
rAudioProcessor *processor = AUDIO.mixedProcessor;
|
||||
while (processor)
|
||||
{
|
||||
processor->process(pFramesOut, frameCount);
|
||||
processor->process(pFramesOut, frameCount, processor->context);
|
||||
processor = processor->next;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1585,7 +1585,7 @@ RLAPI RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3
|
|||
//------------------------------------------------------------------------------------
|
||||
// Audio Loading and Playing Functions (Module: audio)
|
||||
//------------------------------------------------------------------------------------
|
||||
typedef void (*AudioCallback)(void *bufferData, unsigned int frames);
|
||||
typedef void (*AudioCallback)(void *bufferData, unsigned int frames, void *context);
|
||||
|
||||
// Audio device management functions
|
||||
RLAPI void InitAudioDevice(void); // Initialize audio device and context
|
||||
|
|
@ -1657,12 +1657,12 @@ RLAPI void SetAudioStreamVolume(AudioStream stream, float volume); // Set vol
|
|||
RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
|
||||
RLAPI void SetAudioStreamPan(AudioStream stream, float pan); // Set pan for audio stream (0.5 is centered)
|
||||
RLAPI void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
||||
RLAPI void SetAudioStreamCallback(AudioStream stream, AudioCallback callback); // Audio thread callback to request new data
|
||||
RLAPI void SetAudioStreamCallback(AudioStream stream, AudioCallback callback, void* context); // Audio thread callback to request new data
|
||||
|
||||
RLAPI void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Attach audio stream processor to stream, receives the samples as <float>s
|
||||
RLAPI void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor, void* context); // Attach audio stream processor to stream, receives the samples as <float>s
|
||||
RLAPI void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Detach audio stream processor from stream
|
||||
|
||||
RLAPI void AttachAudioMixedProcessor(AudioCallback processor); // Attach audio stream processor to the entire audio pipeline, receives the samples as <float>s
|
||||
RLAPI void AttachAudioMixedProcessor(AudioCallback processor, void* context); // Attach audio stream processor to the entire audio pipeline, receives the samples as <float>s
|
||||
RLAPI void DetachAudioMixedProcessor(AudioCallback processor); // Detach audio stream processor from the entire audio pipeline
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user