-
Notifications
You must be signed in to change notification settings - Fork 11
/
SoundPulse.cpp
213 lines (176 loc) · 4.98 KB
/
SoundPulse.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
/*
* Copyright (C) 2006-2010,2015,2021,2024 by Jonathan Naylor G4KLX
* Copyright (C) 2014 by John Wiseman, G8BPQ
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SoundPulse.h"
#include "Log.h"
#include <cassert>
#if defined(__linux__)
#include <endian.h>
#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/endian.h>
#else
#error Platform not supported
#endif
CSoundPulse::CSoundPulse(const std::string& readDevice, const std::string& writeDevice, unsigned int sampleRate, unsigned int blockSize) :
m_readDevice(readDevice),
m_writeDevice(writeDevice),
m_sampleRate(sampleRate),
m_blockSize(blockSize),
m_callback(NULL),
m_id(-1),
m_reader(NULL),
m_writer(NULL)
{
assert(sampleRate > 0U);
assert(blockSize > 0U);
}
CSoundPulse::~CSoundPulse()
{
}
void CSoundPulse::setCallback(IAudioCallback* callback, int id)
{
assert(callback != NULL);
m_callback = callback;
m_id = id;
}
bool CSoundPulse::open()
{
pa_sample_spec ss;
#if __BYTE_ORDER == __LITTLE_ENDIAN
ss.format = PA_SAMPLE_FLOAT32LE;
#else
ss.format = PA_SAMPLE_FLOAT32BE;
#endif
ss.rate = m_sampleRate;
ss.channels = 1;
pa_simple* playHandle = ::pa_simple_new(NULL, "M17Client", PA_STREAM_PLAYBACK, (m_writeDevice == "default") ? NULL : m_writeDevice.c_str(), "Receive", &ss, NULL, NULL, NULL);
if (!playHandle) {
LogError("Cannot open playback audio device %s", m_writeDevice.c_str());
return false;
}
pa_simple* recHandle = ::pa_simple_new(NULL, "M17Client", PA_STREAM_RECORD, (m_readDevice == "default") ? NULL : m_readDevice.c_str(), "Transmit", &ss, NULL, NULL, NULL);
if (!recHandle) {
LogError("Cannot open capture audio device %s", m_readDevice.c_str());
return false;
}
LogMessage("Opened %s:%s Rate %u", m_writeDevice.c_str(), m_readDevice.c_str(), m_sampleRate);
m_reader = new CSoundPulseReader(recHandle, m_blockSize, ss.channels, m_callback, m_id);
m_writer = new CSoundPulseWriter(playHandle, m_blockSize, ss.channels, m_callback, m_id);
m_reader->run();
m_writer->run();
return true;
}
void CSoundPulse::close()
{
m_reader->kill();
m_writer->kill();
m_reader->wait();
m_writer->wait();
}
bool CSoundPulse::isWriterBusy() const
{
return m_writer->isBusy();
}
CSoundPulseReader::CSoundPulseReader(pa_simple* handle, unsigned int blockSize, unsigned int channels, IAudioCallback* callback, int id) :
CThread(),
m_handle(handle),
m_blockSize(blockSize),
m_channels(channels),
m_callback(callback),
m_id(id),
m_killed(false),
m_samples(NULL)
{
assert(handle != NULL);
assert(blockSize > 0U);
assert(channels == 1U || channels == 2U);
assert(callback != NULL);
m_samples = new float[blockSize];
}
CSoundPulseReader::~CSoundPulseReader()
{
delete[] m_samples;
}
void CSoundPulseReader::entry()
{
LogMessage("Starting PulseAudio reader thread");
while (!m_killed) {
int err;
if (::pa_simple_read(m_handle, (uint8_t *)m_samples, m_blockSize * sizeof(float), &err) < 0) {
LogWarning("pa_simple_read error %d", err);
sleep(5UL);
} else {
m_callback->readCallback(m_samples, m_blockSize, m_id);
}
}
LogMessage("Stopping PulseAudio reader thread");
::pa_simple_free(m_handle);
}
void CSoundPulseReader::kill()
{
m_killed = true;
}
CSoundPulseWriter::CSoundPulseWriter(pa_simple* handle, unsigned int blockSize, unsigned int channels, IAudioCallback* callback, int id) :
CThread(),
m_handle(handle),
m_blockSize(blockSize),
m_channels(channels),
m_callback(callback),
m_id(id),
m_killed(false),
m_busy(false),
m_samples(NULL)
{
assert(handle != NULL);
assert(blockSize > 0U);
assert(channels == 1U || channels == 2U);
assert(callback != NULL);
m_samples = new float[2U * blockSize];
}
CSoundPulseWriter::~CSoundPulseWriter()
{
delete[] m_samples;
}
void CSoundPulseWriter::entry()
{
LogMessage("Starting PulseAudio writer thread");
while (!m_killed) {
int nSamples = 2 * m_blockSize;
m_callback->writeCallback(m_samples, nSamples, m_id);
if (nSamples == 0) {
sleep(5UL);
} else {
int err;
m_busy = true;
if (::pa_simple_write(m_handle, (uint8_t *)m_samples, nSamples * sizeof(float), &err) < 0) {
LogWarning("pa_simple_write error %d", err);
}
m_busy = false;
}
}
LogMessage("Stopping PulseAudio writer thread");
::pa_simple_free(m_handle);
}
void CSoundPulseWriter::kill()
{
m_killed = true;
}
bool CSoundPulseWriter::isBusy() const
{
return m_busy;
}