JackClient.cpp 7.14 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
3
4
5
6
7
8
9
10
11
12
13
//
// File: JackClient.h
// Created by: cc <cc@sony.stanford.edu>
// Created on: Sat Jul 26 13:08:21 2003
//
// mod for displaying channels in jack counting from 1
#define ONE_BASED (i+1)

#include "JackClient.h"
#include "math.h"
#include "string.h"
#include "audioDevice.h"
#include "qstring.h"
jcaceres's avatar
jcaceres committed
14
#include <qtextstream.h>
jcaceres's avatar
jcaceres committed
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
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;

static void
error ()
{
};
static void
error (const char *desc)
{
	printf ("JACK error: %s\n", desc);
}

static void
jack_shutdown (void *arg)
{
	exit (1);
}

static int srate_callback (jack_nframes_t nframes, void *arg)
{
	printf ("the sample rate is now %d/sec\n", (int) nframes);
	cout << "bogus -- fixme --  nothing has been changed" << endl;
	return 0;
}

static int
xx_global_process_xx (unsigned int nframes, void *argc)
{

	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
/*		
		               for (int i = 0; i < xx_nChans_xx; i++)
		  {
		  d = sin (iPhase[i]);
		  s = (signed short) floor (d * 32768.0 * 0.1);
		  inBuf[f + i] = s;
		  }
		  iPhase[0] += 0.15; // cool diff tones!
		  iPhase[1] += 0.17;
*/
		for (int i = 0; i < xx_nChans_xx; i++)
		{
			outFrame = outBufPtr[i];
			d = (double) outBuf[f + i] / 32768.0;
			outFrame[j] = d;
		}
		// 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;
}

JackClient::JackClient (QString name, int nChans, int nFrames, bool output, bool input, AudioDevice * ad, int aro):Stk ()
{
    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++)
	{
jcaceres's avatar
jcaceres committed
156
		snprintf (namebuf, sizeof (namebuf) - 1, "jacktrip_%d", i);
jcaceres's avatar
jcaceres committed
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
		if ((client = jack_client_new (namebuf)) != 0)
		{
			break;
		}
	}
	if (!client)
		printf ("jack server not running?\n");
	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;
		QTextOStream (&iName) << "input" << ONE_BASED;
		QTextOStream (&oName) << "output" << ONE_BASED;

		if (output)
			output_port[ONE_BASED] =
				jack_port_register (client, oName,
						    JACK_DEFAULT_AUDIO_TYPE,
						    JackPortIsOutput, 0);
		if (input)
			input_port[ONE_BASED] =
				jack_port_register (client, iName,
						    JACK_DEFAULT_AUDIO_TYPE,
						    JackPortIsInput, 0);
	}
}


void
JackClient::stop ()
{
	jack_client_close (client);
		printf ("jack_client_close");
}

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

	if (jack_activate (client))
	{
		printf ("cannot activate client");
//              return 1;
	}
	go ();
}

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

	/* 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])))
		{
			fprintf (stderr, "cannot connect input ports\n");
		}
	}

	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]))
		{
			fprintf (stderr, "cannot connect output ports\n");
		}

	}
	free (ports);
}

JackClient::~JackClient ()
{
	stop ();
}