| 1 |
/* |
| 2 |
* Copyright (C) 2018-2023 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "audio_stream.hpp" |
| 27 |
#include <algorithm> |
| 28 |
|
| 29 |
const std::string AudioStream::AUDIO_OUT_CLIENT_NAME = "BambooTracker"; |
| 30 |
|
| 31 |
AudioStream::AudioStream(QObject *parent) |
| 32 |
: QObject(parent), |
| 33 |
rate_(0), |
| 34 |
intrRate_(0), |
| 35 |
intrCount_(0), |
| 36 |
intrCountRest_(0), |
| 37 |
gcb_(nullptr), |
| 38 |
gcbPtr_(nullptr), |
| 39 |
tuState_(-1), |
| 40 |
started_(false), |
| 41 |
quitNotify_(false), |
| 42 |
tickNotifier_([this]() { tickNotifierRun(); }) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
AudioStream::~AudioStream() |
| 47 |
{ |
| 48 |
quitNotify_.store(true); |
| 49 |
tickNotifierSem_.release(); |
| 50 |
tickNotifier_.join(); |
| 51 |
} |
| 52 |
|
| 53 |
void AudioStream::setGenerateCallback(GenerateCallback* cb, void* cbPtr) |
| 54 |
{ |
| 55 |
std::lock_guard<std::mutex> lock(mutex_); |
| 56 |
gcb_ = cb; |
| 57 |
gcbPtr_ = cbPtr; |
| 58 |
} |
| 59 |
|
| 60 |
void AudioStream::setTickUpdateCallback(TickUpdateCallback* cb, void* cbPtr) |
| 61 |
{ |
| 62 |
std::lock_guard<std::mutex> lock(mutex_); |
| 63 |
tucb_ = cb; |
| 64 |
tucbPtr_ = cbPtr; |
| 65 |
} |
| 66 |
|
| 67 |
bool AudioStream::initialize(uint32_t rate, uint32_t duration, uint32_t intrRate, |
| 68 |
const QString& backend, const QString& device, QString* errDetail) |
| 69 |
{ |
| 70 |
Q_UNUSED(duration) |
| 71 |
Q_UNUSED(backend) |
| 72 |
Q_UNUSED(device) |
| 73 |
Q_UNUSED(errDetail) |
| 74 |
|
| 75 |
started_ = false; |
| 76 |
rate_ = rate; |
| 77 |
setInterruption(intrRate); |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
void AudioStream::setInterruption(uint32_t intrRate) |
| 82 |
{ |
| 83 |
std::lock_guard<std::mutex> lock(mutex_); |
| 84 |
intrRate_ = intrRate; |
| 85 |
intrCount_ = rate_ / intrRate_; |
| 86 |
} |
| 87 |
|
| 88 |
uint32_t AudioStream::getStreamRate() const noexcept |
| 89 |
{ |
| 90 |
return rate_; |
| 91 |
} |
| 92 |
|
| 93 |
void AudioStream::start() |
| 94 |
{ |
| 95 |
std::lock_guard<std::mutex> lock(mutex_); |
| 96 |
started_ = true; |
| 97 |
} |
| 98 |
|
| 99 |
void AudioStream::stop() |
| 100 |
{ |
| 101 |
std::lock_guard<std::mutex> lock(mutex_); |
| 102 |
started_ = false; |
| 103 |
} |
| 104 |
|
| 105 |
bool AudioStream::generate(int16_t* container, uint32_t nSamples) |
| 106 |
{ |
| 107 |
GenerateCallback* gcb = nullptr; |
| 108 |
void* gcbPtr = nullptr; |
| 109 |
TickUpdateCallback* tucb = nullptr; |
| 110 |
bool started = false; |
| 111 |
|
| 112 |
std::unique_lock<std::mutex> lock(mutex_, std::try_to_lock); |
| 113 |
if (lock.owns_lock()) { |
| 114 |
gcb = gcb_; |
| 115 |
gcbPtr = gcbPtr_; |
| 116 |
tucb = tucb_; |
| 117 |
started = started_; |
| 118 |
} |
| 119 |
|
| 120 |
if (!gcb || !tucb || !started) { |
| 121 |
std::fill(container, container + (nSamples << 1), 0); |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
int16_t* destPtr = container; |
| 126 |
while (nSamples) { |
| 127 |
if (!intrCountRest_) { // Interruption |
| 128 |
intrCountRest_ = intrCount_; // Set counts to next interruption |
| 129 |
generateTick(); |
| 130 |
} |
| 131 |
|
| 132 |
size_t count = std::min(intrCountRest_, nSamples); |
| 133 |
nSamples -= count; |
| 134 |
intrCountRest_ -= count; |
| 135 |
|
| 136 |
bool result = gcb(destPtr, count, gcbPtr); |
| 137 |
if (!result) { |
| 138 |
// Something went wrong in sample generation callback |
| 139 |
emit streamErrorInCallback(QVariant()); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
destPtr += (count << 1); // Move head |
| 144 |
} |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
void AudioStream::generateTick() |
| 150 |
{ |
| 151 |
tuState_.store(tucb_(tucbPtr_)); |
| 152 |
tickNotifierSem_.release(); |
| 153 |
} |
| 154 |
|
| 155 |
void AudioStream::tickNotifierRun() |
| 156 |
{ |
| 157 |
while (true) { |
| 158 |
tickNotifierSem_.acquire(); |
| 159 |
|
| 160 |
if (quitNotify_.load()) return; |
| 161 |
|
| 162 |
emit streamInterrupted(tuState_.load()); |
| 163 |
} |
| 164 |
} |