stop depending on audio from data

This commit is contained in:
Nevyn Bengtsson 2020-12-15 10:11:28 +01:00 committed by Bjorn
parent 8b594332dc
commit e1c94c3084
4 changed files with 31 additions and 28 deletions

View File

@ -10,9 +10,11 @@
#include <stdio.h>
#include "lib/miniaudio/miniaudio.h"
#include "audio/spatializer.h"
#include "audio/audio_internal.h"
static const ma_format miniAudioFormatFromLovr[] = {
[SAMPLE_I16] = ma_format_s16,
[SAMPLE_F32] = ma_format_f32
};
#define OUTPUT_FORMAT SAMPLE_F32
#define OUTPUT_CHANNELS 2
@ -60,7 +62,7 @@ static bool mix(Source* source, float* output, uint32_t count) {
// frameLimitOut =
while (count > 0) {
uint32_t chunk = MIN(sizeof(raw) / bytesPerAudioFrame(source->sound->channels, source->sound->format),
uint32_t chunk = MIN(sizeof(raw) / SampleFormatBytesPerFrame(source->sound->channels, source->sound->format),
ma_data_converter_get_required_input_frame_count(source->converter, count));
// ^^^ Note need to min `count` with 'capacity of aux buffer' and 'capacity of mix buffer'
// could skip min-ing with one of the buffers if you can guarantee that one is bigger/equal to the other (you can because their formats are known)
@ -72,7 +74,7 @@ static bool mix(Source* source, float* output, uint32_t count) {
if (source->spatial) {
state.spatializer->apply(source, source->transform, aux, mix, framesOut);
} else {
memcpy(mix, aux, framesOut * bytesPerAudioFrame(OUTPUT_CHANNELS, SAMPLE_F32));
memcpy(mix, aux, framesOut * SampleFormatBytesPerFrame(OUTPUT_CHANNELS, SAMPLE_F32));
}
for (uint32_t i = 0; i < framesOut * OUTPUT_CHANNELS; i++) {
@ -116,7 +118,7 @@ static void onPlayback(ma_device* device, void* output, const void* _, uint32_t
static void onCapture(ma_device* device, void* output, const void* input, uint32_t frames) {
// note: ma_pcm_rb is lockless
void *store;
size_t bytesPerFrame = bytesPerAudioFrame(CAPTURE_CHANNELS, OUTPUT_FORMAT);
size_t bytesPerFrame = SampleFormatBytesPerFrame(CAPTURE_CHANNELS, OUTPUT_FORMAT);
while(frames > 0) {
uint32_t availableFrames = frames;
ma_result acquire_status = ma_pcm_rb_acquire_write(&state.captureRingbuffer, &availableFrames, &store);
@ -422,7 +424,7 @@ struct SoundData* lovrAudioCapture(uint32_t frameCount, SoundData *soundData, ui
lovrAssert(offset + frameCount <= soundData->frames, "Tried to write samples past the end of a SoundData buffer");
}
uint32_t bytesPerFrame = bytesPerAudioFrame(CAPTURE_CHANNELS, OUTPUT_FORMAT);
uint32_t bytesPerFrame = SampleFormatBytesPerFrame(CAPTURE_CHANNELS, OUTPUT_FORMAT);
while(frameCount > 0) {
uint32_t availableFramesInRB = frameCount;
void *store;

View File

@ -1,15 +0,0 @@
#include "lib/miniaudio/miniaudio.h"
static const ma_format miniAudioFormatFromLovr[] = {
[SAMPLE_I16] = ma_format_s16,
[SAMPLE_F32] = ma_format_f32
};
static const ma_format sampleSizes[] = {
[SAMPLE_I16] = 2,
[SAMPLE_F32] = 4
};
static inline size_t bytesPerAudioFrame(int channelCount, SampleFormat fmt) {
return channelCount * sampleSizes[fmt];
}

View File

@ -4,15 +4,29 @@
#include "core/ref.h"
#include "lib/stb/stb_vorbis.h"
#include "lib/miniaudio/miniaudio.h"
#include "audio/audio_internal.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static const ma_format miniAudioFormatFromLovr[] = {
[SAMPLE_I16] = ma_format_s16,
[SAMPLE_F32] = ma_format_f32
};
static const ma_format sampleSizes[] = {
[SAMPLE_I16] = 2,
[SAMPLE_F32] = 4
};
size_t SampleFormatBytesPerFrame(int channelCount, SampleFormat fmt)
{
return channelCount * sampleSizes[fmt];
}
static uint32_t lovrSoundDataReadRaw(SoundData* soundData, uint32_t offset, uint32_t count, void* data) {
uint8_t* p = soundData->blob->data;
uint32_t n = MIN(count, soundData->frames - offset);
size_t stride = bytesPerAudioFrame(soundData->channels, soundData->format);
size_t stride = SampleFormatBytesPerFrame(soundData->channels, soundData->format);
memcpy(data, p + offset * stride, n * stride);
return n;
}
@ -52,7 +66,7 @@ static uint32_t lovrSoundDataReadMp3(SoundData* soundData, uint32_t offset, uint
*/
static uint32_t lovrSoundDataReadRing(SoundData* soundData, uint32_t offset, uint32_t count, void* data) {
size_t bytesPerFrame = bytesPerAudioFrame(soundData->channels, soundData->format);
size_t bytesPerFrame = SampleFormatBytesPerFrame(soundData->channels, soundData->format);
size_t totalRead = 0;
while(count > 0) {
uint32_t availableFramesInRing = count;
@ -87,7 +101,7 @@ SoundData* lovrSoundDataCreateRaw(uint32_t frameCount, uint32_t channelCount, ui
soundData->blob = blob;
lovrRetain(blob);
} else {
size_t size = frameCount * bytesPerAudioFrame(channelCount, format);
size_t size = frameCount * SampleFormatBytesPerFrame(channelCount, format);
void* data = calloc(1, size);
lovrAssert(data, "Out of memory");
soundData->blob = lovrBlobCreate(data, size, "SoundData");
@ -124,7 +138,7 @@ SoundData* lovrSoundDataCreateFromFile(struct Blob* blob, bool decode) {
if (decode) {
soundData->read = lovrSoundDataReadRaw;
size_t size = soundData->frames * bytesPerAudioFrame(soundData->channels, soundData->format);
size_t size = soundData->frames * SampleFormatBytesPerFrame(soundData->channels, soundData->format);
void* data = calloc(1, size);
lovrAssert(data, "Out of memory");
soundData->blob = lovrBlobCreate(data, size, "SoundData");
@ -148,7 +162,7 @@ size_t lovrSoundDataStreamAppendBlob(SoundData *dest, struct Blob* blob) {
void *store;
size_t blobOffset = 0;
size_t bytesPerFrame = bytesPerAudioFrame(dest->channels, dest->format);
size_t bytesPerFrame = SampleFormatBytesPerFrame(dest->channels, dest->format);
size_t frameCount = blob->size / bytesPerFrame;
size_t framesAppended = 0;
while(frameCount > 0) {
@ -177,7 +191,7 @@ size_t lovrSoundDataStreamAppendSound(SoundData *dest, SoundData *src) {
}
void lovrSoundDataSetSample(SoundData* soundData, size_t index, float value) {
size_t byteIndex = index * bytesPerAudioFrame(soundData->channels, soundData->format);
size_t byteIndex = index * SampleFormatBytesPerFrame(soundData->channels, soundData->format);
lovrAssert(byteIndex < soundData->blob->size, "Sample index out of range");
switch (soundData->format) {
case SAMPLE_I16: ((int16_t*) soundData->blob->data)[index] = value * SHRT_MAX; break;

View File

@ -15,6 +15,8 @@ typedef enum {
SAMPLE_I16
} SampleFormat;
size_t SampleFormatBytesPerFrame(int channelCount, SampleFormat fmt);
typedef struct SoundData {
SoundDataReader* read;
void* decoder;