Skip to content

Commit

Permalink
Merge pull request #3110 from booto/alsa-buffer-type
Browse files Browse the repository at this point in the history
AlsaSound: fix buffer type, clean up macros
  • Loading branch information
Tilka committed Sep 30, 2015
2 parents efd370f + 6c28ea5 commit 6305437
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
18 changes: 4 additions & 14 deletions Source/Core/AudioCommon/AlsaSoundStream.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -63,7 +53,7 @@ void AlsaSound::SoundLoop()
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);
m_mixer->Mix(mix_buffer, frames_to_deliver);
int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
if (rc == -EPIPE)
{
Expand Down Expand Up @@ -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));
Expand All @@ -156,15 +146,15 @@ 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;
}

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));
ERROR_LOG(AUDIO, "Cannot set maximum buffer size: %s\n", snd_strerror(err));
return false;
}

Expand Down
12 changes: 10 additions & 2 deletions Source/Core/AudioCommon/AlsaSoundStream.h
Expand Up @@ -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;
Expand All @@ -35,6 +34,15 @@ class AlsaSound final : public SoundStream
}

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,
Expand All @@ -45,7 +53,7 @@ class AlsaSound final : public SoundStream
bool AlsaInit();
void AlsaShutdown();

u8 *mix_buffer;
s16 mix_buffer[BUFFER_SIZE_MAX * CHANNEL_COUNT];
std::thread thread;
std::atomic<ALSAThreadStatus> m_thread_status;
std::condition_variable cv;
Expand Down

0 comments on commit 6305437

Please sign in to comment.