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
|
#ifndef __jack_dsp__
#define __jack_dsp__
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <jack/jack.h>
#include "faust/audio/audio.h"
#include "faust/audio/dsp.h"
static int _srate(jack_nframes_t nframes, void *);
static void _jack_shutdown(void *);
static int _process (jack_nframes_t nframes, void *client);
#ifdef _OPENMP
static void* _jackthread(void* arg);
#endif
/******************************************************************************
*******************************************************************************
JACK AUDIO INTERFACE
*******************************************************************************
*******************************************************************************/
class jackaudio : public audio {
dsp* fDsp;
jack_client_t* fClient;
int fNumInChans; // number of input channels
int fNumOutChans; // number of output channels
jack_port_t * fInput_ports[256]; // Jack input ports
jack_port_t * fOutput_ports[256]; // Jack output ports
float* fInChannel[256]; // tables of noninterleaved input channels for FAUST
float* fOutChannel[256]; // tables of noninterleaved output channels for FAUST
public:
jackaudio() : fClient(0), fNumInChans(0), fNumOutChans(0) {}
virtual ~jackaudio() { stop(); }
virtual bool init(const char*name, dsp* DSP) {
fDsp = DSP;
if ((fClient = jack_client_open(name, JackNullOption, NULL)) == 0) {
fprintf(stderr, "jack server not running?\n");
return false;
}
#ifdef _OPENMP
jack_set_process_thread(fClient, _jackthread, this);
#else
jack_set_process_callback(fClient, _process, this);
#endif
jack_set_sample_rate_callback(fClient, _srate, 0);
jack_on_shutdown(fClient, _jack_shutdown, 0);
fNumInChans = fDsp->getNumInputs();
fNumOutChans = fDsp->getNumOutputs();
for (int i = 0; i < fNumInChans; i++) {
char buf[256];
snprintf(buf, 256, "in_%d", i);
fInput_ports[i] = jack_port_register(fClient, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
}
for (int i = 0; i < fNumOutChans; i++) {
char buf[256];
snprintf(buf, 256, "out_%d", i);
fOutput_ports[i] = jack_port_register(fClient, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
}
fDsp->init(jack_get_sample_rate(fClient));
return true;
}
virtual bool start(bool autoconnect) {
if (jack_activate(fClient)) {
fprintf(stderr, "cannot activate client");
return false;
}
if(autoconnect){
char** physicalInPorts = (char **)jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
char** physicalOutPorts = (char **)jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
if (physicalOutPorts != NULL) {
for (int i = 0; i < fNumInChans && physicalOutPorts[i]; i++)
jack_connect(fClient, physicalOutPorts[i], jack_port_name(fInput_ports[i]));
}
if (physicalInPorts != NULL) {
for (int i = 0; i < fNumOutChans && physicalInPorts[i]; i++)
jack_connect(fClient, jack_port_name(fOutput_ports[i]), physicalInPorts[i]);
}
}
return true;
}
virtual bool start(){
return start(true);
}
virtual void stop() {
if (fClient) {
jack_deactivate(fClient);
for (int i = 0; i < fNumInChans; i++)
jack_port_unregister(fClient, fInput_ports[i]);
for (int i = 0; i < fNumOutChans; i++)
jack_port_unregister(fClient, fOutput_ports[i]);
jack_client_close(fClient);
fClient = 0;
}
}
// jack callbacks
int process (jack_nframes_t nframes) {
AVOIDDENORMALS;
for (int i = 0; i < fNumInChans; i++)
fInChannel[i] = (float *)jack_port_get_buffer(fInput_ports[i], nframes);
for (int i = 0; i < fNumOutChans; i++)
fOutChannel[i] = (float *)jack_port_get_buffer(fOutput_ports[i], nframes);
fDsp->compute(nframes, fInChannel, fOutChannel);
return 0;
}
#ifdef _OPENMP
void process_thread () {
jack_nframes_t nframes;
while (1) {
nframes = jack_cycle_wait(fClient);
process (nframes);
jack_cycle_signal(fClient, 0);
}
}
#endif
};
//----------------------------------------------------------------------------
// Jack Callbacks
//----------------------------------------------------------------------------
static int _srate(jack_nframes_t nframes, void *)
{
printf("the sample rate is now %u/sec\n", nframes);
return 0;
}
static void _jack_shutdown(void *)
{
exit(1);
}
static int _process(jack_nframes_t nframes, void *client)
{
jackaudio* jackclient = (jackaudio*)client;
return jackclient->process (nframes);
}
#ifdef _OPENMP
static void* _jackthread(void* arg)
{
jackaudio* jackclient = (jackaudio*)arg;
jackclient->process_thread();
return 0;
}
#endif
#endif
|