|
| 1 | +// |
| 2 | +// by Weikton 05.09.23 |
| 3 | +// |
| 4 | +#include "Channel.h" |
| 5 | + |
| 6 | +#include "PluginConfig.h" |
| 7 | +#include "Record.h" |
| 8 | + |
| 9 | +Channel::Channel(const uint32_t channelFlags) |
| 10 | + : handle(BASS_StreamCreate(SV::kFrequency2, 1, channelFlags, STREAMPROC_PUSH, nullptr)) |
| 11 | + , decoder(opus_decoder_create(SV::kFrequency, 1, &opusErrorCode)) |
| 12 | +{ |
| 13 | + if(this->handle == NULL) LogVoice("[sv:err:channel] : " |
| 14 | + "failed to create bass channel (code:%d)", BASS_ErrorGetCode()); |
| 15 | + if(this->decoder == nullptr) LogVoice("[sv:err:channel] : " |
| 16 | + "failed to create opus decoder (code:%d)", this->opusErrorCode); |
| 17 | + |
| 18 | + if(this->handle == NULL || this->decoder == nullptr) |
| 19 | + { |
| 20 | + if(this->decoder != nullptr) opus_decoder_destroy(this->decoder); |
| 21 | + if(this->handle != NULL) BASS_StreamFree(this->handle); |
| 22 | + throw std::exception(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +Channel::~Channel() noexcept |
| 27 | +{ |
| 28 | + if(this->playing) this->channelstop = true; |
| 29 | + //if(this->playing && this->stopCallback != nullptr) |
| 30 | + //this->stopCallback(*this); |
| 31 | + |
| 32 | + opus_decoder_destroy(this->decoder); |
| 33 | + BASS_StreamFree(this->handle); |
| 34 | +} |
| 35 | + |
| 36 | +HSTREAM Channel::GetHandle() const noexcept |
| 37 | +{ |
| 38 | + return this->handle; |
| 39 | +} |
| 40 | + |
| 41 | +void Channel::SetSpeaker(const uint16_t speaker) noexcept |
| 42 | +{ |
| 43 | + this->speaker = speaker; |
| 44 | +} |
| 45 | + |
| 46 | +bool Channel::HasSpeaker() const noexcept |
| 47 | +{ |
| 48 | + return this->speaker != SV::kNonePlayer; |
| 49 | +} |
| 50 | + |
| 51 | +uint16_t Channel::GetSpeaker() const noexcept |
| 52 | +{ |
| 53 | + return this->speaker; |
| 54 | +} |
| 55 | + |
| 56 | +bool Channel::IsActive() const noexcept |
| 57 | +{ |
| 58 | + const auto bufferSize = BASS_ChannelGetData(this->handle, nullptr, BASS_DATA_AVAILABLE); |
| 59 | + return bufferSize != -1 && bufferSize != 0; |
| 60 | +} |
| 61 | + |
| 62 | +void Channel::Reset() noexcept |
| 63 | +{ |
| 64 | + BASS_ChannelPause(this->handle); |
| 65 | + BASS_ChannelSetPosition(this->handle, 0, BASS_POS_BYTE); |
| 66 | + opus_decoder_ctl(this->decoder, OPUS_RESET_STATE); |
| 67 | + |
| 68 | + //if(this->playing && this->stopCallback != nullptr) |
| 69 | + //this->stopCallback(*this); |
| 70 | + |
| 71 | + this->speaker = SV::kNonePlayer; |
| 72 | + this->expectedPacketNumber = 0; |
| 73 | + this->initialized = false; |
| 74 | + this->playing = false; |
| 75 | +} |
| 76 | + |
| 77 | +void Channel::Push(const uint32_t packetNumber, const uint8_t* const dataPtr, const uint32_t dataSize) noexcept |
| 78 | +{ |
| 79 | + if(!this->initialized || packetNumber == NULL) |
| 80 | + { |
| 81 | + LogVoice("[sv:dbg:channel:push] : init channel (speaker:%hu)", this->speaker); |
| 82 | + |
| 83 | + BASS_ChannelPause(this->handle); |
| 84 | + BASS_ChannelSetPosition(this->handle, 0, BASS_POS_BYTE); |
| 85 | + opus_decoder_ctl(this->decoder, OPUS_RESET_STATE); |
| 86 | + |
| 87 | + if(this->playing) this->channelstop = true; |
| 88 | + //if(this->playing && this->stopCallback != nullptr) |
| 89 | + //this->stopCallback(*this); |
| 90 | + |
| 91 | + this->initialized = true; |
| 92 | + this->playing = false; |
| 93 | + } |
| 94 | + else if(packetNumber < this->expectedPacketNumber) |
| 95 | + { |
| 96 | + LogVoice("[sv:dbg:channel:push] : late packet to channel (speaker:%hu) " |
| 97 | + "(pack:%u;expPack:%u)", this->speaker, packetNumber, this->expectedPacketNumber); |
| 98 | + |
| 99 | + return; |
| 100 | + } |
| 101 | + else if(packetNumber > this->expectedPacketNumber) |
| 102 | + { |
| 103 | + LogVoice("[sv:dbg:channel:push] : lost packet to channel (speaker:%hu) " |
| 104 | + "(pack:%u;expPack:%u)", this->speaker, packetNumber, this->expectedPacketNumber); |
| 105 | + |
| 106 | + if(const int length = opus_decode(this->decoder, dataPtr, dataSize, this->decBuffer.data(), |
| 107 | + SV::kFrameSizeInSamples, true); length == static_cast<int>(SV::kFrameSizeInSamples)) |
| 108 | + { |
| 109 | + BASS_StreamPutData(this->handle, this->decBuffer.data(), SV::kFrameSizeInBytes); |
| 110 | + |
| 111 | + // put frame into playback buffer |
| 112 | + speex_echo_playback(Record::speexEchoState, this->decBuffer.data()); |
| 113 | + } |
| 114 | + |
| 115 | + goto gohere; |
| 116 | + } |
| 117 | + |
| 118 | + if(const int length = opus_decode(this->decoder, dataPtr, dataSize, this->decBuffer.data(), |
| 119 | + SV::kFrameSizeInSamples, false); length == static_cast<int>(SV::kFrameSizeInSamples)) |
| 120 | + { |
| 121 | + BASS_StreamPutData(this->handle, this->decBuffer.data(), SV::kFrameSizeInBytes); |
| 122 | + |
| 123 | + // put frame into playback buffer |
| 124 | + speex_echo_playback(Record::speexEchoState, this->decBuffer.data()); |
| 125 | + } |
| 126 | + |
| 127 | +gohere: |
| 128 | + const auto channelStatus = BASS_ChannelIsActive(this->handle); |
| 129 | + const auto bufferSize = BASS_ChannelGetData(this->handle, nullptr, BASS_DATA_AVAILABLE); |
| 130 | + |
| 131 | + if((channelStatus == BASS_ACTIVE_PAUSED || channelStatus == BASS_ACTIVE_STOPPED) && |
| 132 | + bufferSize != -1 && bufferSize >= SV::kChannelPreBufferFramesCount * SV::kFrameSizeInBytes) |
| 133 | + { |
| 134 | + LogVoice("[sv:dbg:channel:push] : playing channel (speaker:%hu)", this->speaker); |
| 135 | + |
| 136 | + BASS_ChannelPlay(this->handle, 0); |
| 137 | + |
| 138 | + if(!this->playing) this->channelplay = true; |
| 139 | + //if(!this->playing && this->playCallback != nullptr) |
| 140 | + //this->playCallback(*this); |
| 141 | + |
| 142 | + this->playing = true; |
| 143 | + } |
| 144 | + |
| 145 | + this->expectedPacketNumber = packetNumber + 1; |
| 146 | +} |
| 147 | + |
| 148 | +void Channel::SetPlayCallback(PlayCallback playCallback) noexcept |
| 149 | +{ |
| 150 | + this->playCallback = std::move(playCallback); |
| 151 | +} |
| 152 | + |
| 153 | +void Channel::SetStopCallback(StopCallback stopCallback) noexcept |
| 154 | +{ |
| 155 | + this->stopCallback = std::move(stopCallback); |
| 156 | +} |
0 commit comments