From 34692ab826abc8f8faa61bdb2280b742424528f1 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Sat, 7 Dec 2013 15:14:29 -0500 Subject: Remove unnecessary Src/ folders --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 214 ++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 Source/Core/AudioCommon/AlsaSoundStream.cpp (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp new file mode 100644 index 0000000000..4c84ae69e1 --- /dev/null +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -0,0 +1,214 @@ +// Copyright 2013 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include + +#include "Common.h" +#include "Thread.h" +#include "AlsaSoundStream.h" + +#define FRAME_COUNT_MIN 256 +#define BUFFER_SIZE_MAX 8192 +#define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2) + +AlsaSound::AlsaSound(CMixer *mixer) : SoundStream(mixer), thread_data(0), handle(NULL), frames_to_deliver(FRAME_COUNT_MIN) +{ + mix_buffer = new u8[BUFFER_SIZE_BYTES]; +} + +AlsaSound::~AlsaSound() +{ + delete [] mix_buffer; +} + +bool AlsaSound::Start() +{ + thread = std::thread(std::mem_fun(&AlsaSound::SoundLoop), this); + thread_data = 0; + return true; +} + +void AlsaSound::Stop() +{ + thread_data = 1; + thread.join(); +} + +void AlsaSound::Update() +{ + // don't need to do anything here. +} + +// Called on audio thread. +void AlsaSound::SoundLoop() +{ + if (!AlsaInit()) { + thread_data = 2; + return; + } + Common::SetCurrentThreadName("Audio thread - alsa"); + while (!thread_data) + { + m_mixer->Mix(reinterpret_cast(mix_buffer), frames_to_deliver); + int rc = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver); + if (rc == -EPIPE) + { + // Underrun + snd_pcm_prepare(handle); + } + else if (rc < 0) + { + ERROR_LOG(AUDIO, "writei fail: %s", snd_strerror(rc)); + } + } + AlsaShutdown(); + thread_data = 2; +} + +bool AlsaSound::AlsaInit() +{ + unsigned int sample_rate = m_mixer->GetSampleRate(); + int err; + int dir; + snd_pcm_sw_params_t *swparams; + snd_pcm_hw_params_t *hwparams; + snd_pcm_uframes_t buffer_size,buffer_size_max; + unsigned int periods; + + err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0); + if (err < 0) + { + ERROR_LOG(AUDIO, "Audio open error: %s\n", snd_strerror(err)); + return false; + } + + snd_pcm_hw_params_alloca(&hwparams); + + err = snd_pcm_hw_params_any(handle, hwparams); + if (err < 0) + { + ERROR_LOG(AUDIO, "Broken configuration for this PCM: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); + if (err < 0) + { + ERROR_LOG(AUDIO, "Access type not available: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE); + if (err < 0) + { + ERROR_LOG(AUDIO, "Sample format not available: %s\n", snd_strerror(err)); + return false; + } + + dir = 0; + err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &sample_rate, &dir); + if (err < 0) + { + ERROR_LOG(AUDIO, "Rate not available: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_hw_params_set_channels(handle, hwparams, 2); + if (err < 0) + { + ERROR_LOG(AUDIO, "Channels count not available: %s\n", snd_strerror(err)); + return false; + } + + periods = BUFFER_SIZE_MAX / FRAME_COUNT_MIN; + err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &periods, &dir); + if (err < 0) + { + ERROR_LOG(AUDIO, "Cannot set Minimum periods: %s\n", snd_strerror(err)); + return false; + } + + buffer_size_max = BUFFER_SIZE_MAX; + err = snd_pcm_hw_params_set_buffer_size_max(handle, hwparams, &buffer_size_max); + if (err < 0) + { + ERROR_LOG(AUDIO, "Cannot set minimum buffer size: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_hw_params(handle, hwparams); + if (err < 0) + { + ERROR_LOG(AUDIO, "Unable to install hw params: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size); + if (err < 0) + { + ERROR_LOG(AUDIO, "Cannot get buffer size: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_hw_params_get_periods_max(hwparams, &periods, &dir); + if (err < 0) + { + ERROR_LOG(AUDIO, "Cannot get periods: %s\n", snd_strerror(err)); + return false; + } + + //periods is the number of fragments alsa can wait for during one + //buffer_size + frames_to_deliver = buffer_size / periods; + //limit the minimum size. pulseaudio advertises a minimum of 32 samples. + if (frames_to_deliver < FRAME_COUNT_MIN) + frames_to_deliver = FRAME_COUNT_MIN; + //it is probably a bad idea to try to send more than one buffer of data + if ((unsigned int)frames_to_deliver > buffer_size) + frames_to_deliver = buffer_size; + NOTICE_LOG(AUDIO, "ALSA gave us a %ld sample \"hardware\" buffer with %d periods. Will send %d samples per fragments.\n", buffer_size, periods, frames_to_deliver); + + snd_pcm_sw_params_alloca(&swparams); + + err = snd_pcm_sw_params_current(handle, swparams); + if (err < 0) + { + ERROR_LOG(AUDIO, "cannot init sw params: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0U); + if (err < 0) + { + ERROR_LOG(AUDIO, "cannot set start thresh: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_sw_params(handle, swparams); + if (err < 0) + { + ERROR_LOG(AUDIO, "cannot set sw params: %s\n", snd_strerror(err)); + return false; + } + + err = snd_pcm_prepare(handle); + if (err < 0) + { + ERROR_LOG(AUDIO, "Unable to prepare: %s\n", snd_strerror(err)); + return false; + } + NOTICE_LOG(AUDIO, "ALSA successfully initialized.\n"); + return true; +} + +void AlsaSound::AlsaShutdown() +{ + if (handle != NULL) + { + snd_pcm_drop(handle); + snd_pcm_close(handle); + handle = NULL; + } +} + -- cgit v1.2.3 From 2afe2152712981e21d6bda6f029292ed2b1cf91e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 Feb 2014 05:18:15 -0500 Subject: Convert all includes to relative paths. --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 4c84ae69e1..a88fae0b65 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -4,9 +4,9 @@ #include -#include "Common.h" -#include "Thread.h" -#include "AlsaSoundStream.h" +#include "AudioCommon/AlsaSoundStream.h" +#include "Common/Common.h" +#include "Common/Thread.h" #define FRAME_COUNT_MIN 256 #define BUFFER_SIZE_MAX 8192 -- cgit v1.2.3 From d802d392811be44d34ae9cd23f616db93e54c50f Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 9 Mar 2014 21:14:26 +0100 Subject: clang-modernize -use-nullptr and s/\bNULL\b/nullptr/g for *.cpp/h/mm files not compiled on my machine --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index a88fae0b65..ba4855400f 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -12,7 +12,7 @@ #define BUFFER_SIZE_MAX 8192 #define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2) -AlsaSound::AlsaSound(CMixer *mixer) : SoundStream(mixer), thread_data(0), handle(NULL), frames_to_deliver(FRAME_COUNT_MIN) +AlsaSound::AlsaSound(CMixer *mixer) : SoundStream(mixer), thread_data(0), handle(nullptr), frames_to_deliver(FRAME_COUNT_MIN) { mix_buffer = new u8[BUFFER_SIZE_BYTES]; } @@ -204,11 +204,11 @@ bool AlsaSound::AlsaInit() void AlsaSound::AlsaShutdown() { - if (handle != NULL) + if (handle != nullptr) { snd_pcm_drop(handle); snd_pcm_close(handle); - handle = NULL; + handle = nullptr; } } -- cgit v1.2.3 From e79895e372c00b1464fb3a28ec0a0b337b660d6f Mon Sep 17 00:00:00 2001 From: lioncash Date: Tue, 1 Apr 2014 12:09:22 -0400 Subject: Replace use of the deprecated mem_fun function with mem_fn. --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index ba4855400f..6eb8d68369 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -24,7 +24,7 @@ AlsaSound::~AlsaSound() bool AlsaSound::Start() { - thread = std::thread(std::mem_fun(&AlsaSound::SoundLoop), this); + thread = std::thread(std::mem_fn(&AlsaSound::SoundLoop), this); thread_data = 0; return true; } -- cgit v1.2.3 From 789a500ddc477dcdc809b8f771bbd8176c45ae1e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 8 Sep 2014 13:39:51 -0400 Subject: AudioCommon: Remove unnecessary usages of mem_fn --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 6eb8d68369..de6877c278 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -2,8 +2,6 @@ // Licensed under GPLv2 // Refer to the license.txt file included. -#include - #include "AudioCommon/AlsaSoundStream.h" #include "Common/Common.h" #include "Common/Thread.h" @@ -24,7 +22,7 @@ AlsaSound::~AlsaSound() bool AlsaSound::Start() { - thread = std::thread(std::mem_fn(&AlsaSound::SoundLoop), this); + thread = std::thread(&AlsaSound::SoundLoop, this); thread_data = 0; return true; } -- cgit v1.2.3 From fbc64984ca7de7db10b1a8a4f49002f260c93569 Mon Sep 17 00:00:00 2001 From: Rohit Nirmal Date: Sun, 7 Sep 2014 20:06:58 -0500 Subject: Include CommonTypes.h instead of Common.h. --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 6eb8d68369..f1044c126e 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -5,7 +5,7 @@ #include #include "AudioCommon/AlsaSoundStream.h" -#include "Common/Common.h" +#include "Common/CommonTypes.h" #include "Common/Thread.h" #define FRAME_COUNT_MIN 256 -- cgit v1.2.3 From 353205132c6fa5da8edbe1a4097cef2fad872794 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 10 May 2015 00:20:27 -0400 Subject: AlsaSoundStream: Convert volatile variables to atomics --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 5f76c60de7..099a3dee35 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -23,13 +23,13 @@ AlsaSound::~AlsaSound() bool AlsaSound::Start() { thread = std::thread(&AlsaSound::SoundLoop, this); - thread_data = 0; + thread_data.store(0); return true; } void AlsaSound::Stop() { - thread_data = 1; + thread_data.store(1); thread.join(); } @@ -42,11 +42,11 @@ void AlsaSound::Update() void AlsaSound::SoundLoop() { if (!AlsaInit()) { - thread_data = 2; + thread_data.store(2); return; } Common::SetCurrentThreadName("Audio thread - alsa"); - while (!thread_data) + while (thread_data.load() == 0) { m_mixer->Mix(reinterpret_cast(mix_buffer), frames_to_deliver); int rc = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver); @@ -61,7 +61,7 @@ void AlsaSound::SoundLoop() } } AlsaShutdown(); - thread_data = 2; + thread_data.store(2); } bool AlsaSound::AlsaInit() -- cgit v1.2.3 From f907e5cacee4f0a6c250f0d640eb11d006475f6b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 10 May 2015 02:30:24 -0400 Subject: AlsaSoundStream: Use an enum class for signifying ALSA thread state --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 099a3dee35..2bcb4ddbfb 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -10,7 +10,11 @@ #define BUFFER_SIZE_MAX 8192 #define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2) -AlsaSound::AlsaSound(CMixer *mixer) : SoundStream(mixer), thread_data(0), handle(nullptr), frames_to_deliver(FRAME_COUNT_MIN) +AlsaSound::AlsaSound(CMixer *mixer) + : SoundStream(mixer) + , m_thread_status(ALSAThreadStatus::STOPPED) + , handle(nullptr) + , frames_to_deliver(FRAME_COUNT_MIN) { mix_buffer = new u8[BUFFER_SIZE_BYTES]; } @@ -22,14 +26,14 @@ AlsaSound::~AlsaSound() bool AlsaSound::Start() { + m_thread_status.store(ALSAThreadStatus::RUNNING); thread = std::thread(&AlsaSound::SoundLoop, this); - thread_data.store(0); return true; } void AlsaSound::Stop() { - thread_data.store(1); + m_thread_status.store(ALSAThreadStatus::STOPPING); thread.join(); } @@ -42,11 +46,11 @@ void AlsaSound::Update() void AlsaSound::SoundLoop() { if (!AlsaInit()) { - thread_data.store(2); + m_thread_status.store(ALSAThreadStatus::STOPPED); return; } Common::SetCurrentThreadName("Audio thread - alsa"); - while (thread_data.load() == 0) + while (m_thread_status.load() == ALSAThreadStatus::RUNNING) { m_mixer->Mix(reinterpret_cast(mix_buffer), frames_to_deliver); int rc = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver); @@ -61,7 +65,7 @@ void AlsaSound::SoundLoop() } } AlsaShutdown(); - thread_data.store(2); + m_thread_status.store(ALSAThreadStatus::STOPPED); } bool AlsaSound::AlsaInit() -- cgit v1.2.3 From 35ee8a136237042efa6987e954c135f91a065cd0 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 24 May 2015 04:13:02 -0400 Subject: SoundStream: Internally construct the mixer Instead of creating the mixer externally and then passing it in, it can just be made within the class. --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 2bcb4ddbfb..b9e92d23a4 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -10,9 +10,8 @@ #define BUFFER_SIZE_MAX 8192 #define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2) -AlsaSound::AlsaSound(CMixer *mixer) - : SoundStream(mixer) - , m_thread_status(ALSAThreadStatus::STOPPED) +AlsaSound::AlsaSound() + : m_thread_status(ALSAThreadStatus::STOPPED) , handle(nullptr) , frames_to_deliver(FRAME_COUNT_MIN) { -- cgit v1.2.3 From cefcb0ace9d363b3679b4e93bcc9ec05f1e5f4f8 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Mon, 18 May 2015 01:08:10 +0200 Subject: Update license headers to GPLv2+ --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index b9e92d23a4..f03f477e5f 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -1,5 +1,5 @@ // Copyright 2013 Dolphin Emulator Project -// Licensed under GPLv2 +// Licensed under GPLv2+ // Refer to the license.txt file included. #include "AudioCommon/AlsaSoundStream.h" -- cgit v1.2.3 From 30ebb2459eb97ba544547183854775df8460b475 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 24 May 2015 06:55:12 +0200 Subject: Set copyright year to when a file was created --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/AudioCommon/AlsaSoundStream.cpp') diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index f03f477e5f..5902edba94 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -1,4 +1,4 @@ -// Copyright 2013 Dolphin Emulator Project +// Copyright 2009 Dolphin Emulator Project // Licensed under GPLv2+ // Refer to the license.txt file included. -- cgit v1.2.3