/*
Audio playback and capture library. Choice of public domain or MIT-0. See license statements at the end of this file.
-miniaudio - v0.11.24 - TBD
+miniaudio - v0.11.24 - 2026-01-17
David Reid - mackron@gmail.com
/* Platform/backend detection. */
-#if defined(_WIN32) || defined(__COSMOPOLITAN__)
+#if defined(_WIN32)
#define MA_WIN32
#if defined(MA_FORCE_UWP) || (defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_PC_APP) && WINAPI_FAMILY == WINAPI_FAMILY_PC_APP) || (defined(WINAPI_FAMILY_PHONE_APP) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)))
#define MA_WIN32_UWP
MA_CHANNEL_AUX_29 = 49,
MA_CHANNEL_AUX_30 = 50,
MA_CHANNEL_AUX_31 = 51,
+
+ /* Count. */
+ MA_CHANNEL_POSITION_COUNT,
+
+ /* Aliases. */
MA_CHANNEL_LEFT = MA_CHANNEL_FRONT_LEFT,
MA_CHANNEL_RIGHT = MA_CHANNEL_FRONT_RIGHT,
- MA_CHANNEL_POSITION_COUNT = (MA_CHANNEL_AUX_31 + 1)
} _ma_channel_position; /* Do not use `_ma_channel_position` directly. Use `ma_channel` instead. */
typedef enum
#if defined(MA_WIN32_DESKTOP) /* DirectSound and WinMM backends are only supported on desktops. */
#define MA_SUPPORT_DSOUND
#define MA_SUPPORT_WINMM
-
- /* Don't enable JACK here if compiling with Cosmopolitan. It'll be enabled in the Linux section below. */
- #if !defined(__COSMOPOLITAN__)
- #define MA_SUPPORT_JACK /* JACK is technically supported on Windows, but I don't know how many people use it in practice... */
- #endif
+ #define MA_SUPPORT_JACK /* JACK is technically supported on Windows, but I don't know how many people use it in practice... */
#endif
#endif
#if defined(MA_UNIX) && !defined(MA_ORBIS) && !defined(MA_PROSPERO)
#if defined(MA_LINUX)
- #if !defined(MA_ANDROID) && !defined(__COSMOPOLITAN__) /* ALSA is not supported on Android. */
+ #if !defined(MA_ANDROID) && !defined(MA_EMSCRIPTEN) /* ALSA is not supported on Android. */
#define MA_SUPPORT_ALSA
#endif
#endif
ma_decoding_backend_vtable** ppCustomDecodingBackendVTables;
ma_uint32 customDecodingBackendCount;
void* pCustomDecodingBackendUserData;
+ ma_resampler_config resampling;
} ma_resource_manager_config;
MA_API ma_resource_manager_config ma_resource_manager_config_init(void);
MA_API ma_uint32 ma_node_graph_get_channels(const ma_node_graph* pNodeGraph);
MA_API ma_uint64 ma_node_graph_get_time(const ma_node_graph* pNodeGraph);
MA_API ma_result ma_node_graph_set_time(ma_node_graph* pNodeGraph, ma_uint64 globalTime);
+MA_API ma_uint32 ma_node_graph_get_processing_size_in_frames(const ma_node_graph* pNodeGraph);
ma_bool8 isPitchDisabled; /* Pitching can be explicitly disabled with MA_SOUND_FLAG_NO_PITCH to optimize processing. */
ma_bool8 isSpatializationDisabled; /* Spatialization can be explicitly disabled with MA_SOUND_FLAG_NO_SPATIALIZATION. */
ma_uint8 pinnedListenerIndex; /* The index of the listener this node should always use for spatialization. If set to MA_LISTENER_INDEX_CLOSEST the engine will use the closest listener. */
+ ma_resampler_config resampling;
} ma_engine_node_config;
MA_API ma_engine_node_config ma_engine_node_config_init(ma_engine* pEngine, ma_engine_node_type type, ma_uint32 flags);
ma_uint32 volumeSmoothTimeInPCMFrames;
ma_mono_expansion_mode monoExpansionMode;
ma_fader fader;
- ma_linear_resampler resampler; /* For pitch shift. */
+ ma_resampler resampler; /* For pitch shift. */
ma_spatializer spatializer;
ma_panner panner;
ma_gainer volumeGainer; /* This will only be used if volumeSmoothTimeInPCMFrames is > 0. */
ma_uint64 loopPointEndInPCMFrames;
ma_sound_end_proc endCallback; /* Fired when the sound reaches the end. Will be fired from the audio thread. Do not restart, uninitialize or otherwise change the state of the sound from here. Instead fire an event or set a variable to indicate to a different thread to change the start of the sound. Will not be fired in response to a scheduled stop with ma_sound_set_stop_time_*(). */
void* pEndCallbackUserData;
+ ma_resampler_config pitchResampling;
#ifndef MA_NO_RESOURCE_MANAGER
ma_resource_manager_pipeline_notifications initNotifications;
#endif
MA_ATOMIC(4, ma_bool32) atEnd;
ma_sound_end_proc endCallback;
void* pEndCallbackUserData;
- ma_bool8 ownsDataSource;
+ float* pProcessingCache; /* Will be null if pDataSource is null. */
+ ma_uint32 processingCacheFramesRemaining;
+ ma_uint32 processingCacheCap;
+ ma_bool8 ownsDataSource;
/*
We're declaring a resource manager data source object here to save us a malloc when loading a
ma_vfs* pResourceManagerVFS; /* A pointer to a pre-allocated VFS object to use with the resource manager. This is ignored if pResourceManager is not NULL. */
ma_engine_process_proc onProcess; /* Fired at the end of each call to ma_engine_read_pcm_frames(). For engine's that manage their own internal device (the default configuration), this will be fired from the audio thread, and you do not need to call ma_engine_read_pcm_frames() manually in order to trigger this. */
void* pProcessUserData; /* User data that's passed into onProcess. */
+ ma_resampler_config resourceManagerResampling; /* The resampling config to use with the resource manager. */
+ ma_resampler_config pitchResampling; /* The resampling config for the pitch and Doppler effects. You will typically want this to be a fast resampler. For high quality stuff, it's recommended that you pre-resample. */
} ma_engine_config;
MA_API ma_engine_config ma_engine_config_init(void);
ma_mono_expansion_mode monoExpansionMode;
ma_engine_process_proc onProcess;
void* pProcessUserData;
+ ma_resampler_config pitchResamplingConfig;
};
MA_API ma_result ma_engine_init(const ma_engine_config* pConfig, ma_engine* pEngine);
MA_API ma_data_source* ma_sound_get_data_source(const ma_sound* pSound);
MA_API ma_result ma_sound_start(ma_sound* pSound);
MA_API ma_result ma_sound_stop(ma_sound* pSound);
-MA_API ma_result ma_sound_stop_with_fade_in_pcm_frames(ma_sound* pSound, ma_uint64 fadeLengthInFrames); /* Will overwrite any scheduled stop and fade. */
-MA_API ma_result ma_sound_stop_with_fade_in_milliseconds(ma_sound* pSound, ma_uint64 fadeLengthInFrames); /* Will overwrite any scheduled stop and fade. */
+MA_API ma_result ma_sound_stop_with_fade_in_pcm_frames(ma_sound* pSound, ma_uint64 fadeLengthInFrames); /* Will overwrite any scheduled stop and fade. If you want to restart the sound, first reset it with `ma_sound_reset_stop_time_and_fade()`. There are plans to make this less awkward in the future. */
+MA_API ma_result ma_sound_stop_with_fade_in_milliseconds(ma_sound* pSound, ma_uint64 fadeLengthInFrames); /* Will overwrite any scheduled stop and fade. If you want to restart the sound, first reset it with `ma_sound_reset_stop_time_and_fade()`. There are plans to make this less awkward in the future. */
+MA_API void ma_sound_reset_start_time(ma_sound* pSound);
+MA_API void ma_sound_reset_stop_time(ma_sound* pSound);
+MA_API void ma_sound_reset_fade(ma_sound* pSound);
+MA_API void ma_sound_reset_stop_time_and_fade(ma_sound* pSound); /* Resets fades and scheduled stop time. Does not seek back to the start. */
MA_API void ma_sound_set_volume(ma_sound* pSound, float volume);
MA_API float ma_sound_get_volume(const ma_sound* pSound);
MA_API void ma_sound_set_pan(ma_sound* pSound, float pan);
#endif
/* Intrinsics Support */
-#if (defined(MA_X64) || defined(MA_X86)) && !defined(__COSMOPOLITAN__)
+#if defined(MA_X64) || defined(MA_X86)
#if defined(_MSC_VER) && !defined(__clang__)
/* MSVC. */
#if _MSC_VER >= 1400 && !defined(MA_NO_SSE2) /* 2005 */
}
#elif defined(MA_X86) || defined(MA_X64)
{
- #if defined(MA_SUPPORT_SSE2) && defined(__SSE2__) && !(defined(__TINYC__) || defined(__WATCOMC__) || defined(__COSMOPOLITAN__)) /* <-- Add compilers that lack support for _mm_getcsr() and _mm_setcsr() to this list. */
+ #if defined(MA_SUPPORT_SSE2) && defined(__SSE2__) && !(defined(__TINYC__) || defined(__WATCOMC__)) /* <-- Add compilers that lack support for _mm_getcsr() and _mm_setcsr() to this list. */
{
prevState = _mm_getcsr();
_mm_setcsr(prevState | MA_MM_DENORMALS_ZERO_MASK | MA_MM_FLUSH_ZERO_MASK);
}
#elif defined(MA_X86) || defined(MA_X64)
{
- #if defined(MA_SUPPORT_SSE2) && defined(__SSE2__) && !(defined(__TINYC__) || defined(__WATCOMC__) || defined(__COSMOPOLITAN__)) /* <-- Add compilers that lack support for _mm_getcsr() and _mm_setcsr() to this list. */
+ #if defined(MA_SUPPORT_SSE2) && defined(__SSE2__) && !(defined(__TINYC__) || defined(__WATCOMC__)) /* <-- Add compilers that lack support for _mm_getcsr() and _mm_setcsr() to this list. */
{
_mm_setcsr(prevState);
}
int priorityStep = (priorityMax - priorityMin) / 7; /* 7 = number of priorities supported by miniaudio. */
struct sched_param sched;
- if (pthread_attr_getschedparam(&attr, &sched) == 0) {
+ if (priorityMin != -1 && priorityMax != -1 && pthread_attr_getschedparam(&attr, &sched) == 0) {
if (priority == ma_thread_priority_idle) {
sched.sched_priority = priorityMin;
} else if (priority == ma_thread_priority_realtime) {
struct timespec newTime;
clock_gettime(MA_CLOCK_ID, &newTime);
- pTimer->counter = (newTime.tv_sec * 1000000000) + newTime.tv_nsec;
+ pTimer->counter = ((ma_int64)newTime.tv_sec * 1000000000) + newTime.tv_nsec;
}
static MA_INLINE double ma_timer_get_time_in_seconds(ma_timer* pTimer)
struct timespec newTime;
clock_gettime(MA_CLOCK_ID, &newTime);
- newTimeCounter = (newTime.tv_sec * 1000000000) + newTime.tv_nsec;
+ newTimeCounter = ((ma_uint64)newTime.tv_sec * 1000000000) + newTime.tv_nsec;
oldTimeCounter = pTimer->counter;
return (newTimeCounter - oldTimeCounter) / 1000000000.0;
struct timeval newTime;
gettimeofday(&newTime, NULL);
- pTimer->counter = (newTime.tv_sec * 1000000) + newTime.tv_usec;
+ pTimer->counter = ((ma_int64)newTime.tv_sec * 1000000) + newTime.tv_usec;
}
static MA_INLINE double ma_timer_get_time_in_seconds(ma_timer* pTimer)
struct timeval newTime;
gettimeofday(&newTime, NULL);
- newTimeCounter = (newTime.tv_sec * 1000000) + newTime.tv_usec;
+ newTimeCounter = ((ma_uint64)newTime.tv_sec * 1000000) + newTime.tv_usec;
oldTimeCounter = pTimer->counter;
return (newTimeCounter - oldTimeCounter) / 1000000.0;
result = ma_result_from_pulse(((ma_pa_context_connect_proc)pContext->pulse.pa_context_connect)((ma_pa_context*)pPulseContext, pServerName, (tryAutoSpawn) ? MA_PA_CONTEXT_NOFLAGS : MA_PA_CONTEXT_NOAUTOSPAWN, NULL));
if (result != MA_SUCCESS) {
ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_ERROR, "[PulseAudio] Failed to connect PulseAudio context.");
+ ((ma_pa_context_unref_proc)pContext->pulse.pa_context_unref)((ma_pa_context*)(pPulseContext));
((ma_pa_mainloop_free_proc)pContext->pulse.pa_mainloop_free)((ma_pa_mainloop*)(pMainLoop));
return result;
}
result = ma_wait_for_pa_context_to_connect__pulse(pContext, pMainLoop, pPulseContext);
if (result != MA_SUCCESS) {
ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_ERROR, "[PulseAudio] Waiting for connection failed.");
+ ((ma_pa_context_unref_proc)pContext->pulse.pa_context_unref)((ma_pa_context*)(pPulseContext));
((ma_pa_mainloop_free_proc)pContext->pulse.pa_mainloop_free)((ma_pa_mainloop*)(pMainLoop));
return result;
}
frameCount = pDevice->capture.internalPeriodSizeInFrames;
}
+ /*
+ If this is called by the device has not yet been started we need to return early, making sure we output silence to
+ the output buffer.
+ */
if (ma_device_get_state(pDevice) != ma_device_state_started) {
- /* Fill the output buffer with zero to avoid a noise sound */
for (int i = 0; i < outputCount; i += 1) {
MA_ZERO_MEMORY(pOutputs[i].data, pOutputs[i].numberOfChannels * frameCount * sizeof(float));
}
if (outputCount > 0) {
/* If it's a capture-only device, we'll need to output silence. */
if (pDevice->type == ma_device_type_capture) {
- MA_ZERO_MEMORY(pOutputs[0].data, frameCount * pDevice->playback.internalChannels * sizeof(float));
+ for (int i = 0; i < outputCount; i += 1) {
+ MA_ZERO_MEMORY(pOutputs[i].data, pOutputs[i].numberOfChannels * frameCount * sizeof(float));
+ }
} else {
ma_device_process_pcm_frames_playback__webaudio(pDevice, frameCount, pDevice->webaudio.pIntermediaryBuffer);
pOutputs[0].data[frameCount*iChannel + iFrame] = pDevice->webaudio.pIntermediaryBuffer[iFrame*pDevice->playback.internalChannels + iChannel];
}
}
+
+ /*
+ Just above we output data to the first output buffer. Here we just make sure we're putting silence into any
+ remaining output buffers.
+ */
+ for (int i = 1; i < outputCount; i += 1) { /* <-- Note that the counter starts at 1 instead of 0. */
+ MA_ZERO_MEMORY(pOutputs[i].data, pOutputs[i].numberOfChannels * frameCount * sizeof(float));
+ }
}
}
a += d;
}
}
+
+ pFramesOut = ma_offset_ptr(pFramesOut, interpolatedFrameCount * sizeof(float));
+ pFramesIn = ma_offset_ptr(pFramesIn, interpolatedFrameCount * sizeof(float));
}
+ frameCount -= interpolatedFrameCount;
+
/* Make sure the timer is updated. */
pGainer->t = (ma_uint32)ma_min(pGainer->t + interpolatedFrameCount, pGainer->config.smoothTimeInFrames);
-
- /* Adjust our arguments so the next part can work normally. */
- frameCount -= interpolatedFrameCount;
- pFramesOut = ma_offset_ptr(pFramesOut, interpolatedFrameCount * sizeof(float));
- pFramesIn = ma_offset_ptr(pFramesIn, interpolatedFrameCount * sizeof(float));
}
/* All we need to do here is apply the new gains using an optimized path. */
MA_API ma_result ma_spatializer_process_pcm_frames(ma_spatializer* pSpatializer, ma_spatializer_listener* pListener, void* pFramesOut, const void* pFramesIn, ma_uint64 frameCount)
{
- ma_channel* pChannelMapIn = pSpatializer->pChannelMapIn;
- ma_channel* pChannelMapOut = pListener->config.pChannelMapOut;
+ ma_channel* pChannelMapIn;
+ ma_channel* pChannelMapOut;
- if (pSpatializer == NULL) {
+ if (pSpatializer == NULL || pListener == NULL) {
return MA_INVALID_ARGS;
}
+ pChannelMapIn = pSpatializer->pChannelMapIn;
+ pChannelMapOut = pListener->config.pChannelMapOut;
+
/* If we're not spatializing we need to run an optimized path. */
if (ma_atomic_load_i32(&pSpatializer->attenuationModel) == ma_attenuation_model_none) {
if (ma_spatializer_listener_is_enabled(pListener)) {
We'll need the listener velocity for doppler pitch calculations. The speed of sound is
defined by the listener, so we'll grab that here too.
*/
- if (pListener != NULL) {
- listenerVel = ma_spatializer_listener_get_velocity(pListener);
- speedOfSound = pListener->config.speedOfSound;
- } else {
- listenerVel = ma_vec3f_init_3f(0, 0, 0);
- speedOfSound = MA_DEFAULT_SPEED_OF_SOUND;
- }
+ listenerVel = ma_spatializer_listener_get_velocity(pListener);
+ speedOfSound = pListener->config.speedOfSound;
- if (pListener == NULL || ma_spatializer_get_positioning(pSpatializer) == ma_positioning_relative) {
- /* There's no listener or we're using relative positioning. */
+ if (ma_spatializer_get_positioning(pSpatializer) == ma_positioning_relative) {
relativePos = ma_spatializer_get_position(pSpatializer);
relativeDir = ma_spatializer_get_direction(pSpatializer);
} else {
/*
- We've found a listener and we're using absolute positioning. We need to transform the
- sound's position and direction so that it's relative to listener. Later on we'll use
- this for determining the factors to apply to each channel to apply the panning effect.
+ We're using absolute positioning. We need to transform the sound's position and
+ direction so that it's relative to listener. Later on we'll use this for determining
+ the factors to apply to each channel to apply the panning effect.
*/
ma_spatializer_get_relative_position_and_direction(pSpatializer, pListener, &relativePos, &relativeDir);
}
return MA_FALSE;
}
- if (channelPosition >= MA_CHANNEL_AUX_0 && channelPosition <= MA_CHANNEL_AUX_31) {
+ if (channelPosition >= MA_CHANNEL_AUX_0) {
return MA_FALSE;
}
if (result == MA_NOT_IMPLEMENTED) {
/* Not implemented. Fall back to seek/tell/seek. */
- ma_result result;
ma_int64 cursor;
ma_int64 sizeInBytes;
**************************************************************************************************************************************************************/
#if !defined(MA_NO_WAV) && (!defined(MA_NO_DECODING) || !defined(MA_NO_ENCODING))
+#define MA_HAS_WAV
+
/* dr_wav_h begin */
#ifndef ma_dr_wav_h
#define ma_dr_wav_h
#define MA_DR_WAV_XSTRINGIFY(x) MA_DR_WAV_STRINGIFY(x)
#define MA_DR_WAV_VERSION_MAJOR 0
#define MA_DR_WAV_VERSION_MINOR 14
-#define MA_DR_WAV_VERSION_REVISION 1
+#define MA_DR_WAV_VERSION_REVISION 4
#define MA_DR_WAV_VERSION_STRING MA_DR_WAV_XSTRINGIFY(MA_DR_WAV_VERSION_MAJOR) "." MA_DR_WAV_XSTRINGIFY(MA_DR_WAV_VERSION_MINOR) "." MA_DR_WAV_XSTRINGIFY(MA_DR_WAV_VERSION_REVISION)
#include <stddef.h>
#define MA_DR_WAVE_FORMAT_PCM 0x1
#endif /* MA_NO_WAV */
#if !defined(MA_NO_FLAC) && !defined(MA_NO_DECODING)
+#define MA_HAS_FLAC
+
/* dr_flac_h begin */
#ifndef ma_dr_flac_h
#define ma_dr_flac_h
#define MA_DR_FLAC_XSTRINGIFY(x) MA_DR_FLAC_STRINGIFY(x)
#define MA_DR_FLAC_VERSION_MAJOR 0
#define MA_DR_FLAC_VERSION_MINOR 13
-#define MA_DR_FLAC_VERSION_REVISION 2
+#define MA_DR_FLAC_VERSION_REVISION 3
#define MA_DR_FLAC_VERSION_STRING MA_DR_FLAC_XSTRINGIFY(MA_DR_FLAC_VERSION_MAJOR) "." MA_DR_FLAC_XSTRINGIFY(MA_DR_FLAC_VERSION_MINOR) "." MA_DR_FLAC_XSTRINGIFY(MA_DR_FLAC_VERSION_REVISION)
#include <stddef.h>
#if defined(_MSC_VER) && _MSC_VER >= 1700
#endif /* MA_NO_FLAC */
#if !defined(MA_NO_MP3) && !defined(MA_NO_DECODING)
+#define MA_HAS_MP3
+
#ifndef MA_DR_MP3_NO_SIMD
#if (defined(MA_NO_NEON) && defined(MA_ARM)) || (defined(MA_NO_SSE2) && (defined(MA_X86) || defined(MA_X64)))
#define MA_DR_MP3_NO_SIMD
#define MA_DR_MP3_XSTRINGIFY(x) MA_DR_MP3_STRINGIFY(x)
#define MA_DR_MP3_VERSION_MAJOR 0
#define MA_DR_MP3_VERSION_MINOR 7
-#define MA_DR_MP3_VERSION_REVISION 2
+#define MA_DR_MP3_VERSION_REVISION 3
#define MA_DR_MP3_VERSION_STRING MA_DR_MP3_XSTRINGIFY(MA_DR_MP3_VERSION_MAJOR) "." MA_DR_MP3_XSTRINGIFY(MA_DR_MP3_VERSION_MINOR) "." MA_DR_MP3_XSTRINGIFY(MA_DR_MP3_VERSION_REVISION)
#include <stddef.h>
#define MA_DR_MP3_MAX_PCM_FRAMES_PER_MP3_FRAME 1152
/* WAV */
#ifdef ma_dr_wav_h
-#define MA_HAS_WAV
typedef struct
{
/* FLAC */
#ifdef ma_dr_flac_h
-#define MA_HAS_FLAC
typedef struct
{
/* MP3 */
#ifdef ma_dr_mp3_h
-#define MA_HAS_MP3
typedef struct
{
We use trial and error to open a decoder. We prioritize custom decoders so that if they
implement the same encoding format they take priority over the built-in decoders.
*/
+ result = ma_decoder_init_custom__internal(pConfig, pDecoder);
if (result != MA_SUCCESS) {
- result = ma_decoder_init_custom__internal(pConfig, pDecoder);
- if (result != MA_SUCCESS) {
- onSeek(pDecoder, 0, ma_seek_origin_start);
- }
+ onSeek(pDecoder, 0, ma_seek_origin_start);
}
/*
/* Initialization was successful. Finish up. */
result = ma_decoder__postinit(&config, pDecoder);
if (result != MA_SUCCESS) {
- /*
- The backend was initialized successfully, but for some reason post-initialization failed. This is most likely
- due to an out of memory error. We're going to abort with an error here and not try to recover.
- */
- if (pDecoder->pBackendVTable != NULL && pDecoder->pBackendVTable->onUninit != NULL) {
- pDecoder->pBackendVTable->onUninit(pDecoder->pBackendUserData, &pDecoder->pBackend, &pDecoder->allocationCallbacks);
- }
-
return result;
}
} else {
We use trial and error to open a decoder. We prioritize custom decoders so that if they
implement the same encoding format they take priority over the built-in decoders.
*/
+ result = ma_decoder_init_custom__internal(&config, pDecoder);
if (result != MA_SUCCESS) {
- result = ma_decoder_init_custom__internal(&config, pDecoder);
- if (result != MA_SUCCESS) {
- ma_decoder__on_seek_vfs(pDecoder, 0, ma_seek_origin_start);
- }
+ ma_decoder__on_seek_vfs(pDecoder, 0, ma_seek_origin_start);
}
/*
We use trial and error to open a decoder. We prioritize custom decoders so that if they
implement the same encoding format they take priority over the built-in decoders.
*/
+ result = ma_decoder_init_custom__internal(&config, pDecoder);
if (result != MA_SUCCESS) {
- result = ma_decoder_init_custom__internal(&config, pDecoder);
- if (result != MA_SUCCESS) {
- ma_decoder__on_seek_vfs(pDecoder, 0, ma_seek_origin_start);
- }
+ ma_decoder__on_seek_vfs(pDecoder, 0, ma_seek_origin_start);
}
/*
/* Initialization was successful. Finish up. */
result = ma_decoder__postinit(&config, pDecoder);
if (result != MA_SUCCESS) {
- /*
- The backend was initialized successfully, but for some reason post-initialization failed. This is most likely
- due to an out of memory error. We're going to abort with an error here and not try to recover.
- */
- if (pDecoder->pBackendVTable != NULL && pDecoder->pBackendVTable->onUninit != NULL) {
- pDecoder->pBackendVTable->onUninit(pDecoder->pBackendUserData, &pDecoder->pBackend, &pDecoder->allocationCallbacks);
- }
-
return result;
}
} else {
/* Initialization was successful. Finish up. */
result = ma_decoder__postinit(&config, pDecoder);
if (result != MA_SUCCESS) {
- /*
- The backend was initialized successfully, but for some reason post-initialization failed. This is most likely
- due to an out of memory error. We're going to abort with an error here and not try to recover.
- */
- if (pDecoder->pBackendVTable != NULL && pDecoder->pBackendVTable->onUninit != NULL) {
- pDecoder->pBackendVTable->onUninit(pDecoder->pBackendUserData, &pDecoder->pBackend, &pDecoder->allocationCallbacks);
- }
-
return result;
}
} else {
config.decodedSampleRate = 0;
config.jobThreadCount = 1; /* A single miniaudio-managed job thread by default. */
config.jobQueueCapacity = MA_JOB_TYPE_RESOURCE_MANAGER_QUEUE_CAPACITY;
+ config.resampling = ma_resampler_config_init(ma_format_unknown, 0, 0, 0, ma_resample_algorithm_linear); /* Format/channels/rate doesn't matter here. */
/* Flags. */
config.flags = 0;
config.ppCustomBackendVTables = pResourceManager->config.ppCustomDecodingBackendVTables;
config.customBackendCount = pResourceManager->config.customDecodingBackendCount;
config.pCustomBackendUserData = pResourceManager->config.pCustomDecodingBackendUserData;
+ config.resampling = pResourceManager->config.resampling;
return config;
}
MA_API ma_result ma_resource_manager_data_buffer_get_cursor_in_pcm_frames(ma_resource_manager_data_buffer* pDataBuffer, ma_uint64* pCursor)
{
- /* We cannot be using the data source after it's been uninitialized. */
- MA_ASSERT(ma_resource_manager_data_buffer_node_result(pDataBuffer->pNode) != MA_UNAVAILABLE);
-
if (pDataBuffer == NULL || pCursor == NULL) {
return MA_INVALID_ARGS;
}
+ /* We cannot be using the data source after it's been uninitialized. */
+ MA_ASSERT(ma_resource_manager_data_buffer_node_result(pDataBuffer->pNode) != MA_UNAVAILABLE);
+
*pCursor = 0;
switch (ma_resource_manager_data_buffer_node_get_data_supply_type(pDataBuffer->pNode))
MA_API ma_result ma_resource_manager_data_buffer_get_length_in_pcm_frames(ma_resource_manager_data_buffer* pDataBuffer, ma_uint64* pLength)
{
- /* We cannot be using the data source after it's been uninitialized. */
- MA_ASSERT(ma_resource_manager_data_buffer_node_result(pDataBuffer->pNode) != MA_UNAVAILABLE);
-
if (pDataBuffer == NULL || pLength == NULL) {
return MA_INVALID_ARGS;
}
+ /* We cannot be using the data source after it's been uninitialized. */
+ MA_ASSERT(ma_resource_manager_data_buffer_node_result(pDataBuffer->pNode) != MA_UNAVAILABLE);
+
if (ma_resource_manager_data_buffer_node_get_data_supply_type(pDataBuffer->pNode) == ma_resource_manager_data_supply_type_unknown) {
return MA_BUSY; /* Still loading. */
}
return ma_resource_manager_post_job(pResourceManager, pJob); /* Out of order. */
}
- ma_resource_manager_data_buffer_node_free(pResourceManager, pDataBufferNode);
-
/* The event needs to be signalled last. */
if (pJob->data.resourceManager.freeDataBufferNode.pDoneNotification != NULL) {
ma_async_notification_signal(pJob->data.resourceManager.freeDataBufferNode.pDoneNotification);
}
ma_atomic_fetch_add_32(&pDataBufferNode->executionPointer, 1);
+
+ ma_resource_manager_data_buffer_node_free(pResourceManager, pDataBufferNode);
+
return MA_SUCCESS;
}
return ma_node_set_time(&pNodeGraph->endpoint, globalTime); /* Global time is just the local time of the endpoint. */
}
+MA_API ma_uint32 ma_node_graph_get_processing_size_in_frames(const ma_node_graph* pNodeGraph)
+{
+ if (pNodeGraph == NULL) {
+ return 0;
+ }
+
+ return pNodeGraph->processingSizeInFrames;
+}
+
#define MA_NODE_OUTPUT_BUS_FLAG_HAS_READ 0x01 /* Whether or not this bus ready to read more data. Only used on nodes with multiple output buses. */
its start time not having been reached yet. Also, the stop time may have also been reached in
which case it'll be considered stopped.
*/
- if (ma_node_get_state_time(pNode, ma_node_state_started) > globalTimeBeg) {
- return ma_node_state_stopped; /* Start time has not yet been reached. */
+ if (ma_node_get_state_time(pNode, ma_node_state_stopped) < globalTimeBeg) {
+ return ma_node_state_stopped; /* End time is before the start of the range. */
}
- if (ma_node_get_state_time(pNode, ma_node_state_stopped) <= globalTimeEnd) {
- return ma_node_state_stopped; /* Stop time has been reached. */
+ if (ma_node_get_state_time(pNode, ma_node_state_started) > globalTimeEnd) {
+ return ma_node_state_stopped; /* Start time is after the end of the range. */
}
/* Getting here means the node is marked as started and is within its start/stop times. */
return MA_INVALID_ARGS; /* Invalid output bus index. */
}
+ globalTimeBeg = globalTime;
+ globalTimeEnd = globalTime + frameCount;
+
/* Don't do anything if we're in a stopped state. */
- if (ma_node_get_state_by_time_range(pNode, globalTime, globalTime + frameCount) != ma_node_state_started) {
+ if (ma_node_get_state_by_time_range(pNode, globalTimeBeg, globalTimeEnd) != ma_node_state_started) {
return MA_SUCCESS; /* We're in a stopped state. This is not an error - we just need to not read anything. */
}
-
- globalTimeBeg = globalTime;
- globalTimeEnd = globalTime + frameCount;
startTime = ma_node_get_state_time(pNode, ma_node_state_started);
stopTime = ma_node_get_state_time(pNode, ma_node_state_stopped);
therefore need to offset it by a number of frames to accommodate. The same thing applies for
the stop time.
*/
- timeOffsetBeg = (globalTimeBeg < startTime) ? (ma_uint32)(globalTimeEnd - startTime) : 0;
+ timeOffsetBeg = (globalTimeBeg < startTime) ? (ma_uint32)(startTime - globalTimeBeg) : 0;
timeOffsetEnd = (globalTimeEnd > stopTime) ? (ma_uint32)(globalTimeEnd - stopTime) : 0;
/* Trim based on the start offset. We need to silence the start of the buffer. */
if (timeOffsetBeg > 0) {
+ MA_ASSERT(timeOffsetBeg <= frameCount);
+ if (timeOffsetBeg > frameCount) {
+ timeOffsetBeg = frameCount;
+ }
+
ma_silence_pcm_frames(pFramesOut, timeOffsetBeg, ma_format_f32, ma_node_get_output_channels(pNode, outputBusIndex));
pFramesOut += timeOffsetBeg * ma_node_get_output_channels(pNode, outputBusIndex);
frameCount -= timeOffsetBeg;
/* Trim based on the end offset. We don't need to silence the tail section because we'll just have a reduced value written to pFramesRead. */
if (timeOffsetEnd > 0) {
+ MA_ASSERT(timeOffsetEnd <= frameCount);
+ if (timeOffsetEnd > frameCount) {
+ timeOffsetEnd = frameCount;
+ }
+
frameCount -= timeOffsetEnd;
}
MA_ASSERT(pSound != NULL);
ma_atomic_exchange_32(&pSound->atEnd, atEnd);
+ /*
+ When this function is called the state of the sound will not yet be in a stopped state. This makes it confusing
+ because an end callback will intuitively expect ma_sound_is_playing() to return false from inside the callback.
+ I'm therefore no longer firing the callback here and will instead fire it manually in the *next* processing step
+ when the state should be set to stopped as expected.
+ */
+ #if 0
/* Fire any callbacks or events. */
if (atEnd) {
if (pSound->endCallback != NULL) {
pSound->endCallback(pSound->pEndCallbackUserData, pSound);
}
}
+ #endif
}
static ma_bool32 ma_sound_get_at_end(const ma_sound* pSound)
config.isPitchDisabled = (flags & MA_SOUND_FLAG_NO_PITCH) != 0;
config.isSpatializationDisabled = (flags & MA_SOUND_FLAG_NO_SPATIALIZATION) != 0;
config.monoExpansionMode = pEngine->monoExpansionMode;
+ config.resampling = pEngine->pitchResamplingConfig;
return config;
}
if (isUpdateRequired) {
float basePitch = (float)pEngineNode->sampleRate / ma_engine_get_sample_rate(pEngineNode->pEngine);
- ma_linear_resampler_set_rate_ratio(&pEngineNode->resampler, basePitch * pEngineNode->oldPitch * pEngineNode->oldDopplerPitch);
+ ma_resampler_set_rate_ratio(&pEngineNode->resampler, basePitch * pEngineNode->oldPitch * pEngineNode->oldDopplerPitch);
}
}
return !ma_atomic_load_explicit_32(&pEngineNode->isSpatializationDisabled, ma_atomic_memory_order_acquire);
}
-static ma_uint64 ma_engine_node_get_required_input_frame_count(const ma_engine_node* pEngineNode, ma_uint64 outputFrameCount)
-{
- ma_uint64 inputFrameCount = 0;
-
- if (ma_engine_node_is_pitching_enabled(pEngineNode)) {
- ma_result result = ma_linear_resampler_get_required_input_frame_count(&pEngineNode->resampler, outputFrameCount, &inputFrameCount);
- if (result != MA_SUCCESS) {
- inputFrameCount = 0;
- }
- } else {
- inputFrameCount = outputFrameCount; /* No resampling, so 1:1. */
- }
-
- return inputFrameCount;
-}
-
static ma_result ma_engine_node_set_volume(ma_engine_node* pEngineNode, float volume)
{
if (pEngineNode == NULL) {
ma_uint64 resampleFrameCountIn = framesAvailableIn;
ma_uint64 resampleFrameCountOut = framesAvailableOut;
- ma_linear_resampler_process_pcm_frames(&pEngineNode->resampler, pRunningFramesIn, &resampleFrameCountIn, pWorkingBuffer, &resampleFrameCountOut);
+ ma_resampler_process_pcm_frames(&pEngineNode->resampler, pRunningFramesIn, &resampleFrameCountIn, pWorkingBuffer, &resampleFrameCountOut);
isWorkingBufferValid = MA_TRUE;
framesJustProcessedIn = (ma_uint32)resampleFrameCountIn;
/* If we're marked at the end we need to stop the sound and do nothing. */
if (ma_sound_at_end(pSound)) {
ma_sound_stop(pSound);
+
+ if (pSound->endCallback != NULL) {
+ pSound->endCallback(pSound->pEndCallbackUserData, pSound);
+ }
+
*pFrameCountOut = 0;
return;
}
/* Keep reading until we've read as much as was requested or we reach the end of the data source. */
while (totalFramesRead < frameCount) {
ma_uint32 framesRemaining = frameCount - totalFramesRead;
- ma_uint32 framesToRead;
ma_uint64 framesJustRead;
ma_uint32 frameCountIn;
ma_uint32 frameCountOut;
const float* pRunningFramesIn;
float* pRunningFramesOut;
- /*
- The first thing we need to do is read into the temporary buffer. We can calculate exactly
- how many input frames we'll need after resampling.
- */
- framesToRead = (ma_uint32)ma_engine_node_get_required_input_frame_count(&pSound->engineNode, framesRemaining);
- if (framesToRead > tempCapInFrames) {
- framesToRead = tempCapInFrames;
- }
+ /* If there's any input frames sitting in the cache get those processed first. */
+ if (pSound->processingCacheFramesRemaining > 0) {
+ pRunningFramesIn = pSound->pProcessingCache;
+ frameCountIn = pSound->processingCacheFramesRemaining;
- result = ma_data_source_read_pcm_frames(pSound->pDataSource, temp, framesToRead, &framesJustRead);
+ pRunningFramesOut = ma_offset_pcm_frames_ptr_f32(ppFramesOut[0], totalFramesRead, ma_node_get_output_channels(pNode, 0));
+ frameCountOut = framesRemaining;
- /* If we reached the end of the sound we'll want to mark it as at the end and stop it. This should never be returned for looping sounds. */
- if (result == MA_AT_END) {
- ma_sound_set_at_end(pSound, MA_TRUE); /* This will be set to false in ma_sound_start(). */
- }
+ ma_engine_node_process_pcm_frames__general(&pSound->engineNode, &pRunningFramesIn, &frameCountIn, &pRunningFramesOut, &frameCountOut);
- pRunningFramesOut = ma_offset_pcm_frames_ptr_f32(ppFramesOut[0], totalFramesRead, ma_node_get_output_channels(pNode, 0));
+ MA_ASSERT(frameCountIn <= pSound->processingCacheFramesRemaining);
+ pSound->processingCacheFramesRemaining -= frameCountIn;
- frameCountIn = (ma_uint32)framesJustRead;
- frameCountOut = framesRemaining;
+ /* Move any remaining data in the cache down. */
+ if (pSound->processingCacheFramesRemaining > 0) {
+ MA_MOVE_MEMORY(pSound->pProcessingCache, ma_offset_pcm_frames_ptr_f32(pSound->pProcessingCache, frameCountIn, dataSourceChannels), pSound->processingCacheFramesRemaining * ma_get_bytes_per_frame(ma_format_f32, dataSourceChannels));
+ }
+
+ totalFramesRead += (ma_uint32)frameCountOut; /* Safe cast. */
- /* Convert if necessary. */
- if (dataSourceFormat == ma_format_f32) {
- /* Fast path. No data conversion necessary. */
- pRunningFramesIn = (float*)temp;
- ma_engine_node_process_pcm_frames__general(&pSound->engineNode, &pRunningFramesIn, &frameCountIn, &pRunningFramesOut, &frameCountOut);
+ if (result != MA_SUCCESS || ma_sound_at_end(pSound)) {
+ break; /* Might have reached the end. */
+ }
} else {
- /* Slow path. Need to do sample format conversion to f32. If we give the f32 buffer the same count as the first temp buffer, we're guaranteed it'll be large enough. */
- float tempf32[MA_DATA_CONVERTER_STACK_BUFFER_SIZE]; /* Do not do `MA_DATA_CONVERTER_STACK_BUFFER_SIZE/sizeof(float)` here like we've done in other places. */
- ma_convert_pcm_frames_format(tempf32, ma_format_f32, temp, dataSourceFormat, framesJustRead, dataSourceChannels, ma_dither_mode_none);
+ /* Getting here means there's nothing in the cache. Read more data from the data source. */
+ if (dataSourceFormat == ma_format_f32) {
+ /* Fast path. No conversion to f32 necessary. */
+ result = ma_data_source_read_pcm_frames(pSound->pDataSource, pSound->pProcessingCache, pSound->processingCacheCap, &framesJustRead);
+ } else {
+ /* Slow path. Need to convert to f32. */
+ ma_uint64 totalFramesConverted = 0;
+
+ while (totalFramesConverted < pSound->processingCacheCap) {
+ ma_uint64 framesConverted;
+ ma_uint32 framesToConvertThisIteration = pSound->processingCacheCap - (ma_uint32)totalFramesConverted;
+ if (framesToConvertThisIteration > tempCapInFrames) {
+ framesToConvertThisIteration = tempCapInFrames;
+ }
- /* Now that we have our samples in f32 format we can process like normal. */
- pRunningFramesIn = tempf32;
- ma_engine_node_process_pcm_frames__general(&pSound->engineNode, &pRunningFramesIn, &frameCountIn, &pRunningFramesOut, &frameCountOut);
- }
+ result = ma_data_source_read_pcm_frames(pSound->pDataSource, temp, framesToConvertThisIteration, &framesConverted);
+ if (result != MA_SUCCESS) {
+ break;
+ }
+
+ ma_convert_pcm_frames_format(ma_offset_pcm_frames_ptr_f32(pSound->pProcessingCache, totalFramesConverted, dataSourceChannels), ma_format_f32, temp, dataSourceFormat, framesConverted, dataSourceChannels, ma_dither_mode_none);
+ totalFramesConverted += framesConverted;
+ }
+
+ framesJustRead = totalFramesConverted;
+ }
+
+ MA_ASSERT(framesJustRead <= pSound->processingCacheCap);
+ pSound->processingCacheFramesRemaining = (ma_uint32)framesJustRead;
- /* We should have processed all of our input frames since we calculated the required number of input frames at the top. */
- MA_ASSERT(frameCountIn == framesJustRead);
- totalFramesRead += (ma_uint32)frameCountOut; /* Safe cast. */
+ /* If we reached the end of the sound we'll want to mark it as at the end and stop it. This should never be returned for looping sounds. */
+ if (result == MA_AT_END) {
+ ma_sound_set_at_end(pSound, MA_TRUE); /* This will be set to false in ma_sound_start(). */
+ }
- if (result != MA_SUCCESS || ma_sound_at_end(pSound)) {
- break; /* Might have reached the end. */
+ if (result != MA_SUCCESS || ma_sound_at_end(pSound)) {
+ break;
+ }
}
}
}
ma_engine_node_process_pcm_frames__general((ma_engine_node*)pNode, ppFramesIn, pFrameCountIn, ppFramesOut, pFrameCountOut);
}
-static ma_result ma_engine_node_get_required_input_frame_count__group(ma_node* pNode, ma_uint32 outputFrameCount, ma_uint32* pInputFrameCount)
-{
- ma_uint64 inputFrameCount;
-
- MA_ASSERT(pInputFrameCount != NULL);
-
- /* Our pitch will affect this calculation. We need to update it. */
- ma_engine_node_update_pitch_if_required((ma_engine_node*)pNode);
-
- inputFrameCount = ma_engine_node_get_required_input_frame_count((ma_engine_node*)pNode, outputFrameCount);
- if (inputFrameCount > 0xFFFFFFFF) {
- inputFrameCount = 0xFFFFFFFF; /* Will never happen because miniaudio will only ever process in relatively small chunks. */
- }
-
- *pInputFrameCount = (ma_uint32)inputFrameCount;
-
- return MA_SUCCESS;
-}
-
static ma_node_vtable g_ma_engine_node_vtable__sound =
{
static ma_node_vtable g_ma_engine_node_vtable__group =
{
ma_engine_node_process_pcm_frames__group,
- ma_engine_node_get_required_input_frame_count__group,
+ NULL, /* onGetRequiredInputFrameCount */
1, /* Groups have one input bus. */
1, /* Groups have one output bus. */
MA_NODE_FLAG_DIFFERENT_PROCESSING_RATES /* The engine node does resampling so should let miniaudio know about it. */
ma_result result;
size_t tempHeapSize;
ma_node_config baseNodeConfig;
- ma_linear_resampler_config resamplerConfig;
+ ma_resampler_config resamplerConfig;
ma_spatializer_config spatializerConfig;
ma_gainer_config gainerConfig;
+ ma_uint32 sampleRate;
ma_uint32 channelsIn;
ma_uint32 channelsOut;
ma_channel defaultStereoChannelMap[2] = {MA_CHANNEL_SIDE_LEFT, MA_CHANNEL_SIDE_RIGHT}; /* <-- Consistent with the default channel map of a stereo listener. Means channel conversion can run on a fast path. */
pHeapLayout->sizeInBytes = 0;
+ sampleRate = (pConfig->sampleRate > 0) ? pConfig->sampleRate : ma_engine_get_sample_rate(pConfig->pEngine);
channelsIn = (pConfig->channelsIn != 0) ? pConfig->channelsIn : ma_engine_get_channels(pConfig->pEngine);
channelsOut = (pConfig->channelsOut != 0) ? pConfig->channelsOut : ma_engine_get_channels(pConfig->pEngine);
/* Resmapler. */
- resamplerConfig = ma_linear_resampler_config_init(ma_format_f32, channelsIn, 1, 1); /* Input and output sample rates don't affect the calculation of the heap size. */
- resamplerConfig.lpfOrder = 0;
+ resamplerConfig = pConfig->resampling;
+ resamplerConfig.format = ma_format_f32;
+ resamplerConfig.channels = channelsIn;
+ resamplerConfig.sampleRateIn = sampleRate;
+ resamplerConfig.sampleRateOut = ma_engine_get_sample_rate(pConfig->pEngine);
- result = ma_linear_resampler_get_heap_size(&resamplerConfig, &tempHeapSize);
+ result = ma_resampler_get_heap_size(&resamplerConfig, &tempHeapSize);
if (result != MA_SUCCESS) {
return result; /* Failed to retrieve the size of the heap for the resampler. */
}
ma_result result;
ma_engine_node_heap_layout heapLayout;
ma_node_config baseNodeConfig;
- ma_linear_resampler_config resamplerConfig;
+ ma_resampler_config resamplerConfig;
ma_fader_config faderConfig;
ma_spatializer_config spatializerConfig;
ma_panner_config pannerConfig;
*/
/* We'll always do resampling first. */
- resamplerConfig = ma_linear_resampler_config_init(ma_format_f32, baseNodeConfig.pInputChannels[0], pEngineNode->sampleRate, ma_engine_get_sample_rate(pEngineNode->pEngine));
- resamplerConfig.lpfOrder = 0; /* <-- Need to disable low-pass filtering for pitch shifting for now because there's cases where the biquads are becoming unstable. Need to figure out a better fix for this. */
+ resamplerConfig = pConfig->resampling;
+ resamplerConfig.format = ma_format_f32;
+ resamplerConfig.channels = baseNodeConfig.pInputChannels[0];
+ resamplerConfig.sampleRateIn = pEngineNode->sampleRate;
+ resamplerConfig.sampleRateOut = ma_engine_get_sample_rate(pEngineNode->pEngine);
- result = ma_linear_resampler_init_preallocated(&resamplerConfig, ma_offset_ptr(pHeap, heapLayout.resamplerOffset), &pEngineNode->resampler);
+ result = ma_resampler_init_preallocated(&resamplerConfig, ma_offset_ptr(pHeap, heapLayout.resamplerOffset), &pEngineNode->resampler);
if (result != MA_SUCCESS) {
goto error1;
}
/* No need for allocation callbacks here because we use a preallocated heap. */
error3: ma_spatializer_uninit(&pEngineNode->spatializer, NULL);
-error2: ma_linear_resampler_uninit(&pEngineNode->resampler, NULL);
+error2: ma_resampler_uninit(&pEngineNode->resampler, NULL);
error1: ma_node_uninit(&pEngineNode->baseNode, NULL);
error0: return result;
}
}
ma_spatializer_uninit(&pEngineNode->spatializer, pAllocationCallbacks);
- ma_linear_resampler_uninit(&pEngineNode->resampler, pAllocationCallbacks);
+ ma_resampler_uninit(&pEngineNode->resampler, pAllocationCallbacks);
/* Free the heap last. */
if (pEngineNode->_ownsHeap) {
if (pEngine != NULL) {
config.monoExpansionMode = pEngine->monoExpansionMode;
+ config.pitchResampling = pEngine->pitchResamplingConfig;
} else {
config.monoExpansionMode = ma_mono_expansion_mode_default;
+
+ config.pitchResampling = ma_resampler_config_init(ma_format_f32, 0, 0, 0, ma_resample_algorithm_linear);
+ config.pitchResampling.linear.lpfOrder = 0; /* <-- Need to disable low-pass filtering for pitch shifting for now because there's cases where the biquads are becoming unstable. Need to figure out a better fix for this. */
}
config.rangeEndInPCMFrames = ~((ma_uint64)0);
if (pEngine != NULL) {
config.monoExpansionMode = pEngine->monoExpansionMode;
+ config.pitchResampling = pEngine->pitchResamplingConfig;
} else {
config.monoExpansionMode = ma_mono_expansion_mode_default;
+
+ config.pitchResampling = ma_resampler_config_init(ma_format_f32, 0, 0, 0, ma_resample_algorithm_linear);
+ config.pitchResampling.linear.lpfOrder = 0; /* <-- Need to disable low-pass filtering for pitch shifting for now because there's cases where the biquads are becoming unstable. Need to figure out a better fix for this. */
}
return config;
ma_engine_config config;
MA_ZERO_OBJECT(&config);
- config.listenerCount = 1; /* Always want at least one listener. */
- config.monoExpansionMode = ma_mono_expansion_mode_default;
+ config.listenerCount = 1; /* Always want at least one listener. */
+ config.monoExpansionMode = ma_mono_expansion_mode_default;
+ config.resourceManagerResampling = ma_resampler_config_init(ma_format_unknown, 0, 0, 0, ma_resample_algorithm_linear);
+
+ config.pitchResampling = ma_resampler_config_init(ma_format_f32, 0, 0, 0, ma_resample_algorithm_linear);
+ config.pitchResampling.linear.lpfOrder = 0; /* <-- Need to disable low-pass filtering for pitch shifting for now because there's cases where the biquads are becoming unstable. Need to figure out a better fix for this. */
return config;
}
pEngine->defaultVolumeSmoothTimeInPCMFrames = engineConfig.defaultVolumeSmoothTimeInPCMFrames;
pEngine->onProcess = engineConfig.onProcess;
pEngine->pProcessUserData = engineConfig.pProcessUserData;
+ pEngine->pitchResamplingConfig = engineConfig.pitchResampling;
ma_allocation_callbacks_init_copy(&pEngine->allocationCallbacks, &engineConfig.allocationCallbacks);
#if !defined(MA_NO_RESOURCE_MANAGER)
resourceManagerConfig.decodedSampleRate = ma_engine_get_sample_rate(pEngine);
ma_allocation_callbacks_init_copy(&resourceManagerConfig.allocationCallbacks, &pEngine->allocationCallbacks);
resourceManagerConfig.pVFS = engineConfig.pResourceManagerVFS;
+ resourceManagerConfig.resampling = engineConfig.resourceManagerResampling;
/* The Emscripten build cannot use threads unless it's targeting pthreads. */
#if defined(MA_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
}
+ /*
+ When pulling data from a data source we need a processing cache to hold onto unprocessed input data from the data source
+ after doing resampling.
+ */
+ if (pSound->pDataSource != NULL) {
+ pSound->processingCacheFramesRemaining = 0;
+ pSound->processingCacheCap = ma_node_graph_get_processing_size_in_frames(&pEngine->nodeGraph);
+ if (pSound->processingCacheCap == 0) {
+ pSound->processingCacheCap = 512;
+ }
+
+ pSound->pProcessingCache = (float*)ma_calloc(pSound->processingCacheCap * ma_get_bytes_per_frame(ma_format_f32, engineNodeConfig.channelsIn), &pEngine->allocationCallbacks);
+ if (pSound->pProcessingCache == NULL) {
+ ma_engine_node_uninit(&pSound->engineNode, &pEngine->allocationCallbacks);
+ return MA_OUT_OF_MEMORY;
+ }
+ }
+
+
/* Apply initial range and looping state to the data source if applicable. */
if (pConfig->rangeBegInPCMFrames != 0 || pConfig->rangeEndInPCMFrames != ~((ma_uint64)0)) {
ma_data_source_set_range_in_pcm_frames(ma_sound_get_data_source(pSound), pConfig->rangeBegInPCMFrames, pConfig->rangeEndInPCMFrames);
*/
ma_engine_node_uninit(&pSound->engineNode, &pSound->engineNode.pEngine->allocationCallbacks);
+ if (pSound->pProcessingCache != NULL) {
+ ma_free(pSound->pProcessingCache, &pSound->engineNode.pEngine->allocationCallbacks);
+ pSound->pProcessingCache = NULL;
+ }
+
/* Once the sound is detached from the group we can guarantee that it won't be referenced by the mixer thread which means it's safe for us to destroy the data source. */
#ifndef MA_NO_RESOURCE_MANAGER
if (pSound->ownsDataSource) {
return ma_sound_stop_with_fade_in_pcm_frames(pSound, (fadeLengthInMilliseconds * sampleRate) / 1000);
}
+MA_API void ma_sound_reset_start_time(ma_sound* pSound)
+{
+ ma_sound_set_start_time_in_pcm_frames(pSound, 0);
+}
+
+MA_API void ma_sound_reset_stop_time(ma_sound* pSound)
+{
+ ma_sound_set_stop_time_in_pcm_frames(pSound, ~(ma_uint64)0);
+}
+
+MA_API void ma_sound_reset_fade(ma_sound* pSound)
+{
+ ma_sound_set_fade_in_pcm_frames(pSound, 0, 1, 0);
+}
+
+MA_API void ma_sound_reset_stop_time_and_fade(ma_sound* pSound)
+{
+ ma_sound_reset_stop_time(pSound);
+ ma_sound_reset_fade(pSound);
+}
+
MA_API void ma_sound_set_volume(ma_sound* pSound, float volume)
{
if (pSound == NULL) {
}
if (pSampleRate != NULL) {
- *pSampleRate = pSound->engineNode.resampler.config.sampleRateIn;
+ *pSampleRate = pSound->engineNode.resampler.sampleRateIn;
}
if (pChannelMap != NULL) {
ma_dr_wav* pWav = (ma_dr_wav*)pUserData;
ma_int64 newCursor;
MA_DR_WAV_ASSERT(pWav != NULL);
- newCursor = pWav->memoryStream.currentReadPos;
if (origin == MA_DR_WAV_SEEK_SET) {
newCursor = 0;
} else if (origin == MA_DR_WAV_SEEK_CUR) {
ma_dr_wav* pWav = (ma_dr_wav*)pUserData;
ma_int64 newCursor;
MA_DR_WAV_ASSERT(pWav != NULL);
- newCursor = pWav->memoryStreamWrite.currentWritePos;
if (origin == MA_DR_WAV_SEEK_SET) {
newCursor = 0;
} else if (origin == MA_DR_WAV_SEEK_CUR) {
newCursor = (ma_int64)pWav->memoryStreamWrite.dataSize;
} else {
MA_DR_WAV_ASSERT(!"Invalid seek origin");
- return MA_INVALID_ARGS;
+ return MA_FALSE;
}
newCursor += offset;
if (newCursor < 0) {
pWav->msadpcm.cachedFrames[2] = pWav->msadpcm.prevFrames[0][0];
pWav->msadpcm.cachedFrames[3] = pWav->msadpcm.prevFrames[0][1];
pWav->msadpcm.cachedFrameCount = 2;
- if (pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff1Table)) {
+ if (pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff2Table)) {
return totalFramesRead;
}
} else {
pWav->msadpcm.cachedFrames[2] = pWav->msadpcm.prevFrames[0][1];
pWav->msadpcm.cachedFrames[3] = pWav->msadpcm.prevFrames[1][1];
pWav->msadpcm.cachedFrameCount = 2;
- if (pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[1] >= ma_dr_wav_countof(coeff2Table)) {
+ if (pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff2Table) ||
+ pWav->msadpcm.predictor[1] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[1] >= ma_dr_wav_countof(coeff2Table)) {
return totalFramesRead;
}
}
if (pWav->channels == 1) {
ma_int32 newSample0;
ma_int32 newSample1;
+ if (pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff2Table)) {
+ return totalFramesRead;
+ }
newSample0 = ((pWav->msadpcm.prevFrames[0][1] * coeff1Table[pWav->msadpcm.predictor[0]]) + (pWav->msadpcm.prevFrames[0][0] * coeff2Table[pWav->msadpcm.predictor[0]])) >> 8;
newSample0 += nibble0 * pWav->msadpcm.delta[0];
newSample0 = ma_dr_wav_clamp(newSample0, -32768, 32767);
} else {
ma_int32 newSample0;
ma_int32 newSample1;
+ if (pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[0] >= ma_dr_wav_countof(coeff2Table)) {
+ return totalFramesRead;
+ }
newSample0 = ((pWav->msadpcm.prevFrames[0][1] * coeff1Table[pWav->msadpcm.predictor[0]]) + (pWav->msadpcm.prevFrames[0][0] * coeff2Table[pWav->msadpcm.predictor[0]])) >> 8;
newSample0 += nibble0 * pWav->msadpcm.delta[0];
newSample0 = ma_dr_wav_clamp(newSample0, -32768, 32767);
}
pWav->msadpcm.prevFrames[0][0] = pWav->msadpcm.prevFrames[0][1];
pWav->msadpcm.prevFrames[0][1] = newSample0;
+ if (pWav->msadpcm.predictor[1] >= ma_dr_wav_countof(coeff1Table) || pWav->msadpcm.predictor[1] >= ma_dr_wav_countof(coeff2Table)) {
+ return totalFramesRead;
+ }
newSample1 = ((pWav->msadpcm.prevFrames[1][1] * coeff1Table[pWav->msadpcm.predictor[1]]) + (pWav->msadpcm.prevFrames[1][0] * coeff2Table[pWav->msadpcm.predictor[1]])) >> 8;
newSample1 += nibble1 * pWav->msadpcm.delta[1];
newSample1 = ma_dr_wav_clamp(newSample1, -32768, 32767);
ma_int16* pSampleData;
ma_uint64 framesRead;
MA_DR_WAV_ASSERT(pWav != NULL);
+ if (pWav->channels == 0 || pWav->totalPCMFrameCount > MA_SIZE_MAX / pWav->channels / sizeof(ma_int16)) {
+ ma_dr_wav_uninit(pWav);
+ return NULL;
+ }
sampleDataSize = pWav->totalPCMFrameCount * pWav->channels * sizeof(ma_int16);
if (sampleDataSize > MA_SIZE_MAX) {
ma_dr_wav_uninit(pWav);
float* pSampleData;
ma_uint64 framesRead;
MA_DR_WAV_ASSERT(pWav != NULL);
+ if (pWav->channels == 0 || pWav->totalPCMFrameCount > MA_SIZE_MAX / pWav->channels / sizeof(float)) {
+ ma_dr_wav_uninit(pWav);
+ return NULL;
+ }
sampleDataSize = pWav->totalPCMFrameCount * pWav->channels * sizeof(float);
if (sampleDataSize > MA_SIZE_MAX) {
ma_dr_wav_uninit(pWav);
ma_int32* pSampleData;
ma_uint64 framesRead;
MA_DR_WAV_ASSERT(pWav != NULL);
+ if (pWav->channels == 0 || pWav->totalPCMFrameCount > MA_SIZE_MAX / pWav->channels / sizeof(ma_int32)) {
+ ma_dr_wav_uninit(pWav);
+ return NULL;
+ }
sampleDataSize = pWav->totalPCMFrameCount * pWav->channels * sizeof(ma_int32);
if (sampleDataSize > MA_SIZE_MAX) {
ma_dr_wav_uninit(pWav);
{
ma_uint64 r;
__asm__ __volatile__ (
- "lzcnt{ %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc"
+ "rep; bsr{q %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc"
);
return (ma_uint32)r;
}
{
ma_uint32 r;
__asm__ __volatile__ (
- "lzcnt{l %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc"
+ "rep; bsr{l %1, %0| %0, %1}" : "=r"(r) : "r"(x) : "cc"
);
return r;
}
- #elif defined(MA_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(__ARM_ARCH_6M__) && !defined(MA_64BIT)
+ #elif defined(MA_ARM) && (defined(__ARM_ARCH) && __ARM_ARCH >= 5) && !defined(__ARM_ARCH_6M__) && !(defined(__thumb__) && !defined(__thumb2__)) && !defined(MA_64BIT)
{
unsigned int r;
__asm__ __volatile__ (
}
}
blockSizeRemaining -= metadata.data.picture.pictureDataSize;
+ (void)blockSizeRemaining;
metadata.data.picture.pPictureData = (const ma_uint8*)pPictureData;
if (metadata.data.picture.pictureDataOffset != 0 || metadata.data.picture.pPictureData != NULL) {
onMeta(pUserDataMD, &metadata);
{ \
type* pSampleData = NULL; \
ma_uint64 totalPCMFrameCount; \
+ type buffer[4096]; \
+ ma_uint64 pcmFramesRead; \
+ size_t sampleDataBufferSize = sizeof(buffer); \
\
MA_DR_FLAC_ASSERT(pFlac != NULL); \
\
- totalPCMFrameCount = pFlac->totalPCMFrameCount; \
+ totalPCMFrameCount = 0; \
\
- if (totalPCMFrameCount == 0) { \
- type buffer[4096]; \
- ma_uint64 pcmFramesRead; \
- size_t sampleDataBufferSize = sizeof(buffer); \
- \
- pSampleData = (type*)ma_dr_flac__malloc_from_callbacks(sampleDataBufferSize, &pFlac->allocationCallbacks); \
- if (pSampleData == NULL) { \
- goto on_error; \
- } \
- \
- while ((pcmFramesRead = (ma_uint64)ma_dr_flac_read_pcm_frames_##extension(pFlac, sizeof(buffer)/sizeof(buffer[0])/pFlac->channels, buffer)) > 0) { \
- if (((totalPCMFrameCount + pcmFramesRead) * pFlac->channels * sizeof(type)) > sampleDataBufferSize) { \
- type* pNewSampleData; \
- size_t newSampleDataBufferSize; \
+ pSampleData = (type*)ma_dr_flac__malloc_from_callbacks(sampleDataBufferSize, &pFlac->allocationCallbacks); \
+ if (pSampleData == NULL) { \
+ goto on_error; \
+ } \
\
- newSampleDataBufferSize = sampleDataBufferSize * 2; \
- pNewSampleData = (type*)ma_dr_flac__realloc_from_callbacks(pSampleData, newSampleDataBufferSize, sampleDataBufferSize, &pFlac->allocationCallbacks); \
- if (pNewSampleData == NULL) { \
- ma_dr_flac__free_from_callbacks(pSampleData, &pFlac->allocationCallbacks); \
- goto on_error; \
- } \
+ while ((pcmFramesRead = (ma_uint64)ma_dr_flac_read_pcm_frames_##extension(pFlac, sizeof(buffer)/sizeof(buffer[0])/pFlac->channels, buffer)) > 0) { \
+ if (((totalPCMFrameCount + pcmFramesRead) * pFlac->channels * sizeof(type)) > sampleDataBufferSize) { \
+ type* pNewSampleData; \
+ size_t newSampleDataBufferSize; \
\
- sampleDataBufferSize = newSampleDataBufferSize; \
- pSampleData = pNewSampleData; \
+ newSampleDataBufferSize = sampleDataBufferSize * 2; \
+ pNewSampleData = (type*)ma_dr_flac__realloc_from_callbacks(pSampleData, newSampleDataBufferSize, sampleDataBufferSize, &pFlac->allocationCallbacks); \
+ if (pNewSampleData == NULL) { \
+ ma_dr_flac__free_from_callbacks(pSampleData, &pFlac->allocationCallbacks); \
+ goto on_error; \
} \
\
- MA_DR_FLAC_COPY_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), buffer, (size_t)(pcmFramesRead*pFlac->channels*sizeof(type))); \
- totalPCMFrameCount += pcmFramesRead; \
- } \
- \
- \
- MA_DR_FLAC_ZERO_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), (size_t)(sampleDataBufferSize - totalPCMFrameCount*pFlac->channels*sizeof(type))); \
- } else { \
- ma_uint64 dataSize = totalPCMFrameCount*pFlac->channels*sizeof(type); \
- if (dataSize > (ma_uint64)MA_SIZE_MAX) { \
- goto on_error; \
+ sampleDataBufferSize = newSampleDataBufferSize; \
+ pSampleData = pNewSampleData; \
} \
\
- pSampleData = (type*)ma_dr_flac__malloc_from_callbacks((size_t)dataSize, &pFlac->allocationCallbacks); \
- if (pSampleData == NULL) { \
- goto on_error; \
- } \
- \
- totalPCMFrameCount = ma_dr_flac_read_pcm_frames_##extension(pFlac, pFlac->totalPCMFrameCount, pSampleData); \
+ MA_DR_FLAC_COPY_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), buffer, (size_t)(pcmFramesRead*pFlac->channels*sizeof(type))); \
+ totalPCMFrameCount += pcmFramesRead; \
} \
\
+ \
+ MA_DR_FLAC_ZERO_MEMORY(pSampleData + (totalPCMFrameCount*pFlac->channels), (size_t)(sampleDataBufferSize - totalPCMFrameCount*pFlac->channels*sizeof(type))); \
+ \
if (sampleRateOut) *sampleRateOut = pFlac->sampleRate; \
if (channelsOut) *channelsOut = pFlac->channels; \
if (totalPCMFrameCountOut) *totalPCMFrameCountOut = totalPCMFrameCount; \
((ma_uint32)ape[25] << 8) |
((ma_uint32)ape[26] << 16) |
((ma_uint32)ape[27] << 24);
- streamEndOffset -= 32 + tagSize;
- streamLen -= 32 + tagSize;
- if (onMeta != NULL) {
- if (onSeek(pUserData, streamEndOffset, MA_DR_MP3_SEEK_END)) {
- size_t apeTagSize = (size_t)tagSize + 32;
- ma_uint8* pTagData = (ma_uint8*)ma_dr_mp3_malloc(apeTagSize, pAllocationCallbacks);
- if (pTagData != NULL) {
- if (onRead(pUserData, pTagData, apeTagSize) == apeTagSize) {
- ma_dr_mp3__on_meta(pMP3, MA_DR_MP3_METADATA_TYPE_APE, pTagData, apeTagSize);
+ if (32 + tagSize < streamLen) {
+ streamEndOffset -= 32 + tagSize;
+ streamLen -= 32 + tagSize;
+ if (onMeta != NULL) {
+ if (onSeek(pUserData, streamEndOffset, MA_DR_MP3_SEEK_END)) {
+ size_t apeTagSize = (size_t)tagSize + 32;
+ ma_uint8* pTagData = (ma_uint8*)ma_dr_mp3_malloc(apeTagSize, pAllocationCallbacks);
+ if (pTagData != NULL) {
+ if (onRead(pUserData, pTagData, apeTagSize) == apeTagSize) {
+ ma_dr_mp3__on_meta(pMP3, MA_DR_MP3_METADATA_TYPE_APE, pTagData, apeTagSize);
+ }
+ ma_dr_mp3_free(pTagData, pAllocationCallbacks);
}
- ma_dr_mp3_free(pTagData, pAllocationCallbacks);
}
}
+ } else {
}
}
}
{
ma_dr_mp3_bs bs;
ma_dr_mp3_L3_gr_info grInfo[4];
- const ma_uint8* pTagData = pFirstFrameData;
ma_dr_mp3_bs_init(&bs, pFirstFrameData + MA_DR_MP3_HDR_SIZE, firstFrameInfo.frame_bytes - MA_DR_MP3_HDR_SIZE);
if (MA_DR_MP3_HDR_IS_CRC(pFirstFrameData)) {
ma_dr_mp3_bs_get_bits(&bs, 16);
if (ma_dr_mp3_L3_read_side_info(&bs, grInfo, pFirstFrameData) >= 0) {
ma_bool32 isXing = MA_FALSE;
ma_bool32 isInfo = MA_FALSE;
+ const ma_uint8* pTagData;
const ma_uint8* pTagDataBeg;
pTagDataBeg = pFirstFrameData + MA_DR_MP3_HDR_SIZE + (bs.pos/8);
pTagData = pTagDataBeg;
ma_dr_mp3* pMP3 = (ma_dr_mp3*)pUserData;
ma_int64 newCursor;
MA_DR_MP3_ASSERT(pMP3 != NULL);
- newCursor = pMP3->memory.currentReadPos;
if (origin == MA_DR_MP3_SEEK_SET) {
newCursor = 0;
} else if (origin == MA_DR_MP3_SEEK_CUR) {
pNewFrames = (float*)ma_dr_mp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
if (pNewFrames == NULL) {
ma_dr_mp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
+ pFrames = NULL;
+ totalFramesRead = 0;
break;
}
pFrames = pNewFrames;
pNewFrames = (ma_int16*)ma_dr_mp3__realloc_from_callbacks(pFrames, (size_t)newFramesBufferSize, (size_t)oldFramesBufferSize, &pMP3->allocationCallbacks);
if (pNewFrames == NULL) {
ma_dr_mp3__free_from_callbacks(pFrames, &pMP3->allocationCallbacks);
+ pFrames = NULL;
+ totalFramesRead = 0;
break;
}
pFrames = pNewFrames;