Cleanup the openal microphone code a bit.

This commit is contained in:
riccardom 2010-01-13 10:48:18 +00:00
parent f82946cd96
commit a7032a211e
1 changed files with 26 additions and 13 deletions

View File

@ -39,30 +39,34 @@ u8 Mic_WriteBuf;
int MicButtonPressed;
ALCdevice *alDevice = 0;
ALCdevice *alCaptureDevice = 0;
ALCcontext *alContext = 0;
ALCdevice *alDevice;
ALCdevice *alCaptureDevice;
ALCcontext *alContext;
BOOL Mic_Init()
{
ALenum err;
const char *szDefaultCaptureDevice;
if (!(alDevice = alcOpenDevice(0))) {
INFO("Failed to Initialize Open AL\n");
return FALSE;
}
if( !(alContext = alcCreateContext(alDevice, 0))) {
INFO("Failed to create OpenAL context\n");
return 0;
return FALSE;
}
if( !alcMakeContextCurrent(alContext)) {
INFO("Failed to make OpenAL context current\n");
return 0;
return FALSE;
}
alDistanceModel(AL_INVERSE_DISTANCE);
const char *szDefaultCaptureDevice = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
szDefaultCaptureDevice = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
LOG("Default OpenAL Capture Device is '%s'\n\n", szDefaultCaptureDevice);
alCaptureDevice = alcCaptureOpenDevice(szDefaultCaptureDevice, 16000, AL_FORMAT_STEREO8, MIC_BUFSIZE);
ALenum err = alGetError();
err = alGetError();
if (err != AL_NO_ERROR) {
INFO("Failed to alCaptureOpenDevice, ALenum %i\n", err);
return 0;
@ -71,6 +75,7 @@ BOOL Mic_Init()
Mic_Inited = TRUE;
Mic_Reset();
return TRUE;
}
@ -78,6 +83,7 @@ void Mic_Reset()
{
if (!Mic_Inited)
return;
memset(Mic_Buffer, 0, MIC_BUFSIZE*2*2);
Mic_BufPos = 0;
Mic_PlayBuf = 1;
@ -88,6 +94,7 @@ void Mic_DeInit()
{
if (!Mic_Inited)
return;
Mic_Inited = FALSE;
if (alDevice) {
@ -106,40 +113,46 @@ void Mic_DeInit()
static void alReadSound()
{
ALenum err;
int num;
alcGetIntegerv(alCaptureDevice, ALC_CAPTURE_SAMPLES, 1, &num);
if (num < MIC_BUFSIZE-1) {
LOG("not enough microphone data waiting! (%i samples)\n", num);
} else {
LOG("%i samples waiting\n", num);
}
LOG("%i samples waiting\n", num);
if (num > MIC_BUFSIZE)
num = MIC_BUFSIZE;
alcCaptureSamples(alCaptureDevice, Mic_Buffer[Mic_WriteBuf], num);
ALenum err = alGetError();
err = alGetError();
if (err != AL_NO_ERROR) {
INFO("Failed to alcCaptureSamples, ALenum %i\n", err);
return;
}
}
u8 stats_max;
u8 stats_min;
u8 Mic_ReadSample()
{
static u8 stats_max;
static u8 stats_min;
u8 ret;
if (Mic_BufPos >= MIC_BUFSIZE) {
alReadSound();
Mic_BufPos = 0;
Mic_PlayBuf ^= 1;
Mic_WriteBuf ^= 1;
}
u8 ret;
ret = Mic_Buffer[Mic_PlayBuf][Mic_BufPos >> 1];
if (ret > stats_max)
stats_max = ret;
if (ret < stats_min)
stats_min = ret;
Mic_BufPos += 2;
return ret;
}