2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-14 13:18:21 -07:00
|
|
|
|
2014-09-08 11:39:51 -06:00
|
|
|
#include <cstring>
|
2008-12-14 13:18:21 -07:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "AudioCommon/AOSoundStream.h"
|
|
|
|
#include "AudioCommon/Mixer.h"
|
2008-08-16 15:58:07 -06:00
|
|
|
|
2009-01-28 17:57:55 -07:00
|
|
|
#if defined(HAVE_AO) && HAVE_AO
|
2008-08-16 15:58:07 -06:00
|
|
|
|
2009-01-28 17:57:55 -07:00
|
|
|
void AOSound::SoundLoop()
|
|
|
|
{
|
2011-12-30 21:22:48 -07:00
|
|
|
Common::SetCurrentThreadName("Audio thread - ao");
|
|
|
|
|
2009-03-26 03:29:14 -06:00
|
|
|
uint_32 numBytesToRender = 256;
|
2010-06-19 23:02:26 -06:00
|
|
|
ao_initialize();
|
|
|
|
default_driver = ao_default_driver_id();
|
|
|
|
format.bits = 16;
|
|
|
|
format.channels = 2;
|
|
|
|
format.rate = m_mixer->GetSampleRate();
|
|
|
|
format.byte_format = AO_FMT_LITTLE;
|
2013-03-19 19:51:12 -06:00
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
device = ao_open_live(default_driver, &format, nullptr /* no options */);
|
2010-06-19 23:02:26 -06:00
|
|
|
if (!device)
|
2009-02-20 15:04:52 -07:00
|
|
|
{
|
2011-01-12 19:05:58 -07:00
|
|
|
PanicAlertT("AudioCommon: Error opening AO device.\n");
|
2009-02-03 15:06:18 -07:00
|
|
|
ao_shutdown();
|
|
|
|
Stop();
|
|
|
|
return;
|
2010-06-19 23:02:26 -06:00
|
|
|
}
|
2009-01-28 17:57:55 -07:00
|
|
|
|
2010-06-19 23:02:26 -06:00
|
|
|
buf_size = format.bits/8 * format.channels * format.rate;
|
2009-01-28 17:57:55 -07:00
|
|
|
|
2015-05-09 21:48:22 -06:00
|
|
|
while (m_run_thread.load())
|
2009-02-20 15:04:52 -07:00
|
|
|
{
|
2010-06-19 23:02:26 -06:00
|
|
|
m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
|
2013-03-19 19:51:12 -06:00
|
|
|
|
2011-03-04 23:11:26 -07:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(soundCriticalSection);
|
2009-12-13 04:51:29 -07:00
|
|
|
ao_play(device, (char*)realtimeBuffer, numBytesToRender);
|
2011-03-04 23:11:26 -07:00
|
|
|
}
|
2009-03-26 03:29:14 -06:00
|
|
|
|
2009-12-18 12:52:04 -07:00
|
|
|
soundSyncEvent.Wait();
|
2009-02-20 15:04:52 -07:00
|
|
|
}
|
2009-01-28 17:57:55 -07:00
|
|
|
}
|
2008-12-14 13:18:21 -07:00
|
|
|
|
2009-02-20 15:04:52 -07:00
|
|
|
bool AOSound::Start()
|
|
|
|
{
|
2015-05-09 21:48:22 -06:00
|
|
|
m_run_thread.store(true);
|
2010-06-19 23:02:26 -06:00
|
|
|
memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
|
2013-03-19 19:51:12 -06:00
|
|
|
|
2014-09-08 11:39:51 -06:00
|
|
|
thread = std::thread(&AOSound::SoundLoop, this);
|
2010-06-19 23:02:26 -06:00
|
|
|
return true;
|
2009-01-28 17:57:55 -07:00
|
|
|
}
|
2008-12-14 13:18:21 -07:00
|
|
|
|
2009-02-20 15:04:52 -07:00
|
|
|
void AOSound::Update()
|
|
|
|
{
|
2010-06-19 23:02:26 -06:00
|
|
|
soundSyncEvent.Set();
|
2008-08-16 15:58:07 -06:00
|
|
|
}
|
2009-11-07 13:01:39 -07:00
|
|
|
|
2009-01-28 17:57:55 -07:00
|
|
|
void AOSound::Stop()
|
|
|
|
{
|
2015-05-09 21:48:22 -06:00
|
|
|
m_run_thread.store(false);
|
2010-06-19 23:02:26 -06:00
|
|
|
soundSyncEvent.Set();
|
2009-03-26 03:29:14 -06:00
|
|
|
|
2011-03-04 23:11:26 -07:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(soundCriticalSection);
|
2011-01-27 13:47:58 -07:00
|
|
|
thread.join();
|
2009-12-18 12:52:04 -07:00
|
|
|
|
2010-06-19 23:02:26 -06:00
|
|
|
if (device)
|
|
|
|
ao_close(device);
|
|
|
|
|
2009-12-18 12:52:04 -07:00
|
|
|
ao_shutdown();
|
2010-06-19 23:02:26 -06:00
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
device = nullptr;
|
2011-03-04 23:11:26 -07:00
|
|
|
}
|
2009-01-28 17:57:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|