This repository was archived by the owner on Jan 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransmitter.cpp
More file actions
363 lines (315 loc) · 11.4 KB
/
Transmitter.cpp
File metadata and controls
363 lines (315 loc) · 11.4 KB
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
#include "Transmitter.h"
#include "./thirdparty/json.hpp"
#include "./src/GUIStyle.h"
#include "../thirdparty/netlib/src/netlib.h"
using namespace iplug;
#include "IControls.h"
#include "IPlug_include_in_plug_src.h"
using namespace transmitter;
Transmitter::Transmitter(const iplug::InstanceInfo& info) : iplug::Plugin(info, iplug::MakeConfig(kNumParams, kNumPrograms)) {
if (netlib_init() == -1) {
assert(false); // Well no point in doing any of this without networking
}
/**
* Setup the controls
*/
GetParam(kVolume)->InitGain("Volume own");
GetParam(kVolumeRemote)->InitGain("Volume remote");
GetParam(kBitRate)->InitDouble("Bitrate", 128, 1, 512, 0.1, "kBit/s");
GetParam(kComplexity)->InitDouble("OPUS Complexity", 10, 0, 10, 1);
GetParam(kPacketLoss)->InitPercentage("OPUS Expected packet loss");
GetParam(kBufferSize)->InitDouble("Receive Buffer Size", 960 * 2, 16, 10000, 1);
GetParam(kFrameSize)->InitEnum(
"OPUS Frame Size", 1, 4, "Samples", iplug::IParam::kFlagsNone,
"", "120", "240", "480", "960"
);
#if IPLUG_EDITOR // http://bit.ly/2S64BDd
mMakeGraphicsFunc = [&]() {
return MakeGraphics(*this, PLUG_WIDTH, PLUG_HEIGHT, PLUG_FPS, 1.);
};
mLayoutFunc = [&](IGraphics* pGraphics) {
mGraphics = pGraphics;
pGraphics->AttachCornerResizer(iplug::igraphics::EUIResizerMode::Scale, false);
pGraphics->AttachPanelBackground(style::BACKGROUND);
pGraphics->AttachTextEntryControl();
// pGraphics->AttachPopupMenuControl();
mGraphics->SetKeyHandlerFunc([&](const IKeyPress& key, const bool isUp) {
if (!isUp) { // Only handle key down
if (key.S) { // Check modifiers like shift first
if (key.VK == iplug::kVK_C) {
mGraphics->SetTextInClipboard(mMasterId->GetLabelString());
return true;
}
if (key.VK == iplug::kVK_V) {
WDL_String data;
mGraphics->GetTextFromClipboard(data);
mMasterServer->SetLabelStr(data.Get());
connect(true);
return true;
}
}
}
return false;
});
pGraphics->LoadFont("Roboto-Regular", ROBOTO_FN);
IRECT b = pGraphics->GetBounds();
b.B = style::TAB_BAR_HEIGHT;
/**
* Tab Bar
*/
mMainTabButton = new IVButtonControl(b.SubRectHorizontal(2, 0), [&](IControl* caller) {
switchTab(false);
::SplashClickActionFunc(caller);
}, "Master Server Mode");
pGraphics->AttachControl(mMainTabButton);
mDirectTabButton = new IVButtonControl(b.SubRectHorizontal(2, 1), [&](IControl* caller) {
switchTab(true);
::SplashClickActionFunc(caller);
}, "Direct Connect Mode");
pGraphics->AttachControl(mDirectTabButton);
b.T = b.B + style::PADDING;
b.B += 30;
pGraphics->AttachControl(new ITextControl(b, "Connection settings"));
/**
* Main tab controls
*/
b.T = b.B + style::PADDING;
b.B += 150;
IRECT left = b.GetReducedFromRight(style::BUTTON_WIDTH);
IRECT right = b.GetFromRight(style::BUTTON_WIDTH);
mMasterServer = new TextControl(left.SubRectVertical(2, 0), [&](IControl* pCaller) {
::SplashClickActionFunc(pCaller);
TextControl* c = dynamic_cast<TextControl*>(pCaller);
if (c == nullptr) { return; }
c->callback = [&](const char* host) {
return host;
};
GetUI()->CreateTextEntry(*pCaller, IText(20.f), pCaller->GetRECT(), c->GetLabelString());
}, DEFAULT_MASTER_SERVER);
mMainTab.Add(mMasterServer);
mMainTab.Add(new IVButtonControl(right.SubRectVertical(2, 0), [&](IControl* pCaller) {
::SplashClickActionFunc(pCaller);
connect(true);
}, "Connect"));
mMasterId = new IVButtonControl2(
left.SubRectVertical(2, 1), [&](IControl* pCaller) {
::SplashClickActionFunc(pCaller);
auto button = dynamic_cast<IVButtonControl2*>(pCaller);
const char* url = button->GetLabelString();
if (strncmp(url, STR_OWNID_HINT, 30) == 0) {
return;
}
GetUI()->SetTextInClipboard(button->GetLabelString());
// GetUI()->OpenURL(button->GetLabelString());
GetUI()->ReleaseMouseCapture();
},
mMSession ? mMSession->getOwnAddress() : STR_OWNID_HINT
);
mMainTab.Add(mMasterId);
mMainTab.Add(new IVButtonControl(right.SubRectVertical(2, 1), [&](IControl* pCaller) {
::SplashClickActionFunc(pCaller);
connect(false);
}, "get new ID"));
/**
* Direct connection tab controls
*/
// https://api.ipify.org/
mOwnIp = new ITextControl(b.SubRectVertical(2, 0), "OwnIP");
mDirectTab.Add(mOwnIp);
mDirectPeer = new TextControl(left.SubRectVertical(2, 1), [&](IControl* pCaller) {
::SplashClickActionFunc(pCaller);
TextControl* c = dynamic_cast<TextControl*>(pCaller);
if (c == nullptr) { return; }
c->callback = [&](const char* host) {
return host;
};
GetUI()->CreateTextEntry(*pCaller, IText(20.f), pCaller->GetRECT(), "DirectPeerPopup");
}, "DirectPeer");
mDirectTab.Add(mDirectPeer);
mDirectTab.Add(new IVButtonControl(right.SubRectVertical(2, 1), [&](IControl* pCaller) {
::SplashClickActionFunc(pCaller);
}, "Connect Peer"));
// mDirectTab.Add(new IVKnobControl())
for (int i = 0; i < mDirectTab.GetSize(); i++) {
pGraphics->AttachControl(mDirectTab.Get(i));
}
for (int i = 0; i < mMainTab.GetSize(); i++) {
pGraphics->AttachControl(mMainTab.Get(i));
}
switchTab(false);
/**
* General opus controls
*/
b.T = b.B + style::PADDING;
b.B += 30;
pGraphics->AttachControl(new ITextControl(b, "Opus encoder settings"));
b.T = b.B + style::PADDING;
b.B = pGraphics->GetBounds().B;
const int nRows = 3;
const int nCols = 3;
int cellIdx = -1;
auto nextCell = [&]() {
return b.GetGridCell(++cellIdx, nRows, nCols).GetHPadded(-5.);
};
auto sameCell = [&]() {
return b.GetGridCell(cellIdx, nRows, nCols).GetHPadded(-5.);
};
pGraphics->AttachControl(new IVKnobControl(nextCell(), kVolume), true);
pGraphics->AttachControl(new IVKnobControl(nextCell(), kVolumeRemote), true);
pGraphics->AttachControl(new IVKnobControl(nextCell(), kBitRate), true);
pGraphics->AttachControl(new IVKnobControl(nextCell(), kPacketLoss), true);
pGraphics->AttachControl(new IVKnobControl(nextCell(), kComplexity), true);
pGraphics->AttachControl(new ITextControl(nextCell().SubRectVertical(2, 0), "Frame Size"));
pGraphics->AttachControl(new ICaptionControl(sameCell().SubRectVertical(2, 1), kFrameSize, IText(24.f), iplug::igraphics::DEFAULT_FGCOLOR));
pGraphics->AttachControl(new IVKnobControl(nextCell(), kBufferSize), true);
//pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100).GetVShifted(-100), kGain));
//mSessionManager.init("127.0.0.1", 55556, 55555);
//if (mSessionManager.getId().empty()) {
// mSessionManager.start();
//}
//pGraphics->AttachControl(new ITextControl(b.GetMidVPadded(50), mSessionManager.getId().c_str(), IText(50)));
//if (mSessionManager.getId() == "!1") {
// mSessionManager.connectAsListener("!0");
//}
};
#endif
}
Transmitter::~Transmitter() {
netlib_quit();
if (mResamplingSetup) {
for (int c = 0; c < mChannelCount; c++) {
delete[] mResamplingBuffer[c];
}
delete[] mResamplingBuffer;
}
}
void Transmitter::switchTab(bool directTab) {
for (int i = 0; i < mDirectTab.GetSize(); i++) {
mDirectTab.Get(i)->Hide(!directTab);
}
for (int i = 0; i < mMainTab.GetSize(); i++) {
mMainTab.Get(i)->Hide(directTab);
}
mMainTabButton->SetStyle(!directTab ? style::TAB_TEXT_ACTIVE : style::TAB_TEXT_INACTIVE);
mDirectTabButton->SetStyle(directTab ? style::TAB_TEXT_ACTIVE : style::TAB_TEXT_INACTIVE);
if (directTab) {
delete mMSession;
mMSession = nullptr;
}
mGraphics->SetAllControlsDirty();
}
bool Transmitter::SerializeState(iplug::IByteChunk& chunk) const {
WDL_String serialized;
if (serialized.GetLength() < 1) {
return false;
}
chunk.PutStr(serialized.Get());
return true;
}
int Transmitter::UnserializeState(const iplug::IByteChunk& chunk, int startPos) {
WDL_String json_string;
return startPos;
}
void Transmitter::OnUIClose() {
/**
* Everything attached to the graphics will also be deleted once the UI
* is closed, so just clear out the references
*/
mMainTab.Empty(false);
mDirectTab.Empty(false);
mGraphics = nullptr;;
}
void Transmitter::connect(bool keepId) {
setupResampling();
std::string id;
if (mMSession != nullptr) {
id = mMSession->getId();
delete mMSession;
mMSession = nullptr;
}
mMSession = new MasterServerSession(mMasterServer->GetLabelString(), keepId ? id : "");
if (mMSession->getError()) {
mMasterId->SetLabelStr("Couldn't connect to the Masterserver!");
}
else {
mMasterId->SetLabelStr(mMSession->getOwnAddress());
}
}
void Transmitter::setupResampling() {
const double sr = GetSampleRate();
if (sr != 48000.0 && !mResamplingSetup) {
mResamplingBuffer = new sample * [mChannelCount];
for (int c = 0; c < mChannelCount; c++) {
mResamplingBuffer[c] = new sample[MAX_BUFFER_SIZE * 4];
}
int err;
mUp.init(mChannelCount, sr, 48000, 5, &err);
mDown.init(mChannelCount, 48000, sr, 5, &err);
mResamplingSetup = true;
}
}
#if IPLUG_DSP
void Transmitter::ProcessBlock(sample** inputs, sample** outputs, int nFrames) {
/**
* Process the block in smaller bits since it's too large
* Also abused to lower the delay a feedback node creates
*/
if (nFrames > mMaxBlockSize) {
const int overhang = nFrames % mMaxBlockSize;
int s = 0;
while (true) {
for (int c = 0; c < mChannelCount; c++) {
mSliceBuffer[0][c] = &inputs[c][s];
mSliceBuffer[1][c] = &outputs[c][s];
}
s += mMaxBlockSize;
if (s <= nFrames) {
ProcessBlock(mSliceBuffer[0], mSliceBuffer[1], mMaxBlockSize);
}
else {
if (overhang > 0) {
ProcessBlock(mSliceBuffer[0], mSliceBuffer[1], overhang);
}
return;
}
}
}
const int nChans = NOutChansConnected();
const sample volOwn = DBToAmp(GetParam(kVolume)->Value());
const sample volRemote = DBToAmp(GetParam(kVolumeRemote)->Value());
const sample bufferSize = GetParam(kBufferSize)->Value();
for (int i = 0; i < nFrames; i++) {
for (int c = 0; c < nChans; c++) {
outputs[c][i] = 0;
}
}
if (mMSession != nullptr && !GetRenderingOffline()) {
mMSession->setBufferSize(bufferSize);
if (mResamplingSetup) {
unsigned resampledFrames = 0;
for (int c = 0; c < mChannelCount; c++) {
unsigned int inl = nFrames;
unsigned int outl = MAX_BUFFER_SIZE * 4;
mUp.process(c, inputs[c], &inl, mResamplingBuffer[c], &outl);
resampledFrames = outl;
}
mMSession->ProcessBlock(const_cast<const sample**>(mResamplingBuffer), mResamplingBuffer, resampledFrames);
for (int c = 0; c < 2; c++) {
unsigned int inl = resampledFrames;
unsigned int outl = nFrames;
mDown.process(c, mResamplingBuffer[c], &inl, outputs[c], &outl);
}
} else {
mMSession->ProcessBlock(const_cast<const sample**>(inputs), outputs, nFrames);
}
}
/**
* Add the original audio input back and adjust the volumes
*/
for (int i = 0; i < nFrames; i++) {
for (int c = 0; c < nChans; c++) {
outputs[c][i] = outputs[c][i] * volRemote + inputs[c][i] * volOwn;
}
}
}
#endif