added EX audio callback functions
This commit is contained in:
parent
c0829bc69e
commit
4ec7f4576f
2 changed files with 144 additions and 859 deletions
203
src/raudio.c
203
src/raudio.c
|
|
@ -338,7 +338,11 @@ typedef enum {
|
|||
struct rAudioBuffer {
|
||||
ma_data_converter converter; // Audio data converter
|
||||
|
||||
AudioCallback callback; // Audio buffer callback for buffer filling on audio threads
|
||||
// NOTE: Only one of these may be set at a time.
|
||||
AudioCallback callback; // Audio buffer callback for buffer filling on audio threads
|
||||
AudioCallbackEx callback_ex; // Same as callback, but with context pointer
|
||||
void *callback_ex_ctx; // Context pointer passed to SetAudioStreamCallbackEx
|
||||
|
||||
rAudioProcessor *processor; // Audio processor
|
||||
|
||||
float volume; // Audio buffer volume
|
||||
|
|
@ -364,7 +368,12 @@ struct rAudioBuffer {
|
|||
// Audio processor struct
|
||||
// NOTE: Useful to apply effects to an AudioBuffer
|
||||
struct rAudioProcessor {
|
||||
|
||||
// NOTE: Just like rAudioBuffer, only one of these process properties may be set at a time.
|
||||
AudioCallback process; // Processor callback function
|
||||
AudioCallbackEx process_ex; // Processor callback function (with context pointer)
|
||||
void *process_ex_ctx; // Context pointer to
|
||||
|
||||
rAudioProcessor *next; // Next audio processor on the list
|
||||
rAudioProcessor *prev; // Previous audio processor on the list
|
||||
};
|
||||
|
|
@ -592,6 +601,8 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
|
|||
audioBuffer->pan = 0.0f; // Center
|
||||
|
||||
audioBuffer->callback = NULL;
|
||||
audioBuffer->callback_ex = NULL;
|
||||
audioBuffer->callback_ex_ctx = NULL;
|
||||
audioBuffer->processor = NULL;
|
||||
|
||||
audioBuffer->playing = false;
|
||||
|
|
@ -2229,6 +2240,19 @@ void SetAudioStreamBufferSizeDefault(int size)
|
|||
AUDIO.Buffer.defaultSize = size;
|
||||
}
|
||||
|
||||
// Audio thread callback to request new data (with context)
|
||||
void SetAudioStreamCallbackEx(AudioStream stream, AudioCallbackEx callback, void *context)
|
||||
{
|
||||
if (stream.buffer != NULL)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
stream.buffer->callback = NULL;
|
||||
stream.buffer->callback_ex = callback;
|
||||
stream.buffer->callback_ex_ctx = context;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
}
|
||||
|
||||
// Audio thread callback to request new data
|
||||
void SetAudioStreamCallback(AudioStream stream, AudioCallback callback)
|
||||
{
|
||||
|
|
@ -2236,10 +2260,60 @@ void SetAudioStreamCallback(AudioStream stream, AudioCallback callback)
|
|||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
stream.buffer->callback = callback;
|
||||
stream.buffer->callback_ex = NULL;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
}
|
||||
|
||||
// Appends a given processor to the end of a linked list
|
||||
// (list is double pointer such that we can manipulate the pointer itself)
|
||||
void AppendAudioProcessorToLinkedList(rAudioProcessor **list, rAudioProcessor *processor)
|
||||
{
|
||||
// if head is NULL to begin with, set it to processor.
|
||||
if (*list) {
|
||||
rAudioProcessor *node = (*list);
|
||||
while (node->next) {
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
// node is the tail of the list now
|
||||
processor->prev = node;
|
||||
node->next = processor;
|
||||
} else {
|
||||
*list = processor;
|
||||
}
|
||||
}
|
||||
|
||||
// Technically doesn't need a pointer to pointer, but uses one
|
||||
// for parity with AppendAudioProcessorToLinkedList
|
||||
void RemoveAudioProcessorFromLinkedList(rAudioProcessor **list, AudioCallback callback)
|
||||
{
|
||||
rAudioProcessor *node = *list;
|
||||
while (node) {
|
||||
if (node->process == callback) {
|
||||
if (node->prev) node->prev->next = node->next;
|
||||
if (node->next) node->next->prev = node->prev;
|
||||
RL_FREE(node);
|
||||
break;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveAudioProcessorExFromLinkedList(rAudioProcessor **list, AudioCallbackEx callback, void *context)
|
||||
{
|
||||
rAudioProcessor *node = *list;
|
||||
while (node) {
|
||||
if (node->process_ex == callback && node->process_ex_ctx == context) {
|
||||
if (node->prev) node->prev->next = node->next;
|
||||
if (node->next) node->next->prev = node->prev;
|
||||
RL_FREE(node);
|
||||
break;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -2250,18 +2324,20 @@ void AttachAudioStreamProcessor(AudioStream stream, AudioCallback process)
|
|||
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
|
||||
processor->process = process;
|
||||
|
||||
rAudioProcessor *last = stream.buffer->processor;
|
||||
AppendAudioProcessorToLinkedList(&stream.buffer->processor, processor);
|
||||
|
||||
while (last && last->next)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
if (last)
|
||||
{
|
||||
processor->prev = last;
|
||||
last->next = processor;
|
||||
}
|
||||
else stream.buffer->processor = processor;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
||||
void AttachAudioStreamProcessorEx(AudioStream stream, AudioCallbackEx process, void *context)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
|
||||
processor->process_ex = process;
|
||||
processor->process_ex_ctx = context;
|
||||
|
||||
AppendAudioProcessorToLinkedList(&stream.buffer->processor, processor);
|
||||
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
|
@ -2271,24 +2347,17 @@ void DetachAudioStreamProcessor(AudioStream stream, AudioCallback process)
|
|||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
rAudioProcessor *processor = stream.buffer->processor;
|
||||
RemoveAudioProcessorFromLinkedList(&stream.buffer->processor, process);
|
||||
|
||||
while (processor)
|
||||
{
|
||||
rAudioProcessor *next = processor->next;
|
||||
rAudioProcessor *prev = processor->prev;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
||||
if (processor->process == process)
|
||||
{
|
||||
if (stream.buffer->processor == processor) stream.buffer->processor = next;
|
||||
if (prev) prev->next = next;
|
||||
if (next) next->prev = prev;
|
||||
// Remove processor from audio stream
|
||||
void DetachAudioStreamProcessorEx(AudioStream stream, AudioCallbackEx process, void *context)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
RL_FREE(processor);
|
||||
}
|
||||
|
||||
processor = next;
|
||||
}
|
||||
RemoveAudioProcessorExFromLinkedList(&stream.buffer->processor, process, context);
|
||||
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
|
@ -2303,45 +2372,41 @@ void AttachAudioMixedProcessor(AudioCallback process)
|
|||
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
|
||||
processor->process = process;
|
||||
|
||||
rAudioProcessor *last = AUDIO.mixedProcessor;
|
||||
|
||||
while (last && last->next)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
if (last)
|
||||
{
|
||||
processor->prev = last;
|
||||
last->next = processor;
|
||||
}
|
||||
else AUDIO.mixedProcessor = processor;
|
||||
AppendAudioProcessorToLinkedList(&AUDIO.mixedProcessor, processor);
|
||||
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
||||
void AttachAudioMixedProcessorEx(AudioCallbackEx process, void *context)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
|
||||
processor->process_ex = process;
|
||||
processor->process_ex_ctx = context;
|
||||
|
||||
AppendAudioProcessorToLinkedList(&AUDIO.mixedProcessor, processor);
|
||||
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
||||
|
||||
// Remove processor from audio pipeline
|
||||
void DetachAudioMixedProcessor(AudioCallback process)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
rAudioProcessor *processor = AUDIO.mixedProcessor;
|
||||
RemoveAudioProcessorFromLinkedList(&AUDIO.mixedProcessor, process);
|
||||
|
||||
while (processor)
|
||||
{
|
||||
rAudioProcessor *next = processor->next;
|
||||
rAudioProcessor *prev = processor->prev;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
||||
if (processor->process == process)
|
||||
{
|
||||
if (AUDIO.mixedProcessor == processor) AUDIO.mixedProcessor = next;
|
||||
if (prev) prev->next = next;
|
||||
if (next) next->prev = prev;
|
||||
// Remove processor with context from audio pipeline
|
||||
void DetachAudioMixedProcessorEx(AudioCallbackEx process, void *context)
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
|
||||
RL_FREE(processor);
|
||||
}
|
||||
|
||||
processor = next;
|
||||
}
|
||||
RemoveAudioProcessorExFromLinkedList(&AUDIO.mixedProcessor, process, context);
|
||||
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
|
@ -2359,11 +2424,14 @@ static void OnLog(void *pUserData, ma_uint32 level, const char *pMessage)
|
|||
static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, void *framesOut, ma_uint32 frameCount)
|
||||
{
|
||||
// Using audio buffer callback
|
||||
if (audioBuffer->callback)
|
||||
{
|
||||
audioBuffer->callback(framesOut, frameCount);
|
||||
if (audioBuffer->callback || audioBuffer->callback_ex) {
|
||||
if (audioBuffer->callback) {
|
||||
audioBuffer->callback(framesOut, frameCount);
|
||||
} else if (audioBuffer->callback_ex) {
|
||||
audioBuffer->callback_ex(framesOut, frameCount, audioBuffer->callback_ex_ctx);
|
||||
}
|
||||
|
||||
audioBuffer->framesProcessed += frameCount;
|
||||
|
||||
return frameCount;
|
||||
}
|
||||
|
||||
|
|
@ -2539,9 +2607,12 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
|
|||
|
||||
// Apply processors chain if defined
|
||||
rAudioProcessor *processor = audioBuffer->processor;
|
||||
while (processor)
|
||||
{
|
||||
processor->process(framesIn, framesJustRead);
|
||||
while (processor) {
|
||||
if (processor->process) {
|
||||
processor->process(framesIn, framesJustRead);
|
||||
} else if (processor->process_ex) {
|
||||
processor->process_ex(framesIn, framesJustRead, processor->process_ex_ctx);
|
||||
}
|
||||
processor = processor->next;
|
||||
}
|
||||
|
||||
|
|
@ -2583,9 +2654,13 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
|
|||
}
|
||||
|
||||
rAudioProcessor *processor = AUDIO.mixedProcessor;
|
||||
while (processor)
|
||||
{
|
||||
processor->process(pFramesOut, frameCount);
|
||||
while (processor) {
|
||||
if (processor->process) {
|
||||
processor->process(pFramesOut, frameCount);
|
||||
} else if (processor->process_ex) {
|
||||
processor->process_ex(pFramesOut, frameCount, processor->process_ex_ctx);
|
||||
}
|
||||
|
||||
processor = processor->next;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -462,8 +462,6 @@ typedef struct rAudioProcessor rAudioProcessor;
|
|||
// AudioStream, custom audio stream
|
||||
typedef struct AudioStream {
|
||||
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
|
||||
rAudioProcessor *processor; // Pointer to internal data processor, useful for audio effects
|
||||
|
||||
unsigned int sampleRate; // Frequency (samples per second)
|
||||
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
||||
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
||||
|
|
@ -1640,6 +1638,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 (*AudioCallbackEx)(void *bufferData, unsigned int frames, void *context);
|
||||
|
||||
// Audio device management functions
|
||||
RLAPI void InitAudioDevice(void); // Initialize audio device and context
|
||||
|
|
@ -1712,12 +1711,17 @@ RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pit
|
|||
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 SetAudioStreamCallbackEx(AudioStream stream, AudioCallbackEx callback, void *context); // Audio thread callback to request new data (with context pointer)
|
||||
|
||||
RLAPI void AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Attach audio stream processor to stream, receives frames x 2 samples as 'float' (stereo)
|
||||
RLAPI void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processor); // Detach audio stream processor from stream
|
||||
RLAPI void AttachAudioStreamProcessorEx(AudioStream stream, AudioCallbackEx processor, void *context); // Attach audio stream processor to stream, receives frames x 2 samples as 'float' (stereo) (with context pointer)
|
||||
RLAPI void DetachAudioStreamProcessorEx(AudioStream stream, AudioCallbackEx processor, void *context); // Detach audio stream processor from stream (with context pointer)
|
||||
|
||||
RLAPI void AttachAudioMixedProcessor(AudioCallback processor); // Attach audio stream processor to the entire audio pipeline, receives frames x 2 samples as 'float' (stereo)
|
||||
RLAPI void DetachAudioMixedProcessor(AudioCallback processor); // Detach audio stream processor from the entire audio pipeline
|
||||
RLAPI void AttachAudioMixedProcessorEx(AudioCallbackEx processor, void *context); // Attach audio stream processor to the entire audio pipeline, receives frames x 2 samples as 'float' (stereo) (with context pointer)
|
||||
RLAPI void DetachAudioMixedProcessorEx(AudioCallbackEx processor, void *context); // Detach audio stream processor from the entire audio pipeline (with context pointer)
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue