SoundData -> SourceData;

This commit is contained in:
bjorn 2017-01-05 21:10:01 -08:00
parent b28d822797
commit d437d04259
5 changed files with 57 additions and 57 deletions

View File

@ -8,11 +8,11 @@ static ALenum lovrSourceGetState(Source* source) {
return state;
}
Source* lovrSourceCreate(SoundData* soundData) {
Source* lovrSourceCreate(SourceData* sourceData) {
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
if (!source) return NULL;
source->soundData = soundData;
source->sourceData = sourceData;
source->isLooping = 0;
alGenSources(1, &source->id);
alGenBuffers(SOURCE_BUFFERS, source->buffers);
@ -24,26 +24,26 @@ void lovrSourceDestroy(const Ref* ref) {
Source* source = containerof(ref, Source);
alDeleteSources(1, &source->id);
alDeleteBuffers(SOURCE_BUFFERS, source->buffers);
lovrSoundDataDestroy(source->soundData);
lovrSourceDataDestroy(source->sourceData);
free(source);
}
int lovrSourceGetBitDepth(Source* source) {
return source->soundData->bitDepth;
return source->sourceData->bitDepth;
}
int lovrSourceGetChannels(Source* source) {
return source->soundData->channels;
return source->sourceData->channels;
}
int lovrSourceGetDuration(Source* source) {
return source->soundData->samples;
return source->sourceData->samples;
}
// Get the OpenAL sound format for the sound
ALenum lovrSourceGetFormat(Source* source) {
int channels = source->soundData->channels;
int bitDepth = source->soundData->bitDepth;
int channels = source->sourceData->channels;
int bitDepth = source->sourceData->bitDepth;
if (bitDepth == 8 && channels == 1) {
return AL_FORMAT_MONO8;
@ -69,7 +69,7 @@ void lovrSourceGetPosition(Source* source, float* x, float* y, float* z) {
}
int lovrSourceGetSampleRate(Source* source) {
return source->soundData->sampleRate;
return source->sourceData->sampleRate;
}
float lovrSourceGetVolume(Source* source) {
@ -135,7 +135,7 @@ void lovrSourceRewind(Source* source) {
void lovrSourceSeek(Source* source, int sample) {
int wasPaused = lovrSourceIsPaused(source);
lovrSourceStop(source);
lovrSoundDataSeek(source->soundData, sample);
lovrSourceDataSeek(source->sourceData, sample);
lovrSourcePlay(source);
if (wasPaused) {
lovrSourcePause(source);
@ -173,33 +173,33 @@ void lovrSourceStop(Source* source) {
alSourcei(source->id, AL_BUFFER, AL_NONE);
// Rewind the decoder
lovrSoundDataRewind(source->soundData);
lovrSourceDataRewind(source->sourceData);
}
// Fills buffers with data and queues them, called once initially and over time to stream more data
void lovrSourceStream(Source* source, ALuint* buffers, int count) {
SoundData* soundData = source->soundData;
SourceData* sourceData = source->sourceData;
ALenum format = lovrSourceGetFormat(source);
int frequency = soundData->sampleRate;
int frequency = sourceData->sampleRate;
int samples = 0;
int n = 0;
// Keep decoding until there is nothing left to decode or all the buffers are filled
while (n < count && (samples = lovrSoundDataDecode(soundData)) != 0) {
alBufferData(buffers[n++], format, soundData->buffer, samples * sizeof(ALshort), frequency);
while (n < count && (samples = lovrSourceDataDecode(sourceData)) != 0) {
alBufferData(buffers[n++], format, sourceData->buffer, samples * sizeof(ALshort), frequency);
}
alSourceQueueBuffers(source->id, n, buffers);
if (samples == 0 && source->isLooping && n < count) {
lovrSoundDataRewind(soundData);
lovrSourceDataRewind(sourceData);
return lovrSourceStream(source, buffers + n, count - n);
}
}
int lovrSourceTell(Source* source) {
int decoderOffset = lovrSoundDataTell(source->soundData);
int samplesPerBuffer = source->soundData->bufferSize / source->soundData->channels / sizeof(ALshort);
int decoderOffset = lovrSourceDataTell(source->sourceData);
int samplesPerBuffer = source->sourceData->bufferSize / source->sourceData->channels / sizeof(ALshort);
int queuedBuffers, sampleOffset;
alGetSourcei(source->id, AL_BUFFERS_QUEUED, &queuedBuffers);
alGetSourcei(source->id, AL_SAMPLE_OFFSET, &sampleOffset);
@ -207,7 +207,7 @@ int lovrSourceTell(Source* source) {
int offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset;
if (offset < 0) {
return offset + source->soundData->samples;
return offset + source->sourceData->samples;
} else {
return offset;
}

View File

@ -20,11 +20,11 @@ typedef struct {
int bufferSize;
void* buffer;
void* decoder;
} SoundData;
} SourceData;
typedef struct {
Ref ref;
SoundData* soundData;
SourceData* sourceData;
ALuint id;
ALuint buffers[SOURCE_BUFFERS];
int isLooping;
@ -32,7 +32,7 @@ typedef struct {
#endif
Source* lovrSourceCreate(SoundData* soundData);
Source* lovrSourceCreate(SourceData* sourceData);
void lovrSourceDestroy(const Ref* ref);
int lovrSourceGetBitDepth(Source* source);
int lovrSourceGetChannels(Source* source);

View File

@ -2,41 +2,41 @@
#include "vendor/stb/stb_vorbis.h"
#include <stdlib.h>
SoundData* lovrSoundDataFromFile(void* data, int size) {
SoundData* soundData = malloc(sizeof(SoundData));
if (!soundData) return NULL;
SourceData* lovrSourceDataFromFile(void* data, int size) {
SourceData* sourceData = malloc(sizeof(SourceData));
if (!sourceData) return NULL;
stb_vorbis* decoder = stb_vorbis_open_memory(data, size, NULL, NULL);
if (!decoder) {
free(soundData);
free(sourceData);
return NULL;
}
stb_vorbis_info info = stb_vorbis_get_info(decoder);
soundData->bitDepth = 16;
soundData->channels = info.channels;
soundData->sampleRate = info.sample_rate;
soundData->samples = stb_vorbis_stream_length_in_samples(decoder);
soundData->decoder = decoder;
soundData->bufferSize = soundData->channels * 4096 * sizeof(short);
soundData->buffer = malloc(soundData->bufferSize);
sourceData->bitDepth = 16;
sourceData->channels = info.channels;
sourceData->sampleRate = info.sample_rate;
sourceData->samples = stb_vorbis_stream_length_in_samples(decoder);
sourceData->decoder = decoder;
sourceData->bufferSize = sourceData->channels * 4096 * sizeof(short);
sourceData->buffer = malloc(sourceData->bufferSize);
return soundData;
return sourceData;
}
void lovrSoundDataDestroy(SoundData* soundData) {
stb_vorbis_close(soundData->decoder);
free(soundData->buffer);
free(soundData);
void lovrSourceDataDestroy(SourceData* sourceData) {
stb_vorbis_close(sourceData->decoder);
free(sourceData->buffer);
free(sourceData);
}
int lovrSoundDataDecode(SoundData* soundData) {
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
short* buffer = (short*) soundData->buffer;
int channels = soundData->channels;
int capacity = soundData->bufferSize / sizeof(short);
int lovrSourceDataDecode(SourceData* sourceData) {
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
short* buffer = (short*) sourceData->buffer;
int channels = sourceData->channels;
int capacity = sourceData->bufferSize / sizeof(short);
int samples = 0;
while (samples < capacity) {
@ -48,17 +48,17 @@ int lovrSoundDataDecode(SoundData* soundData) {
return samples;
}
void lovrSoundDataRewind(SoundData* soundData) {
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
void lovrSourceDataRewind(SourceData* sourceData) {
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
stb_vorbis_seek_start(decoder);
}
void lovrSoundDataSeek(SoundData* soundData, int sample) {
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
void lovrSourceDataSeek(SourceData* sourceData, int sample) {
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
stb_vorbis_seek(decoder, sample);
}
int lovrSoundDataTell(SoundData* soundData) {
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
int lovrSourceDataTell(SourceData* sourceData) {
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
return stb_vorbis_get_sample_offset(decoder);
}

View File

@ -1,8 +1,8 @@
#include "audio/source.h"
SoundData* lovrSoundDataFromFile(void* data, int size);
void lovrSoundDataDestroy(SoundData* soundData);
int lovrSoundDataDecode(SoundData* soundData);
void lovrSoundDataRewind(SoundData* soundData);
void lovrSoundDataSeek(SoundData* soundData, int sample);
int lovrSoundDataTell(SoundData* soundData);
SourceData* lovrSourceDataFromFile(void* data, int size);
void lovrSourceDataDestroy(SourceData* sourceData);
int lovrSourceDataDecode(SourceData* sourceData);
void lovrSourceDataRewind(SourceData* sourceData);
void lovrSourceDataSeek(SourceData* sourceData, int sample);
int lovrSourceDataTell(SourceData* sourceData);

View File

@ -41,7 +41,7 @@ int l_lovrAudioNewSource(lua_State* L) {
return luaL_error(L, "Could not load source from file '%s'", filename);
}
SoundData* soundData = lovrSoundDataFromFile(data, size);
luax_pushtype(L, Source, lovrSourceCreate(soundData));
SourceData* sourceData = lovrSourceDataFromFile(data, size);
luax_pushtype(L, Source, lovrSourceCreate(sourceData));
return 1;
}