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