summaryrefslogtreecommitdiff
path: root/libs/raylib/src/raudio.c
diff options
context:
space:
mode:
authorLuca Sas <sas.luca.alex@gmail.com>2020-10-01 19:17:58 +0100
committerLuca Sas <sas.luca.alex@gmail.com>2020-10-01 19:17:58 +0100
commit45ee5878397822c41b9109d0e3bc2d635c8c8634 (patch)
treee94f7a4c3d7c94760ff5b96886a2632f0d3d5f6b /libs/raylib/src/raudio.c
parent224c1c2087195c48d4a116ba9db0faa209e50332 (diff)
downloadgamejam-slgj-2024-45ee5878397822c41b9109d0e3bc2d635c8c8634.tar.gz
gamejam-slgj-2024-45ee5878397822c41b9109d0e3bc2d635c8c8634.tar.bz2
gamejam-slgj-2024-45ee5878397822c41b9109d0e3bc2d635c8c8634.zip
Updated to raylib 3.0
Diffstat (limited to 'libs/raylib/src/raudio.c')
-rw-r--r--libs/raylib/src/raudio.c1220
1 files changed, 665 insertions, 555 deletions
diff --git a/libs/raylib/src/raudio.c b/libs/raylib/src/raudio.c
index 9fde6e9..6313b16 100644
--- a/libs/raylib/src/raudio.c
+++ b/libs/raylib/src/raudio.c
@@ -4,11 +4,11 @@
*
* FEATURES:
* - Manage audio device (init/close)
+* - Manage raw audio context
+* - Manage mixing channels
* - Load and unload audio files
* - Format wave data (sample rate, size, channels)
* - Play/Stop/Pause/Resume loaded audio
-* - Manage mixing channels
-* - Manage raw audio context
*
* CONFIGURATION:
*
@@ -78,37 +78,140 @@
#include "utils.h" // Required for: fopen() Android mapping
#endif
+#if defined(_WIN32)
+// To avoid conflicting windows.h symbols with raylib, some flags are defined
+// WARNING: Those flags avoid inclusion of some Win32 headers that could be required
+// by user at some point and won't be included...
+//-------------------------------------------------------------------------------------
+
+// If defined, the following flags inhibit definition of the indicated items.
+#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
+#define NOVIRTUALKEYCODES // VK_*
+#define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
+#define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
+#define NOSYSMETRICS // SM_*
+#define NOMENUS // MF_*
+#define NOICONS // IDI_*
+#define NOKEYSTATES // MK_*
+#define NOSYSCOMMANDS // SC_*
+#define NORASTEROPS // Binary and Tertiary raster ops
+#define NOSHOWWINDOW // SW_*
+#define OEMRESOURCE // OEM Resource values
+#define NOATOM // Atom Manager routines
+#define NOCLIPBOARD // Clipboard routines
+#define NOCOLOR // Screen colors
+#define NOCTLMGR // Control and Dialog routines
+#define NODRAWTEXT // DrawText() and DT_*
+#define NOGDI // All GDI defines and routines
+#define NOKERNEL // All KERNEL defines and routines
+#define NOUSER // All USER defines and routines
+//#define NONLS // All NLS defines and routines
+#define NOMB // MB_* and MessageBox()
+#define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
+#define NOMETAFILE // typedef METAFILEPICT
+#define NOMINMAX // Macros min(a,b) and max(a,b)
+#define NOMSG // typedef MSG and associated routines
+#define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
+#define NOSCROLL // SB_* and scrolling routines
+#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
+#define NOSOUND // Sound driver routines
+#define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
+#define NOWH // SetWindowsHook and WH_*
+#define NOWINOFFSETS // GWL_*, GCL_*, associated routines
+#define NOCOMM // COMM driver routines
+#define NOKANJI // Kanji support stuff.
+#define NOHELP // Help engine interface.
+#define NOPROFILER // Profiler interface.
+#define NODEFERWINDOWPOS // DeferWindowPos routines
+#define NOMCX // Modem Configuration Extensions
+
+// Type required before windows.h inclusion
+typedef struct tagMSG *LPMSG;
+
+#include <windows.h>
+
+// Type required by some unused function...
+typedef struct tagBITMAPINFOHEADER {
+ DWORD biSize;
+ LONG biWidth;
+ LONG biHeight;
+ WORD biPlanes;
+ WORD biBitCount;
+ DWORD biCompression;
+ DWORD biSizeImage;
+ LONG biXPelsPerMeter;
+ LONG biYPelsPerMeter;
+ DWORD biClrUsed;
+ DWORD biClrImportant;
+} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
+
+#include <objbase.h>
+#include <mmreg.h>
+#include <mmsystem.h>
+
+// Some required types defined for MSVC/TinyC compiler
+#if defined(_MSC_VER) || defined(__TINYC__)
+ #include "propidl.h"
+#endif
+#endif
+
+#define MA_MALLOC RL_MALLOC
+#define MA_FREE RL_FREE
+
#define MA_NO_JACK
#define MINIAUDIO_IMPLEMENTATION
-#include "external/miniaudio.h" // miniaudio library
-#undef PlaySound // Win32 API: windows.h > mmsystem.h defines PlaySound macro
+#include "external/miniaudio.h" // miniaudio library
+#undef PlaySound // Win32 API: windows.h > mmsystem.h defines PlaySound macro
-#include <stdlib.h> // Required for: malloc(), free()
-#include <string.h> // Required for: strcmp(), strncmp()
-#include <stdio.h> // Required for: FILE, fopen(), fclose(), fread()
+#include <stdlib.h> // Required for: malloc(), free()
+#include <stdio.h> // Required for: FILE, fopen(), fclose(), fread()
+
+#if defined(RAUDIO_STANDALONE)
+ #include <string.h> // Required for: strcmp() [Used in IsFileExtension()]
+
+ #if !defined(TRACELOG)
+ #define TRACELOG(level, ...) (void)0
+ #endif
+#endif
#if defined(SUPPORT_FILEFORMAT_OGG)
+ // TODO: Remap malloc()/free() calls to RL_MALLOC/RL_FREE
+
#define STB_VORBIS_IMPLEMENTATION
#include "external/stb_vorbis.h" // OGG loading functions
#endif
#if defined(SUPPORT_FILEFORMAT_XM)
+ #define JARXM_MALLOC RL_MALLOC
+ #define JARXM_FREE RL_FREE
+
#define JAR_XM_IMPLEMENTATION
#include "external/jar_xm.h" // XM loading functions
#endif
#if defined(SUPPORT_FILEFORMAT_MOD)
+ #define JARMOD_MALLOC RL_MALLOC
+ #define JARMOD_FREE RL_FREE
+
#define JAR_MOD_IMPLEMENTATION
#include "external/jar_mod.h" // MOD loading functions
#endif
#if defined(SUPPORT_FILEFORMAT_FLAC)
+ #define DRFLAC_MALLOC RL_MALLOC
+ #define DRFLAC_REALLOC RL_REALLOC
+ #define DRFLAC_FREE RL_FREE
+
#define DR_FLAC_IMPLEMENTATION
#define DR_FLAC_NO_WIN32_IO
#include "external/dr_flac.h" // FLAC loading functions
#endif
#if defined(SUPPORT_FILEFORMAT_MP3)
+ #define DRMP3_MALLOC RL_MALLOC
+ #define DRMP3_REALLOC RL_REALLOC
+ #define DRMP3_FREE RL_FREE
+
#define DR_MP3_IMPLEMENTATION
#include "external/dr_mp3.h" // MP3 loading functions
#endif
@@ -120,11 +223,11 @@
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
-// NOTE: Music buffer size is defined by number of samples, independent of sample size and channels number
-// After some math, considering a sampleRate of 48000, a buffer refill rate of 1/60 seconds and a
-// standard double-buffering system, a 4096 samples buffer has been chosen, it should be enough
-// In case of music-stalls, just increase this number
-#define AUDIO_BUFFER_SIZE 4096 // PCM data samples (i.e. 16bit, Mono: 8Kb)
+#define AUDIO_DEVICE_FORMAT ma_format_f32
+#define AUDIO_DEVICE_CHANNELS 2
+#define AUDIO_DEVICE_SAMPLE_RATE 44100
+
+#define MAX_AUDIO_BUFFER_POOL_CHANNELS 16
//----------------------------------------------------------------------------------
// Types and Structures Definition
@@ -155,14 +258,80 @@ typedef enum {
} TraceLogType;
#endif
+// NOTE: Different logic is used when feeding data to the playback device
+// depending on whether or not data is streamed (Music vs Sound)
+typedef enum {
+ AUDIO_BUFFER_USAGE_STATIC = 0,
+ AUDIO_BUFFER_USAGE_STREAM
+} AudioBufferUsage;
+
+// Audio buffer structure
+struct rAudioBuffer {
+ ma_data_converter converter; // Audio data converter
+
+ float volume; // Audio buffer volume
+ float pitch; // Audio buffer pitch
+
+ bool playing; // Audio buffer state: AUDIO_PLAYING
+ bool paused; // Audio buffer state: AUDIO_PAUSED
+ bool looping; // Audio buffer looping, always true for AudioStreams
+ int usage; // Audio buffer usage mode: STATIC or STREAM
+
+ bool isSubBufferProcessed[2]; // SubBuffer processed (virtual double buffer)
+ unsigned int sizeInFrames; // Total buffer size in frames
+ unsigned int frameCursorPos; // Frame cursor position
+ unsigned int totalFramesProcessed; // Total frames processed in this buffer (required for play timing)
+
+ unsigned char *data; // Data buffer, on music stream keeps filling
+
+ rAudioBuffer *next; // Next audio buffer on the list
+ rAudioBuffer *prev; // Previous audio buffer on the list
+};
+
+#define AudioBuffer rAudioBuffer // HACK: To avoid CoreAudio (macOS) symbol collision
+
+// Audio data context
+typedef struct AudioData {
+ struct {
+ ma_context context; // miniaudio context data
+ ma_device device; // miniaudio device
+ ma_mutex lock; // miniaudio mutex lock
+ bool isReady; // Check if audio device is ready
+ } System;
+ struct {
+ 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
+ } Buffer;
+ struct {
+ AudioBuffer *pool[MAX_AUDIO_BUFFER_POOL_CHANNELS]; // Multichannel AudioBuffer pointers pool
+ unsigned int poolCounter; // AudioBuffer pointers pool counter
+ unsigned int channels[MAX_AUDIO_BUFFER_POOL_CHANNELS]; // AudioBuffer pool channels
+ } MultiChannel;
+} AudioData;
+
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
-// ...
+static AudioData AUDIO = { // Global AUDIO context
+
+ // NOTE: Music buffer size is defined by number of samples, independent of sample size and channels number
+ // After some math, considering a sampleRate of 48000, a buffer refill rate of 1/60 seconds and a
+ // 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 = 4096
+};
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
+static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message);
+static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount);
+static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, float localVolume);
+
+static void InitAudioBufferPool(void); // Initialise the multichannel buffer pool
+static void CloseAudioBufferPool(void); // Close the audio buffers pool
+
#if defined(SUPPORT_FILEFORMAT_WAV)
static Wave LoadWAV(const char *fileName); // Load WAV file
static int SaveWAV(Wave wave, const char *fileName); // Save wave data as WAV file
@@ -178,74 +347,17 @@ static Wave LoadMP3(const char *fileName); // Load MP3 file
#endif
#if defined(RAUDIO_STANDALONE)
-bool IsFileExtension(const char *fileName, const char *ext); // Check file extension
-void TraceLog(int msgType, const char *text, ...); // Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
+bool IsFileExtension(const char *fileName, const char *ext);// Check file extension
+void TraceLog(int msgType, const char *text, ...); // Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
#endif
//----------------------------------------------------------------------------------
-// AudioBuffer Functionality
-//----------------------------------------------------------------------------------
-#define DEVICE_FORMAT ma_format_f32
-#define DEVICE_CHANNELS 2
-#define DEVICE_SAMPLE_RATE 44100
-
-#define MAX_AUDIO_BUFFER_POOL_CHANNELS 16
-
-typedef enum { AUDIO_BUFFER_USAGE_STATIC = 0, AUDIO_BUFFER_USAGE_STREAM } AudioBufferUsage;
-
-// Audio buffer structure
-// NOTE: Slightly different logic is used when feeding data to the
-// playback device depending on whether or not data is streamed
-struct rAudioBuffer {
- ma_pcm_converter dsp; // PCM data converter
-
- float volume; // Audio buffer volume
- float pitch; // Audio buffer pitch
-
- bool playing; // Audio buffer state: AUDIO_PLAYING
- bool paused; // Audio buffer state: AUDIO_PAUSED
- bool looping; // Audio buffer looping, always true for AudioStreams
- int usage; // Audio buffer usage mode: STATIC or STREAM
-
- bool isSubBufferProcessed[2]; // SubBuffer processed (virtual double buffer)
- unsigned int frameCursorPos; // Frame cursor position
- unsigned int bufferSizeInFrames; // Total buffer size in frames
- unsigned int totalFramesProcessed; // Total frames processed in this buffer (required for play timming)
-
- unsigned char *buffer; // Data buffer, on music stream keeps filling
-
- rAudioBuffer *next; // Next audio buffer on the list
- rAudioBuffer *prev; // Previous audio buffer on the list
-};
-
-#define AudioBuffer rAudioBuffer // HACK: To avoid CoreAudio (macOS) symbol collision
-
-// Audio buffers are tracked in a linked list
-static AudioBuffer *firstAudioBuffer = NULL; // Pointer to first AudioBuffer in the list
-static AudioBuffer *lastAudioBuffer = NULL; // Pointer to last AudioBuffer in the list
-
-// miniaudio global variables
-static ma_context context; // miniaudio context data
-static ma_device device; // miniaudio device
-static ma_mutex audioLock; // miniaudio mutex lock
-static bool isAudioInitialized = false; // Check if audio device is initialized
-static float masterVolume = 1.0f; // Master volume (multiplied on output mixing)
-
-// Multi channel playback global variables
-static AudioBuffer *audioBufferPool[MAX_AUDIO_BUFFER_POOL_CHANNELS] = { 0 }; // Multichannel AudioBuffer pointers pool
-static unsigned int audioBufferPoolCounter = 0; // AudioBuffer pointers pool counter
-static unsigned int audioBufferPoolChannels[MAX_AUDIO_BUFFER_POOL_CHANNELS] = { 0 }; // AudioBuffer pool channels
-
-// miniaudio functions declaration
-static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message);
-static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount);
-static ma_uint32 OnAudioBufferDSPRead(ma_pcm_converter *pDSP, void *pFramesOut, ma_uint32 frameCount, void *pUserData);
-static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, float localVolume);
-
// AudioBuffer management functions declaration
// NOTE: Those functions are not exposed by raylib... for the moment
-AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, int usage);
-void CloseAudioBuffer(AudioBuffer *buffer);
+//----------------------------------------------------------------------------------
+AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage);
+void UnloadAudioBuffer(AudioBuffer *buffer);
+
bool IsAudioBufferPlaying(AudioBuffer *buffer);
void PlayAudioBuffer(AudioBuffer *buffer);
void StopAudioBuffer(AudioBuffer *buffer);
@@ -256,338 +368,105 @@ void SetAudioBufferPitch(AudioBuffer *buffer, float pitch);
void TrackAudioBuffer(AudioBuffer *buffer);
void UntrackAudioBuffer(AudioBuffer *buffer);
-
-//----------------------------------------------------------------------------------
-// miniaudio functions definitions
-//----------------------------------------------------------------------------------
-
-// Log callback function
-static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message)
-{
- (void)pContext;
- (void)pDevice;
-
- TraceLog(LOG_ERROR, message); // All log messages from miniaudio are errors
-}
-
-// Sending audio data to device callback function
-// NOTE: All the mixing takes place here
-static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount)
-{
- (void)pDevice;
-
- // Mixing is basically just an accumulation, we need to initialize the output buffer to 0
- memset(pFramesOut, 0, frameCount*pDevice->playback.channels*ma_get_bytes_per_sample(pDevice->playback.format));
-
- // Using a mutex here for thread-safety which makes things not real-time
- // This is unlikely to be necessary for this project, but may want to consider how you might want to avoid this
- ma_mutex_lock(&audioLock);
- {
- for (AudioBuffer *audioBuffer = firstAudioBuffer; audioBuffer != NULL; audioBuffer = audioBuffer->next)
- {
- // Ignore stopped or paused sounds
- if (!audioBuffer->playing || audioBuffer->paused) continue;
-
- ma_uint32 framesRead = 0;
-
- while (1)
- {
- if (framesRead > frameCount)
- {
- TraceLog(LOG_DEBUG, "Mixed too many frames from audio buffer");
- break;
- }
-
- if (framesRead == frameCount) break;
-
- // Just read as much data as we can from the stream
- ma_uint32 framesToRead = (frameCount - framesRead);
-
- while (framesToRead > 0)
- {
- float tempBuffer[1024]; // 512 frames for stereo
-
- ma_uint32 framesToReadRightNow = framesToRead;
- if (framesToReadRightNow > sizeof(tempBuffer)/sizeof(tempBuffer[0])/DEVICE_CHANNELS)
- {
- framesToReadRightNow = sizeof(tempBuffer)/sizeof(tempBuffer[0])/DEVICE_CHANNELS;
- }
-
- ma_uint32 framesJustRead = (ma_uint32)ma_pcm_converter_read(&audioBuffer->dsp, tempBuffer, framesToReadRightNow);
- if (framesJustRead > 0)
- {
- float *framesOut = (float *)pFramesOut + (framesRead*device.playback.channels);
- float *framesIn = tempBuffer;
-
- MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer->volume);
-
- framesToRead -= framesJustRead;
- framesRead += framesJustRead;
- }
-
- if (!audioBuffer->playing)
- {
- framesRead = frameCount;
- break;
- }
-
- // If we weren't able to read all the frames we requested, break
- if (framesJustRead < framesToReadRightNow)
- {
- if (!audioBuffer->looping)
- {
- StopAudioBuffer(audioBuffer);
- break;
- }
- else
- {
- // Should never get here, but just for safety,
- // move the cursor position back to the start and continue the loop
- audioBuffer->frameCursorPos = 0;
- continue;
- }
- }
- }
-
- // If for some reason we weren't able to read every frame we'll need to break from the loop
- // Not doing this could theoretically put us into an infinite loop
- if (framesToRead > 0) break;
- }
- }
- }
-
- ma_mutex_unlock(&audioLock);
-}
-
-// DSP read from audio buffer callback function
-static ma_uint32 OnAudioBufferDSPRead(ma_pcm_converter *pDSP, void *pFramesOut, ma_uint32 frameCount, void *pUserData)
-{
- AudioBuffer *audioBuffer = (AudioBuffer *)pUserData;
-
- ma_uint32 subBufferSizeInFrames = (audioBuffer->bufferSizeInFrames > 1)? audioBuffer->bufferSizeInFrames/2 : audioBuffer->bufferSizeInFrames;
- ma_uint32 currentSubBufferIndex = audioBuffer->frameCursorPos/subBufferSizeInFrames;
-
- if (currentSubBufferIndex > 1)
- {
- TraceLog(LOG_DEBUG, "Frame cursor position moved too far forward in audio stream");
- return 0;
- }
-
- // Another thread can update the processed state of buffers so
- // we just take a copy here to try and avoid potential synchronization problems
- bool isSubBufferProcessed[2];
- isSubBufferProcessed[0] = audioBuffer->isSubBufferProcessed[0];
- isSubBufferProcessed[1] = audioBuffer->isSubBufferProcessed[1];
-
- ma_uint32 frameSizeInBytes = ma_get_bytes_per_sample(audioBuffer->dsp.formatConverterIn.config.formatIn)*audioBuffer->dsp.formatConverterIn.config.channels;
-
- // Fill out every frame until we find a buffer that's marked as processed. Then fill the remainder with 0
- ma_uint32 framesRead = 0;
- while (1)
- {
- // We break from this loop differently depending on the buffer's usage
- // - For static buffers, we simply fill as much data as we can
- // - For streaming buffers we only fill the halves of the buffer that are processed
- // Unprocessed halves must keep their audio data in-tact
- if (audioBuffer->usage == AUDIO_BUFFER_USAGE_STATIC)
- {
- if (framesRead >= frameCount) break;
- }
- else
- {
- if (isSubBufferProcessed[currentSubBufferIndex]) break;
- }
-
- ma_uint32 totalFramesRemaining = (frameCount - framesRead);
- if (totalFramesRemaining == 0) break;
-
- ma_uint32 framesRemainingInOutputBuffer;
- if (audioBuffer->usage == AUDIO_BUFFER_USAGE_STATIC)
- {
- framesRemainingInOutputBuffer = audioBuffer->bufferSizeInFrames - audioBuffer->frameCursorPos;
- }
- else
- {
- ma_uint32 firstFrameIndexOfThisSubBuffer = subBufferSizeInFrames*currentSubBufferIndex;
- framesRemainingInOutputBuffer = subBufferSizeInFrames - (audioBuffer->frameCursorPos - firstFrameIndexOfThisSubBuffer);
- }
-
- ma_uint32 framesToRead = totalFramesRemaining;
- if (framesToRead > framesRemainingInOutputBuffer) framesToRead = framesRemainingInOutputBuffer;
-
- memcpy((unsigned char *)pFramesOut + (framesRead*frameSizeInBytes), audioBuffer->buffer + (audioBuffer->frameCursorPos*frameSizeInBytes), framesToRead*frameSizeInBytes);
- audioBuffer->frameCursorPos = (audioBuffer->frameCursorPos + framesToRead)%audioBuffer->bufferSizeInFrames;
- framesRead += framesToRead;
-
- // If we've read to the end of the buffer, mark it as processed
- if (framesToRead == framesRemainingInOutputBuffer)
- {
- audioBuffer->isSubBufferProcessed[currentSubBufferIndex] = true;
- isSubBufferProcessed[currentSubBufferIndex] = true;
-
- currentSubBufferIndex = (currentSubBufferIndex + 1)%2;
-
- // We need to break from this loop if we're not looping
- if (!audioBuffer->looping)
- {
- StopAudioBuffer(audioBuffer);
- break;
- }
- }
- }
-
- // Zero-fill excess
- ma_uint32 totalFramesRemaining = (frameCount - framesRead);
- if (totalFramesRemaining > 0)
- {
- memset((unsigned char *)pFramesOut + (framesRead*frameSizeInBytes), 0, totalFramesRemaining*frameSizeInBytes);
-
- // For static buffers we can fill the remaining frames with silence for safety, but we don't want
- // to report those frames as "read". The reason for this is that the caller uses the return value
- // to know whether or not a non-looping sound has finished playback.
- if (audioBuffer->usage != AUDIO_BUFFER_USAGE_STATIC) framesRead += totalFramesRemaining;
- }
-
- return framesRead;
-}
-
-// This is the main mixing function. Mixing is pretty simple in this project - it's just an accumulation.
-// NOTE: framesOut is both an input and an output. It will be initially filled with zeros outside of this function.
-static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, float localVolume)
-{
- for (ma_uint32 iFrame = 0; iFrame < frameCount; ++iFrame)
- {
- for (ma_uint32 iChannel = 0; iChannel < device.playback.channels; ++iChannel)
- {
- float *frameOut = framesOut + (iFrame*device.playback.channels);
- const float *frameIn = framesIn + (iFrame*device.playback.channels);
-
- frameOut[iChannel] += (frameIn[iChannel]*masterVolume*localVolume);
- }
- }
-}
-
-// Initialise the multichannel buffer pool
-static void InitAudioBufferPool()
-{
- // Dummy buffers
- for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
- {
- audioBufferPool[i] = InitAudioBuffer(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, 0, AUDIO_BUFFER_USAGE_STATIC);
- }
-}
-
-// Close the audio buffers pool
-static void CloseAudioBufferPool()
-{
- for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
- {
- RL_FREE(audioBufferPool[i]->buffer);
- RL_FREE(audioBufferPool[i]);
- }
-}
-
//----------------------------------------------------------------------------------
// Module Functions Definition - Audio Device initialization and Closing
//----------------------------------------------------------------------------------
// Initialize audio device
void InitAudioDevice(void)
{
+ // TODO: Load AUDIO context memory dynamically?
+
// Init audio context
- ma_context_config contextConfig = ma_context_config_init();
- contextConfig.logCallback = OnLog;
+ ma_context_config ctxConfig = ma_context_config_init();
+ ctxConfig.logCallback = OnLog;
- ma_result result = ma_context_init(NULL, 0, &contextConfig, &context);
+ ma_result result = ma_context_init(NULL, 0, &ctxConfig, &AUDIO.System.context);
if (result != MA_SUCCESS)
{
- TraceLog(LOG_ERROR, "Failed to initialize audio context");
+ TRACELOG(LOG_ERROR, "AUDIO: Failed to initialize context");
return;
}
// Init audio device
// NOTE: Using the default device. Format is floating point because it simplifies mixing.
ma_device_config config = ma_device_config_init(ma_device_type_playback);
- config.playback.pDeviceID = NULL; // NULL for the default playback device.
- config.playback.format = DEVICE_FORMAT;
- config.playback.channels = DEVICE_CHANNELS;
- config.capture.pDeviceID = NULL; // NULL for the default capture device.
+ config.playback.pDeviceID = NULL; // NULL for the default playback AUDIO.System.device.
+ config.playback.format = AUDIO_DEVICE_FORMAT;
+ config.playback.channels = AUDIO_DEVICE_CHANNELS;
+ config.capture.pDeviceID = NULL; // NULL for the default capture AUDIO.System.device.
config.capture.format = ma_format_s16;
config.capture.channels = 1;
- config.sampleRate = DEVICE_SAMPLE_RATE;
+ config.sampleRate = AUDIO_DEVICE_SAMPLE_RATE;
config.dataCallback = OnSendAudioDataToDevice;
config.pUserData = NULL;
- result = ma_device_init(&context, &config, &device);
+ result = ma_device_init(&AUDIO.System.context, &config, &AUDIO.System.device);
if (result != MA_SUCCESS)
{
- TraceLog(LOG_ERROR, "Failed to initialize audio playback device");
- ma_context_uninit(&context);
+ TRACELOG(LOG_ERROR, "AUDIO: Failed to initialize playback device");
+ ma_context_uninit(&AUDIO.System.context);
return;
}
// Keep the device running the whole time. May want to consider doing something a bit smarter and only have the device running
// while there's at least one sound being played.
- result = ma_device_start(&device);
+ result = ma_device_start(&AUDIO.System.device);
if (result != MA_SUCCESS)
{
- TraceLog(LOG_ERROR, "Failed to start audio playback device");
- ma_device_uninit(&device);
- ma_context_uninit(&context);
+ TRACELOG(LOG_ERROR, "AUDIO: Failed to start playback device");
+ ma_device_uninit(&AUDIO.System.device);
+ ma_context_uninit(&AUDIO.System.context);
return;
}
// Mixing happens on a seperate thread which means we need to synchronize. I'm using a mutex here to make things simple, but may
// want to look at something a bit smarter later on to keep everything real-time, if that's necessary.
- if (ma_mutex_init(&context, &audioLock) != MA_SUCCESS)
+ if (ma_mutex_init(&AUDIO.System.context, &AUDIO.System.lock) != MA_SUCCESS)
{
- TraceLog(LOG_ERROR, "Failed to create mutex for audio mixing");
- ma_device_uninit(&device);
- ma_context_uninit(&context);
+ TRACELOG(LOG_ERROR, "AUDIO: Failed to create mutex for mixing");
+ ma_device_uninit(&AUDIO.System.device);
+ ma_context_uninit(&AUDIO.System.context);
return;
}
- TraceLog(LOG_INFO, "Audio device initialized successfully");
- TraceLog(LOG_INFO, "Audio backend: miniaudio / %s", ma_get_backend_name(context.backend));
- TraceLog(LOG_INFO, "Audio format: %s -> %s", ma_get_format_name(device.playback.format), ma_get_format_name(device.playback.internalFormat));
- TraceLog(LOG_INFO, "Audio channels: %d -> %d", device.playback.channels, device.playback.internalChannels);
- TraceLog(LOG_INFO, "Audio sample rate: %d -> %d", device.sampleRate, device.playback.internalSampleRate);
- TraceLog(LOG_INFO, "Audio buffer size: %d", device.playback.internalBufferSizeInFrames);
+ TRACELOG(LOG_INFO, "AUDIO: Device initialized successfully");
+ TRACELOG(LOG_INFO, " > Backend: miniaudio / %s", ma_get_backend_name(AUDIO.System.context.backend));
+ TRACELOG(LOG_INFO, " > Format: %s -> %s", ma_get_format_name(AUDIO.System.device.playback.format), ma_get_format_name(AUDIO.System.device.playback.internalFormat));
+ TRACELOG(LOG_INFO, " > Channels: %d -> %d", AUDIO.System.device.playback.channels, AUDIO.System.device.playback.internalChannels);
+ TRACELOG(LOG_INFO, " > Sample rate: %d -> %d", AUDIO.System.device.sampleRate, AUDIO.System.device.playback.internalSampleRate);
+ TRACELOG(LOG_INFO, " > Periods size: %d", AUDIO.System.device.playback.internalPeriodSizeInFrames*AUDIO.System.device.playback.internalPeriods);
InitAudioBufferPool();
- TraceLog(LOG_INFO, "Audio multichannel pool size: %i", MAX_AUDIO_BUFFER_POOL_CHANNELS);
- isAudioInitialized = true;
+ AUDIO.System.isReady = true;
}
// Close the audio device for all contexts
void CloseAudioDevice(void)
{
- if (isAudioInitialized)
+ if (AUDIO.System.isReady)
{
- ma_mutex_uninit(&audioLock);
- ma_device_uninit(&device);
- ma_context_uninit(&context);
+ ma_mutex_uninit(&AUDIO.System.lock);
+ ma_device_uninit(&AUDIO.System.device);
+ ma_context_uninit(&AUDIO.System.context);
CloseAudioBufferPool();
- TraceLog(LOG_INFO, "Audio device closed successfully");
+ TRACELOG(LOG_INFO, "AUDIO: Device closed successfully");
}
- else TraceLog(LOG_WARNING, "Could not close audio device because it is not currently initialized");
+ else TRACELOG(LOG_WARNING, "AUDIO: Device could not be closed, not currently initialized");
}
// Check if device has been initialized successfully
bool IsAudioDeviceReady(void)
{
- return isAudioInitialized;
+ return AUDIO.System.isReady;
}
// Set master volume (listener)
void SetMasterVolume(float volume)
{
- if (volume < 0.0f) volume = 0.0f;
- else if (volume > 1.0f) volume = 1.0f;
-
- masterVolume = volume;
+ ma_device_set_master_volume(&AUDIO.System.device, volume);
}
//----------------------------------------------------------------------------------
@@ -595,36 +474,27 @@ void SetMasterVolume(float volume)
//----------------------------------------------------------------------------------
// Initialize a new audio buffer (filled with silence)
-AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, 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));
if (audioBuffer == NULL)
{
- TraceLog(LOG_ERROR, "InitAudioBuffer() : Failed to allocate memory for audio buffer");
+ TRACELOG(LOG_ERROR, "AUDIO: Failed to allocate memory for buffer");
return NULL;
}
-
- audioBuffer->buffer = RL_CALLOC(bufferSizeInFrames*channels*ma_get_bytes_per_sample(format), 1);
+
+ audioBuffer->data = RL_CALLOC(sizeInFrames*channels*ma_get_bytes_per_sample(format), 1);
// Audio data runs through a format converter
- ma_pcm_converter_config dspConfig;
- memset(&dspConfig, 0, sizeof(dspConfig));
- dspConfig.formatIn = format;
- dspConfig.formatOut = DEVICE_FORMAT;
- dspConfig.channelsIn = channels;
- dspConfig.channelsOut = DEVICE_CHANNELS;
- dspConfig.sampleRateIn = sampleRate;
- dspConfig.sampleRateOut = DEVICE_SAMPLE_RATE;
- dspConfig.onRead = OnAudioBufferDSPRead; // Callback on data reading
- dspConfig.pUserData = audioBuffer; // Audio data pointer
- dspConfig.allowDynamicSampleRate = true; // Required for pitch shifting
-
- ma_result result = ma_pcm_converter_init(&dspConfig, &audioBuffer->dsp);
+ ma_data_converter_config converterConfig = ma_data_converter_config_init(format, AUDIO_DEVICE_FORMAT, channels, AUDIO_DEVICE_CHANNELS, sampleRate, AUDIO_DEVICE_SAMPLE_RATE);
+ converterConfig.resampling.allowDynamicSampleRate = true; // Required for pitch shifting
+
+ ma_result result = ma_data_converter_init(&converterConfig, &audioBuffer->converter);
if (result != MA_SUCCESS)
{
- TraceLog(LOG_ERROR, "InitAudioBuffer() : Failed to create data conversion pipeline");
+ TRACELOG(LOG_ERROR, "AUDIO: Failed to create data conversion pipeline");
RL_FREE(audioBuffer);
return NULL;
}
@@ -637,7 +507,7 @@ AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
audioBuffer->looping = false;
audioBuffer->usage = usage;
audioBuffer->frameCursorPos = 0;
- audioBuffer->bufferSizeInFrames = bufferSizeInFrames;
+ audioBuffer->sizeInFrames = sizeInFrames;
// Buffers should be marked as processed by default so that a call to
// UpdateAudioStream() immediately after initialization works correctly
@@ -651,15 +521,15 @@ AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
}
// Delete an audio buffer
-void CloseAudioBuffer(AudioBuffer *buffer)
+void UnloadAudioBuffer(AudioBuffer *buffer)
{
if (buffer != NULL)
{
+ ma_data_converter_uninit(&buffer->converter);
UntrackAudioBuffer(buffer);
- RL_FREE(buffer->buffer);
+ RL_FREE(buffer->data);
RL_FREE(buffer);
}
- else TraceLog(LOG_ERROR, "CloseAudioBuffer() : No audio buffer");
}
// Check if an audio buffer is playing
@@ -668,7 +538,6 @@ bool IsAudioBufferPlaying(AudioBuffer *buffer)
bool result = false;
if (buffer != NULL) result = (buffer->playing && !buffer->paused);
- else TraceLog(LOG_WARNING, "IsAudioBufferPlaying() : No audio buffer");
return result;
}
@@ -684,7 +553,6 @@ void PlayAudioBuffer(AudioBuffer *buffer)
buffer->paused = false;
buffer->frameCursorPos = 0;
}
- else TraceLog(LOG_ERROR, "PlayAudioBuffer() : No audio buffer");
}
// Stop an audio buffer
@@ -702,28 +570,24 @@ void StopAudioBuffer(AudioBuffer *buffer)
buffer->isSubBufferProcessed[1] = true;
}
}
- else TraceLog(LOG_ERROR, "StopAudioBuffer() : No audio buffer");
}
// Pause an audio buffer
void PauseAudioBuffer(AudioBuffer *buffer)
{
if (buffer != NULL) buffer->paused = true;
- else TraceLog(LOG_ERROR, "PauseAudioBuffer() : No audio buffer");
}
// Resume an audio buffer
void ResumeAudioBuffer(AudioBuffer *buffer)
{
if (buffer != NULL) buffer->paused = false;
- else TraceLog(LOG_ERROR, "ResumeAudioBuffer() : No audio buffer");
}
// Set volume for an audio buffer
void SetAudioBufferVolume(AudioBuffer *buffer, float volume)
{
if (buffer != NULL) buffer->volume = volume;
- else TraceLog(LOG_WARNING, "SetAudioBufferVolume() : No audio buffer");
}
// Set pitch for an audio buffer
@@ -737,46 +601,45 @@ void SetAudioBufferPitch(AudioBuffer *buffer, float pitch)
// Note that this changes the duration of the sound:
// - higher pitches will make the sound faster
// - lower pitches make it slower
- ma_uint32 newOutputSampleRate = (ma_uint32)((float)buffer->dsp.src.config.sampleRateOut/pitchMul);
- buffer->pitch *= (float)buffer->dsp.src.config.sampleRateOut/newOutputSampleRate;
+ ma_uint32 newOutputSampleRate = (ma_uint32)((float)buffer->converter.config.sampleRateOut/pitchMul);
+ buffer->pitch *= (float)buffer->converter.config.sampleRateOut/newOutputSampleRate;
- ma_pcm_converter_set_output_sample_rate(&buffer->dsp, newOutputSampleRate);
+ ma_data_converter_set_rate(&buffer->converter, buffer->converter.config.sampleRateIn, newOutputSampleRate);
}
- else TraceLog(LOG_WARNING, "SetAudioBufferPitch() : No audio buffer");
}
// Track audio buffer to linked list next position
void TrackAudioBuffer(AudioBuffer *buffer)
{
- ma_mutex_lock(&audioLock);
+ ma_mutex_lock(&AUDIO.System.lock);
{
- if (firstAudioBuffer == NULL) firstAudioBuffer = buffer;
+ if (AUDIO.Buffer.first == NULL) AUDIO.Buffer.first = buffer;
else
{
- lastAudioBuffer->next = buffer;
- buffer->prev = lastAudioBuffer;
+ AUDIO.Buffer.last->next = buffer;
+ buffer->prev = AUDIO.Buffer.last;
}
- lastAudioBuffer = buffer;
+ AUDIO.Buffer.last = buffer;
}
- ma_mutex_unlock(&audioLock);
+ ma_mutex_unlock(&AUDIO.System.lock);
}
// Untrack audio buffer from linked list
void UntrackAudioBuffer(AudioBuffer *buffer)
{
- ma_mutex_lock(&audioLock);
+ ma_mutex_lock(&AUDIO.System.lock);
{
- if (buffer->prev == NULL) firstAudioBuffer = buffer->next;
+ if (buffer->prev == NULL) AUDIO.Buffer.first = buffer->next;
else buffer->prev->next = buffer->next;
- if (buffer->next == NULL) lastAudioBuffer = buffer->prev;
+ if (buffer->next == NULL) AUDIO.Buffer.last = buffer->prev;
else buffer->next->prev = buffer->prev;
buffer->prev = NULL;
buffer->next = NULL;
}
- ma_mutex_unlock(&audioLock);
+ ma_mutex_unlock(&AUDIO.System.lock);
}
//----------------------------------------------------------------------------------
@@ -801,7 +664,7 @@ Wave LoadWave(const char *fileName)
#if defined(SUPPORT_FILEFORMAT_MP3)
else if (IsFileExtension(fileName, ".mp3")) wave = LoadMP3(fileName);
#endif
- else TraceLog(LOG_WARNING, "[%s] Audio fileformat not supported, it can't be loaded", fileName);
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] File format not supported", fileName);
return wave;
}
@@ -829,7 +692,7 @@ Sound LoadSoundFromWave(Wave wave)
{
// When using miniaudio we need to do our own mixing.
// To simplify this we need convert the format of each sound to be consistent with
- // the format used to open the playback device. We can do this two ways:
+ // the format used to open the playback AUDIO.System.device. We can do this two ways:
//
// 1) Convert the whole sound in one go at load time (here).
// 2) Convert the audio data in chunks at mixing time.
@@ -839,19 +702,19 @@ Sound LoadSoundFromWave(Wave wave)
ma_format formatIn = ((wave.sampleSize == 8)? ma_format_u8 : ((wave.sampleSize == 16)? ma_format_s16 : ma_format_f32));
ma_uint32 frameCountIn = wave.sampleCount/wave.channels;
- ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, NULL, formatIn, wave.channels, wave.sampleRate, frameCountIn);
- if (frameCount == 0) TraceLog(LOG_WARNING, "LoadSoundFromWave() : Failed to get frame count for format conversion");
+ ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, 0, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, NULL, frameCountIn, formatIn, wave.channels, wave.sampleRate);
+ if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed to get frame count for format conversion");
- AudioBuffer *audioBuffer = InitAudioBuffer(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, frameCount, AUDIO_BUFFER_USAGE_STATIC);
- if (audioBuffer == NULL) TraceLog(LOG_WARNING, "LoadSoundFromWave() : Failed to create audio buffer");
+ AudioBuffer *audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, frameCount, AUDIO_BUFFER_USAGE_STATIC);
+ if (audioBuffer == NULL) TRACELOG(LOG_WARNING, "SOUND: Failed to create buffer");
- frameCount = (ma_uint32)ma_convert_frames(audioBuffer->buffer, audioBuffer->dsp.formatConverterIn.config.formatIn, audioBuffer->dsp.formatConverterIn.config.channels, audioBuffer->dsp.src.config.sampleRateIn, wave.data, formatIn, wave.channels, wave.sampleRate, frameCountIn);
- if (frameCount == 0) TraceLog(LOG_WARNING, "LoadSoundFromWave() : Format conversion failed");
+ frameCount = (ma_uint32)ma_convert_frames(audioBuffer->data, frameCount, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, wave.data, frameCountIn, formatIn, wave.channels, wave.sampleRate);
+ if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed format conversion");
- sound.sampleCount = frameCount*DEVICE_CHANNELS;
- sound.stream.sampleRate = DEVICE_SAMPLE_RATE;
+ sound.sampleCount = frameCount*AUDIO_DEVICE_CHANNELS;
+ sound.stream.sampleRate = AUDIO_DEVICE_SAMPLE_RATE;
sound.stream.sampleSize = 32;
- sound.stream.channels = DEVICE_CHANNELS;
+ sound.stream.channels = AUDIO_DEVICE_CHANNELS;
sound.stream.buffer = audioBuffer;
}
@@ -863,30 +726,27 @@ void UnloadWave(Wave wave)
{
if (wave.data != NULL) RL_FREE(wave.data);
- TraceLog(LOG_INFO, "Unloaded wave data from RAM");
+ TRACELOG(LOG_INFO, "WAVE: Unloaded wave data from RAM");
}
// Unload sound
void UnloadSound(Sound sound)
{
- CloseAudioBuffer(sound.stream.buffer);
+ UnloadAudioBuffer(sound.stream.buffer);
- TraceLog(LOG_INFO, "Unloaded sound data from RAM");
+ TRACELOG(LOG_INFO, "WAVE: Unloaded sound data from RAM");
}
// Update sound buffer with new data
void UpdateSound(Sound sound, const void *data, int samplesCount)
{
- AudioBuffer *audioBuffer = sound.stream.buffer;
-
- if (audioBuffer != NULL)
+ if (sound.stream.buffer != NULL)
{
- StopAudioBuffer(audioBuffer);
+ StopAudioBuffer(sound.stream.buffer);
// TODO: May want to lock/unlock this since this data buffer is read at mixing time
- memcpy(audioBuffer->buffer, data, samplesCount*audioBuffer->dsp.formatConverterIn.config.channels*ma_get_bytes_per_sample(audioBuffer->dsp.formatConverterIn.config.formatIn));
+ memcpy(sound.stream.buffer->data, data, samplesCount*ma_get_bytes_per_frame(sound.stream.buffer->converter.config.formatIn, sound.stream.buffer->converter.config.channelsIn));
}
- else TraceLog(LOG_ERROR, "UpdateSound() : Invalid sound - no audio buffer");
}
// Export wave data to file
@@ -902,13 +762,12 @@ void ExportWave(Wave wave, const char *fileName)
{
// Export raw sample data (without header)
// NOTE: It's up to the user to track wave parameters
- FILE *rawFile = fopen(fileName, "wb");
- success = fwrite(wave.data, wave.sampleCount*wave.channels*wave.sampleSize/8, 1, rawFile);
- fclose(rawFile);
+ SaveFileData(fileName, wave.data, wave.sampleCount*wave.channels*wave.sampleSize/8);
+ success = true;
}
- if (success) TraceLog(LOG_INFO, "Wave exported successfully: %s", fileName);
- else TraceLog(LOG_WARNING, "Wave could not be exported.");
+ if (success) TRACELOG(LOG_INFO, "FILEIO: [%s] Wave data exported successfully", fileName);
+ else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export wave data", fileName);
}
// Export wave sample data to code (.h)
@@ -943,10 +802,10 @@ void ExportWaveAsCode(Wave wave, const char *fileName)
#endif
fprintf(txtFile, "// Wave data information\n");
- fprintf(txtFile, "#define %s_SAMPLE_COUNT %i\n", varFileName, wave.sampleCount);
- fprintf(txtFile, "#define %s_SAMPLE_RATE %i\n", varFileName, wave.sampleRate);
- fprintf(txtFile, "#define %s_SAMPLE_SIZE %i\n", varFileName, wave.sampleSize);
- fprintf(txtFile, "#define %s_CHANNELS %i\n\n", varFileName, wave.channels);
+ fprintf(txtFile, "#define %s_SAMPLE_COUNT %u\n", varFileName, wave.sampleCount);
+ fprintf(txtFile, "#define %s_SAMPLE_RATE %u\n", varFileName, wave.sampleRate);
+ fprintf(txtFile, "#define %s_SAMPLE_SIZE %u\n", varFileName, wave.sampleSize);
+ fprintf(txtFile, "#define %s_CHANNELS %u\n\n", varFileName, wave.channels);
// Write byte data as hexadecimal text
fprintf(txtFile, "static unsigned char %s_DATA[%i] = { ", varFileName, dataSize);
@@ -973,13 +832,13 @@ void PlaySoundMulti(Sound sound)
// find the first non playing pool entry
for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
{
- if (audioBufferPoolChannels[i] > oldAge)
+ if (AUDIO.MultiChannel.channels[i] > oldAge)
{
- oldAge = audioBufferPoolChannels[i];
+ oldAge = AUDIO.MultiChannel.channels[i];
oldIndex = i;
}
- if (!IsAudioBufferPlaying(audioBufferPool[i]))
+ if (!IsAudioBufferPlaying(AUDIO.MultiChannel.pool[i]))
{
index = i;
break;
@@ -989,45 +848,44 @@ void PlaySoundMulti(Sound sound)
// If no none playing pool members can be index choose the oldest
if (index == -1)
{
- TraceLog(LOG_WARNING,"pool age %i ended a sound early no room in buffer pool", audioBufferPoolCounter);
+ TRACELOG(LOG_WARNING, "SOUND: Buffer pool is already full, count: %i", AUDIO.MultiChannel.poolCounter);
if (oldIndex == -1)
{
// Shouldn't be able to get here... but just in case something odd happens!
- TraceLog(LOG_ERROR,"sound buffer pool couldn't determine oldest buffer not playing sound");
-
+ TRACELOG(LOG_WARNING, "SOUND: Buffer pool could not determine oldest buffer not playing sound");
return;
}
index = oldIndex;
// Just in case...
- StopAudioBuffer(audioBufferPool[index]);
+ StopAudioBuffer(AUDIO.MultiChannel.pool[index]);
}
// Experimentally mutex lock doesn't seem to be needed this makes sense
- // as audioBufferPool[index] isn't playing and the only stuff we're copying
+ // as pool[index] isn't playing and the only stuff we're copying
// shouldn't be changing...
- audioBufferPoolChannels[index] = audioBufferPoolCounter;
- audioBufferPoolCounter++;
+ AUDIO.MultiChannel.channels[index] = AUDIO.MultiChannel.poolCounter;
+ AUDIO.MultiChannel.poolCounter++;
- audioBufferPool[index]->volume = sound.stream.buffer->volume;
- audioBufferPool[index]->pitch = sound.stream.buffer->pitch;
- audioBufferPool[index]->looping = sound.stream.buffer->looping;
- audioBufferPool[index]->usage = sound.stream.buffer->usage;
- audioBufferPool[index]->isSubBufferProcessed[0] = false;
- audioBufferPool[index]->isSubBufferProcessed[1] = false;
- audioBufferPool[index]->bufferSizeInFrames = sound.stream.buffer->bufferSizeInFrames;
- audioBufferPool[index]->buffer = sound.stream.buffer->buffer;
+ AUDIO.MultiChannel.pool[index]->volume = sound.stream.buffer->volume;
+ AUDIO.MultiChannel.pool[index]->pitch = sound.stream.buffer->pitch;
+ AUDIO.MultiChannel.pool[index]->looping = sound.stream.buffer->looping;
+ AUDIO.MultiChannel.pool[index]->usage = sound.stream.buffer->usage;
+ AUDIO.MultiChannel.pool[index]->isSubBufferProcessed[0] = false;
+ AUDIO.MultiChannel.pool[index]->isSubBufferProcessed[1] = false;
+ AUDIO.MultiChannel.pool[index]->sizeInFrames = sound.stream.buffer->sizeInFrames;
+ AUDIO.MultiChannel.pool[index]->data = sound.stream.buffer->data;
- PlayAudioBuffer(audioBufferPool[index]);
+ PlayAudioBuffer(AUDIO.MultiChannel.pool[index]);
}
// Stop any sound played with PlaySoundMulti()
void StopSoundMulti(void)
{
- for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++) StopAudioBuffer(audioBufferPool[i]);
+ for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++) StopAudioBuffer(AUDIO.MultiChannel.pool[i]);
}
// Get number of sounds playing in the multichannel buffer pool
@@ -1037,7 +895,7 @@ int GetSoundsPlaying(void)
for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
{
- if (IsAudioBufferPlaying(audioBufferPool[i])) counter++;
+ if (IsAudioBufferPlaying(AUDIO.MultiChannel.pool[i])) counter++;
}
return counter;
@@ -1087,19 +945,19 @@ void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels)
ma_uint32 frameCountIn = wave->sampleCount; // Is wave->sampleCount actually the frame count? That terminology needs to change, if so.
- ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, formatOut, channels, sampleRate, NULL, formatIn, wave->channels, wave->sampleRate, frameCountIn);
+ ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, 0, formatOut, channels, sampleRate, NULL, frameCountIn, formatIn, wave->channels, wave->sampleRate);
if (frameCount == 0)
{
- TraceLog(LOG_ERROR, "WaveFormat() : Failed to get frame count for format conversion.");
+ TRACELOG(LOG_WARNING, "WAVE: Failed to get frame count for format conversion");
return;
}
void *data = RL_MALLOC(frameCount*channels*(sampleSize/8));
- frameCount = (ma_uint32)ma_convert_frames(data, formatOut, channels, sampleRate, wave->data, formatIn, wave->channels, wave->sampleRate, frameCountIn);
+ frameCount = (ma_uint32)ma_convert_frames(data, frameCount, formatOut, channels, sampleRate, wave->data, frameCountIn, formatIn, wave->channels, wave->sampleRate);
if (frameCount == 0)
{
- TraceLog(LOG_ERROR, "WaveFormat() : Format conversion failed.");
+ TRACELOG(LOG_WARNING, "WAVE: Failed format conversion");
return;
}
@@ -1148,7 +1006,7 @@ void WaveCrop(Wave *wave, int initSample, int finalSample)
RL_FREE(wave->data);
wave->data = data;
}
- else TraceLog(LOG_WARNING, "Wave crop range out of bounds");
+ else TRACELOG(LOG_WARNING, "WAVE: Crop range out of bounds");
}
// Get samples data from wave as a floats array
@@ -1230,7 +1088,7 @@ Music LoadMusicStream(const char *fileName)
music.ctxType = MUSIC_AUDIO_MP3;
music.stream = InitAudioStream(ctxMp3->sampleRate, 32, ctxMp3->channels);
- music.sampleCount = drmp3_get_pcm_frame_count(ctxMp3)*ctxMp3->channels;
+ music.sampleCount = (unsigned int)drmp3_get_pcm_frame_count(ctxMp3)*ctxMp3->channels;
music.loopCount = 0; // Infinite loop by default
musicLoaded = true;
}
@@ -1243,14 +1101,14 @@ Music LoadMusicStream(const char *fileName)
int result = jar_xm_create_context_from_file(&ctxXm, 48000, fileName);
- if (result == 0) // XM context created successfully
+ if (result == 0) // XM AUDIO.System.context created successfully
{
music.ctxType = MUSIC_MODULE_XM;
jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops
// NOTE: Only stereo is supported for XM
music.stream = InitAudioStream(48000, 16, 2);
- music.sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm);
+ music.sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm)*2;
music.loopCount = 0; // Infinite loop by default
jar_xm_reset(ctxXm); // make sure we start at the beginning of the song
musicLoaded = true;
@@ -1263,7 +1121,6 @@ Music LoadMusicStream(const char *fileName)
else if (IsFileExtension(fileName, ".mod"))
{
jar_mod_context_t *ctxMod = RL_MALLOC(sizeof(jar_mod_context_t));
- music.ctxData = ctxMod;
jar_mod_init(ctxMod);
int result = jar_mod_load_file(ctxMod, fileName);
@@ -1274,9 +1131,11 @@ Music LoadMusicStream(const char *fileName)
// NOTE: Only stereo is supported for MOD
music.stream = InitAudioStream(48000, 16, 2);
- music.sampleCount = (unsigned int)jar_mod_max_samples(ctxMod);
+ music.sampleCount = (unsigned int)jar_mod_max_samples(ctxMod)*2;
music.loopCount = 0; // Infinite loop by default
musicLoaded = true;
+
+ music.ctxData = ctxMod;
}
}
#endif
@@ -1300,16 +1159,16 @@ Music LoadMusicStream(const char *fileName)
else if (music.ctxType == MUSIC_MODULE_MOD) { jar_mod_unload((jar_mod_context_t *)music.ctxData); RL_FREE(music.ctxData); }
#endif
- TraceLog(LOG_WARNING, "[%s] Music file could not be opened", fileName);
+ TRACELOG(LOG_WARNING, "FILEIO: [%s] Music file could not be opened", fileName);
}
else
{
// Show some music stream info
- TraceLog(LOG_INFO, "[%s] Music file successfully loaded:", fileName);
- TraceLog(LOG_INFO, " Total samples: %i", music.sampleCount);
- TraceLog(LOG_INFO, " Sample rate: %i Hz", music.stream.sampleRate);
- TraceLog(LOG_INFO, " Sample size: %i bits", music.stream.sampleSize);
- TraceLog(LOG_INFO, " Channels: %i (%s)", music.stream.channels, (music.stream.channels == 1)? "Mono" : (music.stream.channels == 2)? "Stereo" : "Multi");
+ TRACELOG(LOG_INFO, "FILEIO: [%s] Music file successfully loaded:", fileName);
+ TRACELOG(LOG_INFO, " > Total samples: %i", music.sampleCount);
+ TRACELOG(LOG_INFO, " > Sample rate: %i Hz", music.stream.sampleRate);
+ TRACELOG(LOG_INFO, " > Sample size: %i bits", music.stream.sampleSize);
+ TRACELOG(LOG_INFO, " > Channels: %i (%s)", music.stream.channels, (music.stream.channels == 1)? "Mono" : (music.stream.channels == 2)? "Stereo" : "Multi");
}
return music;
@@ -1341,20 +1200,16 @@ void UnloadMusicStream(Music music)
// Start music playing (open stream)
void PlayMusicStream(Music music)
{
- AudioBuffer *audioBuffer = music.stream.buffer;
-
- if (audioBuffer != NULL)
+ if (music.stream.buffer != NULL)
{
// For music streams, we need to make sure we maintain the frame cursor position
// This is a hack for this section of code in UpdateMusicStream()
// NOTE: In case window is minimized, music stream is stopped, just make sure to
// play again on window restore: if (IsMusicPlaying(music)) PlayMusicStream(music);
- ma_uint32 frameCursorPos = audioBuffer->frameCursorPos;
+ ma_uint32 frameCursorPos = music.stream.buffer->frameCursorPos;
PlayAudioStream(music.stream); // WARNING: This resets the cursor position.
- audioBuffer->frameCursorPos = frameCursorPos;
+ music.stream.buffer->frameCursorPos = frameCursorPos;
}
- else TraceLog(LOG_ERROR, "PlayMusicStream() : No audio buffer");
-
}
// Pause music playing
@@ -1374,7 +1229,6 @@ void StopMusicStream(Music music)
{
StopAudioStream(music.stream);
- // Restart music context
switch (music.ctxType)
{
#if defined(SUPPORT_FILEFORMAT_OGG)
@@ -1399,9 +1253,11 @@ void StopMusicStream(Music music)
// Update (re-fill) music buffers if data already processed
void UpdateMusicStream(Music music)
{
+ if (music.stream.buffer == NULL) return;
+
bool streamEnding = false;
- unsigned int subBufferSizeInFrames = music.stream.buffer->bufferSizeInFrames/2;
+ unsigned int subBufferSizeInFrames = music.stream.buffer->sizeInFrames/2;
// NOTE: Using dynamic allocation because it could require more than 16KB
void *pcm = RL_CALLOC(subBufferSizeInFrames*music.stream.channels*music.stream.sampleSize/8, 1);
@@ -1540,9 +1396,12 @@ float GetMusicTimePlayed(Music music)
{
float secondsPlayed = 0.0f;
- //ma_uint32 frameSizeInBytes = ma_get_bytes_per_sample(music.stream.buffer->dsp.formatConverterIn.config.formatIn)*music.stream.buffer->dsp.formatConverterIn.config.channels;
- unsigned int samplesPlayed = music.stream.buffer->totalFramesProcessed*music.stream.channels;
- secondsPlayed = (float)samplesPlayed/(music.stream.sampleRate*music.stream.channels);
+ if (music.stream.buffer != NULL)
+ {
+ //ma_uint32 frameSizeInBytes = ma_get_bytes_per_sample(music.stream.buffer->dsp.formatConverterIn.config.formatIn)*music.stream.buffer->dsp.formatConverterIn.config.channels;
+ unsigned int samplesPlayed = music.stream.buffer->totalFramesProcessed*music.stream.channels;
+ secondsPlayed = (float)samplesPlayed / (music.stream.sampleRate*music.stream.channels);
+ }
return secondsPlayed;
}
@@ -1559,19 +1418,20 @@ AudioStream InitAudioStream(unsigned int sampleRate, unsigned int sampleSize, un
ma_format formatIn = ((stream.sampleSize == 8)? ma_format_u8 : ((stream.sampleSize == 16)? ma_format_s16 : ma_format_f32));
// The size of a streaming buffer must be at least double the size of a period
- unsigned int periodSize = device.playback.internalBufferSizeInFrames/device.playback.internalPeriods;
- unsigned int subBufferSize = AUDIO_BUFFER_SIZE;
+ unsigned int periodSize = AUDIO.System.device.playback.internalPeriodSizeInFrames;
+ unsigned int subBufferSize = AUDIO.Buffer.defaultSize; // Default buffer size (audio stream)
if (subBufferSize < periodSize) subBufferSize = periodSize;
- stream.buffer = InitAudioBuffer(formatIn, stream.channels, stream.sampleRate, subBufferSize*2, AUDIO_BUFFER_USAGE_STREAM);
+ // Create a double audio buffer of defined size
+ stream.buffer = LoadAudioBuffer(formatIn, stream.channels, stream.sampleRate, subBufferSize*2, AUDIO_BUFFER_USAGE_STREAM);
if (stream.buffer != NULL)
{
stream.buffer->looping = true; // Always loop for streaming buffers
- TraceLog(LOG_INFO, "Audio stream loaded successfully (%i Hz, %i bit, %s)", stream.sampleRate, stream.sampleSize, (stream.channels == 1)? "Mono" : "Stereo");
+ TRACELOG(LOG_INFO, "STREAM: Initialized successfully (%i Hz, %i bit, %s)", stream.sampleRate, stream.sampleSize, (stream.channels == 1)? "Mono" : "Stereo");
}
- else TraceLog(LOG_ERROR, "InitAudioStream() : Failed to create audio buffer");
+ else TRACELOG(LOG_WARNING, "STREAM: Failed to load audio buffer, stream could not be created");
return stream;
}
@@ -1579,9 +1439,9 @@ AudioStream InitAudioStream(unsigned int sampleRate, unsigned int sampleSize, un
// Close audio stream and free memory
void CloseAudioStream(AudioStream stream)
{
- CloseAudioBuffer(stream.buffer);
+ UnloadAudioBuffer(stream.buffer);
- TraceLog(LOG_INFO, "Unloaded audio stream data");
+ TRACELOG(LOG_INFO, "STREAM: Unloaded audio stream data from RAM");
}
// Update audio stream buffers with data
@@ -1589,32 +1449,30 @@ void CloseAudioStream(AudioStream stream)
// NOTE 2: To unqueue a buffer it needs to be processed: IsAudioStreamProcessed()
void UpdateAudioStream(AudioStream stream, const void *data, int samplesCount)
{
- AudioBuffer *audioBuffer = stream.buffer;
-
- if (audioBuffer != NULL)
+ if (stream.buffer != NULL)
{
- if (audioBuffer->isSubBufferProcessed[0] || audioBuffer->isSubBufferProcessed[1])
+ if (stream.buffer->isSubBufferProcessed[0] || stream.buffer->isSubBufferProcessed[1])
{
ma_uint32 subBufferToUpdate = 0;
- if (audioBuffer->isSubBufferProcessed[0] && audioBuffer->isSubBufferProcessed[1])
+ if (stream.buffer->isSubBufferProcessed[0] && stream.buffer->isSubBufferProcessed[1])
{
// Both buffers are available for updating.
// Update the first one and make sure the cursor is moved back to the front.
subBufferToUpdate = 0;
- audioBuffer->frameCursorPos = 0;
+ stream.buffer->frameCursorPos = 0;
}
else
{
// Just update whichever sub-buffer is processed.
- subBufferToUpdate = (audioBuffer->isSubBufferProcessed[0])? 0 : 1;
+ subBufferToUpdate = (stream.buffer->isSubBufferProcessed[0])? 0 : 1;
}
- ma_uint32 subBufferSizeInFrames = audioBuffer->bufferSizeInFrames/2;
- unsigned char *subBuffer = audioBuffer->buffer + ((subBufferSizeInFrames*stream.channels*(stream.sampleSize/8))*subBufferToUpdate);
+ ma_uint32 subBufferSizeInFrames = stream.buffer->sizeInFrames/2;
+ unsigned char *subBuffer = stream.buffer->data + ((subBufferSizeInFrames*stream.channels*(stream.sampleSize/8))*subBufferToUpdate);
// TODO: Get total frames processed on this buffer... DOES NOT WORK.
- audioBuffer->totalFramesProcessed += subBufferSizeInFrames;
+ stream.buffer->totalFramesProcessed += subBufferSizeInFrames;
// Does this API expect a whole buffer to be updated in one go?
// Assuming so, but if not will need to change this logic.
@@ -1632,23 +1490,18 @@ void UpdateAudioStream(AudioStream stream, const void *data, int samplesCount)
if (leftoverFrameCount > 0) memset(subBuffer + bytesToWrite, 0, leftoverFrameCount*stream.channels*(stream.sampleSize/8));
- audioBuffer->isSubBufferProcessed[subBufferToUpdate] = false;
+ stream.buffer->isSubBufferProcessed[subBufferToUpdate] = false;
}
- else TraceLog(LOG_ERROR, "UpdateAudioStream() : Attempting to write too many frames to buffer");
+ else TRACELOG(LOG_WARNING, "STREAM: Attempting to write too many frames to buffer");
}
- else TraceLog(LOG_ERROR, "UpdateAudioStream() : Audio buffer not available for updating");
+ else TRACELOG(LOG_WARNING, "STREAM: Buffer not available for updating");
}
- else TraceLog(LOG_ERROR, "UpdateAudioStream() : No audio buffer");
}
// Check if any audio stream buffers requires refill
bool IsAudioStreamProcessed(AudioStream stream)
{
- if (stream.buffer == NULL)
- {
- TraceLog(LOG_ERROR, "IsAudioStreamProcessed() : No audio buffer");
- return false;
- }
+ if (stream.buffer == NULL) return false;
return (stream.buffer->isSubBufferProcessed[0] || stream.buffer->isSubBufferProcessed[1]);
}
@@ -1683,20 +1536,291 @@ void StopAudioStream(AudioStream stream)
StopAudioBuffer(stream.buffer);
}
+// Set volume for audio stream (1.0 is max level)
void SetAudioStreamVolume(AudioStream stream, float volume)
{
SetAudioBufferVolume(stream.buffer, volume);
}
+// Set pitch for audio stream (1.0 is base level)
void SetAudioStreamPitch(AudioStream stream, float pitch)
{
SetAudioBufferPitch(stream.buffer, pitch);
}
+// Default size for new audio streams
+void SetAudioStreamBufferSizeDefault(int size)
+{
+ AUDIO.Buffer.defaultSize = size;
+}
+
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
+// Log callback function
+static void OnLog(ma_context *pContext, ma_device *pDevice, ma_uint32 logLevel, const char *message)
+{
+ (void)pContext;
+ (void)pDevice;
+
+ TRACELOG(LOG_ERROR, "miniaudio: %s", message); // All log messages from miniaudio are errors
+}
+
+// Reads audio data from an AudioBuffer object in internal format.
+static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, void *framesOut, ma_uint32 frameCount)
+{
+ ma_uint32 subBufferSizeInFrames = (audioBuffer->sizeInFrames > 1)? audioBuffer->sizeInFrames/2 : audioBuffer->sizeInFrames;
+ ma_uint32 currentSubBufferIndex = audioBuffer->frameCursorPos/subBufferSizeInFrames;
+
+ if (currentSubBufferIndex > 1) return 0;
+
+ // Another thread can update the processed state of buffers so
+ // we just take a copy here to try and avoid potential synchronization problems
+ bool isSubBufferProcessed[2];
+ isSubBufferProcessed[0] = audioBuffer->isSubBufferProcessed[0];
+ isSubBufferProcessed[1] = audioBuffer->isSubBufferProcessed[1];
+
+ ma_uint32 frameSizeInBytes = ma_get_bytes_per_frame(audioBuffer->converter.config.formatIn, audioBuffer->converter.config.channelsIn);
+
+ // Fill out every frame until we find a buffer that's marked as processed. Then fill the remainder with 0
+ ma_uint32 framesRead = 0;
+ while (1)
+ {
+ // We break from this loop differently depending on the buffer's usage
+ // - For static buffers, we simply fill as much data as we can
+ // - For streaming buffers we only fill the halves of the buffer that are processed
+ // Unprocessed halves must keep their audio data in-tact
+ if (audioBuffer->usage == AUDIO_BUFFER_USAGE_STATIC)
+ {
+ if (framesRead >= frameCount) break;
+ }
+ else
+ {
+ if (isSubBufferProcessed[currentSubBufferIndex]) break;
+ }
+
+ ma_uint32 totalFramesRemaining = (frameCount - framesRead);
+ if (totalFramesRemaining == 0) break;
+
+ ma_uint32 framesRemainingInOutputBuffer;
+ if (audioBuffer->usage == AUDIO_BUFFER_USAGE_STATIC)
+ {
+ framesRemainingInOutputBuffer = audioBuffer->sizeInFrames - audioBuffer->frameCursorPos;
+ }
+ else
+ {
+ ma_uint32 firstFrameIndexOfThisSubBuffer = subBufferSizeInFrames*currentSubBufferIndex;
+ framesRemainingInOutputBuffer = subBufferSizeInFrames - (audioBuffer->frameCursorPos - firstFrameIndexOfThisSubBuffer);
+ }
+
+ ma_uint32 framesToRead = totalFramesRemaining;
+ if (framesToRead > framesRemainingInOutputBuffer) framesToRead = framesRemainingInOutputBuffer;
+
+ memcpy((unsigned char *)framesOut + (framesRead*frameSizeInBytes), audioBuffer->data + (audioBuffer->frameCursorPos*frameSizeInBytes), framesToRead*frameSizeInBytes);
+ audioBuffer->frameCursorPos = (audioBuffer->frameCursorPos + framesToRead)%audioBuffer->sizeInFrames;
+ framesRead += framesToRead;
+
+ // If we've read to the end of the buffer, mark it as processed
+ if (framesToRead == framesRemainingInOutputBuffer)
+ {
+ audioBuffer->isSubBufferProcessed[currentSubBufferIndex] = true;
+ isSubBufferProcessed[currentSubBufferIndex] = true;
+
+ currentSubBufferIndex = (currentSubBufferIndex + 1)%2;
+
+ // We need to break from this loop if we're not looping
+ if (!audioBuffer->looping)
+ {
+ StopAudioBuffer(audioBuffer);
+ break;
+ }
+ }
+ }
+
+ // Zero-fill excess
+ ma_uint32 totalFramesRemaining = (frameCount - framesRead);
+ if (totalFramesRemaining > 0)
+ {
+ memset((unsigned char *)framesOut + (framesRead*frameSizeInBytes), 0, totalFramesRemaining*frameSizeInBytes);
+
+ // For static buffers we can fill the remaining frames with silence for safety, but we don't want
+ // to report those frames as "read". The reason for this is that the caller uses the return value
+ // to know whether or not a non-looping sound has finished playback.
+ if (audioBuffer->usage != AUDIO_BUFFER_USAGE_STATIC) framesRead += totalFramesRemaining;
+ }
+
+ return framesRead;
+}
+
+// Reads audio data from an AudioBuffer object in device format. Returned data will be in a format appropriate for mixing.
+static ma_uint32 ReadAudioBufferFramesInMixingFormat(AudioBuffer *audioBuffer, float *framesOut, ma_uint32 frameCount)
+{
+ // What's going on here is that we're continuously converting data from the AudioBuffer's internal format to the mixing format, which
+ // should be defined by the output format of the data converter. We do this until frameCount frames have been output. The important
+ // detail to remember here is that we never, ever attempt to read more input data than is required for the specified number of output
+ // frames. This can be achieved with ma_data_converter_get_required_input_frame_count().
+ ma_uint8 inputBuffer[4096];
+ ma_uint32 inputBufferFrameCap = sizeof(inputBuffer) / ma_get_bytes_per_frame(audioBuffer->converter.config.formatIn, audioBuffer->converter.config.channelsIn);
+
+ ma_uint32 totalOutputFramesProcessed = 0;
+ while (totalOutputFramesProcessed < frameCount)
+ {
+ ma_uint64 outputFramesToProcessThisIteration = frameCount - totalOutputFramesProcessed;
+
+ ma_uint64 inputFramesToProcessThisIteration = ma_data_converter_get_required_input_frame_count(&audioBuffer->converter, outputFramesToProcessThisIteration);
+ if (inputFramesToProcessThisIteration > inputBufferFrameCap)
+ {
+ inputFramesToProcessThisIteration = inputBufferFrameCap;
+ }
+
+ float *runningFramesOut = framesOut + (totalOutputFramesProcessed * audioBuffer->converter.config.channelsOut);
+
+ /* At this point we can convert the data to our mixing format. */
+ ma_uint64 inputFramesProcessedThisIteration = ReadAudioBufferFramesInInternalFormat(audioBuffer, inputBuffer, (ma_uint32)inputFramesToProcessThisIteration); /* Safe cast. */
+ ma_uint64 outputFramesProcessedThisIteration = outputFramesToProcessThisIteration;
+ ma_data_converter_process_pcm_frames(&audioBuffer->converter, inputBuffer, &inputFramesProcessedThisIteration, runningFramesOut, &outputFramesProcessedThisIteration);
+
+ totalOutputFramesProcessed += (ma_uint32)outputFramesProcessedThisIteration; /* Safe cast. */
+
+ if (inputFramesProcessedThisIteration < inputFramesToProcessThisIteration)
+ {
+ break; /* Ran out of input data. */
+ }
+
+ /* This should never be hit, but will add it here for safety. Ensures we get out of the loop when no input nor output frames are processed. */
+ if (inputFramesProcessedThisIteration == 0 && outputFramesProcessedThisIteration == 0)
+ {
+ break;
+ }
+ }
+
+ return totalOutputFramesProcessed;
+}
+
+
+// Sending audio data to device callback function
+// NOTE: All the mixing takes place here
+static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount)
+{
+ (void)pDevice;
+
+ // Mixing is basically just an accumulation, we need to initialize the output buffer to 0
+ memset(pFramesOut, 0, frameCount*pDevice->playback.channels*ma_get_bytes_per_sample(pDevice->playback.format));
+
+ // Using a mutex here for thread-safety which makes things not real-time
+ // This is unlikely to be necessary for this project, but may want to consider how you might want to avoid this
+ ma_mutex_lock(&AUDIO.System.lock);
+ {
+ for (AudioBuffer *audioBuffer = AUDIO.Buffer.first; audioBuffer != NULL; audioBuffer = audioBuffer->next)
+ {
+ // Ignore stopped or paused sounds
+ if (!audioBuffer->playing || audioBuffer->paused) continue;
+
+ ma_uint32 framesRead = 0;
+
+ while (1)
+ {
+ if (framesRead >= frameCount) break;
+
+ // Just read as much data as we can from the stream
+ ma_uint32 framesToRead = (frameCount - framesRead);
+
+ while (framesToRead > 0)
+ {
+ float tempBuffer[1024]; // 512 frames for stereo
+
+ ma_uint32 framesToReadRightNow = framesToRead;
+ if (framesToReadRightNow > sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS)
+ {
+ framesToReadRightNow = sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS;
+ }
+
+ ma_uint32 framesJustRead = ReadAudioBufferFramesInMixingFormat(audioBuffer, tempBuffer, framesToReadRightNow);
+ if (framesJustRead > 0)
+ {
+ float *framesOut = (float *)pFramesOut + (framesRead*AUDIO.System.device.playback.channels);
+ float *framesIn = tempBuffer;
+
+ MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer->volume);
+
+ framesToRead -= framesJustRead;
+ framesRead += framesJustRead;
+ }
+
+ if (!audioBuffer->playing)
+ {
+ framesRead = frameCount;
+ break;
+ }
+
+ // If we weren't able to read all the frames we requested, break
+ if (framesJustRead < framesToReadRightNow)
+ {
+ if (!audioBuffer->looping)
+ {
+ StopAudioBuffer(audioBuffer);
+ break;
+ }
+ else
+ {
+ // Should never get here, but just for safety,
+ // move the cursor position back to the start and continue the loop
+ audioBuffer->frameCursorPos = 0;
+ continue;
+ }
+ }
+ }
+
+ // If for some reason we weren't able to read every frame we'll need to break from the loop
+ // Not doing this could theoretically put us into an infinite loop
+ if (framesToRead > 0) break;
+ }
+ }
+ }
+
+ ma_mutex_unlock(&AUDIO.System.lock);
+}
+
+// This is the main mixing function. Mixing is pretty simple in this project - it's just an accumulation.
+// NOTE: framesOut is both an input and an output. It will be initially filled with zeros outside of this function.
+static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, float localVolume)
+{
+ for (ma_uint32 iFrame = 0; iFrame < frameCount; ++iFrame)
+ {
+ for (ma_uint32 iChannel = 0; iChannel < AUDIO.System.device.playback.channels; ++iChannel)
+ {
+ float *frameOut = framesOut + (iFrame*AUDIO.System.device.playback.channels);
+ const float *frameIn = framesIn + (iFrame*AUDIO.System.device.playback.channels);
+
+ frameOut[iChannel] += (frameIn[iChannel]*localVolume);
+ }
+ }
+}
+
+// Initialise the multichannel buffer pool
+static void InitAudioBufferPool(void)
+{
+ // Dummy buffers
+ for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
+ {
+ AUDIO.MultiChannel.pool[i] = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO_DEVICE_SAMPLE_RATE, 0, AUDIO_BUFFER_USAGE_STATIC);
+ }
+
+ // TODO: Verification required for log
+ TRACELOG(LOG_INFO, "AUDIO: Multichannel pool size: %i", MAX_AUDIO_BUFFER_POOL_CHANNELS);
+}
+
+// Close the audio buffers pool
+static void CloseAudioBufferPool(void)
+{
+ for (int i = 0; i < MAX_AUDIO_BUFFER_POOL_CHANNELS; i++)
+ {
+ RL_FREE(AUDIO.MultiChannel.pool[i]->data);
+ RL_FREE(AUDIO.MultiChannel.pool[i]);
+ }
+}
+
#if defined(SUPPORT_FILEFORMAT_WAV)
// Load WAV file into Wave structure
static Wave LoadWAV(const char *fileName)
@@ -1724,18 +1848,18 @@ static Wave LoadWAV(const char *fileName)
int subChunkSize;
} WAVData;
- WAVRiffHeader wavRiffHeader;
- WAVFormat wavFormat;
- WAVData wavData;
+ WAVRiffHeader wavRiffHeader = { 0 };
+ WAVFormat wavFormat = { 0 };
+ WAVData wavData = { 0 };
Wave wave = { 0 };
- FILE *wavFile;
+ FILE *wavFile = NULL;
wavFile = fopen(fileName, "rb");
if (wavFile == NULL)
{
- TraceLog(LOG_WARNING, "[%s] WAV file could not be opened", fileName);
+ TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open WAV file", fileName);
wave.data = NULL;
}
else
@@ -1744,10 +1868,16 @@ static Wave LoadWAV(const char *fileName)
fread(&wavRiffHeader, sizeof(WAVRiffHeader), 1, wavFile);
// Check for RIFF and WAVE tags
- if (strncmp(wavRiffHeader.chunkID, "RIFF", 4) ||
- strncmp(wavRiffHeader.format, "WAVE", 4))
+ if ((wavRiffHeader.chunkID[0] != 'R') ||
+ (wavRiffHeader.chunkID[1] != 'I') ||
+ (wavRiffHeader.chunkID[2] != 'F') ||
+ (wavRiffHeader.chunkID[3] != 'F') ||
+ (wavRiffHeader.format[0] != 'W') ||
+ (wavRiffHeader.format[1] != 'A') ||
+ (wavRiffHeader.format[2] != 'V') ||
+ (wavRiffHeader.format[3] != 'E'))
{
- TraceLog(LOG_WARNING, "[%s] Invalid RIFF or WAVE Header", fileName);
+ TRACELOG(LOG_WARNING, "WAVE: [%s] RIFF or WAVE header are not valid", fileName);
}
else
{
@@ -1758,7 +1888,7 @@ static Wave LoadWAV(const char *fileName)
if ((wavFormat.subChunkID[0] != 'f') || (wavFormat.subChunkID[1] != 'm') ||
(wavFormat.subChunkID[2] != 't') || (wavFormat.subChunkID[3] != ' '))
{
- TraceLog(LOG_WARNING, "[%s] Invalid Wave format", fileName);
+ TRACELOG(LOG_WARNING, "WAVE: [%s] Wave format header is not valid", fileName);
}
else
{
@@ -1772,7 +1902,7 @@ static Wave LoadWAV(const char *fileName)
if ((wavData.subChunkID[0] != 'd') || (wavData.subChunkID[1] != 'a') ||
(wavData.subChunkID[2] != 't') || (wavData.subChunkID[3] != 'a'))
{
- TraceLog(LOG_WARNING, "[%s] Invalid data header", fileName);
+ TRACELOG(LOG_WARNING, "WAVE: [%s] Data header is not valid", fileName);
}
else
{
@@ -1790,7 +1920,7 @@ static Wave LoadWAV(const char *fileName)
// NOTE: Only support 8 bit, 16 bit and 32 bit sample sizes
if ((wave.sampleSize != 8) && (wave.sampleSize != 16) && (wave.sampleSize != 32))
{
- TraceLog(LOG_WARNING, "[%s] WAV sample size (%ibit) not supported, converted to 16bit", fileName, wave.sampleSize);
+ TRACELOG(LOG_WARNING, "WAVE: [%s] Sample size (%ibit) not supported, converted to 16bit", fileName, wave.sampleSize);
WaveFormat(&wave, wave.sampleRate, 16, wave.channels);
}
@@ -1798,13 +1928,13 @@ static Wave LoadWAV(const char *fileName)
if (wave.channels > 2)
{
WaveFormat(&wave, wave.sampleRate, wave.sampleSize, 2);
- TraceLog(LOG_WARNING, "[%s] WAV channels number (%i) not supported, converted to 2 channels", fileName, wave.channels);
+ TRACELOG(LOG_WARNING, "WAVE: [%s] Channels number (%i) not supported, converted to 2 channels", fileName, wave.channels);
}
// NOTE: subChunkSize comes in bytes, we need to translate it to number of samples
wave.sampleCount = (wavData.subChunkSize/(wave.sampleSize/8))/wave.channels;
- TraceLog(LOG_INFO, "[%s] WAV file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
+ TRACELOG(LOG_INFO, "WAVE: [%s] File loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
}
}
}
@@ -1846,7 +1976,7 @@ static int SaveWAV(Wave wave, const char *fileName)
FILE *wavFile = fopen(fileName, "wb");
- if (wavFile == NULL) TraceLog(LOG_WARNING, "[%s] WAV audio file could not be created", fileName);
+ if (wavFile == NULL) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open audio file", fileName);
else
{
RiffHeader riffHeader;
@@ -1905,7 +2035,7 @@ static Wave LoadOGG(const char *fileName)
stb_vorbis *oggFile = stb_vorbis_open_filename(fileName, NULL, NULL);
- if (oggFile == NULL) TraceLog(LOG_WARNING, "[%s] OGG file could not be opened", fileName);
+ if (oggFile == NULL) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open OGG file", fileName);
else
{
stb_vorbis_info info = stb_vorbis_get_info(oggFile);
@@ -1916,16 +2046,13 @@ static Wave LoadOGG(const char *fileName)
wave.sampleCount = (unsigned int)stb_vorbis_stream_length_in_samples(oggFile)*info.channels; // Independent by channel
float totalSeconds = stb_vorbis_stream_length_in_seconds(oggFile);
- if (totalSeconds > 10) TraceLog(LOG_WARNING, "[%s] Ogg audio length is larger than 10 seconds (%f), that's a big file in memory, consider music streaming", fileName, totalSeconds);
+ if (totalSeconds > 10) TRACELOG(LOG_WARNING, "WAVE: [%s] Ogg audio length larger than 10 seconds (%f), that's a big file in memory, consider music streaming", fileName, totalSeconds);
wave.data = (short *)RL_MALLOC(wave.sampleCount*wave.channels*sizeof(short));
// NOTE: Returns the number of samples to process (be careful! we ask for number of shorts!)
- int numSamplesOgg = stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, (short *)wave.data, wave.sampleCount*wave.channels);
-
- TraceLog(LOG_DEBUG, "[%s] Samples obtained: %i", fileName, numSamplesOgg);
-
- TraceLog(LOG_INFO, "[%s] OGG file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
+ stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, (short *)wave.data, wave.sampleCount*wave.channels);
+ TRACELOG(LOG_INFO, "WAVE: [%s] OGG file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
stb_vorbis_close(oggFile);
}
@@ -1939,21 +2066,24 @@ static Wave LoadOGG(const char *fileName)
// NOTE: Using dr_flac library
static Wave LoadFLAC(const char *fileName)
{
- Wave wave;
+ Wave wave = { 0 };
// Decode an entire FLAC file in one go
- uint64_t totalSampleCount;
+ unsigned long long int totalSampleCount = 0;
wave.data = drflac_open_file_and_read_pcm_frames_s16(fileName, &wave.channels, &wave.sampleRate, &totalSampleCount);
- wave.sampleCount = (unsigned int)totalSampleCount;
- wave.sampleSize = 16;
-
- // NOTE: Only support up to 2 channels (mono, stereo)
- if (wave.channels > 2) TraceLog(LOG_WARNING, "[%s] FLAC channels number (%i) not supported", fileName, wave.channels);
+ if (wave.data == NULL) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to load FLAC data", fileName);
+ else
+ {
+ wave.sampleCount = (unsigned int)totalSampleCount;
+ wave.sampleSize = 16;
- if (wave.data == NULL) TraceLog(LOG_WARNING, "[%s] FLAC data could not be loaded", fileName);
- else TraceLog(LOG_INFO, "[%s] FLAC file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
+ // NOTE: Only support up to 2 channels (mono, stereo)
+ if (wave.channels > 2) TRACELOG(LOG_WARNING, "WAVE: [%s] FLAC channels number (%i) not supported", fileName, wave.channels);
+ TRACELOG(LOG_INFO, "WAVE: [%s] FLAC file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
+ }
+
return wave;
}
#endif
@@ -1966,21 +2096,24 @@ static Wave LoadMP3(const char *fileName)
Wave wave = { 0 };
// Decode an entire MP3 file in one go
- uint64_t totalFrameCount = 0;
+ unsigned long long int totalFrameCount = 0;
drmp3_config config = { 0 };
wave.data = drmp3_open_file_and_read_f32(fileName, &config, &totalFrameCount);
- wave.channels = config.outputChannels;
- wave.sampleRate = config.outputSampleRate;
- wave.sampleCount = (int)totalFrameCount*wave.channels;
- wave.sampleSize = 32;
-
- // NOTE: Only support up to 2 channels (mono, stereo)
- if (wave.channels > 2) TraceLog(LOG_WARNING, "[%s] MP3 channels number (%i) not supported", fileName, wave.channels);
+ if (wave.data == NULL) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to load MP3 data", fileName);
+ else
+ {
+ wave.channels = config.outputChannels;
+ wave.sampleRate = config.outputSampleRate;
+ wave.sampleCount = (int)totalFrameCount*wave.channels;
+ wave.sampleSize = 32;
- if (wave.data == NULL) TraceLog(LOG_WARNING, "[%s] MP3 data could not be loaded", fileName);
- else TraceLog(LOG_INFO, "[%s] MP3 file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
+ // NOTE: Only support up to 2 channels (mono, stereo)
+ if (wave.channels > 2) TRACELOG(LOG_WARNING, "WAVE: [%s] MP3 channels number (%i) not supported", fileName, wave.channels);
+ TRACELOG(LOG_INFO, "WAVE: [%s] MP3 file loaded successfully (%i Hz, %i bit, %s)", fileName, wave.sampleRate, wave.sampleSize, (wave.channels == 1)? "Mono" : "Stereo");
+ }
+
return wave;
}
#endif
@@ -2000,29 +2133,6 @@ bool IsFileExtension(const char *fileName, const char *ext)
return result;
}
-
-// Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
-void TraceLog(int msgType, const char *text, ...)
-{
- va_list args;
- va_start(args, text);
-
- switch (msgType)
- {
- case LOG_INFO: fprintf(stdout, "INFO: "); break;
- case LOG_ERROR: fprintf(stdout, "ERROR: "); break;
- case LOG_WARNING: fprintf(stdout, "WARNING: "); break;
- case LOG_DEBUG: fprintf(stdout, "DEBUG: "); break;
- default: break;
- }
-
- vfprintf(stdout, text, args);
- fprintf(stdout, "\n");
-
- va_end(args);
-
- if (msgType == LOG_ERROR) exit(1);
-}
#endif
#undef AudioBuffer