-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
AlsaSoundStream.cpp
235 lines (202 loc) · 5.53 KB
/
AlsaSoundStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2009 Dolphin Emulator Project
// 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"
AlsaSound::AlsaSound()
: m_thread_status(ALSAThreadStatus::STOPPED)
, handle(nullptr)
, frames_to_deliver(FRAME_COUNT_MIN)
{
}
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;
}
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();
}
void AlsaSound::Update()
{
// don't need to do anything here.
}
// Called on audio thread.
void AlsaSound::SoundLoop()
{
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(mix_buffer, frames_to_deliver);
int rc = 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();
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();
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, CHANNEL_COUNT);
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 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 maximum 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 != nullptr)
{
snd_pcm_drop(handle);
snd_pcm_close(handle);
handle = nullptr;
}
}