-
Notifications
You must be signed in to change notification settings - Fork 3
/
AudioBlock.cpp
207 lines (180 loc) · 6.86 KB
/
AudioBlock.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
// Copyright (c) 2014-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "AudioBlock.hpp"
#include <cctype>
#include <algorithm>
#include <json.hpp>
using json = nlohmann::json;
AudioBlock::AudioBlock(const std::string &blockName, const bool isSink, const Pothos::DType &dtype, const size_t numChans, const std::string &chanMode):
_blockName(blockName),
_isSink(isSink),
_logger(Poco::Logger::get(blockName)),
_stream(nullptr),
_interleaved(chanMode == "INTERLEAVED"),
_sendLabel(false),
_reportLogger(false),
_reportStderror(true)
{
this->registerCall(this, POTHOS_FCN_TUPLE(AudioBlock, overlay));
this->registerCall(this, POTHOS_FCN_TUPLE(AudioBlock, setupDevice));
this->registerCall(this, POTHOS_FCN_TUPLE(AudioBlock, setupStream));
this->registerCall(this, POTHOS_FCN_TUPLE(AudioBlock, setReportMode));
this->registerCall(this, POTHOS_FCN_TUPLE(AudioBlock, setBackoffTime));
PaError err = Pa_Initialize();
if (err != paNoError)
{
throw Pothos::Exception("AudioBlock()", "Pa_Initialize: " + std::string(Pa_GetErrorText(err)));
}
//stream params
_streamParams.channelCount = numChans;
if (dtype == Pothos::DType("float32")) _streamParams.sampleFormat = paFloat32;
if (dtype == Pothos::DType("int32")) _streamParams.sampleFormat = paInt32;
if (dtype == Pothos::DType("int16")) _streamParams.sampleFormat = paInt16;
if (dtype == Pothos::DType("int8")) _streamParams.sampleFormat = paInt8;
if (dtype == Pothos::DType("uint8")) _streamParams.sampleFormat = paUInt8;
if (not _interleaved) _streamParams.sampleFormat |= paNonInterleaved;
}
AudioBlock::~AudioBlock(void)
{
if (_stream != nullptr)
{
PaError err = Pa_CloseStream(_stream);
if (err != paNoError)
{
poco_error_f1(_logger, "Pa_CloseStream: %s", std::string(Pa_GetErrorText(err)));
}
}
PaError err = Pa_Terminate();
if (err != paNoError)
{
poco_error_f1(_logger, "Pa_Terminate: %s", std::string(Pa_GetErrorText(err)));
}
}
std::string AudioBlock::overlay(void) const
{
json topObj;
json params;
json options;
json deviceNameParam;
deviceNameParam["key"] = "deviceName";
//editable drop down for user-controlled input
deviceNameParam["widgetKwargs"]["editable"] = true;
deviceNameParam["widgetType"] = "ComboBox";
//a default option for empty/unspecified device
json defaultOption;
defaultOption["name"] = "Default Device";
defaultOption["value"] = "\"\"";
options.push_back(defaultOption);
//enumerate devices and add to the options list
for (PaDeviceIndex i = 0; i < Pa_GetDeviceCount(); i++)
{
json option;
const std::string deviceName(Pa_GetDeviceInfo(i)->name);
option["name"] = deviceName;
option["value"] = "\""+deviceName+"\"";
options.push_back(option);
}
deviceNameParam["options"] = options;
params.push_back(deviceNameParam);
topObj["params"] = params;
return topObj.dump();
}
void AudioBlock::setupDevice(const std::string &deviceName)
{
if (Pa_GetDeviceCount() == 0) throw Pothos::NotFoundException(
"AudioBlock::setupDevice()", "No devices available");
//empty name, use default
if (deviceName.empty())
{
if (_isSink) _streamParams.device = Pa_GetDefaultOutputDevice();
else _streamParams.device = Pa_GetDefaultInputDevice();
return;
}
//numeric name, use index
if (std::all_of(deviceName.begin(), deviceName.end(), ::isdigit))
{
_streamParams.device = std::stoi(deviceName);
if (_streamParams.device >= Pa_GetDeviceCount()) throw Pothos::RangeException(
"AudioBlock::setupDevice("+deviceName+")", "Device index out of range");
return;
}
//find the match by name
for (PaDeviceIndex i = 0; i < Pa_GetDeviceCount(); i++)
{
if (Pa_GetDeviceInfo(i)->name == deviceName)
{
_streamParams.device = i;
return;
}
}
//cant locate by name
throw Pothos::NotFoundException("AudioBlock::setupDevice("+deviceName+")", "No matching device");
}
void AudioBlock::setupStream(const double sampRate)
{
//get device info
const auto deviceInfo = Pa_GetDeviceInfo(_streamParams.device);
poco_information_f2(_logger, "Using %s through %s",
std::string(deviceInfo->name), std::string(Pa_GetHostApiInfo(deviceInfo->hostApi)->name));
//stream params
if (_isSink) _streamParams.suggestedLatency = (deviceInfo->defaultLowOutputLatency + deviceInfo->defaultHighOutputLatency)/2;
else _streamParams.suggestedLatency = (deviceInfo->defaultLowInputLatency + deviceInfo->defaultHighInputLatency)/2;
_streamParams.hostApiSpecificStreamInfo = nullptr;
const int requestedSize = Pa_GetSampleSize(_streamParams.sampleFormat);
//try stream
PaError err = Pa_IsFormatSupported(_isSink?nullptr:&_streamParams, _isSink?&_streamParams:nullptr, sampRate);
if (err != paNoError)
{
throw Pothos::Exception("AudioBlock::setupStream()", "Pa_IsFormatSupported: " + std::string(Pa_GetErrorText(err)));
}
//open stream
err = Pa_OpenStream(
&_stream, // stream
_isSink?nullptr:&_streamParams, // inputParameters
_isSink?&_streamParams:nullptr, // outputParameters
sampRate, //sampleRate
paFramesPerBufferUnspecified, // framesPerBuffer
0, // streamFlags
nullptr, //streamCallback
nullptr); //userData
if (err != paNoError)
{
throw Pothos::Exception("AudioBlock::setupStream()", "Pa_OpenStream: " + std::string(Pa_GetErrorText(err)));
}
if (Pa_GetSampleSize(_streamParams.sampleFormat) != requestedSize)
{
throw Pothos::Exception("AudioBlock::setupStream()", "Pa_GetSampleSize mismatch");
}
}
void AudioBlock::setReportMode(const std::string &mode)
{
if (mode == "LOGGER"){}
else if (mode == "STDERROR"){}
else if (mode == "DISABLED"){}
else throw Pothos::InvalidArgumentException(
"AudioBlock::setReportMode("+mode+")", "unknown report mode");
_reportLogger = (mode == "LOGGER");
_reportStderror = (mode == "STDERROR");
}
void AudioBlock::setBackoffTime(const long backoff)
{
_backoffTime = std::chrono::duration_cast<std::chrono::high_resolution_clock::duration>(std::chrono::milliseconds(backoff));
}
void AudioBlock::activate(void)
{
_readyTime = std::chrono::high_resolution_clock::now();
PaError err = Pa_StartStream(_stream);
if (err != paNoError)
{
throw Pothos::Exception("AudioBlock::activate()", "Pa_StartStream: " + std::string(Pa_GetErrorText(err)));
}
_sendLabel = true;
}
void AudioBlock::deactivate(void)
{
PaError err = Pa_StopStream(_stream);
if (err != paNoError)
{
throw Pothos::Exception("AudioBlock::deactivate()", "Pa_StopStream: " + std::string(Pa_GetErrorText(err)));
}
}