diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index a132cb5de3..10b05e8441 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -9,21 +9,11 @@ #include "Common/Thread.h" #include "Common/Logging/Log.h" -#define FRAME_COUNT_MIN 256 -#define BUFFER_SIZE_MAX 8192 -#define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2) - AlsaSound::AlsaSound() : m_thread_status(ALSAThreadStatus::STOPPED) , handle(nullptr) , frames_to_deliver(FRAME_COUNT_MIN) { - mix_buffer = new u8[BUFFER_SIZE_BYTES]; -} - -AlsaSound::~AlsaSound() -{ - delete [] mix_buffer; } bool AlsaSound::Start() @@ -63,7 +53,7 @@ void AlsaSound::SoundLoop() std::unique_lock lock(cv_m); cv.wait(lock, [this]{return !m_muted || m_thread_status.load() != ALSAThreadStatus::RUNNING;}); - m_mixer->Mix(reinterpret_cast(mix_buffer), frames_to_deliver); + m_mixer->Mix(mix_buffer, frames_to_deliver); int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver); if (rc == -EPIPE) { @@ -145,7 +135,7 @@ bool AlsaSound::AlsaInit() return false; } - err = snd_pcm_hw_params_set_channels(handle, hwparams, 2); + err = snd_pcm_hw_params_set_channels(handle, hwparams, CHANNEL_COUNT); if (err < 0) { ERROR_LOG(AUDIO, "Channels count not available: %s\n", snd_strerror(err)); @@ -156,7 +146,7 @@ bool AlsaSound::AlsaInit() 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)); + ERROR_LOG(AUDIO, "Cannot set maximum periods per buffer: %s\n", snd_strerror(err)); return false; } @@ -164,7 +154,7 @@ bool AlsaSound::AlsaInit() 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)); + ERROR_LOG(AUDIO, "Cannot set maximum buffer size: %s\n", snd_strerror(err)); return false; } diff --git a/Source/Core/AudioCommon/AlsaSoundStream.h b/Source/Core/AudioCommon/AlsaSoundStream.h index 3654f4b8a5..53053debeb 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.h +++ b/Source/Core/AudioCommon/AlsaSoundStream.h @@ -21,7 +21,6 @@ class AlsaSound final : public SoundStream #if defined(HAVE_ALSA) && HAVE_ALSA public: AlsaSound(); - virtual ~AlsaSound(); bool Start() override; void SoundLoop() override; @@ -35,6 +34,15 @@ public: } private: + // maximum number of frames the buffer can hold + static constexpr size_t BUFFER_SIZE_MAX = 8192; + + // minimum number of frames to deliver in one transfer + static constexpr u32 FRAME_COUNT_MIN = 256; + + // number of channels per frame + static constexpr u32 CHANNEL_COUNT = 2; + enum class ALSAThreadStatus { RUNNING, @@ -45,7 +53,7 @@ private: bool AlsaInit(); void AlsaShutdown(); - u8 *mix_buffer; + s16 mix_buffer[BUFFER_SIZE_MAX * CHANNEL_COUNT]; std::thread thread; std::atomic m_thread_status; std::condition_variable cv;