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