1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-15 18:23:35 +00:00
lovr/src/modules/audio/microphone.c
bjorn 22fe333150 Update refcounting (again);
- Ref struct only stores refcount now and is more general.
- Proxy stores a hash of its type name instead of an enum.
- Variants store additional information instead of using a vtable.
- Remove the concept of superclasses from the API.
- Clean up some miscellaneous includes.
2019-06-02 01:02:26 -07:00

98 lines
2.6 KiB
C

#include "audio/microphone.h"
#include "audio/audio.h"
#include "data/soundData.h"
#include "core/ref.h"
#include "util.h"
#include <AL/al.h>
#include <AL/alc.h>
struct Microphone {
ALCdevice* device;
const char* name;
bool isRecording;
uint32_t sampleRate;
uint32_t bitDepth;
uint32_t channelCount;
};
Microphone* lovrMicrophoneCreate(const char* name, size_t samples, uint32_t sampleRate, uint32_t bitDepth, uint32_t channelCount) {
Microphone* microphone = lovrAlloc(Microphone);
ALCdevice* device = alcCaptureOpenDevice(name, sampleRate, lovrAudioConvertFormat(bitDepth, channelCount), samples);
lovrAssert(device, "Error opening capture device for microphone '%s'", name);
microphone->device = device;
microphone->name = name ? name : alcGetString(device, ALC_CAPTURE_DEVICE_SPECIFIER);
microphone->sampleRate = sampleRate;
microphone->bitDepth = bitDepth;
microphone->channelCount = channelCount;
return microphone;
}
void lovrMicrophoneDestroy(void* ref) {
Microphone* microphone = ref;
lovrMicrophoneStopRecording(microphone);
alcCaptureCloseDevice(microphone->device);
}
uint32_t lovrMicrophoneGetBitDepth(Microphone* microphone) {
return microphone->bitDepth;
}
uint32_t lovrMicrophoneGetChannelCount(Microphone* microphone) {
return microphone->channelCount;
}
SoundData* lovrMicrophoneGetData(Microphone* microphone) {
if (!microphone->isRecording) {
return NULL;
}
size_t samples = lovrMicrophoneGetSampleCount(microphone);
if (samples == 0) {
return NULL;
}
SoundData* soundData = lovrSoundDataCreate(samples, microphone->sampleRate, microphone->bitDepth, microphone->channelCount);
alcCaptureSamples(microphone->device, soundData->blob.data, samples);
return soundData;
}
const char* lovrMicrophoneGetName(Microphone* microphone) {
return microphone->name;
}
size_t lovrMicrophoneGetSampleCount(Microphone* microphone) {
if (!microphone->isRecording) {
return 0;
}
ALCint samples;
alcGetIntegerv(microphone->device, ALC_CAPTURE_SAMPLES, sizeof(ALCint), &samples);
return (size_t) samples;
}
uint32_t lovrMicrophoneGetSampleRate(Microphone* microphone) {
return microphone->sampleRate;
}
bool lovrMicrophoneIsRecording(Microphone* microphone) {
return microphone->isRecording;
}
void lovrMicrophoneStartRecording(Microphone* microphone) {
if (microphone->isRecording) {
return;
}
alcCaptureStart(microphone->device);
microphone->isRecording = true;
}
void lovrMicrophoneStopRecording(Microphone* microphone) {
if (!microphone->isRecording) {
return;
}
alcCaptureStop(microphone->device);
microphone->isRecording = false;
}