ADDED: Audio stream input callback #2212 -WIP-
WARNING: This addition is based on a PR and it's still under review, not sure if it will be maintained in the future. In general, raylib tries to avoid callbacks usage mechanisms.
This commit is contained in:
parent
bcd84cd36d
commit
381236051f
3 changed files with 64 additions and 5 deletions
22
src/raudio.c
22
src/raudio.c
|
@ -311,9 +311,11 @@ typedef enum {
|
|||
AUDIO_BUFFER_USAGE_STREAM
|
||||
} AudioBufferUsage;
|
||||
|
||||
// Audio buffer structure
|
||||
// Audio buffer struct
|
||||
struct rAudioBuffer {
|
||||
ma_data_converter converter; // Audio data converter
|
||||
|
||||
AudioStreamCallback callback; // Audio buffer callback for buffer filling on audio threads
|
||||
|
||||
float volume; // Audio buffer volume
|
||||
float pitch; // Audio buffer pitch
|
||||
|
@ -555,10 +557,13 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
|
|||
audioBuffer->volume = 1.0f;
|
||||
audioBuffer->pitch = 1.0f;
|
||||
audioBuffer->pan = 0.5f;
|
||||
|
||||
audioBuffer->callback = NULL;
|
||||
|
||||
audioBuffer->playing = false;
|
||||
audioBuffer->paused = false;
|
||||
audioBuffer->looping = false;
|
||||
|
||||
audioBuffer->usage = usage;
|
||||
audioBuffer->frameCursorPos = 0;
|
||||
audioBuffer->sizeInFrames = sizeInFrames;
|
||||
|
@ -2028,6 +2033,12 @@ void SetAudioStreamBufferSizeDefault(int size)
|
|||
AUDIO.Buffer.defaultSize = size;
|
||||
}
|
||||
|
||||
// Audio thread callback to request new data
|
||||
void SetAudioStreamCallback(AudioStream stream, AudioStreamCallback callback)
|
||||
{
|
||||
if (stream.buffer != NULL) stream.buffer->callback = callback;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
@ -2041,6 +2052,15 @@ static void OnLog(void *pUserData, ma_uint32 level, const char *pMessage)
|
|||
// Reads audio data from an AudioBuffer object in internal format.
|
||||
static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, void *framesOut, ma_uint32 frameCount)
|
||||
{
|
||||
// Using audio buffer callback
|
||||
if (audioBuffer->callback)
|
||||
{
|
||||
audioBuffer->callback(framesOut, frameCount);
|
||||
audioBuffer->framesProcessed += frameCount;
|
||||
|
||||
return frameCount;
|
||||
}
|
||||
|
||||
ma_uint32 subBufferSizeInFrames = (audioBuffer->sizeInFrames > 1)? audioBuffer->sizeInFrames/2 : audioBuffer->sizeInFrames;
|
||||
ma_uint32 currentSubBufferIndex = audioBuffer->frameCursorPos/subBufferSizeInFrames;
|
||||
|
||||
|
|
|
@ -425,6 +425,8 @@ typedef struct Wave {
|
|||
void *data; // Buffer data pointer
|
||||
} Wave;
|
||||
|
||||
// Opaque structs declaration
|
||||
// NOTE: Actual structs are defined internally in raudio module
|
||||
typedef struct rAudioBuffer rAudioBuffer;
|
||||
|
||||
// AudioStream, custom audio stream
|
||||
|
@ -1472,6 +1474,7 @@ RLAPI RayCollision GetRayCollisionQuad(Ray ray, Vector3 p1, Vector3 p2, Vector3
|
|||
//------------------------------------------------------------------------------------
|
||||
// Audio Loading and Playing Functions (Module: audio)
|
||||
//------------------------------------------------------------------------------------
|
||||
typedef void (*AudioStreamCallback)(void *bufferData, unsigned int frames);
|
||||
|
||||
// Audio device management functions
|
||||
RLAPI void InitAudioDevice(void); // Initialize audio device and context
|
||||
|
@ -1539,6 +1542,7 @@ 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, AudioStreamCallback callback); // Audio thread callback to request new data
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue