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.
This commit is contained in:
parent
90fc7c0376
commit
1612ba63ab
3 changed files with 140 additions and 0 deletions
|
@ -11,6 +11,47 @@
|
|||
|
||||
#include "raylib.h"
|
||||
|
||||
// Audio effect: lowpass filter
|
||||
static void AudioProcessEffectLPF(float *buffer, unsigned int frames)
|
||||
{
|
||||
static float low[2] = { 0.0f, 0.0f };
|
||||
static const float cutoff = 70.0f / 44100.0f; // 70 Hz lowpass filter
|
||||
const float k = cutoff / (cutoff + 0.1591549431f); // RC filter formula
|
||||
|
||||
for (unsigned int i = 0; i < frames*2; i += 2)
|
||||
{
|
||||
float l = buffer[i], r = buffer[i + 1];
|
||||
low[0] += k * (l - low[0]);
|
||||
low[1] += k * (r - low[1]);
|
||||
buffer[i] = low[0];
|
||||
buffer[i + 1] = low[1];
|
||||
}
|
||||
}
|
||||
|
||||
static float *delayBuffer = NULL;
|
||||
static unsigned int delayBufferSize = 0;
|
||||
static unsigned int delayReadIndex = 2;
|
||||
static unsigned int delayWriteIndex = 0;
|
||||
|
||||
// Audio effect: delay
|
||||
static void AudioProcessEffectDelay(float *buffer, unsigned int frames)
|
||||
{
|
||||
for (unsigned int i = 0; i < frames*2; i += 2)
|
||||
{
|
||||
float leftDelay = delayBuffer[delayReadIndex++]; // ERROR: Reading buffer -> WHY??? Maybe thread related???
|
||||
float rightDelay = delayBuffer[delayReadIndex++];
|
||||
|
||||
if (delayReadIndex == delayBufferSize) delayReadIndex = 0;
|
||||
|
||||
buffer[i] = 0.5f*buffer[i] + 0.5f*leftDelay;
|
||||
buffer[i + 1] = 0.5f*buffer[i + 1] + 0.5f*rightDelay;
|
||||
|
||||
delayBuffer[delayWriteIndex++] = buffer[i];
|
||||
delayBuffer[delayWriteIndex++] = buffer[i + 1];
|
||||
if (delayWriteIndex == delayBufferSize) delayWriteIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
|
@ -24,11 +65,17 @@ int main(void)
|
|||
|
||||
Music music = LoadMusicStream("resources/country.mp3");
|
||||
|
||||
// Allocate buffer for the delay effect
|
||||
delayBuffer = (float *)RL_CALLOC(48000*2, sizeof(float)); // 1 second delay (device sampleRate*channels)
|
||||
|
||||
PlayMusicStream(music);
|
||||
|
||||
float timePlayed = 0.0f;
|
||||
bool pause = false;
|
||||
|
||||
bool hasFilter = false;
|
||||
bool hasDelay = false;
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -55,6 +102,22 @@ int main(void)
|
|||
else ResumeMusicStream(music);
|
||||
}
|
||||
|
||||
// Add/Remove effect: lowpass filter
|
||||
if (IsKeyPressed(KEY_F))
|
||||
{
|
||||
hasFilter = !hasFilter;
|
||||
if (hasFilter) AttachAudioStreamProcessor(music.stream, AudioProcessEffectLPF);
|
||||
else DetachAudioStreamProcessor(music.stream, AudioProcessEffectLPF);
|
||||
}
|
||||
|
||||
// Add/Remove effect: delay
|
||||
if (IsKeyPressed(KEY_D))
|
||||
{
|
||||
hasDelay = !hasDelay;
|
||||
if (hasDelay) AttachAudioStreamProcessor(music.stream, AudioProcessEffectDelay);
|
||||
else DetachAudioStreamProcessor(music.stream, AudioProcessEffectDelay);
|
||||
}
|
||||
|
||||
// Get timePlayed scaled to bar dimensions (400 pixels)
|
||||
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
|
||||
|
||||
|
@ -86,6 +149,8 @@ int main(void)
|
|||
|
||||
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
||||
|
||||
RL_FREE(delayBuffer); // Free delay buffer
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
|
70
src/raudio.c
70
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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue