-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffectbuffer.cpp
More file actions
311 lines (258 loc) · 8.57 KB
/
effectbuffer.cpp
File metadata and controls
311 lines (258 loc) · 8.57 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
#include "effectbuffer.h"
#include "audio.h"
#include "ports/inport.h"
#include "ports/outport.h"
#include "effectsLib/inputeffect.h"
#include "effectsLib/outputeffect.h"
#include "effect.h"
#include "effectmap.h"
#include <QDebug>
#include <QtMath>
#include <QAudioBuffer>
//#include <jack/jack.h>
//#undef signals
//#include <jack/control.h>
/*
EffectBuffer* effectbuffer; // for use by jack
*/
//TODO sort out conversions!!!
//TODO sort out multiple instances. Including switching streams etc.
EffectBuffer::EffectBuffer(Audio* parent) : QIODevice(parent)
{
buffer = QByteArray();
//effectbuffer = this;
//effectChain = QList<Effect*>();
//Use default input and output for now
}
/*
int jack_process(jack_nframes_t nframes, void *arg);
void jack_shutdown(void *arg);
int jack_runaudio(int argc, char *argv[]);
jack_client_t *client;
jack_port_t *inputportleft;
jack_port_t *inputportright;
jack_port_t *outputportleft;
jack_port_t *outputportright;
int maxLength;
char* b_in;
char* b_out;
bool start = true;
bool jackclientstarted = false;
int jack_process(jack_nframes_t nframes, void *arg)
{
if (start){
start = false;
}
jack_default_audio_sample_t *inL, *inR, *outL, *outR;
int readLength = sizeof(jack_default_audio_sample_t)*nframes; //sizeof(jack_default_audio_sample_t) * nframes
inL = static_cast<float*>(jack_port_get_buffer(inputportleft, nframes));
inR = static_cast<float*>(jack_port_get_buffer(inputportright, nframes));
outL = static_cast<float*>(jack_port_get_buffer(outputportleft, nframes));
outR = static_cast<float*>(jack_port_get_buffer(outputportright, nframes));
for (InputEffect* in_e : effectbuffer->getInputEffects()){
for (int i = 0; i < readLength; i ++){
memcpy(&b_in[8*i], &inL[i], 4);
memcpy(&b_in[8*i+4], &inR[i], 4);
}
in_e->giveData(b_in, readLength*2);
}
//memcpy(out,in,readLength*2);
for (OutputEffect* out_e : effectbuffer->getOutputEffects()){
effectbuffer->getEffectMap()->readLength = readLength*2;
b_out = effectbuffer->getEffectMap()->getData(out_e, out_e->getPorts().first());
//memcpy(out, writeData,readLength*2);
for (int i = 0; i < readLength; i ++){
memcpy(&outL[i], &b_out[8*i], 4);
memcpy(&outR[i], &b_out[8*i+4], 4);
}
}
//memcpy(outR, in, readLength*2);
//memcpy(outL, in, readLength*2);
return 0;
}
void jack_shutdown(void *arg){
qDebug() << "Jack shutdown";
jack_deactivate(client);
jack_client_close(client);
}
int jack_runaudio(int argc, char *argv[])
{
qDebug() << "Calling this twice??";
const char* clientname;
clientname = static_cast<const char*>("simple");
const char **ports;
const char* servername = NULL;
jack_options_t options = JackNullOption;
jack_status_t status;
//jackctl_server* server = jackctl_server_create(NULL, NULL);
client = jack_client_open(clientname, options, &status, servername);
if (client == NULL){
qDebug() << "Failed to start";
return 0;
}
if (status & JackServerStarted){
qDebug() << "Jack server started";
}
if (status & JackNameNotUnique){
clientname = jack_get_client_name(client);
qDebug() << "Unique name assigned";
}
jack_set_process_callback(client, jack_process, 0);
jack_on_shutdown(client, jack_shutdown, 0);
printf("Engine sample rate: %", PRIu32 "\n",
jack_get_sample_rate(client));
inputportleft = jack_port_register(client, "input left",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0);
inputportright = jack_port_register(client, "input right",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0);
outputportleft = jack_port_register(client, "output left",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
outputportright = jack_port_register(client, "output right",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
if ((inputportleft == NULL) || (inputportright == NULL) ||
(outputportleft == NULL) || (outputportright == NULL)){
fprintf(stderr, "no more JACK ports available\n");
return 0;
}
int bufferlength = 5000;
b_in = new char[bufferlength];
b_out = new char[bufferlength];
for (int i = 0; i < bufferlength; i++){
b_in[i] = 0;
b_out[i] = 0;
}
if (jack_activate(client)){
fprintf(stderr, "cannot activate client");
return 0;
}
ports = jack_get_ports(client, NULL, NULL,
JackPortIsPhysical | JackPortIsOutput);
if (ports == NULL){
fprintf(stderr, "no physical capture ports\n");
return 0;
} else {
qDebug() << "Output ports:";
for (int i = 0; i < sizeof(ports); i++){
qDebug() << ports[i];
}
}
if (jack_connect(client, ports[0], jack_port_name(inputportleft))){
fprintf(stderr, "cannot connect input ports\n");
}
if (jack_connect(client, ports[1], jack_port_name(inputportright))){
fprintf(stderr, "cannot connect input ports\n");
}
free(ports);
ports = jack_get_ports(client, NULL, NULL,
JackPortIsPhysical | JackPortIsInput);
if (ports == NULL){
fprintf(stderr, "no physical playback ports\n");
return 0;
} else {
qDebug() << "Input ports:";
for (int i = 0; i < sizeof(ports); i++){
qDebug() << ports[i];
}
}
if (jack_connect(client, jack_port_name(outputportleft), ports[0])){
fprintf(stderr, "cannot connect output ports\n");
}
if (jack_connect(client, jack_port_name(outputportright), ports[1])){
fprintf(stderr, "cannot connect output ports\n");
}
return 1;
}
*/
//EffectBuffer (QIODevice) is being asked to read data from whatever. Aka - write to "data" (param)
qint64 EffectBuffer::readData(char* data, qint64 maxlen){
Q_ASSERT(static_cast<int>(maxlen) == maxlen); //check that conversion isn't fucking things up
int readLength = qMin(static_cast<int>(maxlen), buffer.length());
//Supply data to inputEffects
for (InputEffect* in_e : inputEffects){
in_e->giveData(buffer.data(), readLength);
}
//Output data from outputEffects
for (OutputEffect* out_e : outputEffects){
// Set readLength for effectMap (temp // ?)
effectMap->readLength = readLength;
char* writeData = effectMap->getData(out_e, out_e->getPorts().first());
if (writeData){
memcpy(data, writeData, readLength);
} else {
qDebug() << "Empty data";
for (int i = 0; i < readLength; i++){
writeData[i] = 0;
}
}
}
//memcpy(data, buffer.data(), readLength);
buffer.remove(0, readLength);
return readLength;
}
qint64 EffectBuffer::writeData(const char* data, qint64 maxlen){
if(maxlen > (buffer.capacity() + buffer.size())){
// Increase buffer capacity to new maximum.
qint64 newCap = buffer.size() + maxlen;
Q_ASSERT(static_cast<int>(newCap) == newCap);
buffer.reserve(static_cast<int>(newCap));
}
Q_ASSERT(static_cast<int>(maxlen) == maxlen);
buffer.append(data, static_cast<int>(maxlen));
return maxlen;
}
void EffectBuffer::setEffectMap(EffectMap *em)
{
effectMap = em;
}
void EffectBuffer::addInputEffect(InputEffect *e)
{
inputEffects.append(e);
}
void EffectBuffer::addOutputEffect(OutputEffect *e)
{
outputEffects.append(e);
}
void EffectBuffer::deleteInputEffect(InputEffect *e)
{
if (inputEffects.contains(e)){
inputEffects.removeOne(e);
} else {
qDebug() << "Failed to remove nonexistent input effect from buffer";
}
}
void EffectBuffer::deleteOutputEffect(OutputEffect *e)
{
if (outputEffects.contains(e)){
outputEffects.removeOne(e);
} else {
qDebug() << "Failed to remove nonexistent output effect from buffer";
}
}
QList<InputEffect *> EffectBuffer::getInputEffects() const
{
return inputEffects;
}
QList<OutputEffect *> EffectBuffer::getOutputEffects() const
{
return outputEffects;
}
EffectMap *EffectBuffer::getEffectMap() const
{
return effectMap;
}
void EffectBuffer::stopJackAudio()
{
//jack_shutdown(NULL);
}
bool EffectBuffer::runJackAudio()
{
/*
if (jack_runaudio(NULL, NULL) == 0)
return false;
else*/
return true;
}