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 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <errno.h>
#include <time.h>
#include <vector>
#include <stack>
#include <string>
#include <map>
#include <iostream>
#include <libgen.h>
#include <jack/jack.h>
// g++ -O3 -lm -lsndfile myfx.cpp
using namespace std;
#define max(x,y) (((x)>(y)) ? (x) : (y))
#define min(x,y) (((x)<(y)) ? (x) : (y))
// abs is now predefined
//template<typename T> T abs (T a) { return (a<T(0)) ? -a : a; }
inline int lsr (int x, int n) { return int(((unsigned int)x) >> n); }
/******************************************************************************
*******************************************************************************
VECTOR INTRINSICS
*******************************************************************************
*******************************************************************************/
//inline void *aligned_calloc(size_t nmemb, size_t size) { return (void*)((unsigned)(calloc((nmemb*size)+15,sizeof(char)))+15 & 0xfffffff0); }
inline void *aligned_calloc(size_t nmemb, size_t size) { return (void*)((size_t)(calloc((nmemb*size)+15,sizeof(char)))+15 & ~15); }
<<includeIntrinsic>>
/******************************************************************************
*******************************************************************************
USER INTERFACE
*******************************************************************************
*******************************************************************************/
class UI
{
bool fStopped;
public:
UI() : fStopped(false) {}
virtual ~UI() {}
// -- active widgets
virtual void addButton(char* label, float* zone) = 0;
virtual void addToggleButton(char* label, float* zone) = 0;
virtual void addCheckButton(char* label, float* zone) = 0;
virtual void addVerticalSlider(char* label, float* zone, float init, float min, float max, float step) = 0;
virtual void addHorizontalSlider(char* label, float* zone, float init, float min, float max, float step) = 0;
virtual void addNumEntry(char* label, float* zone, float init, float min, float max, float step) = 0;
// -- passive widgets
virtual void addNumDisplay(char* label, float* zone, int precision) = 0;
virtual void addTextDisplay(char* label, float* zone, char* names[], float min, float max) = 0;
virtual void addHorizontalBargraph(char* label, float* zone, float min, float max) = 0;
virtual void addVerticalBargraph(char* label, float* zone, float min, float max) = 0;
// -- frames and labels
virtual void openFrameBox(char* label) = 0;
virtual void openTabBox(char* label) = 0;
virtual void openHorizontalBox(char* label) = 0;
virtual void openVerticalBox(char* label) = 0;
virtual void closeBox() = 0;
virtual void show() = 0;
virtual void run() = 0;
void stop() { fStopped = true; }
bool stopped() { return fStopped; }
};
struct param {
float* fZone; float fMin; float fMax;
param(float* z, float a, float b) : fZone(z), fMin(a), fMax(b) {}
};
class CMDUI : public UI
{
int fArgc;
char** fArgv;
stack<string> fPrefix;
map<string, param> fKeyParam;
void addOption(char* label, float* zone, float min, float max)
{
string fullname = fPrefix.top() + label;
fKeyParam.insert(make_pair(fullname, param(zone, min, max)));
}
void openAnyBox(char* label)
{
string prefix;
if (label && label[0]) {
prefix = fPrefix.top() + "-" + label;
} else {
prefix = fPrefix.top();
}
fPrefix.push(prefix);
}
public:
CMDUI(int argc, char *argv[]) : UI(), fArgc(argc), fArgv(argv) { fPrefix.push("--"); }
virtual ~CMDUI() {}
virtual void addButton(char* label, float* zone) {};
virtual void addToggleButton(char* label, float* zone) {};
virtual void addCheckButton(char* label, float* zone) {};
virtual void addVerticalSlider(char* label, float* zone, float init, float min, float max, float step)
{
addOption(label,zone,min,max);
}
virtual void addHorizontalSlider(char* label, float* zone, float init, float min, float max, float step)
{
addOption(label,zone,min,max);
}
virtual void addNumEntry(char* label, float* zone, float init, float min, float max, float step)
{
addOption(label,zone,min,max);
}
// -- passive widgets
virtual void addNumDisplay(char* label, float* zone, int precision) {}
virtual void addTextDisplay(char* label, float* zone, char* names[], float min, float max) {}
virtual void addHorizontalBargraph(char* label, float* zone, float min, float max) {}
virtual void addVerticalBargraph(char* label, float* zone, float min, float max) {}
virtual void openFrameBox(char* label) { openAnyBox(label); }
virtual void openTabBox(char* label) { openAnyBox(label); }
virtual void openHorizontalBox(char* label) { openAnyBox(label); }
virtual void openVerticalBox(char* label) { openAnyBox(label); }
virtual void closeBox() { fPrefix.pop(); }
virtual void show() {}
virtual void run()
{
char c;
printf("Type 'q' to quit\n");
while ((c = getchar()) != 'q') {
sleep(1);
}
}
void print()
{
map<string, param>::iterator i;
cout << fArgc << "\n";
cout << fArgv[0] << " option list : ";
for (i = fKeyParam.begin(); i != fKeyParam.end(); i++) {
cout << "[ " << i->first << " " << i->second.fMin << ".." << i->second.fMax <<" ] ";
}
}
void process_command()
{
map<string, param>::iterator p;
for (int i = 1; i < fArgc; i++) {
if (fArgv[i][0] == '-') {
p = fKeyParam.find(fArgv[i]);
if (p == fKeyParam.end()) {
cout << fArgv[0] << " : unrecognized option " << fArgv[i] << "\n";
print();
exit(1);
}
char* end;
*(p->second.fZone) = float(strtod(fArgv[i+1], &end));
i++;
}
}
}
void process_init()
{
map<string, param>::iterator p;
for (int i = 1; i < fArgc; i++) {
if (fArgv[i][0] == '-') {
p = fKeyParam.find(fArgv[i]);
if (p == fKeyParam.end()) {
cout << fArgv[0] << " : unrecognized option " << fArgv[i] << "\n";
exit(1);
}
char* end;
*(p->second.fZone) = float(strtod(fArgv[i+1], &end));
i++;
}
}
}
};
//----------------------------------------------------------------
// d�inition du processeur de signal
//----------------------------------------------------------------
class dsp {
protected:
int fSamplingFreq;
public:
dsp() {}
virtual ~dsp() {}
virtual int getNumInputs() = 0;
virtual int getNumOutputs() = 0;
virtual void buildUserInterface(UI* interface) = 0;
virtual void init(int samplingRate) = 0;
virtual void compute(int len, float** inputs, float** outputs) = 0;
virtual void conclude() {}
};
//----------------------------------------------------------------------------
// FAUST generated code
//----------------------------------------------------------------------------
<<includeclass>>
mydsp DSP;
/******************************************************************************
*******************************************************************************
JACK AUDIO INTERFACE
*******************************************************************************
*******************************************************************************/
//----------------------------------------------------------------------------
// number of input and output channels
//----------------------------------------------------------------------------
int gNumInChans;
int gNumOutChans;
//----------------------------------------------------------------------------
// Jack ports
//----------------------------------------------------------------------------
jack_port_t *input_ports[256];
jack_port_t *output_ports[256];
//----------------------------------------------------------------------------
// tables of noninterleaved input and output channels for FAUST
//----------------------------------------------------------------------------
float* gInChannel[256];
float* gOutChannel[256];
//----------------------------------------------------------------------------
// Jack Callbacks
//----------------------------------------------------------------------------
int srate(jack_nframes_t nframes, void *arg)
{
printf("the sample rate is now %u/sec\n", nframes);
return 0;
}
void jack_shutdown(void *arg)
{
exit(1);
}
int process (jack_nframes_t nframes, void *arg)
{
for (int i = 0; i < gNumInChans; i++) {
gInChannel[i] = (float *)jack_port_get_buffer(input_ports[i], nframes);
}
for (int i = 0; i < gNumOutChans; i++) {
gOutChannel[i] = (float *)jack_port_get_buffer(output_ports[i], nframes);
}
DSP.compute(nframes, gInChannel, gOutChannel);
return 0;
}
//-------------------------------------------------------------------------
// MAIN
//-------------------------------------------------------------------------
int main(int argc, char *argv[] )
{
char jackname[256];
char** physicalInPorts;
char** physicalOutPorts;
jack_client_t* client;
CMDUI* interface = new CMDUI(argc, argv);
DSP.buildUserInterface(interface);
snprintf(jackname, 255, "%s", basename(argv[0]));
if ((client = jack_client_new(jackname)) == 0) {
fprintf(stderr, "jack server not running?\n");
return 1;
}
jack_set_process_callback(client, process, 0);
jack_set_sample_rate_callback(client, srate, 0);
jack_on_shutdown(client, jack_shutdown, 0);
gNumInChans = DSP.getNumInputs();
gNumOutChans = DSP.getNumOutputs();
for (int i = 0; i < gNumInChans; i++) {
char buf[256];
snprintf(buf, 256, "in_%d", i);
input_ports[i] = jack_port_register(client, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
}
for (int i = 0; i < gNumOutChans; i++) {
char buf[256];
snprintf(buf, 256, "out_%d", i);
output_ports[i] = jack_port_register(client, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
}
DSP.init(jack_get_sample_rate(client));
DSP.buildUserInterface(interface);
interface->process_command();
physicalInPorts = (char **)jack_get_ports(client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
physicalOutPorts = (char **)jack_get_ports(client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
if (jack_activate(client)) {
fprintf(stderr, "cannot activate client");
return 1;
}
if (physicalOutPorts != NULL) {
for (int i = 0; i < gNumInChans && physicalOutPorts[i]; i++) {
jack_connect(client, physicalOutPorts[i], jack_port_name(input_ports[i]));
}
}
if (physicalInPorts != NULL) {
for (int i = 0; i < gNumOutChans && physicalInPorts[i]; i++) {
jack_connect(client, jack_port_name(output_ports[i]), physicalInPorts[i]);
}
}
interface->run();
jack_deactivate(client);
for (int i = 0; i < gNumInChans; i++) {
jack_port_unregister(client, input_ports[i]);
}
for (int i = 0; i < gNumOutChans; i++) {
jack_port_unregister(client, output_ports[i]);
}
jack_client_close(client);
return 0;
}
|