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

View File

@ -2,10 +2,23 @@
#include "audio/source.h"
#include "data/audioStream.h"
#include "lib/maf.h"
#include "lib/vec/vec.h"
#include "util.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) {
if (bitDepth == 8 && channelCount == 1) {
@ -57,7 +70,7 @@ void lovrAudioDestroy() {
lovrRelease(Source, state.sources.data[i]);
}
vec_deinit(&state.sources);
memset(&state, 0, sizeof(AudioState));
memset(&state, 0, sizeof(state));
}
void lovrAudioUpdate() {
@ -98,7 +111,7 @@ void lovrAudioGetDopplerEffect(float* factor, float* 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);
*count = 0;
while (*name) {
@ -164,8 +177,8 @@ void lovrAudioSetDopplerEffect(float factor, float speedOfSound) {
void lovrAudioSetOrientation(quat orientation) {
// Rotate the unit forward/up vectors by the quaternion derived from the specified angle/axis
float f[3] = { 0, 0, -1 };
float u[3] = { 0, 1, 0 };
float f[3] = { 0.f, 0.f, -1.f };
float u[3] = { 0.f, 1.f, 0.f };
quat_init(state.orientation, orientation);
quat_rotate(state.orientation, f);
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 <stdint.h>
@ -12,28 +7,17 @@
struct Source;
typedef struct {
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);
int lovrAudioConvertFormat(int bitDepth, int channelCount);
bool lovrAudioInit(void);
void lovrAudioDestroy(void);
void lovrAudioUpdate(void);
void lovrAudioAdd(struct Source* source);
void lovrAudioGetDopplerEffect(float* factor, float* speedOfSound);
void lovrAudioGetMicrophoneNames(const char* names[MAX_MICROPHONES], uint8_t* count);
void lovrAudioGetOrientation(quat orientation);
void lovrAudioGetPosition(vec3 position);
void lovrAudioGetVelocity(vec3 velocity);
void lovrAudioGetMicrophoneNames(const char* names[MAX_MICROPHONES], uint32_t* count);
void lovrAudioGetOrientation(float* orientation);
void lovrAudioGetPosition(float* position);
void lovrAudioGetVelocity(float* velocity);
float lovrAudioGetVolume(void);
bool lovrAudioHas(struct Source* source);
bool lovrAudioIsSpatialized(void);
@ -41,8 +25,8 @@ void lovrAudioPause(void);
void lovrAudioResume(void);
void lovrAudioRewind(void);
void lovrAudioSetDopplerEffect(float factor, float speedOfSound);
void lovrAudioSetOrientation(quat orientation);
void lovrAudioSetPosition(vec3 position);
void lovrAudioSetVelocity(vec3 velocity);
void lovrAudioSetOrientation(float* orientation);
void lovrAudioSetPosition(float* position);
void lovrAudioSetVelocity(float* velocity);
void lovrAudioSetVolume(float volume);
void lovrAudioStop(void);

View File

@ -1,9 +1,9 @@
#include "audio/microphone.h"
#include "audio/audio.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);
lovrAssert(device, "Error opening capture device for microphone '%s'", name);
microphone->device = device;
@ -20,11 +20,11 @@ void lovrMicrophoneDestroy(void* ref) {
alcCaptureCloseDevice(microphone->device);
}
int lovrMicrophoneGetBitDepth(Microphone* microphone) {
uint32_t lovrMicrophoneGetBitDepth(Microphone* microphone) {
return microphone->bitDepth;
}
int lovrMicrophoneGetChannelCount(Microphone* microphone) {
uint32_t lovrMicrophoneGetChannelCount(Microphone* microphone) {
return microphone->channelCount;
}
@ -33,7 +33,7 @@ SoundData* lovrMicrophoneGetData(Microphone* microphone) {
return NULL;
}
int samples = lovrMicrophoneGetSampleCount(microphone);
size_t samples = lovrMicrophoneGetSampleCount(microphone);
if (samples == 0) {
return NULL;
}
@ -47,17 +47,17 @@ const char* lovrMicrophoneGetName(Microphone* microphone) {
return microphone->name;
}
int lovrMicrophoneGetSampleCount(Microphone* microphone) {
size_t lovrMicrophoneGetSampleCount(Microphone* microphone) {
if (!microphone->isRecording) {
return 0;
}
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;
}

View File

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

View File

@ -2,6 +2,7 @@
#include "audio/audio.h"
#include "data/audioStream.h"
#include "data/soundData.h"
#include "lib/maf.h"
#include <math.h>
#include <stdlib.h>
@ -44,7 +45,7 @@ SourceType lovrSourceGetType(Source* source) {
return source->type;
}
int lovrSourceGetBitDepth(Source* source) {
uint32_t lovrSourceGetBitDepth(Source* source) {
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;
}
int lovrSourceGetChannelCount(Source* source) {
uint32_t lovrSourceGetChannelCount(Source* source) {
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);
}
int lovrSourceGetDuration(Source* source) {
size_t lovrSourceGetDuration(Source* source) {
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);
}
int lovrSourceGetSampleRate(Source* source) {
uint32_t lovrSourceGetSampleRate(Source* source) {
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) {
case SOURCE_STATIC:
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
void lovrSourceStream(Source* source, ALuint* buffers, int count) {
void lovrSourceStream(Source* source, ALuint* buffers, size_t count) {
if (source->type == SOURCE_STATIC) {
return;
}
AudioStream* stream = source->stream;
ALenum format = lovrAudioConvertFormat(stream->bitDepth, stream->channelCount);
int frequency = stream->sampleRate;
int samples = 0;
int n = 0;
uint32_t frequency = stream->sampleRate;
size_t samples = 0;
size_t n = 0;
// Keep decoding until there is nothing left to decode or all the buffers are filled
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) {
case SOURCE_STATIC: {
float offset;
@ -299,13 +300,13 @@ int lovrSourceTell(Source* source) {
}
case SOURCE_STREAM: {
int decoderOffset = lovrAudioStreamTell(source->stream);
int samplesPerBuffer = source->stream->bufferSize / source->stream->channelCount / sizeof(ALshort);
int queuedBuffers, sampleOffset;
size_t decoderOffset = lovrAudioStreamTell(source->stream);
size_t samplesPerBuffer = source->stream->bufferSize / source->stream->channelCount / sizeof(ALshort);
ALsizei queuedBuffers, sampleOffset;
alGetSourcei(source->id, AL_BUFFERS_QUEUED, &queuedBuffers);
alGetSourcei(source->id, AL_SAMPLE_OFFSET, &sampleOffset);
int offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset;
size_t offset = decoderOffset - queuedBuffers * samplesPerBuffer + sampleOffset;
if (offset < 0) {
return offset + source->stream->samples;

View File

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