2009-03-27 15:24:22 +00:00
|
|
|
// Copyright (C) 2003-2009 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
#include "aldlist.h"
|
2009-03-28 17:18:34 +00:00
|
|
|
#include "OpenALStream.h"
|
2009-03-27 15:24:22 +00:00
|
|
|
|
2009-03-31 03:58:47 +00:00
|
|
|
#if defined HAVE_OPENAL && HAVE_OPENAL
|
|
|
|
|
2009-03-27 15:24:22 +00:00
|
|
|
#define AUDIO_NUMBUFFERS (4)
|
2009-04-04 03:58:16 +00:00
|
|
|
//#define AUDIO_SERVICE_UPDATE_PERIOD (20)
|
2009-03-27 15:24:22 +00:00
|
|
|
|
|
|
|
bool OpenALStream::Start()
|
|
|
|
{
|
2009-03-30 14:22:31 +00:00
|
|
|
ALDeviceList *pDeviceList = NULL;
|
|
|
|
ALCcontext *pContext = NULL;
|
|
|
|
ALCdevice *pDevice = NULL;
|
|
|
|
bool bReturn = false;
|
2009-03-30 20:25:36 +00:00
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
pDeviceList = new ALDeviceList();
|
|
|
|
if ((pDeviceList) && (pDeviceList->GetNumDevices()))
|
|
|
|
{
|
2009-04-24 05:42:57 +00:00
|
|
|
#ifdef __APPLE__
|
2009-04-24 01:51:42 +00:00
|
|
|
pDevice = alcOpenDevice((ALCubyte*)pDeviceList->GetDeviceName(pDeviceList->GetDefaultDevice()));
|
2009-04-24 05:42:57 +00:00
|
|
|
#else
|
|
|
|
pDevice = alcOpenDevice((const ALCchar *)pDeviceList->GetDeviceName(pDeviceList->GetDefaultDevice()));
|
|
|
|
#endif
|
2009-03-30 14:22:31 +00:00
|
|
|
if (pDevice)
|
|
|
|
{
|
|
|
|
pContext = alcCreateContext(pDevice, NULL);
|
|
|
|
if (pContext)
|
|
|
|
{
|
|
|
|
alcMakeContextCurrent(pContext);
|
|
|
|
thread = new Common::Thread(OpenALStream::ThreadFunc, (void *)this);
|
|
|
|
bReturn = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alcCloseDevice(pDevice);
|
2009-03-30 20:25:36 +00:00
|
|
|
PanicAlert("OpenAL: can't create context for device %s", pDevice);
|
2009-03-30 14:22:31 +00:00
|
|
|
}
|
2009-03-30 20:25:36 +00:00
|
|
|
} else {
|
|
|
|
PanicAlert("OpenAL: can't open device %s", pDevice);
|
2009-03-30 14:22:31 +00:00
|
|
|
}
|
2009-03-30 20:25:36 +00:00
|
|
|
|
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
delete pDeviceList;
|
2009-03-30 20:25:36 +00:00
|
|
|
} else {
|
|
|
|
PanicAlert("OpenAL: can't find sound devices");
|
2009-03-30 14:22:31 +00:00
|
|
|
}
|
2009-03-30 20:25:36 +00:00
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
return bReturn;
|
2009-03-27 15:24:22 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
void OpenALStream::Stop()
|
2009-03-27 15:24:22 +00:00
|
|
|
{
|
2009-03-30 14:22:31 +00:00
|
|
|
ALCcontext *pContext;
|
|
|
|
ALCdevice *pDevice;
|
2009-03-27 15:24:22 +00:00
|
|
|
|
2009-03-30 20:25:36 +00:00
|
|
|
soundCriticalSection.Enter();
|
|
|
|
threadData = 1;
|
|
|
|
// kick the thread if it's waiting
|
|
|
|
soundSyncEvent.Set();
|
|
|
|
soundCriticalSection.Leave();
|
2009-03-30 14:22:31 +00:00
|
|
|
delete thread;
|
2009-03-30 20:25:36 +00:00
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
pContext = alcGetCurrentContext();
|
|
|
|
pDevice = alcGetContextsDevice(pContext);
|
2009-03-27 15:24:22 +00:00
|
|
|
|
2009-03-30 14:22:31 +00:00
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
alcDestroyContext(pContext);
|
|
|
|
alcCloseDevice(pDevice);
|
2009-03-30 20:25:36 +00:00
|
|
|
|
|
|
|
soundSyncEvent.Shutdown();
|
|
|
|
thread = NULL;
|
2009-03-27 15:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenALStream::Update()
|
|
|
|
{
|
2009-03-30 20:25:36 +00:00
|
|
|
soundSyncEvent.Set();
|
2009-03-27 15:24:22 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 19:47:41 +00:00
|
|
|
THREAD_RETURN OpenALStream::ThreadFunc(void* args)
|
2009-03-30 14:22:31 +00:00
|
|
|
{
|
|
|
|
(reinterpret_cast<OpenALStream *>(args))->SoundLoop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenALStream::SoundLoop()
|
2009-03-27 15:24:22 +00:00
|
|
|
{
|
2009-04-04 03:58:16 +00:00
|
|
|
ALuint uiBuffers[AUDIO_NUMBUFFERS] = {0};
|
|
|
|
ALuint uiSource = 0;
|
|
|
|
ALenum err;
|
|
|
|
u32 ulFrequency = m_mixer->GetSampleRate();
|
|
|
|
// Generate some AL Buffers for streaming
|
|
|
|
alGenBuffers(AUDIO_NUMBUFFERS, (ALuint *)uiBuffers);
|
|
|
|
// Generate a Source to playback the Buffers
|
|
|
|
alGenSources(1, &uiSource);
|
|
|
|
|
|
|
|
memset(realtimeBuffer, 0, OAL_BUFFER_SIZE * sizeof(short));
|
|
|
|
//*
|
|
|
|
for (int iLoop = 0; iLoop < AUDIO_NUMBUFFERS; iLoop++)
|
|
|
|
{
|
|
|
|
// pay load fake data
|
|
|
|
alBufferData(uiBuffers[iLoop], AL_FORMAT_STEREO16, realtimeBuffer, 1024, ulFrequency);
|
|
|
|
alSourceQueueBuffers(uiSource, 1, &uiBuffers[iLoop]);
|
|
|
|
}
|
|
|
|
//*/
|
|
|
|
alSourcePlay(uiSource);
|
|
|
|
err = alGetError();
|
|
|
|
|
|
|
|
while (!threadData)
|
|
|
|
{
|
|
|
|
ALint iBuffersProcessed = 0;
|
|
|
|
alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed);
|
|
|
|
|
|
|
|
if (iBuffersProcessed)
|
|
|
|
{
|
|
|
|
// Remove the Buffer from the Queue. (uiBuffer contains the Buffer ID for the unqueued Buffer)
|
|
|
|
ALuint uiTempBuffer = 0;
|
|
|
|
alSourceUnqueueBuffers(uiSource, 1, &uiTempBuffer);
|
2009-03-27 15:24:22 +00:00
|
|
|
|
2009-04-04 03:58:16 +00:00
|
|
|
soundCriticalSection.Enter();
|
|
|
|
int numBytesToRender = 32768; //ya, this is a hack, we need real data count
|
|
|
|
m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
|
|
|
|
soundCriticalSection.Leave();
|
2009-03-30 20:25:36 +00:00
|
|
|
|
2009-04-04 03:58:16 +00:00
|
|
|
unsigned long ulBytesWritten = 0;
|
|
|
|
//if (ulBytesWritten)
|
|
|
|
{
|
|
|
|
//alBufferData(uiTempBuffer, ulFormat, pDecodeBuffer, ulBytesWritten, ulFrequency);
|
|
|
|
alBufferData(uiTempBuffer, AL_FORMAT_STEREO16, realtimeBuffer, numBytesToRender, ulFrequency);
|
|
|
|
alSourceQueueBuffers(uiSource, 1, &uiTempBuffer);
|
|
|
|
}
|
|
|
|
}
|
2009-03-30 20:25:36 +00:00
|
|
|
if (!threadData)
|
|
|
|
soundSyncEvent.Wait();
|
|
|
|
}
|
2009-04-04 03:58:16 +00:00
|
|
|
alSourceStop(uiSource);
|
|
|
|
alSourcei(uiSource, AL_BUFFER, 0);
|
|
|
|
|
|
|
|
// Clean up buffers and sources
|
|
|
|
alDeleteSources(1, &uiSource);
|
2009-04-07 03:56:21 +00:00
|
|
|
alDeleteBuffers(AUDIO_NUMBUFFERS, (ALuint *)uiBuffers);
|
2009-04-04 03:58:16 +00:00
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
}
|
2009-03-31 03:58:47 +00:00
|
|
|
|
|
|
|
#endif //HAVE_OPENAL
|
2009-04-04 03:58:16 +00:00
|
|
|
|