summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon/AlsaSoundStream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp')
-rw-r--r--Source/Core/AudioCommon/AlsaSoundStream.cpp40
1 files changed, 35 insertions, 5 deletions
diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp
index 5902edba94..a132cb5de3 100644
--- a/Source/Core/AudioCommon/AlsaSoundStream.cpp
+++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp
@@ -2,9 +2,12 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
+#include <mutex>
+
#include "AudioCommon/AlsaSoundStream.h"
#include "Common/CommonTypes.h"
#include "Common/Thread.h"
+#include "Common/Logging/Log.h"
#define FRAME_COUNT_MIN 256
#define BUFFER_SIZE_MAX 8192
@@ -26,6 +29,12 @@ AlsaSound::~AlsaSound()
bool AlsaSound::Start()
{
m_thread_status.store(ALSAThreadStatus::RUNNING);
+ if (!AlsaInit())
+ {
+ m_thread_status.store(ALSAThreadStatus::STOPPED);
+ return false;
+ }
+
thread = std::thread(&AlsaSound::SoundLoop, this);
return true;
}
@@ -33,6 +42,10 @@ bool AlsaSound::Start()
void AlsaSound::Stop()
{
m_thread_status.store(ALSAThreadStatus::STOPPING);
+
+ //Give the opportunity to the audio thread
+ //to realize we are stopping the emulation
+ cv.notify_one();
thread.join();
}
@@ -44,15 +57,14 @@ void AlsaSound::Update()
// Called on audio thread.
void AlsaSound::SoundLoop()
{
- if (!AlsaInit()) {
- m_thread_status.store(ALSAThreadStatus::STOPPED);
- return;
- }
Common::SetCurrentThreadName("Audio thread - alsa");
while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
{
+ std::unique_lock<std::mutex> lock(cv_m);
+ cv.wait(lock, [this]{return !m_muted || m_thread_status.load() != ALSAThreadStatus::RUNNING;});
+
m_mixer->Mix(reinterpret_cast<short *>(mix_buffer), frames_to_deliver);
- int rc = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
+ int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
if (rc == -EPIPE)
{
// Underrun
@@ -67,6 +79,24 @@ void AlsaSound::SoundLoop()
m_thread_status.store(ALSAThreadStatus::STOPPED);
}
+
+void AlsaSound::Clear(bool muted)
+{
+ m_muted = muted;
+ if (m_muted)
+ {
+ std::lock_guard<std::mutex> lock(cv_m);
+ snd_pcm_drop(handle);
+ }
+ else
+ {
+ std::unique_lock<std::mutex> lock(cv_m);
+ snd_pcm_prepare(handle);
+ lock.unlock();
+ cv.notify_one();
+ }
+}
+
bool AlsaSound::AlsaInit()
{
unsigned int sample_rate = m_mixer->GetSampleRate();