Skip to content

Commit

Permalink
Add thread entry and exit callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
KislyjKisel committed Jun 20, 2024
1 parent 52f2a10 commit a17e4b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/raudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ typedef struct tagBITMAPINFOHEADER {
// Threading model: Default: [0] COINIT_MULTITHREADED: COM calls objects on any thread (free threading)
#define MA_COINIT_VALUE 2 // [2] COINIT_APARTMENTTHREADED: Each object has its own thread (apartment model)

static void CallAudioThreadEntryCallback(void);
static void CallAudioThreadExitCallback(void);

#define MA_ON_THREAD_ENTRY CallAudioThreadEntryCallback();
#define MA_ON_THREAD_EXIT CallAudioThreadExitCallback();

#define MINIAUDIO_IMPLEMENTATION
//#define MA_DEBUG_OUTPUT
#include "external/miniaudio.h" // Audio device initialization and management
Expand Down Expand Up @@ -391,6 +397,8 @@ typedef struct AudioData {
int defaultSize; // Default audio buffer size for audio streams
} Buffer;
rAudioProcessor *mixedProcessor;
AudioThreadEntryCallback threadEntryCallback;
AudioThreadExitCallback threadExitCallback;
} AudioData;

//----------------------------------------------------------------------------------
Expand All @@ -403,7 +411,9 @@ static AudioData AUDIO = { // Global AUDIO context
// standard double-buffering system, a 4096 samples buffer has been chosen, it should be enough
// In case of music-stalls, just increase this number
.Buffer.defaultSize = 0,
.mixedProcessor = NULL
.mixedProcessor = NULL,
.threadEntryCallback = NULL,
.threadExitCallback = NULL
};

//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -2327,6 +2337,14 @@ void DetachAudioMixedProcessor(AudioCallback process)
ma_mutex_unlock(&AUDIO.System.lock);
}

void SetAudioThreadEntryCallback(AudioThreadEntryCallback callback) {
AUDIO.threadEntryCallback = callback;
}

void SetAudioThreadExitCallback(AudioThreadExitCallback callback) {
AUDIO.threadExitCallback = callback;
}


//----------------------------------------------------------------------------------
// Module specific Functions Definition
Expand Down Expand Up @@ -2698,6 +2716,19 @@ static void UpdateAudioStreamInLockedState(AudioStream stream, const void *data,
}
}

static void CallAudioThreadEntryCallback(void) {
if (AUDIO.threadEntryCallback) {
AUDIO.threadEntryCallback();
}
}

static void CallAudioThreadExitCallback(void) {
if (AUDIO.threadExitCallback) {
AUDIO.threadExitCallback();
}
}


// Some required functions for audio standalone module version
#if defined(RAUDIO_STANDALONE)
// Check file extension
Expand Down
5 changes: 5 additions & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,8 @@ 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 (*AudioThreadEntryCallback)(void);
typedef void (*AudioThreadExitCallback)(void);

// Audio device management functions
RLAPI void InitAudioDevice(void); // Initialize audio device and context
Expand Down Expand Up @@ -1675,6 +1677,9 @@ RLAPI void DetachAudioStreamProcessor(AudioStream stream, AudioCallback processo
RLAPI void AttachAudioMixedProcessor(AudioCallback processor); // Attach audio stream processor to the entire audio pipeline, receives the samples as 'float'
RLAPI void DetachAudioMixedProcessor(AudioCallback processor); // Detach audio stream processor from the entire audio pipeline

RLAPI void SetAudioThreadEntryCallback(AudioThreadEntryCallback callback); // Audio thread creation callback (usually should be called before audio device initialization, can be NULL)
RLAPI void SetAudioThreadExitCallback(AudioThreadExitCallback callback); // Audio thread destruction callback (can be NULL)

#if defined(__cplusplus)
}
#endif
Expand Down

0 comments on commit a17e4b4

Please sign in to comment.