"MAL_" to "MA_".

This commit is contained in:
David Reid
2019-03-06 20:51:38 +10:00
parent b93faa46e5
commit f1bf58d0f8
19 changed files with 4600 additions and 4600 deletions
+68 -68
View File
@@ -44,19 +44,19 @@ Other Notes:
Random Notes:
- You cannot change the algorithm after initialization.
- It is recommended to keep the mal_resampler object aligned to MAL_SIMD_ALIGNMENT, though it is not necessary.
- Ratios need to be in the range of MAL_RESAMPLER_MIN_RATIO and MAL_RESAMPLER_MAX_RATIO. This is enough to convert
- It is recommended to keep the mal_resampler object aligned to MA_SIMD_ALIGNMENT, though it is not necessary.
- Ratios need to be in the range of MA_RESAMPLER_MIN_RATIO and MA_RESAMPLER_MAX_RATIO. This is enough to convert
to and from 8000 and 384000, which is the smallest and largest standard rates supported by miniaudio. If you need
extreme ratios then you will need to chain resamplers together.
*/
#ifndef mal_resampler_h
#define mal_resampler_h
#define MAL_RESAMPLER_SEEK_NO_CLIENT_READ (1 << 0) /* When set, does not read anything from the client when seeking. This does _not_ call onRead(). */
#define MAL_RESAMPLER_SEEK_INPUT_RATE (1 << 1) /* When set, treats the specified frame count based on the input sample rate rather than the output sample rate. */
#define MA_RESAMPLER_SEEK_NO_CLIENT_READ (1 << 0) /* When set, does not read anything from the client when seeking. This does _not_ call onRead(). */
#define MA_RESAMPLER_SEEK_INPUT_RATE (1 << 1) /* When set, treats the specified frame count based on the input sample rate rather than the output sample rate. */
#ifndef MAL_RESAMPLER_CACHE_SIZE_IN_BYTES
#define MAL_RESAMPLER_CACHE_SIZE_IN_BYTES 4096
#ifndef MA_RESAMPLER_CACHE_SIZE_IN_BYTES
#define MA_RESAMPLER_CACHE_SIZE_IN_BYTES 4096
#endif
typedef struct mal_resampler mal_resampler;
@@ -100,8 +100,8 @@ struct mal_resampler
{
union
{
float f32[MAL_RESAMPLER_CACHE_SIZE_IN_BYTES/sizeof(float)];
mal_int16 s16[MAL_RESAMPLER_CACHE_SIZE_IN_BYTES/sizeof(mal_int16)];
float f32[MA_RESAMPLER_CACHE_SIZE_IN_BYTES/sizeof(float)];
mal_int16 s16[MA_RESAMPLER_CACHE_SIZE_IN_BYTES/sizeof(mal_int16)];
} cache; /* Keep this as the first member of this structure for SIMD alignment purposes. */
mal_uint32 cacheStrideInFrames; /* The number of the samples between channels in the cache. The first sample for channel 0 is cacheStrideInFrames*0. The first sample for channel 1 is cacheStrideInFrames*1, etc. */
mal_uint16 cacheLengthInFrames; /* The number of valid frames sitting in the cache, including the filter window. May be less than the cache's capacity. */
@@ -147,9 +147,9 @@ mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount,
Seeks forward by the specified number of PCM frames.
"options" can be a cobination of the following:
MAL_RESAMPLER_SEEK_NO_CLIENT_READ
MA_RESAMPLER_SEEK_NO_CLIENT_READ
Leaves the contents of the internal cached undefined instead of reading in data from the onRead callback.
MAL_RESAMPLER_SEEK_INPUT_RATE
MA_RESAMPLER_SEEK_INPUT_RATE
Treats "frameCount" as input samples instead of output samples.
*/
mal_uint64 mal_resampler_seek(mal_resampler* pResampler, mal_uint64 frameCount, mal_uint32 options);
@@ -222,11 +222,11 @@ mal_uint64 mal_resampler_get_expected_output_frame_count(mal_resampler* pResampl
#ifdef MINIAUDIO_IMPLEMENTATION
#ifndef MAL_RESAMPLER_MIN_RATIO
#define MAL_RESAMPLER_MIN_RATIO 0.02083333
#ifndef MA_RESAMPLER_MIN_RATIO
#define MA_RESAMPLER_MIN_RATIO 0.02083333
#endif
#ifndef MAL_RESAMPLER_MAX_RATIO
#define MAL_RESAMPLER_MAX_RATIO 48.0
#ifndef MA_RESAMPLER_MAX_RATIO
#define MA_RESAMPLER_MAX_RATIO 48.0
#endif
mal_result mal_resampler_init__linear(mal_resampler* pResampler);
@@ -239,36 +239,36 @@ mal_uint64 mal_resampler_read_f32__sinc(mal_resampler* pResampler, mal_uint64 fr
mal_uint64 mal_resampler_read_s16__sinc(mal_resampler* pResampler, mal_uint64 frameCount, mal_int16** ppFrames);
mal_uint64 mal_resampler_seek__sinc(mal_resampler* pResampler, mal_uint64 frameCount, mal_uint32 options);
static MAL_INLINE float mal_fractional_part_f32(float x)
static MA_INLINE float mal_fractional_part_f32(float x)
{
return x - ((mal_int32)x);
}
static MAL_INLINE double mal_fractional_part_f64(double x)
static MA_INLINE double mal_fractional_part_f64(double x)
{
return x - ((mal_int64)x);
}
#if 0
#define MAL_ALIGN_INT(val, alignment) (((val) + ((alignment)-1)) & ~((alignment)-1))
#define MAL_ALIGN_PTR(ptr, alignment) (void*)MAL_ALIGN_INT(((mal_uintptr)(ptr)), (alignment))
#define MA_ALIGN_INT(val, alignment) (((val) + ((alignment)-1)) & ~((alignment)-1))
#define MA_ALIGN_PTR(ptr, alignment) (void*)MA_ALIGN_INT(((mal_uintptr)(ptr)), (alignment))
/*
This macro declares a set of variables on the stack of a given size in bytes. The variables it creates are:
- mal_uint8 <name>Unaligned[size + MAL_SIMD_ALIGNMENT];
- <type>* <name>[MAL_MAX_CHANNELS];
- mal_uint8 <name>Unaligned[size + MA_SIMD_ALIGNMENT];
- <type>* <name>[MA_MAX_CHANNELS];
- size_t <name>FrameCount; <-- This is the number of samples contained within each sub-buffer of <name>
This does not work for formats that do not have a clean mapping to a primitive C type. s24 will not work here.
*/
#define MAL_DECLARE_ALIGNED_STACK_BUFFER(type, name, size, channels) \
mal_uint8 name##Unaligned[(size) + MAL_SIMD_ALIGNMENT]; \
type* name[MAL_MAX_CHANNELS]; \
size_t name##FrameCount = ((size) & ~((MAL_SIMD_ALIGNMENT)-1)) / sizeof(type); \
#define MA_DECLARE_ALIGNED_STACK_BUFFER(type, name, size, channels) \
mal_uint8 name##Unaligned[(size) + MA_SIMD_ALIGNMENT]; \
type* name[MA_MAX_CHANNELS]; \
size_t name##FrameCount = ((size) & ~((MA_SIMD_ALIGNMENT)-1)) / sizeof(type); \
do { \
mal_uint32 iChannel; \
for (iChannel = 0; iChannel < channels; ++iChannel) { \
name[iChannel] = (type*)((mal_uint8*)MAL_ALIGN_PTR(name##Unaligned, MAL_SIMD_ALIGNMENT) + (iChannel*((size) & ~((MAL_SIMD_ALIGNMENT)-1)))); \
name[iChannel] = (type*)((mal_uint8*)MA_ALIGN_PTR(name##Unaligned, MA_SIMD_ALIGNMENT) + (iChannel*((size) & ~((MA_SIMD_ALIGNMENT)-1)))); \
} \
} while (0)
#endif
@@ -276,17 +276,17 @@ This does not work for formats that do not have a clean mapping to a primitive C
#define mal_filter_window_length_left(length) ((length) >> 1)
#define mal_filter_window_length_right(length) ((length) - mal_filter_window_length_left(length))
static MAL_INLINE mal_uint16 mal_resampler__window_length_left(const mal_resampler* pResampler)
static MA_INLINE mal_uint16 mal_resampler__window_length_left(const mal_resampler* pResampler)
{
return mal_filter_window_length_left(pResampler->windowLength);
}
static MAL_INLINE mal_uint16 mal_resampler__window_length_right(const mal_resampler* pResampler)
static MA_INLINE mal_uint16 mal_resampler__window_length_right(const mal_resampler* pResampler)
{
return mal_filter_window_length_right(pResampler->windowLength);
}
static MAL_INLINE double mal_resampler__calculate_cached_input_time_by_mode(mal_resampler* pResampler, mal_resampler_end_of_input_mode mode)
static MA_INLINE double mal_resampler__calculate_cached_input_time_by_mode(mal_resampler* pResampler, mal_resampler_end_of_input_mode mode)
{
/*
The cached input time depends on whether or not the end of the input is being consumed. If so, it's the difference between the
@@ -303,24 +303,24 @@ static MAL_INLINE double mal_resampler__calculate_cached_input_time_by_mode(mal_
return cachedInputTime;
}
static MAL_INLINE double mal_resampler__calculate_cached_input_time(mal_resampler* pResampler)
static MA_INLINE double mal_resampler__calculate_cached_input_time(mal_resampler* pResampler)
{
return mal_resampler__calculate_cached_input_time_by_mode(pResampler, pResampler->config.endOfInputMode);
}
static MAL_INLINE double mal_resampler__calculate_cached_output_time_by_mode(mal_resampler* pResampler, mal_resampler_end_of_input_mode mode)
static MA_INLINE double mal_resampler__calculate_cached_output_time_by_mode(mal_resampler* pResampler, mal_resampler_end_of_input_mode mode)
{
return mal_resampler__calculate_cached_input_time_by_mode(pResampler, mode) / pResampler->config.ratio;
}
static MAL_INLINE double mal_resampler__calculate_cached_output_time(mal_resampler* pResampler)
static MA_INLINE double mal_resampler__calculate_cached_output_time(mal_resampler* pResampler)
{
return mal_resampler__calculate_cached_output_time_by_mode(pResampler, pResampler->config.endOfInputMode);
}
static MAL_INLINE mal_result mal_resampler__slide_cache_down(mal_resampler* pResampler)
static MA_INLINE mal_result mal_resampler__slide_cache_down(mal_resampler* pResampler)
{
/* This function moves everything from the start of the window to the last loaded frame in the cache down to the front. */
@@ -344,13 +344,13 @@ static MAL_INLINE mal_result mal_resampler__slide_cache_down(mal_resampler* pRes
}
}
return MAL_SUCCESS;
return MA_SUCCESS;
}
typedef union
{
float* f32[MAL_MAX_CHANNELS];
mal_int16* s16[MAL_MAX_CHANNELS];
float* f32[MA_MAX_CHANNELS];
mal_int16* s16[MA_MAX_CHANNELS];
} mal_resampler_deinterleaved_pointers;
typedef union
@@ -366,7 +366,7 @@ mal_result mal_resampler__reload_cache(mal_resampler* pResampler, mal_bool32* pL
mal_result result;
mal_uint32 framesToReadFromClient;
mal_uint32 framesReadFromClient;
mal_bool32 loadedEndOfInput = MAL_FALSE;
mal_bool32 loadedEndOfInput = MA_FALSE;
mal_assert(pLoadedEndOfInput != NULL);
mal_assert(pResampler->windowTime < 65536);
@@ -374,7 +374,7 @@ mal_result mal_resampler__reload_cache(mal_resampler* pResampler, mal_bool32* pL
/* Before loading anything from the client we need to move anything left in the cache down the front. */
result = mal_resampler__slide_cache_down(pResampler);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
return result; /* Should never actually happen. */
}
@@ -422,7 +422,7 @@ mal_result mal_resampler__reload_cache(mal_resampler* pResampler, mal_bool32* pL
mal_assert(framesReadFromClient <= framesToReadFromClient);
if (framesReadFromClient < framesToReadFromClient) {
/* We have reached the end of the input buffer. We do _not_ want to attempt to read any more data from the client in this case. */
loadedEndOfInput = MAL_TRUE;
loadedEndOfInput = MA_TRUE;
*pLoadedEndOfInput = loadedEndOfInput;
}
@@ -451,36 +451,36 @@ mal_result mal_resampler__reload_cache(mal_resampler* pResampler, mal_bool32* pL
}
}
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_resampler_init(const mal_resampler_config* pConfig, mal_resampler* pResampler)
{
if (pResampler == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_zero_object(pResampler);
if (pConfig == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
pResampler->config = *pConfig;
if (pResampler->config.format != mal_format_f32 && pResampler->config.format != mal_format_s16) {
return MAL_INVALID_ARGS; /* Unsupported format. */
return MA_INVALID_ARGS; /* Unsupported format. */
}
if (pResampler->config.channels == 0) {
return MAL_INVALID_ARGS; /* Unsupported channel count. */
return MA_INVALID_ARGS; /* Unsupported channel count. */
}
if (pResampler->config.ratio == 0) {
if (pResampler->config.sampleRateIn == 0 || pResampler->config.sampleRateOut == 0) {
return MAL_INVALID_ARGS; /* Unsupported sample rate. */
return MA_INVALID_ARGS; /* Unsupported sample rate. */
}
pResampler->config.ratio = (double)pResampler->config.sampleRateIn / (double)pResampler->config.sampleRateOut;
}
if (pResampler->config.onRead == NULL) {
return MAL_INVALID_ARGS; /* No input callback specified. */
return MA_INVALID_ARGS; /* No input callback specified. */
}
switch (pResampler->config.algorithm) {
@@ -509,7 +509,7 @@ mal_result mal_resampler_init(const mal_resampler_config* pConfig, mal_resampler
if (pResampler->init != NULL) {
mal_result result = pResampler->init(pResampler);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
return result;
}
}
@@ -520,7 +520,7 @@ mal_result mal_resampler_init(const mal_resampler_config* pConfig, mal_resampler
*/
pResampler->cacheLengthInFrames = mal_resampler__window_length_left(pResampler);
return MAL_SUCCESS;
return MA_SUCCESS;
}
void mal_resampler_uninit(mal_resampler* pResampler)
@@ -531,38 +531,38 @@ void mal_resampler_uninit(mal_resampler* pResampler)
mal_result mal_resampler_set_rate(mal_resampler* pResampler, mal_uint32 sampleRateIn, mal_uint32 sampleRateOut)
{
if (pResampler == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
if (sampleRateIn == 0 || sampleRateOut == 0) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
double ratio = (double)pResampler->config.sampleRateIn / (double)pResampler->config.sampleRateOut;
if (ratio < MAL_RESAMPLER_MIN_RATIO || ratio > MAL_RESAMPLER_MAX_RATIO) {
return MAL_INVALID_ARGS; /* Ratio is too extreme. */
if (ratio < MA_RESAMPLER_MIN_RATIO || ratio > MA_RESAMPLER_MAX_RATIO) {
return MA_INVALID_ARGS; /* Ratio is too extreme. */
}
pResampler->config.sampleRateIn = sampleRateIn;
pResampler->config.sampleRateOut = sampleRateOut;
pResampler->config.ratio = ratio;
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_resampler_set_rate_ratio(mal_resampler* pResampler, double ratio)
{
if (pResampler == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
if (ratio < MAL_RESAMPLER_MIN_RATIO || ratio > MAL_RESAMPLER_MAX_RATIO) {
return MAL_INVALID_ARGS; /* Ratio is too extreme. */
if (ratio < MA_RESAMPLER_MIN_RATIO || ratio > MA_RESAMPLER_MAX_RATIO) {
return MA_INVALID_ARGS; /* Ratio is too extreme. */
}
pResampler->config.ratio = ratio;
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount, void** ppFrames)
@@ -571,7 +571,7 @@ mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount,
mal_uint64 totalFramesRead;
mal_resampler_deinterleaved_pointers runningFramesOutDeinterleaved;
mal_resampler_interleaved_pointers runningFramesOutInterleaved;
mal_bool32 loadedEndOfInput = MAL_FALSE;
mal_bool32 loadedEndOfInput = MA_FALSE;
if (pResampler == NULL) {
return 0; /* Invalid arguments. */
@@ -669,7 +669,7 @@ mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount,
framesJustRead = pResampler->readF32(pResampler, framesToReadRightNow, runningFramesOutDeinterleaved.f32);
} else {
float buffer[mal_countof(pResampler->cache.f32)];
float* ppDeinterleavedFrames[MAL_MAX_CHANNELS];
float* ppDeinterleavedFrames[MA_MAX_CHANNELS];
for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) {
ppDeinterleavedFrames[iChannel] = buffer + (pResampler->cacheStrideInFrames*iChannel);
}
@@ -682,7 +682,7 @@ mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount,
framesJustRead = pResampler->readS16(pResampler, framesToReadRightNow, runningFramesOutDeinterleaved.s16);
} else {
mal_int16 buffer[mal_countof(pResampler->cache.s16)];
mal_int16* ppDeinterleavedFrames[MAL_MAX_CHANNELS];
mal_int16* ppDeinterleavedFrames[MA_MAX_CHANNELS];
for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) {
ppDeinterleavedFrames[iChannel] = buffer + (pResampler->cacheStrideInFrames*iChannel);
}
@@ -693,7 +693,7 @@ mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount,
}
if (framesJustRead != framesToReadRightNow) {
mal_assert(MAL_FALSE);
mal_assert(MA_FALSE);
break; /* Should never hit this. */
}
}
@@ -740,7 +740,7 @@ mal_uint64 mal_resampler_read(mal_resampler* pResampler, mal_uint64 frameCount,
client. If we have already reached the end of the client's data, we don't want to attempt to read more.
*/
result = mal_resampler__reload_cache(pResampler, &loadedEndOfInput);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
break; /* An error occurred when trying to reload the cache from the client. This does _not_ indicate that the end of the input has been reached. */
}
}
@@ -761,9 +761,9 @@ mal_uint64 mal_resampler_seek(mal_resampler* pResampler, mal_uint64 frameCount,
}
/* Seeking is slightly different depending on whether or not we are seeking by the output or input rate. */
if ((options & MAL_RESAMPLER_SEEK_INPUT_RATE) != 0 || pResampler->config.ratio == 1) {
if ((options & MA_RESAMPLER_SEEK_INPUT_RATE) != 0 || pResampler->config.ratio == 1) {
/* Seeking by input rate. This is a simpler case because we don't need to care about the ratio. */
if ((options & MAL_RESAMPLER_SEEK_NO_CLIENT_READ) != 0) {
if ((options & MA_RESAMPLER_SEEK_NO_CLIENT_READ) != 0) {
/*
Not reading from the client. This is the fast path. We can do this in constant time. Because in this mode the contents
of the cache are left undefined and the fractional part of the window time is left exactly the same (since we're seeking
@@ -780,7 +780,7 @@ mal_uint64 mal_resampler_seek(mal_resampler* pResampler, mal_uint64 frameCount,
}
} else {
/* Seeking by output rate. */
if ((options & MAL_RESAMPLER_SEEK_NO_CLIENT_READ) != 0) {
if ((options & MA_RESAMPLER_SEEK_NO_CLIENT_READ) != 0) {
/* Not reading from the client. This is a fast-ish path, though I'm not doing this in constant time like when seeking by input rate. It's easier to just loop. */
} else {
/* Reading from the client. This case is basically the same as reading, but without the filtering. */
@@ -880,7 +880,7 @@ mal_result mal_resampler_init__linear(mal_resampler* pResampler)
/* The linear implementation always has a window length of 2. */
pResampler->windowLength = 2;
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_uint64 mal_resampler_read_f32__linear(mal_resampler* pResampler, mal_uint64 frameCount, float** ppFrames)
@@ -908,7 +908,7 @@ mal_uint64 mal_resampler_read_s16__linear(mal_resampler* pResampler, mal_uint64
/* I'm cheating here and just using the f32 implementation and converting to s16. This will be changed later - for now just focusing on getting it working. */
float bufferF32[mal_countof(pResampler->cache.s16)];
float* ppFramesF32[MAL_MAX_CHANNELS];
float* ppFramesF32[MA_MAX_CHANNELS];
for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) {
ppFramesF32[iChannel] = bufferF32 + (pResampler->cacheStrideInFrames*iChannel);
}
@@ -944,7 +944,7 @@ mal_result mal_resampler_init__sinc(mal_resampler* pResampler)
mal_assert(pResampler != NULL);
/* TODO: Implement me. Need to initialize the sinc table. */
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_uint64 mal_resampler_read_f32__sinc(mal_resampler* pResampler, mal_uint64 frameCount, float** ppFrames)
@@ -972,7 +972,7 @@ mal_uint64 mal_resampler_read_s16__sinc(mal_resampler* pResampler, mal_uint64 fr
/* I'm cheating here and just using the f32 implementation and converting to s16. This will be changed later - for now just focusing on getting it working. */
float bufferF32[mal_countof(pResampler->cache.s16)];
float* ppFramesF32[MAL_MAX_CHANNELS];
float* ppFramesF32[MA_MAX_CHANNELS];
for (mal_uint32 iChannel = 0; iChannel < pResampler->config.channels; ++iChannel) {
ppFramesF32[iChannel] = bufferF32 + (pResampler->cacheStrideInFrames*iChannel);
}
+50 -50
View File
@@ -4,7 +4,7 @@
// - Lock free (assuming single producer, single consumer)
// - Support for interleaved and deinterleaved streams
// - Must allow the caller to allocate their own block of memory
// - Buffers allocated internally must be aligned to MAL_SIMD_ALIGNMENT
// - Buffers allocated internally must be aligned to MA_SIMD_ALIGNMENT
// USAGE
//
@@ -43,7 +43,7 @@
// consumer is the only one allowed to move the read pointer.
// - Thread safety not fully tested - may even be completely broken.
// - Operates on bytes. May end up adding to higher level helpers for doing everything per audio frame.
// - Maximum buffer size is 0x7FFFFFFF-(MAL_SIMD_ALIGNMENT-1) because of reasons.
// - Maximum buffer size is 0x7FFFFFFF-(MA_SIMD_ALIGNMENT-1) because of reasons.
#ifndef mal_ring_buffer_h
#define mal_ring_buffer_h
@@ -100,34 +100,34 @@ void* mal_pcm_rb_get_subbuffer_ptr(mal_pcm_rb* pRB, size_t subbufferIndex, void*
#endif // mal_ring_buffer_h
#ifdef MINIAUDIO_IMPLEMENTATION
MAL_INLINE mal_uint32 mal_rb__extract_offset_in_bytes(mal_uint32 encodedOffset)
MA_INLINE mal_uint32 mal_rb__extract_offset_in_bytes(mal_uint32 encodedOffset)
{
return encodedOffset & 0x7FFFFFFF;
}
MAL_INLINE mal_uint32 mal_rb__extract_offset_loop_flag(mal_uint32 encodedOffset)
MA_INLINE mal_uint32 mal_rb__extract_offset_loop_flag(mal_uint32 encodedOffset)
{
return encodedOffset & 0x80000000;
}
MAL_INLINE void* mal_rb__get_read_ptr(mal_rb* pRB)
MA_INLINE void* mal_rb__get_read_ptr(mal_rb* pRB)
{
mal_assert(pRB != NULL);
return mal_offset_ptr(pRB->pBuffer, mal_rb__extract_offset_in_bytes(pRB->encodedReadOffset));
}
MAL_INLINE void* mal_rb__get_write_ptr(mal_rb* pRB)
MA_INLINE void* mal_rb__get_write_ptr(mal_rb* pRB)
{
mal_assert(pRB != NULL);
return mal_offset_ptr(pRB->pBuffer, mal_rb__extract_offset_in_bytes(pRB->encodedWriteOffset));
}
MAL_INLINE mal_uint32 mal_rb__construct_offset(mal_uint32 offsetInBytes, mal_uint32 offsetLoopFlag)
MA_INLINE mal_uint32 mal_rb__construct_offset(mal_uint32 offsetInBytes, mal_uint32 offsetLoopFlag)
{
return offsetLoopFlag | offsetInBytes;
}
MAL_INLINE void mal_rb__deconstruct_offset(mal_uint32 encodedOffset, mal_uint32* pOffsetInBytes, mal_uint32* pOffsetLoopFlag)
MA_INLINE void mal_rb__deconstruct_offset(mal_uint32 encodedOffset, mal_uint32* pOffsetInBytes, mal_uint32* pOffsetLoopFlag)
{
mal_assert(pOffsetInBytes != NULL);
mal_assert(pOffsetLoopFlag != NULL);
@@ -140,16 +140,16 @@ MAL_INLINE void mal_rb__deconstruct_offset(mal_uint32 encodedOffset, mal_uint32*
mal_result mal_rb_init_ex(size_t subbufferSizeInBytes, size_t subbufferCount, size_t subbufferStrideInBytes, void* pOptionalPreallocatedBuffer, mal_rb* pRB)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
if (subbufferSizeInBytes == 0 || subbufferCount == 0) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
const mal_uint32 maxSubBufferSize = 0x7FFFFFFF - (MAL_SIMD_ALIGNMENT-1);
const mal_uint32 maxSubBufferSize = 0x7FFFFFFF - (MA_SIMD_ALIGNMENT-1);
if (subbufferSizeInBytes > maxSubBufferSize) {
return MAL_INVALID_ARGS; // Maximum buffer size is ~2GB. The most significant bit is a flag for use internally.
return MA_INVALID_ARGS; // Maximum buffer size is ~2GB. The most significant bit is a flag for use internally.
}
@@ -161,21 +161,21 @@ mal_result mal_rb_init_ex(size_t subbufferSizeInBytes, size_t subbufferCount, si
pRB->subbufferStrideInBytes = (mal_uint32)subbufferStrideInBytes;
pRB->pBuffer = pOptionalPreallocatedBuffer;
} else {
// Here is where we allocate our own buffer. We always want to align this to MAL_SIMD_ALIGNMENT for future SIMD optimization opportunity. To do this
// we need to make sure the stride is a multiple of MAL_SIMD_ALIGNMENT.
pRB->subbufferStrideInBytes = (pRB->subbufferSizeInBytes + (MAL_SIMD_ALIGNMENT-1)) & ~MAL_SIMD_ALIGNMENT;
// Here is where we allocate our own buffer. We always want to align this to MA_SIMD_ALIGNMENT for future SIMD optimization opportunity. To do this
// we need to make sure the stride is a multiple of MA_SIMD_ALIGNMENT.
pRB->subbufferStrideInBytes = (pRB->subbufferSizeInBytes + (MA_SIMD_ALIGNMENT-1)) & ~MA_SIMD_ALIGNMENT;
size_t bufferSizeInBytes = (size_t)pRB->subbufferCount*pRB->subbufferStrideInBytes;
pRB->pBuffer = mal_aligned_malloc(bufferSizeInBytes, MAL_SIMD_ALIGNMENT);
pRB->pBuffer = mal_aligned_malloc(bufferSizeInBytes, MA_SIMD_ALIGNMENT);
if (pRB->pBuffer == NULL) {
return MAL_OUT_OF_MEMORY;
return MA_OUT_OF_MEMORY;
}
mal_zero_memory(pRB->pBuffer, bufferSizeInBytes);
pRB->ownsBuffer = MAL_TRUE;
pRB->ownsBuffer = MA_TRUE;
}
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_rb_init(size_t bufferSizeInBytes, void* pOptionalPreallocatedBuffer, mal_rb* pRB)
@@ -197,7 +197,7 @@ void mal_rb_uninit(mal_rb* pRB)
mal_result mal_rb_acquire_read(mal_rb* pRB, size_t* pSizeInBytes, void** ppBufferOut)
{
if (pRB == NULL || pSizeInBytes == NULL || ppBufferOut == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
// The returned buffer should never move ahead of the write pointer.
@@ -228,18 +228,18 @@ mal_result mal_rb_acquire_read(mal_rb* pRB, size_t* pSizeInBytes, void** ppBuffe
*pSizeInBytes = bytesRequested;
(*ppBufferOut) = mal_rb__get_read_ptr(pRB);
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_rb_commit_read(mal_rb* pRB, size_t sizeInBytes, void* pBufferOut)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
// Validate the buffer.
if (pBufferOut != mal_rb__get_read_ptr(pRB)) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_uint32 readOffset = pRB->encodedReadOffset;
@@ -250,7 +250,7 @@ mal_result mal_rb_commit_read(mal_rb* pRB, size_t sizeInBytes, void* pBufferOut)
// Check that sizeInBytes is correct. It should never go beyond the end of the buffer.
mal_uint32 newReadOffsetInBytes = (mal_uint32)(readOffsetInBytes + sizeInBytes);
if (newReadOffsetInBytes > pRB->subbufferSizeInBytes) {
return MAL_INVALID_ARGS; // <-- sizeInBytes will cause the read offset to overflow.
return MA_INVALID_ARGS; // <-- sizeInBytes will cause the read offset to overflow.
}
// Move the read pointer back to the start if necessary.
@@ -261,13 +261,13 @@ mal_result mal_rb_commit_read(mal_rb* pRB, size_t sizeInBytes, void* pBufferOut)
}
mal_atomic_exchange_32(&pRB->encodedReadOffset, mal_rb__construct_offset(newReadOffsetLoopFlag, newReadOffsetInBytes));
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_rb_acquire_write(mal_rb* pRB, size_t* pSizeInBytes, void** ppBufferOut)
{
if (pRB == NULL || pSizeInBytes == NULL || ppBufferOut == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
// The returned buffer should never overtake the read buffer.
@@ -304,18 +304,18 @@ mal_result mal_rb_acquire_write(mal_rb* pRB, size_t* pSizeInBytes, void** ppBuff
mal_zero_memory(*ppBufferOut, *pSizeInBytes);
}
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_rb_commit_write(mal_rb* pRB, size_t sizeInBytes, void* pBufferOut)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
// Validate the buffer.
if (pBufferOut != mal_rb__get_write_ptr(pRB)) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_uint32 writeOffset = pRB->encodedWriteOffset;
@@ -326,7 +326,7 @@ mal_result mal_rb_commit_write(mal_rb* pRB, size_t sizeInBytes, void* pBufferOut
// Check that sizeInBytes is correct. It should never go beyond the end of the buffer.
mal_uint32 newWriteOffsetInBytes = (mal_uint32)(writeOffsetInBytes + sizeInBytes);
if (newWriteOffsetInBytes > pRB->subbufferSizeInBytes) {
return MAL_INVALID_ARGS; // <-- sizeInBytes will cause the read offset to overflow.
return MA_INVALID_ARGS; // <-- sizeInBytes will cause the read offset to overflow.
}
// Move the read pointer back to the start if necessary.
@@ -337,13 +337,13 @@ mal_result mal_rb_commit_write(mal_rb* pRB, size_t sizeInBytes, void* pBufferOut
}
mal_atomic_exchange_32(&pRB->encodedWriteOffset, mal_rb__construct_offset(newWriteOffsetLoopFlag, newWriteOffsetInBytes));
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_rb_seek_read(mal_rb* pRB, size_t offsetInBytes)
{
if (pRB == NULL || offsetInBytes > pRB->subbufferSizeInBytes) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_uint32 readOffset = pRB->encodedReadOffset;
@@ -377,13 +377,13 @@ mal_result mal_rb_seek_read(mal_rb* pRB, size_t offsetInBytes)
}
mal_atomic_exchange_32(&pRB->encodedReadOffset, mal_rb__construct_offset(newReadOffsetInBytes, newReadOffsetLoopFlag));
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_rb_seek_write(mal_rb* pRB, size_t offsetInBytes)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_uint32 readOffset = pRB->encodedReadOffset;
@@ -417,7 +417,7 @@ mal_result mal_rb_seek_write(mal_rb* pRB, size_t offsetInBytes)
}
mal_atomic_exchange_32(&pRB->encodedWriteOffset, mal_rb__construct_offset(newWriteOffsetInBytes, newWriteOffsetLoopFlag));
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_int32 mal_rb_pointer_distance(mal_rb* pRB)
@@ -487,25 +487,25 @@ mal_uint32 mal_pcm_rb_get_bpf(mal_pcm_rb* pRB)
mal_result mal_pcm_rb_init_ex(mal_format format, mal_uint32 channels, size_t subbufferSizeInFrames, size_t subbufferCount, size_t subbufferStrideInFrames, void* pOptionalPreallocatedBuffer, mal_pcm_rb* pRB)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_zero_object(pRB);
mal_uint32 bpf = mal_get_bytes_per_frame(format, channels);
if (bpf == 0) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
mal_result result = mal_rb_init_ex(subbufferSizeInFrames*bpf, subbufferCount, subbufferStrideInFrames*bpf, pOptionalPreallocatedBuffer, &pRB->rb);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
return result;
}
pRB->format = format;
pRB->channels = channels;
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_pcm_rb_init(mal_format format, mal_uint32 channels, size_t bufferSizeInFrames, void* pOptionalPreallocatedBuffer, mal_pcm_rb* pRB)
@@ -528,24 +528,24 @@ mal_result mal_pcm_rb_acquire_read(mal_pcm_rb* pRB, size_t* pSizeInFrames, void*
mal_result result;
if (pRB == NULL || pSizeInFrames == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
sizeInBytes = *pSizeInFrames * mal_pcm_rb_get_bpf(pRB);
result = mal_rb_acquire_read(&pRB->rb, &sizeInBytes, ppBufferOut);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
return result;
}
*pSizeInFrames = sizeInBytes / mal_pcm_rb_get_bpf(pRB);
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_pcm_rb_commit_read(mal_pcm_rb* pRB, size_t sizeInFrames, void* pBufferOut)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
return mal_rb_commit_read(&pRB->rb, sizeInFrames * mal_pcm_rb_get_bpf(pRB), pBufferOut);
@@ -557,24 +557,24 @@ mal_result mal_pcm_rb_acquire_write(mal_pcm_rb* pRB, size_t* pSizeInFrames, void
mal_result result;
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
sizeInBytes = *pSizeInFrames * mal_pcm_rb_get_bpf(pRB);
result = mal_rb_acquire_write(&pRB->rb, &sizeInBytes, ppBufferOut);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
return result;
}
*pSizeInFrames = sizeInBytes / mal_pcm_rb_get_bpf(pRB);
return MAL_SUCCESS;
return MA_SUCCESS;
}
mal_result mal_pcm_rb_commit_write(mal_pcm_rb* pRB, size_t sizeInFrames, void* pBufferOut)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
return mal_rb_commit_write(&pRB->rb, sizeInFrames * mal_pcm_rb_get_bpf(pRB), pBufferOut);
@@ -583,7 +583,7 @@ mal_result mal_pcm_rb_commit_write(mal_pcm_rb* pRB, size_t sizeInFrames, void* p
mal_result mal_pcm_rb_seek_read(mal_pcm_rb* pRB, size_t offsetInFrames)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
return mal_rb_seek_read(&pRB->rb, offsetInFrames * mal_pcm_rb_get_bpf(pRB));
@@ -592,7 +592,7 @@ mal_result mal_pcm_rb_seek_read(mal_pcm_rb* pRB, size_t offsetInFrames)
mal_result mal_pcm_rb_seek_write(mal_pcm_rb* pRB, size_t offsetInFrames)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
return mal_rb_seek_write(&pRB->rb, offsetInFrames * mal_pcm_rb_get_bpf(pRB));
@@ -601,7 +601,7 @@ mal_result mal_pcm_rb_seek_write(mal_pcm_rb* pRB, size_t offsetInFrames)
mal_int32 mal_pcm_rb_pointer_disance(mal_pcm_rb* pRB)
{
if (pRB == NULL) {
return MAL_INVALID_ARGS;
return MA_INVALID_ARGS;
}
return mal_rb_pointer_distance(&pRB->rb) / mal_pcm_rb_get_bpf(pRB);
+2 -2
View File
@@ -1,7 +1,7 @@
#define DR_WAV_IMPLEMENTATION
#include "../../../../dr_libs/dr_wav.h"
#define MAL_DEBUG_OUTPUT
#define MA_DEBUG_OUTPUT
#define MINIAUDIO_IMPLEMENTATION
#include "../../miniaudio.h"
#include "../mal_resampler.h"
@@ -38,7 +38,7 @@ int main(int argc, char** argv)
resamplerConfig.pUserData = NULL;
result = mal_resampler_init(&resamplerConfig, &resampler);
if (result != MAL_SUCCESS) {
if (result != MA_SUCCESS) {
printf("Failed to initialize resampler.\n");
return -1;
}