1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-02 12:33:52 +00:00

Audio cleanup;

This commit is contained in:
bjorn 2019-05-13 03:53:17 -07:00
parent 97e3774718
commit 83b8ee2d08
7 changed files with 78 additions and 77 deletions

View file

@ -6,6 +6,7 @@
#include "audio/source.h" #include "audio/source.h"
#include "data/audioStream.h" #include "data/audioStream.h"
#include "data/soundData.h" #include "data/soundData.h"
#include <stdlib.h>
const char* SourceTypes[] = { const char* SourceTypes[] = {
[SOURCE_STATIC] = "static", [SOURCE_STATIC] = "static",
@ -34,7 +35,7 @@ static int l_lovrAudioGetDopplerEffect(lua_State* L) {
static int l_lovrAudioGetMicrophoneNames(lua_State* L) { static int l_lovrAudioGetMicrophoneNames(lua_State* L) {
const char* names[MAX_MICROPHONES]; const char* names[MAX_MICROPHONES];
uint8_t count; uint32_t count;
lovrAudioGetMicrophoneNames(names, &count); lovrAudioGetMicrophoneNames(names, &count);
if (lua_istable(L, 1)) { if (lua_istable(L, 1)) {
@ -44,7 +45,7 @@ static int l_lovrAudioGetMicrophoneNames(lua_State* L) {
lua_createtable(L, count, 0); lua_createtable(L, count, 0);
} }
for (int i = 0; i < count; i++) { for (uint32_t i = 0; i < count; i++) {
lua_pushstring(L, names[i]); lua_pushstring(L, names[i]);
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }

View file

@ -2,10 +2,23 @@
#include "audio/source.h" #include "audio/source.h"
#include "data/audioStream.h" #include "data/audioStream.h"
#include "lib/maf.h" #include "lib/maf.h"
#include "lib/vec/vec.h"
#include "util.h" #include "util.h"
#include <stdlib.h> #include <stdlib.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>
static AudioState state; static struct {
bool initialized;
ALCdevice* device;
ALCcontext* context;
vec_void_t sources;
bool isSpatialized;
float orientation[4];
float position[3];
float velocity[3];
} state;
ALenum lovrAudioConvertFormat(int bitDepth, int channelCount) { ALenum lovrAudioConvertFormat(int bitDepth, int channelCount) {
if (bitDepth == 8 && channelCount == 1) { if (bitDepth == 8 && channelCount == 1) {
@ -57,7 +70,7 @@ void lovrAudioDestroy() {
lovrRelease(Source, state.sources.data[i]); lovrRelease(Source, state.sources.data[i]);
} }
vec_deinit(&state.sources); vec_deinit(&state.sources);
memset(&state, 0, sizeof(AudioState)); memset(&state, 0, sizeof(state));
} }
void lovrAudioUpdate() { void lovrAudioUpdate() {
@ -98,7 +111,7 @@ void lovrAudioGetDopplerEffect(float* factor, float* speedOfSound) {
alGetFloatv(AL_SPEED_OF_SOUND, speedOfSound); alGetFloatv(AL_SPEED_OF_SOUND, speedOfSound);
} }
void lovrAudioGetMicrophoneNames(const char* names[MAX_MICROPHONES], uint8_t* count) { void lovrAudioGetMicrophoneNames(const char* names[MAX_MICROPHONES], uint32_t* count) {
const char* name = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); const char* name = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
*count = 0; *count = 0;
while (*name) { while (*name) {
@ -164,8 +177,8 @@ void lovrAudioSetDopplerEffect(float factor, float speedOfSound) {
void lovrAudioSetOrientation(quat orientation) { void lovrAudioSetOrientation(quat orientation) {
// Rotate the unit forward/up vectors by the quaternion derived from the specified angle/axis // Rotate the unit forward/up vectors by the quaternion derived from the specified angle/axis
float f[3] = { 0, 0, -1 }; float f[3] = { 0.f, 0.f, -1.f };
float u[3] = { 0, 1, 0 }; float u[3] = { 0.f, 1.f, 0.f };
quat_init(state.orientation, orientation); quat_init(state.orientation, orientation);
quat_rotate(state.orientation, f); quat_rotate(state.orientation, f);
quat_rotate(state.orientation, u); quat_rotate(state.orientation, u);

View file

@ -1,8 +1,3 @@
#include "lib/maf.h"
#include "lib/vec/vec.h"
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -12,28 +7,17 @@
struct Source; struct Source;
typedef struct { int lovrAudioConvertFormat(int bitDepth, int channelCount);
bool initialized;
ALCdevice* device;
ALCcontext* context;
vec_void_t sources;
bool isSpatialized;
float orientation[4];
float position[3];
float velocity[3];
} AudioState;
ALenum lovrAudioConvertFormat(int bitDepth, int channelCount);
bool lovrAudioInit(void); bool lovrAudioInit(void);
void lovrAudioDestroy(void); void lovrAudioDestroy(void);
void lovrAudioUpdate(void); void lovrAudioUpdate(void);
void lovrAudioAdd(struct Source* source); void lovrAudioAdd(struct Source* source);
void lovrAudioGetDopplerEffect(float* factor, float* speedOfSound); void lovrAudioGetDopplerEffect(float* factor, float* speedOfSound);
void lovrAudioGetMicrophoneNames(const char* names[MAX_MICROPHONES], uint8_t* count); void lovrAudioGetMicrophoneNames(const char* names[MAX_MICROPHONES], uint32_t* count);
void lovrAudioGetOrientation(quat orientation); void lovrAudioGetOrientation(float* orientation);
void lovrAudioGetPosition(vec3 position); void lovrAudioGetPosition(float* position);
void lovrAudioGetVelocity(vec3 velocity); void lovrAudioGetVelocity(float* velocity);
float lovrAudioGetVolume(void); float lovrAudioGetVolume(void);
bool lovrAudioHas(struct Source* source); bool lovrAudioHas(struct Source* source);
bool lovrAudioIsSpatialized(void); bool lovrAudioIsSpatialized(void);
@ -41,8 +25,8 @@ void lovrAudioPause(void);
void lovrAudioResume(void); void lovrAudioResume(void);
void lovrAudioRewind(void); void lovrAudioRewind(void);
void lovrAudioSetDopplerEffect(float factor, float speedOfSound); void lovrAudioSetDopplerEffect(float factor, float speedOfSound);
void lovrAudioSetOrientation(quat orientation); void lovrAudioSetOrientation(float* orientation);
void lovrAudioSetPosition(vec3 position); void lovrAudioSetPosition(float* position);
void lovrAudioSetVelocity(vec3 velocity); void lovrAudioSetVelocity(float* velocity);
void lovrAudioSetVolume(float volume); void lovrAudioSetVolume(float volume);
void lovrAudioStop(void); void lovrAudioStop(void);

View file

@ -1,9 +1,9 @@
#include "audio/microphone.h" #include "audio/microphone.h"
#include "audio/audio.h" #include "audio/audio.h"
#include "data/soundData.h" #include "data/soundData.h"
#include "types.h" #include "util.h"
Microphone* lovrMicrophoneInit(Microphone* microphone, const char* name, int samples, int sampleRate, int bitDepth, int channelCount) { Microphone* lovrMicrophoneInit(Microphone* microphone, const char* name, size_t samples, uint32_t sampleRate, uint32_t bitDepth, uint32_t channelCount) {
ALCdevice* device = alcCaptureOpenDevice(name, sampleRate, lovrAudioConvertFormat(bitDepth, channelCount), samples); ALCdevice* device = alcCaptureOpenDevice(name, sampleRate, lovrAudioConvertFormat(bitDepth, channelCount), samples);
lovrAssert(device, "Error opening capture device for microphone '%s'", name); lovrAssert(device, "Error opening capture device for microphone '%s'", name);
microphone->device = device; microphone->device = device;
@ -20,11 +20,11 @@ void lovrMicrophoneDestroy(void* ref) {
alcCaptureCloseDevice(microphone->device); alcCaptureCloseDevice(microphone->device);
} }
int lovrMicrophoneGetBitDepth(Microphone* microphone) { uint32_t lovrMicrophoneGetBitDepth(Microphone* microphone) {
return microphone->bitDepth; return microphone->bitDepth;
} }
int lovrMicrophoneGetChannelCount(Microphone* microphone) { uint32_t lovrMicrophoneGetChannelCount(Microphone* microphone) {
return microphone->channelCount; return microphone->channelCount;
} }
@ -33,7 +33,7 @@ SoundData* lovrMicrophoneGetData(Microphone* microphone) {
return NULL; return NULL;
} }
int samples = lovrMicrophoneGetSampleCount(microphone); size_t samples = lovrMicrophoneGetSampleCount(microphone);
if (samples == 0) { if (samples == 0) {
return NULL; return NULL;
} }
@ -47,17 +47,17 @@ const char* lovrMicrophoneGetName(Microphone* microphone) {
return microphone->name; return microphone->name;
} }
int lovrMicrophoneGetSampleCount(Microphone* microphone) { size_t lovrMicrophoneGetSampleCount(Microphone* microphone) {
if (!microphone->isRecording) { if (!microphone->isRecording) {
return 0; return 0;
} }
ALCint samples; ALCint samples;
alcGetIntegerv(microphone->device, ALC_CAPTURE_SAMPLES, sizeof(ALCint), &samples); alcGetIntegerv(microphone->device, ALC_CAPTURE_SAMPLES, sizeof(ALCint), &samples);
return (int) samples; return (size_t) samples;
} }
int lovrMicrophoneGetSampleRate(Microphone* microphone) { uint32_t lovrMicrophoneGetSampleRate(Microphone* microphone) {
return microphone->sampleRate; return microphone->sampleRate;
} }

View file

@ -1,7 +1,8 @@
#include "types.h" #include "types.h"
#include <stdbool.h>
#include <stdint.h>
#include <AL/al.h> #include <AL/al.h>
#include <AL/alc.h> #include <AL/alc.h>
#include <stdbool.h>
#pragma once #pragma once
@ -12,20 +13,20 @@ typedef struct Microphone {
ALCdevice* device; ALCdevice* device;
const char* name; const char* name;
bool isRecording; bool isRecording;
int sampleRate; uint32_t sampleRate;
int bitDepth; uint32_t bitDepth;
int channelCount; uint32_t channelCount;
} Microphone; } Microphone;
Microphone* lovrMicrophoneInit(Microphone* microphone, const char* name, int samples, int sampleRate, int bitDepth, int channelCount); Microphone* lovrMicrophoneInit(Microphone* microphone, const char* name, size_t samples, uint32_t sampleRate, uint32_t bitDepth, uint32_t channelCount);
#define lovrMicrophoneCreate(...) lovrMicrophoneInit(lovrAlloc(Microphone), __VA_ARGS__) #define lovrMicrophoneCreate(...) lovrMicrophoneInit(lovrAlloc(Microphone), __VA_ARGS__)
void lovrMicrophoneDestroy(void* ref); void lovrMicrophoneDestroy(void* ref);
int lovrMicrophoneGetBitDepth(Microphone* microphone); uint32_t lovrMicrophoneGetBitDepth(Microphone* microphone);
int lovrMicrophoneGetChannelCount(Microphone* microphone); uint32_t lovrMicrophoneGetChannelCount(Microphone* microphone);
struct SoundData* lovrMicrophoneGetData(Microphone* microphone); struct SoundData* lovrMicrophoneGetData(Microphone* microphone);
const char* lovrMicrophoneGetName(Microphone* microphone); const char* lovrMicrophoneGetName(Microphone* microphone);
int lovrMicrophoneGetSampleCount(Microphone* microphone); size_t lovrMicrophoneGetSampleCount(Microphone* microphone);
int lovrMicrophoneGetSampleRate(Microphone* microphone); uint32_t lovrMicrophoneGetSampleRate(Microphone* microphone);
bool lovrMicrophoneIsRecording(Microphone* microphone); bool lovrMicrophoneIsRecording(Microphone* microphone);
void lovrMicrophoneStartRecording(Microphone* microphone); void lovrMicrophoneStartRecording(Microphone* microphone);
void lovrMicrophoneStopRecording(Microphone* microphone); void lovrMicrophoneStopRecording(Microphone* microphone);

View file

@ -2,6 +2,7 @@
#include "audio/audio.h" #include "audio/audio.h"
#include "data/audioStream.h" #include "data/audioStream.h"
#include "data/soundData.h" #include "data/soundData.h"
#include "lib/maf.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
@ -44,7 +45,7 @@ SourceType lovrSourceGetType(Source* source) {
return source->type; return source->type;
} }
int lovrSourceGetBitDepth(Source* source) { uint32_t lovrSourceGetBitDepth(Source* source) {
return source->type == SOURCE_STATIC ? source->soundData->bitDepth : source->stream->bitDepth; return source->type == SOURCE_STATIC ? source->soundData->bitDepth : source->stream->bitDepth;
} }
@ -56,7 +57,7 @@ void lovrSourceGetCone(Source* source, float* innerAngle, float* outerAngle, flo
*outerAngle *= (float) M_PI / 180.f; *outerAngle *= (float) M_PI / 180.f;
} }
int lovrSourceGetChannelCount(Source* source) { uint32_t lovrSourceGetChannelCount(Source* source) {
return source->type == SOURCE_STATIC ? source->soundData->channelCount : source->stream->channelCount; return source->type == SOURCE_STATIC ? source->soundData->channelCount : source->stream->channelCount;
} }
@ -66,7 +67,7 @@ void lovrSourceGetOrientation(Source* source, quat orientation) {
quat_between(orientation, forward, v); quat_between(orientation, forward, v);
} }
int lovrSourceGetDuration(Source* source) { size_t lovrSourceGetDuration(Source* source) {
return source->type == SOURCE_STATIC ? source->soundData->samples : source->stream->samples; return source->type == SOURCE_STATIC ? source->soundData->samples : source->stream->samples;
} }
@ -86,7 +87,7 @@ void lovrSourceGetPosition(Source* source, vec3 position) {
alGetSourcefv(source->id, AL_POSITION, position); alGetSourcefv(source->id, AL_POSITION, position);
} }
int lovrSourceGetSampleRate(Source* source) { uint32_t lovrSourceGetSampleRate(Source* source) {
return source->type == SOURCE_STATIC ? source->soundData->sampleRate : source->stream->sampleRate; return source->type == SOURCE_STATIC ? source->soundData->sampleRate : source->stream->sampleRate;
} }
@ -165,7 +166,7 @@ void lovrSourceRewind(Source* source) {
} }
} }
void lovrSourceSeek(Source* source, int sample) { void lovrSourceSeek(Source* source, size_t sample) {
switch (source->type) { switch (source->type) {
case SOURCE_STATIC: case SOURCE_STATIC:
alSourcef(source->id, AL_SAMPLE_OFFSET, sample); alSourcef(source->id, AL_SAMPLE_OFFSET, sample);
@ -265,16 +266,16 @@ void lovrSourceStop(Source* source) {
} }
// 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, size_t count) {
if (source->type == SOURCE_STATIC) { if (source->type == SOURCE_STATIC) {
return; return;
} }
AudioStream* stream = source->stream; AudioStream* stream = source->stream;
ALenum format = lovrAudioConvertFormat(stream->bitDepth, stream->channelCount); ALenum format = lovrAudioConvertFormat(stream->bitDepth, stream->channelCount);
int frequency = stream->sampleRate; uint32_t frequency = stream->sampleRate;
int samples = 0; size_t samples = 0;
int n = 0; size_t 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 = lovrAudioStreamDecode(stream, NULL, 0)) != 0) { while (n < count && (samples = lovrAudioStreamDecode(stream, NULL, 0)) != 0) {
@ -290,7 +291,7 @@ void lovrSourceStream(Source* source, ALuint* buffers, int count) {
} }
} }
int lovrSourceTell(Source* source) { size_t lovrSourceTell(Source* source) {
switch (source->type) { switch (source->type) {
case SOURCE_STATIC: { case SOURCE_STATIC: {
float offset; float offset;
@ -299,13 +300,13 @@ int lovrSourceTell(Source* source) {
} }
case SOURCE_STREAM: { case SOURCE_STREAM: {
int decoderOffset = lovrAudioStreamTell(source->stream); size_t decoderOffset = lovrAudioStreamTell(source->stream);
int samplesPerBuffer = source->stream->bufferSize / source->stream->channelCount / sizeof(ALshort); size_t samplesPerBuffer = source->stream->bufferSize / source->stream->channelCount / sizeof(ALshort);
int queuedBuffers, sampleOffset; ALsizei 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);
int offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset; size_t offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset;
if (offset < 0) { if (offset < 0) {
return offset + source->stream->samples; return offset + source->stream->samples;

View file

@ -1,8 +1,9 @@
#include "lib/maf.h"
#include "types.h" #include "types.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <AL/al.h> #include <AL/al.h>
#include <AL/alc.h> #include <AL/alc.h>
#include <stdbool.h>
#pragma once #pragma once
@ -37,16 +38,16 @@ Source* lovrSourceInitStream(Source* source, struct AudioStream* stream);
#define lovrSourceCreateStream(...) lovrSourceInitStream(lovrAlloc(Source), __VA_ARGS__) #define lovrSourceCreateStream(...) lovrSourceInitStream(lovrAlloc(Source), __VA_ARGS__)
void lovrSourceDestroy(void* ref); void lovrSourceDestroy(void* ref);
SourceType lovrSourceGetType(Source* source); SourceType lovrSourceGetType(Source* source);
int lovrSourceGetBitDepth(Source* source); uint32_t lovrSourceGetBitDepth(Source* source);
int lovrSourceGetChannelCount(Source* source); uint32_t lovrSourceGetChannelCount(Source* source);
void lovrSourceGetCone(Source* source, float* innerAngle, float* outerAngle, float* outerGain); void lovrSourceGetCone(Source* source, float* innerAngle, float* outerAngle, float* outerGain);
void lovrSourceGetOrientation(Source* source, quat orientation); void lovrSourceGetOrientation(Source* source, float* orientation);
int lovrSourceGetDuration(Source* source); size_t lovrSourceGetDuration(Source* source);
void lovrSourceGetFalloff(Source* source, float* reference, float* max, float* rolloff); void lovrSourceGetFalloff(Source* source, float* reference, float* max, float* rolloff);
float lovrSourceGetPitch(Source* source); float lovrSourceGetPitch(Source* source);
void lovrSourceGetPosition(Source* source, vec3 position); void lovrSourceGetPosition(Source* source, float* position);
void lovrSourceGetVelocity(Source* source, vec3 velocity); void lovrSourceGetVelocity(Source* source, float* velocity);
int lovrSourceGetSampleRate(Source* source); uint32_t lovrSourceGetSampleRate(Source* source);
float lovrSourceGetVolume(Source* source); float lovrSourceGetVolume(Source* source);
void lovrSourceGetVolumeLimits(Source* source, float* min, float* max); void lovrSourceGetVolumeLimits(Source* source, float* min, float* max);
bool lovrSourceIsLooping(Source* source); bool lovrSourceIsLooping(Source* source);
@ -58,17 +59,17 @@ void lovrSourcePause(Source* source);
void lovrSourcePlay(Source* source); void lovrSourcePlay(Source* source);
void lovrSourceResume(Source* source); void lovrSourceResume(Source* source);
void lovrSourceRewind(Source* source); void lovrSourceRewind(Source* source);
void lovrSourceSeek(Source* source, int sample); void lovrSourceSeek(Source* source, size_t sample);
void lovrSourceSetCone(Source* source, float inner, float outer, float outerGain); void lovrSourceSetCone(Source* source, float inner, float outer, float outerGain);
void lovrSourceSetOrientation(Source* source, quat orientation); void lovrSourceSetOrientation(Source* source, float* orientation);
void lovrSourceSetFalloff(Source* source, float reference, float max, float rolloff); void lovrSourceSetFalloff(Source* source, float reference, float max, float rolloff);
void lovrSourceSetLooping(Source* source, bool isLooping); void lovrSourceSetLooping(Source* source, bool isLooping);
void lovrSourceSetPitch(Source* source, float pitch); void lovrSourceSetPitch(Source* source, float pitch);
void lovrSourceSetPosition(Source* source, vec3 position); void lovrSourceSetPosition(Source* source, float* position);
void lovrSourceSetRelative(Source* source, bool isRelative); void lovrSourceSetRelative(Source* source, bool isRelative);
void lovrSourceSetVelocity(Source* source, vec3 velocity); void lovrSourceSetVelocity(Source* source, float* velocity);
void lovrSourceSetVolume(Source* source, float volume); void lovrSourceSetVolume(Source* source, float volume);
void lovrSourceSetVolumeLimits(Source* source, float min, float max); void lovrSourceSetVolumeLimits(Source* source, float min, float max);
void lovrSourceStop(Source* source); void lovrSourceStop(Source* source);
void lovrSourceStream(Source* source, ALuint* buffers, int count); void lovrSourceStream(Source* source, uint32_t* buffers, size_t count);
int lovrSourceTell(Source* source); size_t lovrSourceTell(Source* source);