summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRay <[email protected]>2022-03-30 20:13:02 +0200
committerRay <[email protected]>2022-03-30 20:13:02 +0200
commit1612ba63ab66b2f875fe6aca70e5fabea6b578ca (patch)
treee9aad3e8d84c5f53dff0610de7bd7c729b89ef23 /src
parent90fc7c0376f39d7b19d632e1d69dec715ec3f2a8 (diff)
downloadraylib-1612ba63ab66b2f875fe6aca70e5fabea6b578ca.tar.gz
raylib-1612ba63ab66b2f875fe6aca70e5fabea6b578ca.zip
ADDED: Audio stream processors support -WIP- #2212
This feature is still under consideration/testing and it doesn't work properly, at least the Delay Effect processor.
Diffstat (limited to 'src')
-rw-r--r--src/raudio.c70
-rw-r--r--src/raylib.h5
2 files changed, 75 insertions, 0 deletions
diff --git a/src/raudio.c b/src/raudio.c
index f25de079..9b83afd7 100644
--- a/src/raudio.c
+++ b/src/raudio.c
@@ -316,6 +316,7 @@ struct rAudioBuffer {
ma_data_converter converter; // Audio data converter
AudioCallback callback; // Audio buffer callback for buffer filling on audio threads
+ rAudioProcessor *processor; // Audio processor
float volume; // Audio buffer volume
float pitch; // Audio buffer pitch
@@ -337,6 +338,14 @@ struct rAudioBuffer {
rAudioBuffer *prev; // Previous audio buffer on the list
};
+// Audio processor struct
+// NOTE: Useful to apply effects to an AudioBuffer
+struct rAudioProcessor {
+ AudioCallback process; // Processor callback function
+ rAudioProcessor *next; // Next audio processor on the list
+ rAudioProcessor *prev; // Previous audio processor on the list
+};
+
#define AudioBuffer rAudioBuffer // HACK: To avoid CoreAudio (macOS) symbol collision
// Audio data context
@@ -559,6 +568,7 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
audioBuffer->pan = 0.5f;
audioBuffer->callback = NULL;
+ audioBuffer->processor = NULL;
audioBuffer->playing = false;
audioBuffer->paused = false;
@@ -2039,6 +2049,58 @@ void SetAudioStreamCallback(AudioStream stream, AudioCallback callback)
if (stream.buffer != NULL) stream.buffer->callback = callback;
}
+// 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)
+{
+ ma_mutex_lock(&AUDIO.System.lock);
+
+ rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor));
+ processor->process = process;
+
+ rAudioProcessor *last = stream.buffer->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 DetachAudioStreamProcessor(AudioStream stream, AudioCallback process)
+{
+ ma_mutex_lock(&AUDIO.System.lock);
+
+ rAudioProcessor *processor = stream.buffer->processor;
+
+ while (processor)
+ {
+ rAudioProcessor *next = processor->next;
+ rAudioProcessor *prev = processor->prev;
+
+ if (processor->process == process)
+ {
+ if (stream.buffer->processor == processor) stream.buffer->processor = next;
+ if (prev) prev->next = next;
+ if (next) next->prev = prev;
+
+ RL_FREE(processor);
+ }
+
+ processor = next;
+ }
+
+ ma_mutex_unlock(&AUDIO.System.lock);
+}
+
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
@@ -2235,6 +2297,14 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
float *framesOut = (float *)pFramesOut + (framesRead*AUDIO.System.device.playback.channels);
float *framesIn = tempBuffer;
+ // Apply processors chain if defined
+ rAudioProcessor *processor = audioBuffer->processor;
+ while (processor)
+ {
+ processor->process(framesIn, framesJustRead);
+ processor = processor->next;
+ }
+
MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer);
framesToRead -= framesJustRead;
diff --git a/src/raylib.h b/src/raylib.h
index 1add734b..7cb19927 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -428,10 +428,12 @@ typedef struct Wave {
// Opaque structs declaration
// NOTE: Actual structs are defined internally in raudio module
typedef struct rAudioBuffer rAudioBuffer;
+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)
@@ -1543,6 +1545,9 @@ RLAPI void SetAudioStreamPan(AudioStream stream, float pan); // Set pan
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 AttachAudioStreamProcessor(AudioStream stream, AudioCallback processor);
+RLAPI void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processor);
+
#if defined(__cplusplus)
}
#endif