1
// Copyright 2013 Dolphin Emulator Project
2
// Licensed under GPLv2
3
// Refer to the license.txt file included.
9
#include "AlsaSoundStream.h"
11
#define FRAME_COUNT_MIN 256
12
#define BUFFER_SIZE_MAX 8192
13
#define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2)
15
AlsaSound::AlsaSound(CMixer *mixer) : SoundStream(mixer), thread_data(0), handle(NULL), frames_to_deliver(FRAME_COUNT_MIN)
17
mix_buffer = new u8[BUFFER_SIZE_BYTES];
20
AlsaSound::~AlsaSound()
25
bool AlsaSound::Start()
27
thread = std::thread(std::mem_fun(&AlsaSound::SoundLoop), this);
32
void AlsaSound::Stop()
38
void AlsaSound::Update()
40
// don't need to do anything here.
43
// Called on audio thread.
44
void AlsaSound::SoundLoop()
50
Common::SetCurrentThreadName("Audio thread - alsa");
53
m_mixer->Mix(reinterpret_cast<short *>(mix_buffer), frames_to_deliver);
54
int rc = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
58
snd_pcm_prepare(handle);
62
ERROR_LOG(AUDIO, "writei fail: %s", snd_strerror(rc));
69
bool AlsaSound::AlsaInit()
71
unsigned int sample_rate = m_mixer->GetSampleRate();
74
snd_pcm_sw_params_t *swparams;
75
snd_pcm_hw_params_t *hwparams;
76
snd_pcm_uframes_t buffer_size,buffer_size_max;
79
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
82
ERROR_LOG(AUDIO, "Audio open error: %s\n", snd_strerror(err));
86
snd_pcm_hw_params_alloca(&hwparams);
88
err = snd_pcm_hw_params_any(handle, hwparams);
91
ERROR_LOG(AUDIO, "Broken configuration for this PCM: %s\n", snd_strerror(err));
95
err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
98
ERROR_LOG(AUDIO, "Access type not available: %s\n", snd_strerror(err));
102
err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE);
105
ERROR_LOG(AUDIO, "Sample format not available: %s\n", snd_strerror(err));
110
err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &sample_rate, &dir);
113
ERROR_LOG(AUDIO, "Rate not available: %s\n", snd_strerror(err));
117
err = snd_pcm_hw_params_set_channels(handle, hwparams, 2);
120
ERROR_LOG(AUDIO, "Channels count not available: %s\n", snd_strerror(err));
124
periods = BUFFER_SIZE_MAX / FRAME_COUNT_MIN;
125
err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &periods, &dir);
128
ERROR_LOG(AUDIO, "Cannot set Minimum periods: %s\n", snd_strerror(err));
132
buffer_size_max = BUFFER_SIZE_MAX;
133
err = snd_pcm_hw_params_set_buffer_size_max(handle, hwparams, &buffer_size_max);
136
ERROR_LOG(AUDIO, "Cannot set minimum buffer size: %s\n", snd_strerror(err));
140
err = snd_pcm_hw_params(handle, hwparams);
143
ERROR_LOG(AUDIO, "Unable to install hw params: %s\n", snd_strerror(err));
147
err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
150
ERROR_LOG(AUDIO, "Cannot get buffer size: %s\n", snd_strerror(err));
154
err = snd_pcm_hw_params_get_periods_max(hwparams, &periods, &dir);
157
ERROR_LOG(AUDIO, "Cannot get periods: %s\n", snd_strerror(err));
161
//periods is the number of fragments alsa can wait for during one
163
frames_to_deliver = buffer_size / periods;
164
//limit the minimum size. pulseaudio advertises a minimum of 32 samples.
165
if (frames_to_deliver < FRAME_COUNT_MIN)
166
frames_to_deliver = FRAME_COUNT_MIN;
167
//it is probably a bad idea to try to send more than one buffer of data
168
if ((unsigned int)frames_to_deliver > buffer_size)
169
frames_to_deliver = buffer_size;
170
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);
172
snd_pcm_sw_params_alloca(&swparams);
174
err = snd_pcm_sw_params_current(handle, swparams);
177
ERROR_LOG(AUDIO, "cannot init sw params: %s\n", snd_strerror(err));
181
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0U);
184
ERROR_LOG(AUDIO, "cannot set start thresh: %s\n", snd_strerror(err));
188
err = snd_pcm_sw_params(handle, swparams);
191
ERROR_LOG(AUDIO, "cannot set sw params: %s\n", snd_strerror(err));
195
err = snd_pcm_prepare(handle);
198
ERROR_LOG(AUDIO, "Unable to prepare: %s\n", snd_strerror(err));
201
NOTICE_LOG(AUDIO, "ALSA successfully initialized.\n");
205
void AlsaSound::AlsaShutdown()
209
snd_pcm_drop(handle);
210
snd_pcm_close(handle);