mirror of
https://github.com/bjornbytes/lovr.git
synced 2024-07-04 13:33:34 +00:00
SoundData -> SourceData;
This commit is contained in:
parent
b28d822797
commit
d437d04259
|
@ -8,11 +8,11 @@ static ALenum lovrSourceGetState(Source* source) {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
Source* lovrSourceCreate(SoundData* soundData) {
|
Source* lovrSourceCreate(SourceData* sourceData) {
|
||||||
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
|
Source* source = lovrAlloc(sizeof(Source), lovrSourceDestroy);
|
||||||
if (!source) return NULL;
|
if (!source) return NULL;
|
||||||
|
|
||||||
source->soundData = soundData;
|
source->sourceData = sourceData;
|
||||||
source->isLooping = 0;
|
source->isLooping = 0;
|
||||||
alGenSources(1, &source->id);
|
alGenSources(1, &source->id);
|
||||||
alGenBuffers(SOURCE_BUFFERS, source->buffers);
|
alGenBuffers(SOURCE_BUFFERS, source->buffers);
|
||||||
|
@ -24,26 +24,26 @@ void lovrSourceDestroy(const Ref* ref) {
|
||||||
Source* source = containerof(ref, Source);
|
Source* source = containerof(ref, Source);
|
||||||
alDeleteSources(1, &source->id);
|
alDeleteSources(1, &source->id);
|
||||||
alDeleteBuffers(SOURCE_BUFFERS, source->buffers);
|
alDeleteBuffers(SOURCE_BUFFERS, source->buffers);
|
||||||
lovrSoundDataDestroy(source->soundData);
|
lovrSourceDataDestroy(source->sourceData);
|
||||||
free(source);
|
free(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSourceGetBitDepth(Source* source) {
|
int lovrSourceGetBitDepth(Source* source) {
|
||||||
return source->soundData->bitDepth;
|
return source->sourceData->bitDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSourceGetChannels(Source* source) {
|
int lovrSourceGetChannels(Source* source) {
|
||||||
return source->soundData->channels;
|
return source->sourceData->channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSourceGetDuration(Source* source) {
|
int lovrSourceGetDuration(Source* source) {
|
||||||
return source->soundData->samples;
|
return source->sourceData->samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the OpenAL sound format for the sound
|
// Get the OpenAL sound format for the sound
|
||||||
ALenum lovrSourceGetFormat(Source* source) {
|
ALenum lovrSourceGetFormat(Source* source) {
|
||||||
int channels = source->soundData->channels;
|
int channels = source->sourceData->channels;
|
||||||
int bitDepth = source->soundData->bitDepth;
|
int bitDepth = source->sourceData->bitDepth;
|
||||||
|
|
||||||
if (bitDepth == 8 && channels == 1) {
|
if (bitDepth == 8 && channels == 1) {
|
||||||
return AL_FORMAT_MONO8;
|
return AL_FORMAT_MONO8;
|
||||||
|
@ -69,7 +69,7 @@ void lovrSourceGetPosition(Source* source, float* x, float* y, float* z) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSourceGetSampleRate(Source* source) {
|
int lovrSourceGetSampleRate(Source* source) {
|
||||||
return source->soundData->sampleRate;
|
return source->sourceData->sampleRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
float lovrSourceGetVolume(Source* source) {
|
float lovrSourceGetVolume(Source* source) {
|
||||||
|
@ -135,7 +135,7 @@ void lovrSourceRewind(Source* source) {
|
||||||
void lovrSourceSeek(Source* source, int sample) {
|
void lovrSourceSeek(Source* source, int sample) {
|
||||||
int wasPaused = lovrSourceIsPaused(source);
|
int wasPaused = lovrSourceIsPaused(source);
|
||||||
lovrSourceStop(source);
|
lovrSourceStop(source);
|
||||||
lovrSoundDataSeek(source->soundData, sample);
|
lovrSourceDataSeek(source->sourceData, sample);
|
||||||
lovrSourcePlay(source);
|
lovrSourcePlay(source);
|
||||||
if (wasPaused) {
|
if (wasPaused) {
|
||||||
lovrSourcePause(source);
|
lovrSourcePause(source);
|
||||||
|
@ -173,33 +173,33 @@ void lovrSourceStop(Source* source) {
|
||||||
alSourcei(source->id, AL_BUFFER, AL_NONE);
|
alSourcei(source->id, AL_BUFFER, AL_NONE);
|
||||||
|
|
||||||
// Rewind the decoder
|
// 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
|
// 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) {
|
void lovrSourceStream(Source* source, ALuint* buffers, int count) {
|
||||||
SoundData* soundData = source->soundData;
|
SourceData* sourceData = source->sourceData;
|
||||||
ALenum format = lovrSourceGetFormat(source);
|
ALenum format = lovrSourceGetFormat(source);
|
||||||
int frequency = soundData->sampleRate;
|
int frequency = sourceData->sampleRate;
|
||||||
int samples = 0;
|
int samples = 0;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
// Keep decoding until there is nothing left to decode or all the buffers are filled
|
// Keep decoding until there is nothing left to decode or all the buffers are filled
|
||||||
while (n < count && (samples = lovrSoundDataDecode(soundData)) != 0) {
|
while (n < count && (samples = lovrSourceDataDecode(sourceData)) != 0) {
|
||||||
alBufferData(buffers[n++], format, soundData->buffer, samples * sizeof(ALshort), frequency);
|
alBufferData(buffers[n++], format, sourceData->buffer, samples * sizeof(ALshort), frequency);
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourceQueueBuffers(source->id, n, buffers);
|
alSourceQueueBuffers(source->id, n, buffers);
|
||||||
|
|
||||||
if (samples == 0 && source->isLooping && n < count) {
|
if (samples == 0 && source->isLooping && n < count) {
|
||||||
lovrSoundDataRewind(soundData);
|
lovrSourceDataRewind(sourceData);
|
||||||
return lovrSourceStream(source, buffers + n, count - n);
|
return lovrSourceStream(source, buffers + n, count - n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSourceTell(Source* source) {
|
int lovrSourceTell(Source* source) {
|
||||||
int decoderOffset = lovrSoundDataTell(source->soundData);
|
int decoderOffset = lovrSourceDataTell(source->sourceData);
|
||||||
int samplesPerBuffer = source->soundData->bufferSize / source->soundData->channels / sizeof(ALshort);
|
int samplesPerBuffer = source->sourceData->bufferSize / source->sourceData->channels / sizeof(ALshort);
|
||||||
int queuedBuffers, sampleOffset;
|
int queuedBuffers, sampleOffset;
|
||||||
alGetSourcei(source->id, AL_BUFFERS_QUEUED, &queuedBuffers);
|
alGetSourcei(source->id, AL_BUFFERS_QUEUED, &queuedBuffers);
|
||||||
alGetSourcei(source->id, AL_SAMPLE_OFFSET, &sampleOffset);
|
alGetSourcei(source->id, AL_SAMPLE_OFFSET, &sampleOffset);
|
||||||
|
@ -207,7 +207,7 @@ int lovrSourceTell(Source* source) {
|
||||||
int offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset;
|
int offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset;
|
||||||
|
|
||||||
if (offset < 0) {
|
if (offset < 0) {
|
||||||
return offset + source->soundData->samples;
|
return offset + source->sourceData->samples;
|
||||||
} else {
|
} else {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,11 @@ typedef struct {
|
||||||
int bufferSize;
|
int bufferSize;
|
||||||
void* buffer;
|
void* buffer;
|
||||||
void* decoder;
|
void* decoder;
|
||||||
} SoundData;
|
} SourceData;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Ref ref;
|
Ref ref;
|
||||||
SoundData* soundData;
|
SourceData* sourceData;
|
||||||
ALuint id;
|
ALuint id;
|
||||||
ALuint buffers[SOURCE_BUFFERS];
|
ALuint buffers[SOURCE_BUFFERS];
|
||||||
int isLooping;
|
int isLooping;
|
||||||
|
@ -32,7 +32,7 @@ typedef struct {
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Source* lovrSourceCreate(SoundData* soundData);
|
Source* lovrSourceCreate(SourceData* sourceData);
|
||||||
void lovrSourceDestroy(const Ref* ref);
|
void lovrSourceDestroy(const Ref* ref);
|
||||||
int lovrSourceGetBitDepth(Source* source);
|
int lovrSourceGetBitDepth(Source* source);
|
||||||
int lovrSourceGetChannels(Source* source);
|
int lovrSourceGetChannels(Source* source);
|
||||||
|
|
|
@ -2,41 +2,41 @@
|
||||||
#include "vendor/stb/stb_vorbis.h"
|
#include "vendor/stb/stb_vorbis.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
SoundData* lovrSoundDataFromFile(void* data, int size) {
|
SourceData* lovrSourceDataFromFile(void* data, int size) {
|
||||||
SoundData* soundData = malloc(sizeof(SoundData));
|
SourceData* sourceData = malloc(sizeof(SourceData));
|
||||||
if (!soundData) return NULL;
|
if (!sourceData) return NULL;
|
||||||
|
|
||||||
stb_vorbis* decoder = stb_vorbis_open_memory(data, size, NULL, NULL);
|
stb_vorbis* decoder = stb_vorbis_open_memory(data, size, NULL, NULL);
|
||||||
|
|
||||||
if (!decoder) {
|
if (!decoder) {
|
||||||
free(soundData);
|
free(sourceData);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
stb_vorbis_info info = stb_vorbis_get_info(decoder);
|
stb_vorbis_info info = stb_vorbis_get_info(decoder);
|
||||||
|
|
||||||
soundData->bitDepth = 16;
|
sourceData->bitDepth = 16;
|
||||||
soundData->channels = info.channels;
|
sourceData->channels = info.channels;
|
||||||
soundData->sampleRate = info.sample_rate;
|
sourceData->sampleRate = info.sample_rate;
|
||||||
soundData->samples = stb_vorbis_stream_length_in_samples(decoder);
|
sourceData->samples = stb_vorbis_stream_length_in_samples(decoder);
|
||||||
soundData->decoder = decoder;
|
sourceData->decoder = decoder;
|
||||||
soundData->bufferSize = soundData->channels * 4096 * sizeof(short);
|
sourceData->bufferSize = sourceData->channels * 4096 * sizeof(short);
|
||||||
soundData->buffer = malloc(soundData->bufferSize);
|
sourceData->buffer = malloc(sourceData->bufferSize);
|
||||||
|
|
||||||
return soundData;
|
return sourceData;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrSoundDataDestroy(SoundData* soundData) {
|
void lovrSourceDataDestroy(SourceData* sourceData) {
|
||||||
stb_vorbis_close(soundData->decoder);
|
stb_vorbis_close(sourceData->decoder);
|
||||||
free(soundData->buffer);
|
free(sourceData->buffer);
|
||||||
free(soundData);
|
free(sourceData);
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSoundDataDecode(SoundData* soundData) {
|
int lovrSourceDataDecode(SourceData* sourceData) {
|
||||||
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
|
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
|
||||||
short* buffer = (short*) soundData->buffer;
|
short* buffer = (short*) sourceData->buffer;
|
||||||
int channels = soundData->channels;
|
int channels = sourceData->channels;
|
||||||
int capacity = soundData->bufferSize / sizeof(short);
|
int capacity = sourceData->bufferSize / sizeof(short);
|
||||||
int samples = 0;
|
int samples = 0;
|
||||||
|
|
||||||
while (samples < capacity) {
|
while (samples < capacity) {
|
||||||
|
@ -48,17 +48,17 @@ int lovrSoundDataDecode(SoundData* soundData) {
|
||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrSoundDataRewind(SoundData* soundData) {
|
void lovrSourceDataRewind(SourceData* sourceData) {
|
||||||
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
|
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
|
||||||
stb_vorbis_seek_start(decoder);
|
stb_vorbis_seek_start(decoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrSoundDataSeek(SoundData* soundData, int sample) {
|
void lovrSourceDataSeek(SourceData* sourceData, int sample) {
|
||||||
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
|
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
|
||||||
stb_vorbis_seek(decoder, sample);
|
stb_vorbis_seek(decoder, sample);
|
||||||
}
|
}
|
||||||
|
|
||||||
int lovrSoundDataTell(SoundData* soundData) {
|
int lovrSourceDataTell(SourceData* sourceData) {
|
||||||
stb_vorbis* decoder = (stb_vorbis*) soundData->decoder;
|
stb_vorbis* decoder = (stb_vorbis*) sourceData->decoder;
|
||||||
return stb_vorbis_get_sample_offset(decoder);
|
return stb_vorbis_get_sample_offset(decoder);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include "audio/source.h"
|
#include "audio/source.h"
|
||||||
|
|
||||||
SoundData* lovrSoundDataFromFile(void* data, int size);
|
SourceData* lovrSourceDataFromFile(void* data, int size);
|
||||||
void lovrSoundDataDestroy(SoundData* soundData);
|
void lovrSourceDataDestroy(SourceData* sourceData);
|
||||||
int lovrSoundDataDecode(SoundData* soundData);
|
int lovrSourceDataDecode(SourceData* sourceData);
|
||||||
void lovrSoundDataRewind(SoundData* soundData);
|
void lovrSourceDataRewind(SourceData* sourceData);
|
||||||
void lovrSoundDataSeek(SoundData* soundData, int sample);
|
void lovrSourceDataSeek(SourceData* sourceData, int sample);
|
||||||
int lovrSoundDataTell(SoundData* soundData);
|
int lovrSourceDataTell(SourceData* sourceData);
|
||||||
|
|
|
@ -41,7 +41,7 @@ int l_lovrAudioNewSource(lua_State* L) {
|
||||||
return luaL_error(L, "Could not load source from file '%s'", filename);
|
return luaL_error(L, "Could not load source from file '%s'", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
SoundData* soundData = lovrSoundDataFromFile(data, size);
|
SourceData* sourceData = lovrSourceDataFromFile(data, size);
|
||||||
luax_pushtype(L, Source, lovrSourceCreate(soundData));
|
luax_pushtype(L, Source, lovrSourceCreate(sourceData));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue