JackClient.cpp 8.8 KB
Newer Older
jcaceres's avatar
jcaceres committed
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
/*
  JackTrip: A Multimachine System for High-Quality Audio 
  Network Performance over the Internet

  Copyright (c) 2008 Chris Chafe, Juan-Pablo Caceres,
  SoundWIRE group at CCRMA.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/

/*
 * JackClient.cpp
 */
jcaceres's avatar
jcaceres committed
33
34

#include "JackClient.h"
jcaceres's avatar
jcaceres committed
35
36
37
#include <cmath>
#include <cstring>
#include "AudioDevice.h"
38
39
40
41
//#include "qstring.h"
#include <QString>
//#include <q3textstream.h>
#include <QTextStream>
jcaceres's avatar
jcaceres committed
42

jcaceres's avatar
jcaceres committed
43
44
#define ONE_BASED (i+1)

jcaceres's avatar
jcaceres committed
45
46
using namespace std;

jcaceres's avatar
jcaceres committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
jack_port_t **input_port;
jack_port_t **output_port;
jack_default_audio_sample_t **inBufPtr;
jack_default_audio_sample_t **outBufPtr;
double *inSndFrame;
double *outSndFrame;
signed short *inBuf;
signed short *outBuf;
double *iPhase;

int xx_nChans_xx;
int xx_nFrames_xx;
static AudioDevice *extern_ptr_to_audioDevice;

61
/*
jcaceres's avatar
jcaceres committed
62
63
64
65
  static void
  error ()
  {
  };
66
67
*/

jcaceres's avatar
jcaceres committed
68
69
70
static void
error (const char *desc)
{
jcaceres's avatar
jcaceres committed
71
  printf ("JACK error: %s\n", desc);
jcaceres's avatar
jcaceres committed
72
73
74
}

static void
75
jack_shutdown (void*)// *arg)
jcaceres's avatar
jcaceres committed
76
{
jcaceres's avatar
jcaceres committed
77
  exit (1);
jcaceres's avatar
jcaceres committed
78
79
}

80
static int srate_callback (jack_nframes_t nframes, void*) //arg)
jcaceres's avatar
jcaceres committed
81
{
jcaceres's avatar
jcaceres committed
82
83
84
  printf ("the sample rate is now %d/sec\n", (int) nframes);
  cout << "bogus -- fixme --  nothing has been changed" << endl;
  return 0;
jcaceres's avatar
jcaceres committed
85
86
87
}

static int
88
xx_global_process_xx (unsigned int nframes, void*) //argc)
jcaceres's avatar
jcaceres committed
89
90
{

jcaceres's avatar
jcaceres committed
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
  if ((int) nframes != xx_nFrames_xx)
    cout << "xx_global_process_xx [" << ((int) nframes) <<
      " frames" << endl;
  /* "in" is in from jack, "out" is out to jack */
  //      extern_ptr_to_audioDevice->bufferPtrs (inBuf, outBuf);
  for (int i = 0; i < xx_nChans_xx; i++)
    {
      inBufPtr[i] = (jack_default_audio_sample_t *)
	jack_port_get_buffer (input_port[ONE_BASED], nframes);
      outBufPtr[i] = (jack_default_audio_sample_t *)
	jack_port_get_buffer (output_port[ONE_BASED], nframes);
    }
  double d;
  signed short s;

  extern_ptr_to_audioDevice->outThread->xfrTo (outBuf);	// to jack

  for (unsigned int j = 0; j < nframes; j++)
    {
      jack_default_audio_sample_t *inFrame;
      jack_default_audio_sample_t *outFrame;
      int f = j * xx_nChans_xx;
      for (int i = 0; i < xx_nChans_xx; i++)
	{
	  inFrame = inBufPtr[i];
	  s = (signed short) floor (inFrame[j] * 32768.0);
	  inBuf[f + i] = s;
	}
      // oscillator test on input
      /*		
jcaceres's avatar
jcaceres committed
121
122
	for (int i = 0; i < xx_nChans_xx; i++)
	{
jcaceres's avatar
jcaceres committed
123
124
125
	d = sin (iPhase[i]);
	s = (signed short) floor (d * 32768.0 * 0.1);
	inBuf[f + i] = s;
jcaceres's avatar
jcaceres committed
126
	}
jcaceres's avatar
jcaceres committed
127
128
129
130
	iPhase[0] += 0.15; // cool diff tones!
	iPhase[1] += 0.17;
      */
      for (int i = 0; i < xx_nChans_xx; i++)
jcaceres's avatar
jcaceres committed
131
	{
jcaceres's avatar
jcaceres committed
132
133
134
	  outFrame = outBufPtr[i];
	  d = (double) outBuf[f + i] / 32768.0;
	  outFrame[j] = d;
jcaceres's avatar
jcaceres committed
135
	}
jcaceres's avatar
jcaceres committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
      // oscillator test on output
      /*
	for (int i = 0; i < xx_nChans_xx; i++)
	{
	outFrame = outBufPtr[i];
	d = sin (iPhase[i]);
	outFrame[j] = d * 0.1;
	}
	iPhase[0] += 0.15;
	iPhase[1] += 0.17;
      */
    }

  extern_ptr_to_audioDevice->inThread->xfrFrom (inBuf);	// from jack       
  // cout << "xx_global_process_xx 1024" << endl;
  //      extern_ptr_to_audioDevice->jtick (); // signal transfer completed
  return 0;
jcaceres's avatar
jcaceres committed
153
154
}

155
156
//JackClient::JackClient (QString name, int nChans, int nFrames, bool output, bool input, AudioDevice * ad, int aro)//:Stk () //Romved STK Dependency
JackClient::JackClient (int nChans, int nFrames, bool output, bool input, AudioDevice * ad, int aro)
jcaceres's avatar
jcaceres committed
157
{
jcaceres's avatar
jcaceres committed
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
  alsa_readable_offset = aro;
  audioDevice = ad;
  extern_ptr_to_audioDevice = ad;
  xx_nFrames_xx = this->nFrames = nFrames;
  xx_nChans_xx = this->nChans = nChans;
  inBuf = new signed short[nChans * nFrames];
  outBuf = new signed short[nChans * nFrames];
  input_port = new jack_port_t *[nChans+1]; // ONE_BASED
  output_port = new jack_port_t *[nChans+1]; // ONE_BASED
  inBufPtr = new jack_default_audio_sample_t *[nChans];
  outBufPtr = new jack_default_audio_sample_t *[nChans];
  //              printf ("\nbuffer bytes = %d\n", nChans * nFrames * sizeof(signed short));

  iPhase = new double[nChans];	// sample frame of output channels
  for (int i = 0; i < (nChans); i++)
    iPhase[i] = 0.0;

  /* tell the JACK server to call error() whenever it
   * experiences an error.  Notice that this callback is
   * global to this process, not specific to each client.
   * 
   * This is set here so that it can catch errors in the
   * connection process
   */
  jack_set_error_function (error);

  /* try to become a client of the JACK server */
  // code is from freqtweak
  char
    namebuf[100];
  /* try to become a client of the JACK server */
  // find a name predictably
  for (int i = 1; i < 10; i++)
    {
      snprintf (namebuf, sizeof (namebuf) - 1, "jacktrip_%d", i);
      if ((client = jack_client_new (namebuf)) != 0)
jcaceres's avatar
jcaceres committed
194
	{
jcaceres's avatar
jcaceres committed
195
	  break;
jcaceres's avatar
jcaceres committed
196
	}
jcaceres's avatar
jcaceres committed
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
    }
  if (!client) {
    printf ("Error: JACK server not running?\n");
    exit(1);
  }
  else
    cout << namebuf << " registered with jack" << endl;
  //      if ((client = jack_client_new (name)) == 0)
  //      {
  //              printf ("jack server not running?\n");
  //              return 1;
  //      }

  /* tell the JACK server to call `process()' whenever
   * there is work to be done.
   */

  jack_set_process_callback (client, xx_global_process_xx, 0);

  /* tell the JACK server to call `srate()' whenever
   * the sample rate of the system changes.
   */


  jack_set_sample_rate_callback (client, srate_callback, 0);

  /* tell the JACK server to call `jack_shutdown()' if
   * it ever shuts down, either entirely, or if it
   * just decides to stop calling us.
   */

  jack_on_shutdown (client, jack_shutdown, 0);

  /* display the current sample rate. once the client is activated 
   * (see below), you should rely on your own sample rate
   * callback (see above) for this value.
   */

  printf ("engine sample rate: %d\n",
	  (int) jack_get_sample_rate (client));

  /* create two ports */
  for (int i = 0; i < nChans; i++)
    {
      QString iName, oName;
242
243
244
245
      //QTextOStream (&iName) << "input" << ONE_BASED;
      //QTextOStream (&oName) << "output" << ONE_BASED;
      QTextStream (&iName) << "input" << ONE_BASED;//QT4 Port****
      QTextStream (&oName) << "output" << ONE_BASED;//QT4 Port****
jcaceres's avatar
jcaceres committed
246
247
248

      if (output)
	output_port[ONE_BASED] =
249
	  jack_port_register (client, oName.toLatin1(),
jcaceres's avatar
jcaceres committed
250
251
252
253
			      JACK_DEFAULT_AUDIO_TYPE,
			      JackPortIsOutput, 0);
      if (input)
	input_port[ONE_BASED] =
254
	  jack_port_register (client, iName.toLatin1(),
jcaceres's avatar
jcaceres committed
255
256
257
			      JACK_DEFAULT_AUDIO_TYPE,
			      JackPortIsInput, 0);
    }
jcaceres's avatar
jcaceres committed
258
259
260
261
262
263
}


void
JackClient::stop ()
{
jcaceres's avatar
jcaceres committed
264
265
  jack_client_close (client);
  printf ("jack_client_close");
jcaceres's avatar
jcaceres committed
266
267
268
269
270
271
}

void
JackClient::start ()
{				/* tell the JACK server that we are ready to roll */

jcaceres's avatar
jcaceres committed
272
273
274
275
276
277
  if (jack_activate (client))
    {
      printf ("cannot activate client");
      //              return 1;
    }
  go ();
jcaceres's avatar
jcaceres committed
278
279
280
281
282
283
}

void
JackClient::go ()
{				/* tell the JACK server that we are ready to roll */

jcaceres's avatar
jcaceres committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  /* connect the ports. Note: you can't do this before
   * the client is activated, because we can't allow
   * connections to be made to clients that aren't
   * running.
   */


  if ((ports =
       jack_get_ports (client, NULL, NULL,
		       JackPortIsPhysical | JackPortIsOutput)) == NULL)
    {
      fprintf (stderr, "Cannot find any physical capture ports\n");
      exit (1);
    }
  // ports is alsa capture, input_port is to stream input
  for (int i = 0; i < nChans; i++)
    {

      if (jack_connect
	  (client, ports[i+alsa_readable_offset], 
	   jack_port_name (input_port[ONE_BASED])))
jcaceres's avatar
jcaceres committed
305
	{
jcaceres's avatar
jcaceres committed
306
	  fprintf (stderr, "cannot connect input ports\n");
jcaceres's avatar
jcaceres committed
307
	}
jcaceres's avatar
jcaceres committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    }

  free (ports);

  if ((ports =
       jack_get_ports (client, NULL, NULL,
		       JackPortIsPhysical | JackPortIsInput)) == NULL)
    {
      fprintf (stderr, "Cannot find any physical playback ports\n");
      exit (1);
    }
  // ports is alsa writeable, output_port from stream output
  for (int i = 0; i < nChans; i++)
    {
      if (jack_connect
	  (client, jack_port_name (output_port[ONE_BASED]), ports[i]))
jcaceres's avatar
jcaceres committed
324
	{
jcaceres's avatar
jcaceres committed
325
	  fprintf (stderr, "cannot connect output ports\n");
jcaceres's avatar
jcaceres committed
326
327
	}

jcaceres's avatar
jcaceres committed
328
329
    }
  free (ports);
jcaceres's avatar
jcaceres committed
330
331
332
333
}

JackClient::~JackClient ()
{
jcaceres's avatar
jcaceres committed
334
  stop ();
jcaceres's avatar
jcaceres committed
335
}