CSL  6.0
JackIO.cpp
Go to the documentation of this file.
1 //
2 // JackIO.cpp -- DAC IO using JACK
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "JackIO.h"
7 
8 using namespace csl;
9 
10 // Callback function
11 
12 int JackCallback (jack_nframes_t nframes, void *arg) {
13  JackIO * jack = (JackIO * ) arg;
14  jack_default_audio_sample_t * in, * out;
15  IO_Status tStatus = jack->mStatus;
16 
17  jack_transport_state_t ts = jack_transport_query(jack->mClient, NULL);
18  if (ts == JackTransportRolling) {
19  in = (jack_default_audio_sample_t *) jack_port_get_buffer (jack->mInput_port, nframes);
20  out = (jack_default_audio_sample_t *) jack_port_get_buffer (jack->mOutput_port, nframes);
21 // // copy in to out
22 // memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
23  // setup CSL buffer for I/O
24  Buffer * outBuffer = & (jack->mOutputBuffer);
25  unsigned numOutChannels = outBuffer->mNumChannels;
26  outBuffer->mNumFrames = nframes;
27  jack->pullInput(* outBuffer, out, true);
28  } else if (ts == JackTransportStopped) {
29  if (tStatus == kIORunning)
30  tStatus = kIOExit;
31  }
32  return 0;
33 }
34 
35 void jack_shutdown (void *arg) {
36  logMsg ("Jack shutdown");
37 }
38 
39 // JackIO Class
40 
42 
43 }
44 
45 JackIO::JackIO(unsigned s_rate, unsigned b_size,
46  int in_device, int out_device,
47  unsigned in_chans, unsigned out_chans)
48  : IO(s_rate, b_size, in_device, out_device, in_chans, out_chans) {
49 
50 }
51 
53 
54 }
55 
56 // kIONew,
57 // kIOInit,
58 // kIOOpen,
59 // kIORunning,
60 // kIOClosed,
61 // kIOExit
62 
63 
64 ///< open/close start/stop methods
65 
66 void JackIO::open() throw(CException) {
67  jack_options_t options = JackNullOption;
68  jack_status_t status;
69  const char ** ports;
70  // this set-up code is taken from the Jack simple_client example
71  char * server_name = "CSL_SERVER";
72  char * client_name = "CSL_CLIENT";
73  options |= (jack_options_t) JackServerName;
74 
75  mClient = jack_client_open (client_name, options, &status, server_name);
76  if (mClient == NULL) {
77  logMsg (kLogError, "jack_client_open() failed, " "status = 0x%2.0x", status);
78  if (status & JackServerFailed) {
79  logMsg (kLogError, "Unable to connect to JACK server");
80  }
81  }
82  if (status & JackServerStarted) {
83  logMsg("\tJACK server started");
84  }
85  if (status & JackNameNotUnique) {
86  client_name = jack_get_client_name(mClient);
87  logMsg (kLogError, "unique name `%s' assigned", client_name);
88  }
89 
90  jack_set_process_callback (mClient, JackCallback, 0);
91  jack_on_shutdown (mClient, jack_shutdown, 0);
92 
93  logMsg ("engine sample rate: %g", jack_get_sample_rate (mClient));
94  mInput_port = jack_port_register (mClient, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
95  mOutput_port = jack_port_register (mClient, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
96  if ((mInput_port == NULL) || (mOutput_port == NULL)) {
97  logMsg (kLogError, "no more JACK ports available");
98  exit (1);
99  }
100  if (jack_activate (mClient)) {
101  logMsg (kLogError, "cannot activate client");
102  exit (1);
103  }
104  ports = jack_get_ports (mClient, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
105  if (ports == NULL) {
106  logMsg (kLogError, "no physical capture ports");
107  exit (1);
108  }
109  if (jack_connect (mClient, ports[0], jack_port_name (mInput_port))) {
110  logMsg (kLogError, "cannot connect input ports");
111  }
112  free (ports);
113  ports = jack_get_ports (mClient, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
114  if (ports == NULL) {
115  logMsg (kLogError, "no physical playback ports");
116  exit (1);
117  }
118  if (jack_connect (mClient, jack_port_name (mOutput_port), ports[0])) {
119  logMsg (kLogError, "cannot connect output ports");
120  }
121  free (ports);
122 }
123 
124 void JackIO::close() throw(CException) {
125  jack_client_close (mClient);
126 };
127 
128 void JackIO::start() throw(CException) { ///< start my timer thread
129 }
130 
131 void JackIO::stop() throw(CException) { ///< stop the timer thread
132 }
133 
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
unsigned mNumFrames
num frames used in each buffer
Definition: CSL_Core.h:113
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
IO_Status
IO Status flag.
Definition: CSL_Core.h:720
jack_port_t * mInput_port
Definition: JackIO.h:35
void pullInput(Buffer &outBuffer, SampleBuffer out=0)
get a buffer from the CSL graph
Definition: CSL_Core.cpp:1425
JackIO()
Constructor.
Definition: JackIO.cpp:41
void close()
open/close start/stop methods
Definition: JackIO.cpp:124
void jack_shutdown(void *arg)
Definition: JackIO.cpp:35
JackIO is an IO that uses a thread and a timer to call its graph's nextBuffer(); it doesn't do anythi...
Definition: JackIO.h:22
jack_port_t * mOutput_port
Definition: JackIO.h:36
unsigned mNumChannels
num channels in buffer (num mono buffers)
Definition: CSL_Core.h:112
IO – the abstract I/O scheduling class; subclasses interface to specific I/O APIs.
Definition: CSL_Core.h:752
virtual ~JackIO()
Definition: JackIO.cpp:52
void stop()
stop the timer thread
Definition: JackIO.cpp:131
void start()
start my timer thread
Definition: JackIO.cpp:128
IO_Status mStatus
status flag
Definition: CSL_Core.h:791
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
Buffer mOutputBuffer
the output buffer I use (passed to nextBuffer calls)
Definition: CSL_Core.h:779
jack_client_t * mClient
Definition: JackIO.h:37
int JackCallback(jack_nframes_t nframes, void *arg)
Definition: JackIO.cpp:12
Base class of CSL exceptions (written upper-case). Has a string message.
void open()
open/close start/stop methods
Definition: JackIO.cpp:66