10 static int _srate(jack_nframes_t nframes
, void *);
11 static void _jack_shutdown(void *);
12 static int _process (jack_nframes_t nframes
, void *client
);
14 static void* _jackthread(void* arg
);
17 /******************************************************************************
18 *******************************************************************************
22 *******************************************************************************
23 *******************************************************************************/
25 class jackaudio
: public audio
{
27 jack_client_t
* fClient
;
28 int fNumInChans
; // number of input channels
29 int fNumOutChans
; // number of output channels
30 jack_port_t
* fInput_ports
[256]; // Jack input ports
31 jack_port_t
* fOutput_ports
[256]; // Jack output ports
32 float* fInChannel
[256]; // tables of noninterleaved input channels for FAUST
33 float* fOutChannel
[256]; // tables of noninterleaved output channels for FAUST
36 jackaudio() : fClient(0), fNumInChans(0), fNumOutChans(0) {}
37 virtual ~jackaudio() { stop(); }
39 virtual bool init(const char*name
, dsp
* DSP
) {
41 if ((fClient
= jack_client_open(name
, JackNullOption
, NULL
)) == 0) {
42 fprintf(stderr
, "jack server not running?\n");
46 jack_set_process_thread(fClient
, _jackthread
, this);
48 jack_set_process_callback(fClient
, _process
, this);
51 jack_set_sample_rate_callback(fClient
, _srate
, 0);
52 jack_on_shutdown(fClient
, _jack_shutdown
, 0);
54 fNumInChans
= fDsp
->getNumInputs();
55 fNumOutChans
= fDsp
->getNumOutputs();
57 for (int i
= 0; i
< fNumInChans
; i
++) {
59 snprintf(buf
, 256, "in_%d", i
);
60 fInput_ports
[i
] = jack_port_register(fClient
, buf
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
62 for (int i
= 0; i
< fNumOutChans
; i
++) {
64 snprintf(buf
, 256, "out_%d", i
);
65 fOutput_ports
[i
] = jack_port_register(fClient
, buf
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
67 fDsp
->init(jack_get_sample_rate(fClient
));
71 virtual bool start() {
72 if (jack_activate(fClient
)) {
73 fprintf(stderr
, "cannot activate client");
77 char** physicalInPorts
= (char **)jack_get_ports(fClient
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
);
78 char** physicalOutPorts
= (char **)jack_get_ports(fClient
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
);
79 if (physicalOutPorts
!= NULL
) {
80 for (int i
= 0; i
< fNumInChans
&& physicalOutPorts
[i
]; i
++)
81 jack_connect(fClient
, physicalOutPorts
[i
], jack_port_name(fInput_ports
[i
]));
83 if (physicalInPorts
!= NULL
) {
84 for (int i
= 0; i
< fNumOutChans
&& physicalInPorts
[i
]; i
++)
85 jack_connect(fClient
, jack_port_name(fOutput_ports
[i
]), physicalInPorts
[i
]);
92 jack_deactivate(fClient
);
93 for (int i
= 0; i
< fNumInChans
; i
++)
94 jack_port_unregister(fClient
, fInput_ports
[i
]);
95 for (int i
= 0; i
< fNumOutChans
; i
++)
96 jack_port_unregister(fClient
, fOutput_ports
[i
]);
97 jack_client_close(fClient
);
103 int process (jack_nframes_t nframes
) {
105 for (int i
= 0; i
< fNumInChans
; i
++)
106 fInChannel
[i
] = (float *)jack_port_get_buffer(fInput_ports
[i
], nframes
);
107 for (int i
= 0; i
< fNumOutChans
; i
++)
108 fOutChannel
[i
] = (float *)jack_port_get_buffer(fOutput_ports
[i
], nframes
);
109 fDsp
->compute(nframes
, fInChannel
, fOutChannel
);
114 void process_thread () {
115 jack_nframes_t nframes
;
117 nframes
= jack_cycle_wait(fClient
);
119 jack_cycle_signal(fClient
, 0);
125 //----------------------------------------------------------------------------
127 //----------------------------------------------------------------------------
128 static int _srate(jack_nframes_t nframes
, void *)
130 printf("the sample rate is now %u/sec\n", nframes
);
134 static void _jack_shutdown(void *)
139 static int _process(jack_nframes_t nframes
, void *client
)
141 jackaudio
* jackclient
= (jackaudio
*)client
;
142 return jackclient
->process (nframes
);
146 static void* _jackthread(void* arg
)
148 jackaudio
* jackclient
= (jackaudio
*)arg
;
149 jackclient
->process_thread();