-
Notifications
You must be signed in to change notification settings - Fork 15
/
ANTN.cpp
231 lines (204 loc) · 5.69 KB
/
ANTN.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
#include "Bidoo.hpp"
#include "dsp/digital.hpp"
#include "BidooComponents.hpp"
#include <curl/curl.h>
#include <thread>
#include <pthread.h>
#include <mpg123.h>
#include "dsp/ringbuffer.hpp"
#include "dsp/frame.hpp"
using namespace std;
struct threadData {
mpg123_handle *mh;
DoubleRingBuffer<Frame<2>,262144> *dataRingBuffer;
string url;
int bytes;
int channels;
int encoding;
long rate;
bool play = true;
bool free = true;
};
size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
struct threadData *pData = (struct threadData *) userp;
size_t realsize = size * nmemb;
off_t frame_offset;
size_t done;
size_t i;
int err;
unsigned char *audio;
mpg123_feed(pData->mh, (const unsigned char*) contents, realsize);
do {
err = mpg123_decode_frame(pData->mh, &frame_offset, &audio, &done);
switch(err) {
case MPG123_NEW_FORMAT:
mpg123_getformat(pData->mh, &pData->rate, &pData->channels, &pData->encoding);
pData->bytes = mpg123_encsize(pData->encoding);
break;
case MPG123_OK:
i = 0;
do {
Frame<2> newFrame;
unsigned char l[] = {audio[i+0], audio[i+1], audio[i+2], audio[i+3]};
memcpy(&newFrame.samples[0], &l, sizeof(newFrame.samples[0]));
unsigned char r[] = {audio[i+4], audio[i+5], audio[i+6], audio[i+7]};
memcpy(&newFrame.samples[1], &r, sizeof(newFrame.samples[1]));
pData->dataRingBuffer->push(newFrame);
i += 8;
}
while(i < done);
break;
case MPG123_NEED_MORE:
break;
default:
break;
}
} while(done > 0);
return pData->play ? realsize : 0;
}
void *threadTask(void *data)
{
struct threadData *pData;
pData = (struct threadData *) data;
pData->free = false;
CURL *curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_URL, pData->url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
pData->free = true;
pthread_exit(NULL);
}
struct ANTN : Module {
enum ParamIds {
URL_PARAM,
TRIG_PARAM,
NUM_PARAMS
};
enum InputIds {
NUM_INPUTS
};
enum OutputIds {
OUTL_OUTPUT,
OUTR_OUTPUT,
NUM_OUTPUTS
};
enum LightIds {
NUM_LIGHTS
};
string url;
SchmittTrigger trigTrigger;
size_t index = 0;
bool read = false;
DoubleRingBuffer<Frame<2>,262144> dataRingBuffer;
mpg123_handle *mh = NULL;
int bytes = 0;
int channels = 0;
int encoding = 0;
long rate;
pthread_t *rThread;
threadData tData;
pthread_mutex_t mut;
bool first = true;
ANTN() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {
mpg123_init();
mh = mpg123_new(NULL, NULL);
mpg123_format_none(mh);
mpg123_format(mh, engineGetSampleRate(), 2, MPG123_ENC_FLOAT_32);
mpg123_open_feed(mh);
tData.mh = mh;
tData.dataRingBuffer = &dataRingBuffer;
}
~ANTN() {
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
}
json_t *toJson() override {
json_t *rootJ = json_object();
json_object_set_new(rootJ, "url", json_string(url.c_str()));
return rootJ;
}
void fromJson(json_t *rootJ) override {
json_t *urlJ = json_object_get(rootJ, "url");
if (urlJ)
url = json_string_value(urlJ);
}
void step() override;
};
void ANTN::step() {
if (trigTrigger.process(params[TRIG_PARAM].value)) {
if (!tData.free) {
tData.play = false;
}
else {
tData.url = url;
tData.play = true;
pthread_create(rThread, NULL, threadTask, (void *)&tData);
}
}
if (dataRingBuffer.size()>200000) {
read = true;
}
if (read) {
Frame<2> currentFrame = *dataRingBuffer.startData();
outputs[OUTL_OUTPUT].value = 10*currentFrame.samples[0];
outputs[OUTR_OUTPUT].value = 10*currentFrame.samples[1];
dataRingBuffer.startIncr(1);
}
}
struct ANTNTextField : TextField {
ANTNTextField(ANTN *mod) {
module = mod;
}
void onTextChange() override;
ANTN *module;
};
void ANTNTextField::onTextChange() {
if (text.size() > 0) {
module->url = text;
}
}
ANTNWidget::ANTNWidget() {
ANTN *module = new ANTN();
setModule(module);
box.size = Vec(15*9, 380);
{
SVGPanel *panel = new SVGPanel();
panel->box.size = box.size;
panel->setBackground(SVG::load(assetPlugin(plugin, "res/ANTN.svg")));
addChild(panel);
}
textField = new ANTNTextField(module);
textField->box.pos = Vec(5, 25);
textField->box.size = Vec(125, 100);
textField->multiline = true;
addChild(textField);
addChild(createScrew<ScrewSilver>(Vec(15, 0)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
addChild(createScrew<ScrewSilver>(Vec(15, 365)));
addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));
addParam(createParam<BlueCKD6>(Vec(29, 328), module, ANTN::TRIG_PARAM, 0.0f, 1.0f, 0.0f));
static const float portX0[4] = {34, 67, 101};
addOutput(createOutput<TinyPJ301MPort>(Vec(portX0[2]-13, 334), module, ANTN::OUTL_OUTPUT));
addOutput(createOutput<TinyPJ301MPort>(Vec(portX0[2]+8, 334), module, ANTN::OUTR_OUTPUT));
}
json_t *ANTNWidget::toJson() {
json_t *rootJ = ModuleWidget::toJson();
// text
json_object_set_new(rootJ, "text", json_string(textField->text.c_str()));
return rootJ;
}
void ANTNWidget::fromJson(json_t *rootJ) {
ModuleWidget::fromJson(rootJ);
// text
json_t *textJ = json_object_get(rootJ, "text");
if (textJ)
textField->text = json_string_value(textJ);
}