Add audio processors to the streams system.
This commit is contained in:
parent
551db0281d
commit
0bfa877d9d
|
|
@ -14,6 +14,23 @@
|
||||||
|
|
||||||
#define AUDIO_THREAD_MUSIC_UPDATE true
|
#define AUDIO_THREAD_MUSIC_UPDATE true
|
||||||
|
|
||||||
|
// a simple lowpass filter applied to the music stream
|
||||||
|
static void audioEffectDemo(float* buffer, unsigned int nframes)
|
||||||
|
{
|
||||||
|
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 < nframes*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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
|
|
@ -32,6 +49,7 @@ int main(void)
|
||||||
|
|
||||||
float timePlayed = 0.0f;
|
float timePlayed = 0.0f;
|
||||||
bool pause = false;
|
bool pause = false;
|
||||||
|
bool hasfx = false;
|
||||||
|
|
||||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
@ -62,6 +80,20 @@ int main(void)
|
||||||
else ResumeMusicStream(music);
|
else ResumeMusicStream(music);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add/Remove effect
|
||||||
|
if (IsKeyPressed(KEY_F))
|
||||||
|
{
|
||||||
|
hasfx = !hasfx;
|
||||||
|
if (hasfx)
|
||||||
|
{
|
||||||
|
AddAudioStreamProcessor(music.stream, &audioEffectDemo);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RemoveAudioStreamProcessor(music.stream, &audioEffectDemo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get timePlayed scaled to bar dimensions (400 pixels)
|
// Get timePlayed scaled to bar dimensions (400 pixels)
|
||||||
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
|
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
|
||||||
|
|
||||||
|
|
@ -79,9 +111,11 @@ int main(void)
|
||||||
DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
|
DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
|
||||||
DrawRectangle(200, 200, (int)timePlayed, 12, MAROON);
|
DrawRectangle(200, 200, (int)timePlayed, 12, MAROON);
|
||||||
DrawRectangleLines(200, 200, 400, 12, GRAY);
|
DrawRectangleLines(200, 200, 400, 12, GRAY);
|
||||||
|
DrawRectangleLines(200, 200, 400, 12, GRAY);
|
||||||
|
|
||||||
DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
|
DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
|
||||||
DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
|
DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
|
||||||
|
DrawText("PRESS F TO ADD/REMOVE EFFECT", 208, 310, 20, LIGHTGRAY);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
|
||||||
103
src/raudio.c
103
src/raudio.c
|
|
@ -311,12 +311,22 @@ typedef enum {
|
||||||
AUDIO_BUFFER_USAGE_STREAM
|
AUDIO_BUFFER_USAGE_STREAM
|
||||||
} AudioBufferUsage;
|
} AudioBufferUsage;
|
||||||
|
|
||||||
|
|
||||||
|
// Audio processor - to apply effects to an AudioBuffer
|
||||||
|
struct rAudioProcessor {
|
||||||
|
void (*process)(float*, unsigned int);
|
||||||
|
rAudioProcessor *next;
|
||||||
|
rAudioProcessor *prev;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// Audio buffer structure
|
// Audio buffer structure
|
||||||
struct rAudioBuffer {
|
struct rAudioBuffer {
|
||||||
ma_data_converter converter; // Audio data converter
|
ma_data_converter converter; // Audio data converter
|
||||||
|
|
||||||
void (*audioCallback)(void*, unsigned int); // optional callback for filling in buffer on audio threads
|
void (*audioCallback)(void*, unsigned int, void*); // optional callback for filling in buffer on audio threads
|
||||||
void* audioCallbackData; // optional data passed to callback
|
void* audioCallbackData; // optional data passed to callback
|
||||||
|
rAudioProcessor* processor;
|
||||||
|
|
||||||
float volume; // Audio buffer volume
|
float volume; // Audio buffer volume
|
||||||
float pitch; // Audio buffer pitch
|
float pitch; // Audio buffer pitch
|
||||||
|
|
@ -349,7 +359,6 @@ typedef struct AudioData {
|
||||||
} System;
|
} System;
|
||||||
struct {
|
struct {
|
||||||
AudioBuffer *first; // Pointer to first AudioBuffer in the list
|
AudioBuffer *first; // Pointer to first AudioBuffer in the list
|
||||||
AudioBuffer *last; // Pointer to last AudioBuffer in the list
|
|
||||||
int defaultSize; // Default audio buffer size for audio streams
|
int defaultSize; // Default audio buffer size for audio streams
|
||||||
} Buffer;
|
} Buffer;
|
||||||
struct {
|
struct {
|
||||||
|
|
@ -399,7 +408,7 @@ void PlayAudioBuffer(AudioBuffer *buffer);
|
||||||
void StopAudioBuffer(AudioBuffer *buffer);
|
void StopAudioBuffer(AudioBuffer *buffer);
|
||||||
void PauseAudioBuffer(AudioBuffer *buffer);
|
void PauseAudioBuffer(AudioBuffer *buffer);
|
||||||
void ResumeAudioBuffer(AudioBuffer *buffer);
|
void ResumeAudioBuffer(AudioBuffer *buffer);
|
||||||
void SetAudioBufferCallback(AudioBuffer* buffer, int callback(void*));
|
void SetAudioBufferCallback(AudioBuffer* buffer, void callback(void*, unsigned int, void*), void*);
|
||||||
void SetAudioBufferVolume(AudioBuffer *buffer, float volume);
|
void SetAudioBufferVolume(AudioBuffer *buffer, float volume);
|
||||||
void SetAudioBufferPitch(AudioBuffer *buffer, float pitch);
|
void SetAudioBufferPitch(AudioBuffer *buffer, float pitch);
|
||||||
void TrackAudioBuffer(AudioBuffer *buffer);
|
void TrackAudioBuffer(AudioBuffer *buffer);
|
||||||
|
|
@ -532,7 +541,6 @@ void SetMasterVolume(float volume)
|
||||||
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage)
|
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage)
|
||||||
{
|
{
|
||||||
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(1, sizeof(AudioBuffer));
|
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(1, sizeof(AudioBuffer));
|
||||||
|
|
||||||
if (audioBuffer == NULL)
|
if (audioBuffer == NULL)
|
||||||
{
|
{
|
||||||
TRACELOG(LOG_WARNING, "AUDIO: Failed to allocate memory for buffer");
|
TRACELOG(LOG_WARNING, "AUDIO: Failed to allocate memory for buffer");
|
||||||
|
|
@ -559,6 +567,7 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
|
||||||
audioBuffer->pitch = 1.0f;
|
audioBuffer->pitch = 1.0f;
|
||||||
audioBuffer->audioCallback = NULL;
|
audioBuffer->audioCallback = NULL;
|
||||||
audioBuffer->audioCallbackData = NULL;
|
audioBuffer->audioCallbackData = NULL;
|
||||||
|
audioBuffer->processor = NULL;
|
||||||
audioBuffer->playing = false;
|
audioBuffer->playing = false;
|
||||||
audioBuffer->paused = false;
|
audioBuffer->paused = false;
|
||||||
audioBuffer->looping = false;
|
audioBuffer->looping = false;
|
||||||
|
|
@ -679,14 +688,10 @@ void TrackAudioBuffer(AudioBuffer *buffer)
|
||||||
{
|
{
|
||||||
ma_mutex_lock(&AUDIO.System.lock);
|
ma_mutex_lock(&AUDIO.System.lock);
|
||||||
{
|
{
|
||||||
if (AUDIO.Buffer.first == NULL) AUDIO.Buffer.first = buffer;
|
AudioBuffer* old = AUDIO.Buffer.first;
|
||||||
else
|
buffer->next = old;
|
||||||
{
|
if (old) old->prev = buffer;
|
||||||
AUDIO.Buffer.last->next = buffer;
|
AUDIO.Buffer.first = buffer;
|
||||||
buffer->prev = AUDIO.Buffer.last;
|
|
||||||
}
|
|
||||||
|
|
||||||
AUDIO.Buffer.last = buffer;
|
|
||||||
}
|
}
|
||||||
ma_mutex_unlock(&AUDIO.System.lock);
|
ma_mutex_unlock(&AUDIO.System.lock);
|
||||||
}
|
}
|
||||||
|
|
@ -696,11 +701,11 @@ void UntrackAudioBuffer(AudioBuffer *buffer)
|
||||||
{
|
{
|
||||||
ma_mutex_lock(&AUDIO.System.lock);
|
ma_mutex_lock(&AUDIO.System.lock);
|
||||||
{
|
{
|
||||||
if (buffer->prev == NULL) AUDIO.Buffer.first = buffer->next;
|
AudioBuffer* p = buffer->prev;
|
||||||
else buffer->prev->next = buffer->next;
|
AudioBuffer* n = buffer->next;
|
||||||
|
if (p) p->next = n;
|
||||||
if (buffer->next == NULL) AUDIO.Buffer.last = buffer->prev;
|
if (n) n->prev = p;
|
||||||
else buffer->next->prev = buffer->prev;
|
if (buffer == AUDIO.Buffer.first) AUDIO.Buffer.first = n;
|
||||||
|
|
||||||
buffer->prev = NULL;
|
buffer->prev = NULL;
|
||||||
buffer->next = NULL;
|
buffer->next = NULL;
|
||||||
|
|
@ -1004,7 +1009,7 @@ bool ExportWaveAsCode(Wave wave, const char *fileName)
|
||||||
// Play a sound
|
// Play a sound
|
||||||
void PlaySound(Sound sound)
|
void PlaySound(Sound sound)
|
||||||
{
|
{
|
||||||
PlayAudioBuffer(sound.stream.buffer);
|
PlayAudioStream(sound.stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play a sound in the multichannel buffer pool
|
// Play a sound in the multichannel buffer pool
|
||||||
|
|
@ -1059,6 +1064,7 @@ void PlaySoundMulti(Sound sound)
|
||||||
AUDIO.MultiChannel.pool[index]->pitch = sound.stream.buffer->pitch;
|
AUDIO.MultiChannel.pool[index]->pitch = sound.stream.buffer->pitch;
|
||||||
AUDIO.MultiChannel.pool[index]->audioCallback = sound.stream.buffer->audioCallback;
|
AUDIO.MultiChannel.pool[index]->audioCallback = sound.stream.buffer->audioCallback;
|
||||||
AUDIO.MultiChannel.pool[index]->audioCallbackData = sound.stream.buffer->audioCallbackData;
|
AUDIO.MultiChannel.pool[index]->audioCallbackData = sound.stream.buffer->audioCallbackData;
|
||||||
|
AUDIO.MultiChannel.pool[index]->processor = sound.stream.buffer->processor;
|
||||||
|
|
||||||
AUDIO.MultiChannel.pool[index]->looping = sound.stream.buffer->looping;
|
AUDIO.MultiChannel.pool[index]->looping = sound.stream.buffer->looping;
|
||||||
AUDIO.MultiChannel.pool[index]->usage = sound.stream.buffer->usage;
|
AUDIO.MultiChannel.pool[index]->usage = sound.stream.buffer->usage;
|
||||||
|
|
@ -1621,7 +1627,7 @@ static void MusicStreamCallbackWav(void* buffer, unsigned int nframes, void* dat
|
||||||
drwav* ctx = (drwav*)data;
|
drwav* ctx = (drwav*)data;
|
||||||
// NOTE: Returns the number of samples to process (not required)
|
// NOTE: Returns the number of samples to process (not required)
|
||||||
if (ctx->bitsPerSample == 16) drwav_read_pcm_frames_s16(ctx, nframes, (short*)buffer);
|
if (ctx->bitsPerSample == 16) drwav_read_pcm_frames_s16(ctx, nframes, (short*)buffer);
|
||||||
else if (ctx->bitsPerSample == 32) drwav_read_pcm_frames_f32(ctx, nframes, (short*)buffer);
|
else if (ctx->bitsPerSample == 32) drwav_read_pcm_frames_f32(ctx, nframes, (float*)buffer);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -2119,6 +2125,57 @@ void SetAudioStreamCallback(AudioStream stream, void callback(void*, unsigned in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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 AddAudioStreamProcessor(AudioStream stream, void (*process)(float*, unsigned int))
|
||||||
|
{
|
||||||
|
ma_mutex_lock(&AUDIO.System.lock); // necessary evil.
|
||||||
|
|
||||||
|
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 RemoveAudioStreamProcessor(AudioStream stream, void (*process)(float*, unsigned int))
|
||||||
|
{
|
||||||
|
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
|
// Module specific Functions Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
@ -2315,6 +2372,14 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
|
||||||
float *framesOut = (float *)pFramesOut + (framesRead*AUDIO.System.device.playback.channels);
|
float *framesOut = (float *)pFramesOut + (framesRead*AUDIO.System.device.playback.channels);
|
||||||
float *framesIn = tempBuffer;
|
float *framesIn = tempBuffer;
|
||||||
|
|
||||||
|
// apply processors chain
|
||||||
|
rAudioProcessor *processor = audioBuffer->processor;
|
||||||
|
while (processor)
|
||||||
|
{
|
||||||
|
processor->process(framesIn, framesJustRead);
|
||||||
|
processor = processor->next;
|
||||||
|
}
|
||||||
|
|
||||||
MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer->volume);
|
MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer->volume);
|
||||||
|
|
||||||
framesToRead -= framesJustRead;
|
framesToRead -= framesJustRead;
|
||||||
|
|
|
||||||
|
|
@ -88,11 +88,11 @@ typedef struct Wave {
|
||||||
} Wave;
|
} Wave;
|
||||||
|
|
||||||
typedef struct rAudioBuffer rAudioBuffer;
|
typedef struct rAudioBuffer rAudioBuffer;
|
||||||
|
typedef struct rAudioProcessor rAudioProcessor;
|
||||||
|
|
||||||
// AudioStream, custom audio stream
|
// AudioStream, custom audio stream
|
||||||
typedef struct AudioStream {
|
typedef struct AudioStream {
|
||||||
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
|
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
|
||||||
|
|
||||||
unsigned int sampleRate; // Frequency (samples per second)
|
unsigned int sampleRate; // Frequency (samples per second)
|
||||||
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
||||||
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
||||||
|
|
@ -191,7 +191,8 @@ void SetAudioStreamVolume(AudioStream stream, float volume); // Set volume fo
|
||||||
void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
|
void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
|
||||||
void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
||||||
void SetAudioStreamCallback(AudioStream stream, void callback(void*, unsigned int, void*), void* callbackData); // Audio thread callback to request new data
|
void SetAudioStreamCallback(AudioStream stream, void callback(void*, unsigned int, void*), void* callbackData); // Audio thread callback to request new data
|
||||||
|
void AddAudioStreamProcessor(AudioStream stream, void (*process)(float*, unsigned int));
|
||||||
|
void RemoveAudioStreamProcessor(AudioStream stream, void (*process)(float*, unsigned int));
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -426,11 +426,12 @@ typedef struct Wave {
|
||||||
} Wave;
|
} Wave;
|
||||||
|
|
||||||
typedef struct rAudioBuffer rAudioBuffer;
|
typedef struct rAudioBuffer rAudioBuffer;
|
||||||
|
typedef struct rAudioProcessor rAudioProcessor;
|
||||||
|
|
||||||
// AudioStream, custom audio stream
|
// AudioStream, custom audio stream
|
||||||
typedef struct AudioStream {
|
typedef struct AudioStream {
|
||||||
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
|
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
|
||||||
|
rAudioProcessor* processor;
|
||||||
unsigned int sampleRate; // Frequency (samples per second)
|
unsigned int sampleRate; // Frequency (samples per second)
|
||||||
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
||||||
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
||||||
|
|
@ -1531,6 +1532,8 @@ 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 SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
|
||||||
RLAPI void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
RLAPI void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
||||||
RLAPI void SetAudioStreamCallback(AudioStream stream, void callback(void*, unsigned int, void*), void* callbackData); // Audio thread callback to request new data
|
RLAPI void SetAudioStreamCallback(AudioStream stream, void callback(void*, unsigned int, void*), void* callbackData); // Audio thread callback to request new data
|
||||||
|
RLAPI void AddAudioStreamProcessor(AudioStream stream, void (*process)(float*, unsigned int));
|
||||||
|
RLAPI void RemoveAudioStreamProcessor(AudioStream stream, void (*process)(float*, unsigned int));
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user