-
Notifications
You must be signed in to change notification settings - Fork 0
/
DevMinder.cpp
303 lines (264 loc) · 9.05 KB
/
DevMinder.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "DevMinder.hpp"
// declarations of subclasses for factory method
#include "AlsaMinder.hpp"
#include "RTLSDRMinder.hpp"
void DevMinder::delete_privates() {
if (Pollable::terminating)
return;
for (PluginRunnerSet::iterator ip = plugins.begin(); ip != plugins.end(); /**/) {
Pollable::remove(ip->first);
PluginRunnerSet::iterator del = ip++;
plugins.erase(del);
}
};
int DevMinder::open() {
int rv = hw_open();
downSampleFactor = hwRate / rate;
return rv;
};
void DevMinder::stop(double timeNow) {
shouldBeRunning = false;
Pollable::requestPollFDRegen();
hw_do_stop();
stopTimestamp = timeNow;
stopped = true;
};
int DevMinder::do_restart(double timeNow) {
hasError = 0;
startTimestamp = timeNow;
return hw_do_restart();
}
int DevMinder::start(double timeNow) {
shouldBeRunning = true;
if (hw_running(timeNow))
return 0;
if (!hw_is_open() && hw_open())
return 1;
Pollable::requestPollFDRegen();
int rv = hw_do_start();
if (! rv) {
stopped = false;
// set timestamps to:
// - prevent warning about resuming after long pause
// - allow us to notice no data has been received for too long after startup
lastDataReceived = startTimestamp = timeNow;
}
return rv;
};
void DevMinder::addPluginRunner(std::string &label, shared_ptr < PluginRunner > pr) {
plugins[label] = pr;
};
void DevMinder::removePluginRunner(std::string &label) {
// remove plugin runner
plugins.erase(label);
};
void DevMinder::addRawListener(string &label, int downSampleFactor, bool writeWavHeader, bool downSampleUseAvg) {
shared_ptr < Pollable > sptr;
rawListeners[label] = sptr = Pollable::lookupByNameShared(label);
if (rawListeners.size() == 1) {
this->downSampleFactor = downSampleFactor;
this->downSampleUseAvg = downSampleUseAvg;
for (int i=0; i < MAX_CHANNELS; ++i) {
downSampleAccum[i] = 0;
downSampleCount[i] = downSampleFactor;
}
}
if (writeWavHeader) {
Pollable *ptr = sptr.get();
if (ptr) {
// default max possible frames in .WAV header
// FIXME: hardcoded S16_LE format
WavFileHeader hdr(hwRate / downSampleFactor, numChan, 0x7ffffffe / 2);
ptr->queueOutput(hdr.address(), hdr.size());
}
}
};
void DevMinder::removeRawListener(string &label) {
rawListeners.erase(label);
};
void DevMinder::removeAllRawListeners() {
rawListeners.clear();
};
DevMinder::DevMinder(const string &devName, int rate, unsigned int numChan, unsigned int maxSampleAbs, const string &label, double now, int buffSize):
Pollable(label),
devName(devName),
rate(rate),
numChan(numChan),
maxSampleAbs(maxSampleAbs),
totalFrames(0),
startTimestamp(-1.0),
stopTimestamp(now),
lastDataReceived(-1.0),
shouldBeRunning(false),
stopped(true),
hasError(0),
demodFMForRaw(false),
demodFMLastTheta(0),
sampleBuf(buffSize * numChan)
{
};
DevMinder * DevMinder::getDevMinder(const string &devName, int rate, unsigned int numChan, const string &label, double now) {
DevMinder * dev;
if (devName.substr( 0, 7 ) == "rtlsdr:") {
dev = new RTLSDRMinder(devName, rate, numChan, label, now);
} else {
dev = new AlsaMinder(devName, rate, numChan, label, now);
}
if (dev->open()) {
// there was an error, so throw an exception
dev->delete_privates();
throw std::runtime_error("Could not open source device or could not set required parameters");
}
return dev;
};
DevMinder::~DevMinder() {
delete_privates();
};
string DevMinder::about() {
return "Device '" + label + "' = " + devName;
};
string DevMinder::toJSON() {
ostringstream s;
s << "{"
<< "\"type\":\"DevMinder\","
<< "\"device\":\"" << devName << "\","
<< "\"rate\":" << rate << ","
<< "\"hwRate\":" << hwRate << ","
<< "\"numChan\":" << numChan << ","
<< setprecision(14)
<< "\"startTimestamp\":" << startTimestamp << ","
<< "\"stopTimestamp\":" << stopTimestamp << ","
<< "\"running\":" << (stopped ? "false" : "true") << ","
<< "\"hasError\":" << hasError << ","
<< "\"totalFrames\":" << totalFrames << ","
<< "\"numRawListeners\":" << rawListeners.size()
<< "}";
return s.str();
}
int DevMinder::getNumPollFDs () {
return hw_getNumPollFDs();
};
int DevMinder::getPollFDs (struct pollfd *pollfds) {
// append pollfd(s) for this object to the specified vector
if ( hw_getPollFDs(pollfds) ) {
std::ostringstream msg;
msg << "\"event\":\"devProblem\",\"error\":\"snd_pcm_poll_descriptors returned error.\",\"devLabel\":\"" << label << "\"";
Pollable::asyncMsg(msg.str());
return 1;
}
return 0;
}
void DevMinder::handleEvents ( struct pollfd *pollfds, bool timedOut, double timeNow) {
int avail = hw_handleEvents(pollfds, timedOut);
if (avail < 0) {
std::ostringstream msg;
msg << "\"event\":\"devProblem\",\"error\":\" device returned with error " << (- avail) << "\",\"devLabel\":\"" << label << "\"";
Pollable::asyncMsg(msg.str());
hw_do_restart();
return;
}
if (avail > 0)
lastDataReceived = timeNow;
if (avail * numChan > sampleBuf.capacity()) {
sampleBuf.resize(avail * numChan);
}
double frameTimestamp;
avail = hw_getFrames (& sampleBuf[0], avail, frameTimestamp);
totalFrames += avail;
if (avail > 0) {
// FIXME: assumes interleaved channels
// now downsample sampleBuf, using the running accumulator.
// We downsample in-place, keeping track of the destination
// index in downSampleAvail;
int downSampleAvail = avail;
if (downSampleFactor > 1) {
for (unsigned j = 0; j < numChan; ++j) {
downSampleAvail = 0; // works the same for all channels
if (downSampleUseAvg) {
int16_t * rs = & sampleBuf[j];
int16_t * ds = rs;
for (int i=0; i < avail; ++i) {
downSampleAccum[j] += *rs;
rs += numChan;
if (! --downSampleCount[j]) {
downSampleCount[j] = downSampleFactor;
// simple dithering: round to nearest int, but retain remainder in downSampleAccum
int16_t downSample = (downSampleAccum[j] + downSampleFactor / 2) / downSampleFactor;
*ds = downSample;
downSampleAccum[j] -= downSample * downSampleFactor;
ds += numChan;
++ downSampleAvail;
}
}
} else {
int16_t * rs = & sampleBuf[j];
int16_t * ds = rs;
for (int i=0; i < avail; ++i) {
if (! --downSampleCount[j]) {
downSampleCount[j] = downSampleFactor;
*ds = *rs;
ds += numChan;
++ downSampleAvail;
}
rs += numChan;
}
}
}
}
// if requested, do FM demodulation of the downsamples,
if (numChan == 2 && demodFMForRaw) {
// do in-place FM demodulation with simple but expensive arctan!
// only first avail slots in sampleBuf will end up valid
float dthetaScale = hwRate / (2 * M_PI) / 75000.0 * 32767.0;
for (int i=0; i < downSampleAvail; ++i) {
// get phase angle in -pi..pi
float theta = atan2f(sampleBuf[2*i], sampleBuf[2*i+1]);
float dtheta = theta - demodFMLastTheta;
demodFMLastTheta = theta;
if (dtheta > M_PI) {
dtheta -= 2 * M_PI;
} else if (dtheta < -M_PI) {
dtheta += 2 * M_PI;
}
sampleBuf[i] = roundf(dthetaScale * dtheta);
}
}
// there are now downSampleAvail samples, stored in sampleBuf[0..downSampleAvail * numChan - 1]
for (RawListenerSet::iterator ir = rawListeners.begin(); ir != rawListeners.end(); /**/) {
if (Pollable * ptr = (ir->second).lock().get()) {
ptr->queueOutput((char *) & sampleBuf[0], downSampleAvail * 2 * numChan, frameTimestamp ); // NB: hardcoded S16_LE sample size
++ir;
} else {
RawListenerSet::iterator to_delete = ir++;
rawListeners.erase(to_delete);
}
}
/*
copy from sampleBuf to each attached plugin's buffer,
converting from S16_LE to float, and calling the plugin if its
buffer has reached blocksize
*/
for (PluginRunnerSet::iterator ip = plugins.begin(); ip != plugins.end(); /**/) {
if (boost::shared_ptr < PluginRunner > ptr = (ip->second).lock()) {
ptr->handleData(downSampleAvail, & sampleBuf[0], & sampleBuf[1], 2, frameTimestamp);
++ip;
} else {
PluginRunnerSet::iterator to_delete = ip++;
plugins.erase(to_delete);
}
}
} else if (shouldBeRunning && lastDataReceived >= 0 && timeNow - lastDataReceived > MAX_DEV_QUIET_TIME
&& ! (timeNow > 1000000000 && lastDataReceived < 1000000000)) {
// this device appears to have stopped delivering audio; try restart it
std::ostringstream msg;
msg << "\"event\":\"devStalled\",\"error\":\"no data received for " << (timeNow - lastDataReceived) << " secs;\",\"devLabel\":\"" << label << "\"";
Pollable::asyncMsg(msg.str());
lastDataReceived = timeNow; // wait before next restart
stop(timeNow);
Pollable::requestPollFDRegen();
}
};
void
DevMinder::setDemodFMForRaw(bool demod) {
demodFMForRaw = demod;
};