mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
Added sound input and recording functionality.
This commit is contained in:
@@ -20,6 +20,8 @@
|
||||
|
||||
#include "Audio.h"
|
||||
|
||||
#include <sound/Decoder.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace audio
|
||||
@@ -44,6 +46,14 @@ namespace openal
|
||||
|
||||
if(alcGetError(device) != ALC_NO_ERROR)
|
||||
throw love::Exception("Could not make context current.");
|
||||
|
||||
capture = alcCaptureOpenDevice(alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER),
|
||||
love::sound::Decoder::DEFAULT_SAMPLE_RATE, AL_FORMAT_MONO16, 1048576); // about 23 seconds
|
||||
|
||||
if (!capture) {
|
||||
// We're not going to prevent LOVE from running without a microphone, but we should warn, at least
|
||||
std::cerr << "Warning, couldn't open capture device! No audio input!" << std::endl;
|
||||
}
|
||||
|
||||
// pool must be allocated after AL context.
|
||||
pool = new Pool();
|
||||
@@ -61,6 +71,7 @@ namespace openal
|
||||
|
||||
alcMakeContextCurrent(0);
|
||||
alcDestroyContext(context);
|
||||
alcCaptureCloseDevice(capture);
|
||||
alcCloseDevice(device);
|
||||
}
|
||||
|
||||
@@ -188,6 +199,41 @@ namespace openal
|
||||
{
|
||||
alListenerfv(AL_VELOCITY, v);
|
||||
}
|
||||
|
||||
void Audio::record()
|
||||
{
|
||||
if (!canRecord()) return;
|
||||
alcCaptureStart(capture);
|
||||
}
|
||||
|
||||
love::sound::SoundData * Audio::getRecordedData()
|
||||
{
|
||||
if (!canRecord()) return NULL;
|
||||
int samplerate = love::sound::Decoder::DEFAULT_SAMPLE_RATE;
|
||||
ALCint samples;
|
||||
alcGetIntegerv(capture, ALC_CAPTURE_SAMPLES, 4, &samples);
|
||||
void * data = malloc(samples * (2/sizeof(char)));
|
||||
alcCaptureSamples(capture, data, samples);
|
||||
love::sound::SoundData * sd = new love::sound::SoundData(data, samples, samplerate, 16, 1);
|
||||
free(data);
|
||||
return sd;
|
||||
}
|
||||
|
||||
love::sound::SoundData * Audio::stopRecording(bool returnData)
|
||||
{
|
||||
if (!canRecord()) return NULL;
|
||||
love::sound::SoundData * sd = NULL;
|
||||
if (returnData) {
|
||||
sd = getRecordedData();
|
||||
}
|
||||
alcCaptureStop(capture);
|
||||
return sd;
|
||||
}
|
||||
|
||||
bool Audio::canRecord()
|
||||
{
|
||||
return (capture != NULL);
|
||||
}
|
||||
|
||||
} // openal
|
||||
} // audio
|
||||
|
||||
@@ -59,6 +59,9 @@ namespace openal
|
||||
|
||||
// The OpenAL device.
|
||||
ALCdevice * device;
|
||||
|
||||
// The OpenAL capture device (microphone).
|
||||
ALCdevice * capture;
|
||||
|
||||
// The OpenAL context.
|
||||
ALCcontext * context;
|
||||
@@ -107,6 +110,11 @@ namespace openal
|
||||
void setOrientation(float * v);
|
||||
void getVelocity(float * v) const;
|
||||
void setVelocity(float * v);
|
||||
|
||||
void record();
|
||||
love::sound::SoundData * getRecordedData();
|
||||
love::sound::SoundData * stopRecording(bool returnData);
|
||||
bool canRecord();
|
||||
|
||||
}; // Audio
|
||||
|
||||
|
||||
Reference in New Issue
Block a user