-
Notifications
You must be signed in to change notification settings - Fork 1
/
Channel.cpp
156 lines (125 loc) · 4.91 KB
/
Channel.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
//
// by Weikton 05.09.23
//
#include "Channel.h"
#include "PluginConfig.h"
#include "Record.h"
Channel::Channel(const uint32_t channelFlags)
: handle(BASS_StreamCreate(SV::kFrequency2, 1, channelFlags, STREAMPROC_PUSH, nullptr))
, decoder(opus_decoder_create(SV::kFrequency, 1, &opusErrorCode))
{
if(this->handle == NULL) LogVoice("[sv:err:channel] : "
"failed to create bass channel (code:%d)", BASS_ErrorGetCode());
if(this->decoder == nullptr) LogVoice("[sv:err:channel] : "
"failed to create opus decoder (code:%d)", this->opusErrorCode);
if(this->handle == NULL || this->decoder == nullptr)
{
if(this->decoder != nullptr) opus_decoder_destroy(this->decoder);
if(this->handle != NULL) BASS_StreamFree(this->handle);
throw std::exception();
}
}
Channel::~Channel() noexcept
{
if(this->playing) this->channelstop = true;
//if(this->playing && this->stopCallback != nullptr)
//this->stopCallback(*this);
opus_decoder_destroy(this->decoder);
BASS_StreamFree(this->handle);
}
HSTREAM Channel::GetHandle() const noexcept
{
return this->handle;
}
void Channel::SetSpeaker(const uint16_t speaker) noexcept
{
this->speaker = speaker;
}
bool Channel::HasSpeaker() const noexcept
{
return this->speaker != SV::kNonePlayer;
}
uint16_t Channel::GetSpeaker() const noexcept
{
return this->speaker;
}
bool Channel::IsActive() const noexcept
{
const auto bufferSize = BASS_ChannelGetData(this->handle, nullptr, BASS_DATA_AVAILABLE);
return bufferSize != -1 && bufferSize != 0;
}
void Channel::Reset() noexcept
{
BASS_ChannelPause(this->handle);
BASS_ChannelSetPosition(this->handle, 0, BASS_POS_BYTE);
opus_decoder_ctl(this->decoder, OPUS_RESET_STATE);
//if(this->playing && this->stopCallback != nullptr)
//this->stopCallback(*this);
this->speaker = SV::kNonePlayer;
this->expectedPacketNumber = 0;
this->initialized = false;
this->playing = false;
}
void Channel::Push(const uint32_t packetNumber, const uint8_t* const dataPtr, const uint32_t dataSize) noexcept
{
if(!this->initialized || packetNumber == NULL)
{
LogVoice("[sv:dbg:channel:push] : init channel (speaker:%hu)", this->speaker);
BASS_ChannelPause(this->handle);
BASS_ChannelSetPosition(this->handle, 0, BASS_POS_BYTE);
opus_decoder_ctl(this->decoder, OPUS_RESET_STATE);
if(this->playing) this->channelstop = true;
//if(this->playing && this->stopCallback != nullptr)
//this->stopCallback(*this);
this->initialized = true;
this->playing = false;
}
else if(packetNumber < this->expectedPacketNumber)
{
LogVoice("[sv:dbg:channel:push] : late packet to channel (speaker:%hu) "
"(pack:%u;expPack:%u)", this->speaker, packetNumber, this->expectedPacketNumber);
return;
}
else if(packetNumber > this->expectedPacketNumber)
{
LogVoice("[sv:dbg:channel:push] : lost packet to channel (speaker:%hu) "
"(pack:%u;expPack:%u)", this->speaker, packetNumber, this->expectedPacketNumber);
if(const int length = opus_decode(this->decoder, dataPtr, dataSize, this->decBuffer.data(),
SV::kFrameSizeInSamples, true); length == static_cast<int>(SV::kFrameSizeInSamples))
{
BASS_StreamPutData(this->handle, this->decBuffer.data(), SV::kFrameSizeInBytes);
// put frame into playback buffer
speex_echo_playback(Record::speexEchoState, this->decBuffer.data());
}
goto gohere;
}
if(const int length = opus_decode(this->decoder, dataPtr, dataSize, this->decBuffer.data(),
SV::kFrameSizeInSamples, false); length == static_cast<int>(SV::kFrameSizeInSamples))
{
BASS_StreamPutData(this->handle, this->decBuffer.data(), SV::kFrameSizeInBytes);
// put frame into playback buffer
speex_echo_playback(Record::speexEchoState, this->decBuffer.data());
}
gohere:
const auto channelStatus = BASS_ChannelIsActive(this->handle);
const auto bufferSize = BASS_ChannelGetData(this->handle, nullptr, BASS_DATA_AVAILABLE);
if((channelStatus == BASS_ACTIVE_PAUSED || channelStatus == BASS_ACTIVE_STOPPED) &&
bufferSize != -1 && bufferSize >= SV::kChannelPreBufferFramesCount * SV::kFrameSizeInBytes)
{
LogVoice("[sv:dbg:channel:push] : playing channel (speaker:%hu)", this->speaker);
BASS_ChannelPlay(this->handle, 0);
if(!this->playing) this->channelplay = true;
//if(!this->playing && this->playCallback != nullptr)
//this->playCallback(*this);
this->playing = true;
}
this->expectedPacketNumber = packetNumber + 1;
}
void Channel::SetPlayCallback(PlayCallback playCallback) noexcept
{
this->playCallback = std::move(playCallback);
}
void Channel::SetStopCallback(StopCallback stopCallback) noexcept
{
this->stopCallback = std::move(stopCallback);
}