-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathAudioPlayback.cpp
630 lines (524 loc) · 13.6 KB
/
AudioPlayback.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//
// AudioPlayback.cpp: Put samples in the soundcard
// Copyright (C) 2020 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include <iostream>
#include "AudioPlayback.h"
#include <stdexcept>
#include <sigutils/util/compat-mman.h>
#include <QCoreApplication>
#include <GenericAudioPlayer.h>
#ifdef SIGDIGGER_HAVE_ALSA
# include "AlsaPlayer.h"
#elif defined(SIGDIGGER_HAVE_PORTAUDIO)
# include "PortAudioPlayer.h"
#endif // SIGIDGGER_HAVE_ALSA
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
# define QMutexLocker QMutexLocker<QRecursiveMutex>
#endif
using namespace SigDigger;
#define ATTEMPT(expr, what) \
if ((err = expr) < 0) \
throw std::runtime_error("Failed to " + std::string(what) + ": " + \
std::string(snd_strerror(err)))
///////////////////////////// Playback worker /////////////////////////////////
unsigned int
PlaybackWorker::calcBufferSizeForRate(unsigned int rate)
{
return
qBound(
static_cast<unsigned int>(SIGDIGGER_AUDIO_BUFFER_SIZE_MIN),
static_cast<unsigned int>(
SIGDIGGER_AUDIO_BUFFER_DELAY_MS
* 1e-3f
* static_cast<float>(rate)),
static_cast<unsigned int>(SIGDIGGER_AUDIO_BUFFER_SIZE));
}
PlaybackWorker::PlaybackWorker(
AudioBufferList *instance,
std::string const &dev,
unsigned int sampRate)
{
this->device = dev;
this->sampRate = sampRate;
this->instance = instance;
this->bufferSize = calcBufferSizeForRate(sampRate);
}
unsigned int
PlaybackWorker::getBufferSize(void) const
{
return this->bufferSize;
}
void
PlaybackWorker::setGain(float vol)
{
this->gain = vol;
}
void
PlaybackWorker::play(void)
{
float *buffer;
unsigned int i;
while (this->player != nullptr && (buffer = this->instance->next()) != nullptr) {
bool ok;
for (i = 0; i < this->bufferSize; ++i)
buffer[i] *= this->gain;
ok = this->player->write(buffer, this->bufferSize);
// Done with this buffer, mark as free.
this->instance->release();
if (!ok) {
this->stopPlayback();
emit error("Playback error");
}
// Process pending events
QCoreApplication::processEvents();
}
if (this->player != nullptr)
emit starving();
}
void
PlaybackWorker::startPlayback()
{
if (this->player == nullptr) {
// Reset buffer list
this->instance->clear();
try {
#ifdef SIGDIGGER_HAVE_ALSA
this->player = new AlsaPlayer(
this->device,
this->sampRate,
this->bufferSize);
#elif defined(SIGDIGGER_HAVE_PORTAUDIO)
this->player = new PortAudioPlayer(
this->device,
this->sampRate,
this->bufferSize);
#else
throw std::runtime_error(
"Cannot create audio playback object: audio support disabled at compile time");
#endif // SIGDIGGER_HAVE_ALSA
emit ready();
} catch (std::runtime_error &e) {
this->player = nullptr;
emit error(QString::fromStdString(e.what()));
}
}
}
bool
AudioPlayback::enumerateDevices(std::vector<GenericAudioDevice> &list)
{
#ifdef SIGDIGGER_HAVE_ALSA
return AlsaPlayer::enumerateDevices(list);
#elif defined(SIGDIGGER_HAVE_PORTAUDIO)
return PortAudioPlayer::enumerateDevices(list);
#else
return false;
#endif // SIGDIGER_HAVE_ALSA
}
std::string
AudioPlayback::getDefaultDevice()
{
#ifdef SIGDIGGER_HAVE_ALSA
return AlsaPlayer::getDefaultDevice().devStr;
#elif defined(SIGDIGGER_HAVE_PORTAUDIO)
return PortAudioPlayer::getDefaultDevice().devStr;
#else
return "";
#endif // SIGDIGER_HAVE_ALSA
}
const char *
AudioPlayback::audioLibrary()
{
#ifdef SIGDIGGER_HAVE_ALSA
return "alsa";
#elif defined(SIGDIGGER_HAVE_PORTAUDIO)
return "portaudio";
#else
return nullptr;
#endif // SIGDIGER_HAVE_ALSA
}
void
PlaybackWorker::stopPlayback()
{
if (this->player != nullptr) {
delete this->player;
this->player = nullptr;
}
}
void
PlaybackWorker::setSampleRate(unsigned int rate)
{
if (this->sampRate != rate) {
this->sampRate = rate;
this->bufferSize = calcBufferSizeForRate(rate);
if (this->player != nullptr) {
this->stopPlayback();
this->startPlayback();
}
}
}
void
PlaybackWorker::halt(void)
{
this->stopPlayback();
emit finished();
}
////////////////////////////////// Audio buffer ///////////////////////////////
AudioBuffer::AudioBuffer()
{
float *buf;
// Ladies and gentlemen, behold the COBOL
if ((buf = static_cast<float *>(mmap(
nullptr,
SIGDIGGER_AUDIO_BUFFER_ALLOC,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0))) == reinterpret_cast<float *>(-1)) {
throw std::runtime_error(
"Failed to allocate "
+ std::to_string(SIGDIGGER_AUDIO_BUFFER_ALLOC)
+ " of buffer bytes for soundcard.");
}
this->data = buf;
}
AudioBuffer::~AudioBuffer()
{
if (this->data != nullptr)
munmap(this->data, SIGDIGGER_AUDIO_BUFFER_ALLOC);
}
////////////////////////////// AudioBufferList /////////////////////////////////
AudioBufferList::AudioBufferList(unsigned int num)
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
: listMutex(QMutex::Recursive)
#endif
{
unsigned int i;
this->allocation.resize(num);
for (i = 0; i < num; ++i) {
AudioBuffer *buffer = &this->allocation[i];
buffer->next = this->freeList;
this->freeList = buffer;
}
this->freeLen = this->totalLen = num;
}
void
AudioBufferList::reset(void)
{
QMutexLocker locker(&this->listMutex);
if (this->current != nullptr) {
AudioBuffer *buffer = this->current;
this->current = nullptr;
buffer->next = this->freeList;
this->freeList = buffer;
}
if (this->playBuffer != nullptr) {
AudioBuffer *buffer = this->playBuffer;
this->playBuffer = nullptr;
buffer->next = this->freeList;
this->freeList = buffer;
}
// Moving the current playlist is actually pretty easy
if (this->playListHead != nullptr) {
this->playListTail->next = this->freeList;
this->freeList = this->playListHead;
this->playListHead = this->playListTail = nullptr;
}
this->freeLen = this->totalLen;
this->playListLen = 0;
}
float *
AudioBufferList::reserve(void)
{
QMutexLocker locker(&this->listMutex);
// You cannot reserve a buffer before committing int
if (this->current != nullptr) {
std::cerr << "Invalid reserve(), please call commit() first!" << std::endl;
return nullptr;
} else if (this->freeList == nullptr) {
// std::cerr << "Free audio buffer list exhausted!" << std::endl;
return nullptr;
}
this->current = this->freeList;
this->freeList = this->freeList->next;
--this->freeLen;
return this->current->data;
}
void
AudioBufferList::commit(void)
{
QMutexLocker locker(&this->listMutex);
// You cannot commit if the current buffer is null
if (this->current == nullptr) {
std::cerr << "Calling commit() with no reserve() is forbidden." << std::endl;
} else {
AudioBuffer *buffer = this->current;
this->current = nullptr;
buffer->prev = nullptr;
buffer->next = this->playListHead;
if (this->playListHead != nullptr)
this->playListHead->prev = buffer;
this->playListHead = buffer;
if (this->playListTail == nullptr)
this->playListTail = buffer;
++this->playListLen;
}
}
float *
AudioBufferList::next(void)
{
QMutexLocker locker(&this->listMutex);
if (this->playBuffer != nullptr) {
std::cerr << "Invalid next(), please call release() first!" << std::endl;
return nullptr;
} else if (this->playListTail == nullptr) {
// Starving
return nullptr;
} else {
AudioBuffer *buffer = this->playListTail;
this->playBuffer = buffer;
this->playListTail = buffer->prev;
if (this->playListTail == nullptr)
this->playListHead = nullptr;
--this->playListLen;
return buffer->data;
}
}
void
AudioBufferList::release(void)
{
QMutexLocker locker(&this->listMutex);
if (this->playBuffer == nullptr) {
std::cerr << "Invalid release(), please call next() first!" << std::endl;
} else {
AudioBuffer *buffer = this->playBuffer;
this->playBuffer = nullptr;
buffer->next = this->freeList;
this->freeList = buffer;
++this->freeLen;
}
}
void
AudioBufferList::clear(void)
{
QMutexLocker locker(&this->listMutex);
// You cannot commit if the current buffer is null
if (this->current != nullptr) {
AudioBuffer *buffer = this->current;
this->current = nullptr;
buffer->prev = nullptr;
buffer->next = this->playListHead;
if (this->playListHead != nullptr)
this->playListHead->prev = buffer;
this->playListHead = buffer;
if (this->playListTail == nullptr)
this->playListTail = buffer;
++this->playListLen;
}
while (next())
release();
}
//////////////////////////////// AudioBuffer ///////////////////////////////////
AudioPlayback::AudioPlayback(std::string const &dev, unsigned int rate)
: bufferList(SIGDIGGER_AUDIO_BUFFER_NUM)
{
this->device = dev;
this->sampRate = rate;
this->bufferSize = PlaybackWorker::calcBufferSizeForRate(rate);
this->startWorker();
}
void
AudioPlayback::startWorker()
{
this->worker = new PlaybackWorker(
&this->bufferList,
this->device,
this->sampRate);
this->workerThread = new QThread();
this->worker->moveToThread(this->workerThread);
connect(
this->worker,
SIGNAL(error(QString)),
this,
SLOT(onError(QString)));
connect(
this->worker,
SIGNAL(starving()),
this,
SLOT(onStarving()));
connect(
this->worker,
SIGNAL(ready()),
this,
SLOT(onReady()));
// On restart, play
connect(
this,
SIGNAL(restart()),
this->worker,
SLOT(play()));
connect(
this,
SIGNAL(startPlayback()),
this->worker,
SLOT(startPlayback()));
connect(
this,
SIGNAL(stopPlayback()),
this->worker,
SLOT(stopPlayback()));
connect(
this,
SIGNAL(sampleRate(unsigned int)),
this->worker,
SLOT(setSampleRate(unsigned int)));
connect(
this,
SIGNAL(gain(float)),
this->worker,
SLOT(setGain(float)));
this->workerThread->start();
}
void
AudioPlayback::onError(QString desc)
{
this->running = false;
emit error(desc);
}
void
AudioPlayback::onStarving(void)
{
if (this->bufferList.getPlayListLen() < SIGDIGGER_AUDIO_BUFFERING_WATERMARK) {
this->completed = 0;
this->buffering = true;
std::cout << "AudioPlayback: reached watermark, buffering again..." << std::endl;
} else {
emit restart();
}
}
AudioPlayback::~AudioPlayback()
{
emit halt();
if (this->workerThread != nullptr) {
this->workerThread->quit();
this->workerThread->wait();
delete this->workerThread;
if (this->worker != nullptr)
delete this->worker;
}
}
unsigned int
AudioPlayback::getSampleRate(void) const
{
return this->sampRate;
}
void
AudioPlayback::cancelPlayBack(void)
{
this->bufferSize = PlaybackWorker::calcBufferSizeForRate(this->sampRate);
this->ptr = 0;
this->ready = false;
this->current_buffer = nullptr;
this->completed = 0;
}
void
AudioPlayback::setSampleRate(unsigned int rate)
{
if (this->sampRate != rate) {
this->sampRate = rate;
this->cancelPlayBack();
emit sampleRate(rate);
}
}
float
AudioPlayback::getVolume(void) const
{
return this->volume;
}
void
AudioPlayback::setVolume(float vol)
{
SUFLOAT gainVal;
if (vol < -60)
gainVal = 0;
else
gainVal = SU_MAG_RAW(vol);
this->volume = vol;
emit gain(gainVal);
}
void
AudioPlayback::start(void)
{
if (!this->running) {
emit startPlayback();
this->buffering = true;
this->running = true;
}
}
void
AudioPlayback::stop(void)
{
if (this->running) {
this->cancelPlayBack();
emit stopPlayback();
this->running = false;
}
}
void
AudioPlayback::write(const SUCOMPLEX *samples, SUSCOUNT size)
{
unsigned int bufferSize = this->bufferSize;
while (size > 0 && this->running && this->ready) {
SUSCOUNT chunk = size;
SUSCOUNT remaining;
float *start;
// No current buffer, try to allocate
if (this->current_buffer == nullptr) {
this->ptr = 0;
if ((this->current_buffer = this->bufferList.reserve()) == nullptr) {
// Somehow the playback thread is slow...
return;
}
}
start = this->current_buffer + this->ptr;
if (chunk > bufferSize - this->ptr)
chunk = bufferSize - this->ptr;
remaining = chunk;
while (remaining-- > 0)
*start++ = SU_C_REAL(*samples++);
this->ptr += chunk;
size -= chunk;
// Buffer full, send to playback thread.
if (this->ptr == bufferSize) {
this->current_buffer = nullptr;
this->bufferList.commit();
// If buffering, we wait until we have SIGDIGGER_AUDIO_BUFFER_MIN
// buffers full. When that happens, we restart the thread.
if (this->buffering) {
if (++this->completed == SIGDIGGER_AUDIO_BUFFER_MIN) {
emit restart();
this->buffering = false;
}
}
}
}
}
void
AudioPlayback::onReady(void)
{
this->ready = true;
}