dolphin/Source/Core/AudioCommon/Src/PulseAudioStream.cpp

100 lines
1.8 KiB
C++
Raw Normal View History

// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <functional>
#include "Common.h"
#include "Thread.h"
#include "PulseAudioStream.h"
2013-01-09 23:39:19 +00:00
namespace
{
2013-01-09 23:39:19 +00:00
const size_t BUFFER_SAMPLES = 512;
const size_t CHANNEL_COUNT = 2;
const size_t BUFFER_SIZE = BUFFER_SAMPLES * CHANNEL_COUNT;
}
2013-01-09 23:39:19 +00:00
PulseAudio::PulseAudio(CMixer *mixer)
: SoundStream(mixer)
, mix_buffer(BUFFER_SIZE)
, thread()
, run_thread()
, pa()
{}
bool PulseAudio::Start()
{
2013-01-09 23:39:19 +00:00
run_thread = true;
thread = std::thread(std::mem_fun(&PulseAudio::SoundLoop), this);
return true;
}
void PulseAudio::Stop()
{
2013-01-09 23:39:19 +00:00
run_thread = false;
thread.join();
}
void PulseAudio::Update()
{
// don't need to do anything here.
}
// Called on audio thread.
void PulseAudio::SoundLoop()
{
Common::SetCurrentThreadName("Audio thread - pulse");
2013-01-09 23:39:19 +00:00
if (PulseInit())
{
2013-01-09 23:39:19 +00:00
while (run_thread)
{
2013-01-09 23:39:19 +00:00
m_mixer->Mix(&mix_buffer[0], mix_buffer.size() / CHANNEL_COUNT);
Write(&mix_buffer[0], mix_buffer.size() * sizeof(s16));
}
2013-01-09 23:39:19 +00:00
PulseShutdown();
}
}
2013-01-09 23:39:19 +00:00
bool PulseAudio::PulseInit()
{
2013-01-09 23:39:19 +00:00
pa_sample_spec ss = {};
ss.format = PA_SAMPLE_S16LE;
ss.channels = 2;
2013-01-14 04:20:33 +00:00
ss.rate = m_mixer->GetSampleRate();
2013-01-09 23:39:19 +00:00
int error;
pa = pa_simple_new(nullptr, "dolphin-emu", PA_STREAM_PLAYBACK,
nullptr, "audio", &ss, nullptr, nullptr, &error);
2013-01-09 23:39:19 +00:00
if (!pa)
{
2013-01-09 23:39:19 +00:00
ERROR_LOG(AUDIO, "PulseAudio failed to initialize: %s",
pa_strerror(error));
return false;
}
2013-01-09 23:39:19 +00:00
else
{
2013-01-09 23:39:19 +00:00
NOTICE_LOG(AUDIO, "Pulse successfully initialized.");
return true;
}
}
2013-01-09 23:39:19 +00:00
void PulseAudio::PulseShutdown()
{
2013-01-09 23:39:19 +00:00
pa_simple_free(pa);
}
2013-01-09 23:39:19 +00:00
void PulseAudio::Write(const void *data, size_t length)
{
2013-01-09 23:39:19 +00:00
int error;
if (pa_simple_write(pa, data, length, &error) < 0)
{
2013-01-09 23:39:19 +00:00
ERROR_LOG(AUDIO, "PulseAudio failed to write data: %s",
pa_strerror(error));
}
}