audioDevice.cpp 8.83 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
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
/**********************************************************************
 * File: audioDevice.cpp
 **********************************************************************
 * A project of the SoundWIRE research group at CCRMA
 * http://www-ccrma.stanford.edu/groups/soundwire/
 * --------------------------------------------------------------------
 * Coded by Daniel Walling (dwalling@ccrma.stanford.edu)
 * --------------------------------------------------------------------
 */

 // updated for Stk 4.1.2
 // RtAudio.cpp is now in project to compile with desired API

#include "audioDevice.h"
#include <time.h>
using namespace std;

AudioDevice::AudioDevice (bool testMode)
{
	if (testMode == false)
	{
		cerr << "You shouldn't ever call new AudioDevice( false );" <<
			endl;
		exit (1);
	}
	else
	{
		probeAudioDevices ();
	}
}

AudioDevice::AudioDevice (int ID, int numBuffers, audioDeviceModeT mode,
			  AudioInfoT audioInfo):
audioDeviceID (ID),
audioInfo (audioInfo),
numBuffers (numBuffers),
mode (mode)
{
	streamID = 0;
	int tmpBufferSize = audioInfo->getFramesPerBuffer ();
	jack = audioInfo->isJack ();
	if (audioInfo->getNumNetHarpStrings () > 0)
	{
		harp = true;
	}
	else
	{
		harp = false;
	}

	if (jack)
		cout << "Creating Jack audio Device.........";
	else
		cout << "Creating RtAudio Device.........";

	try
	{
		switch (mode)
		{
		case RECORD:
			if (jack)
				jdevice =
jcaceres's avatar
jcaceres committed
63
					new JackClient (APP_NAME,
jcaceres's avatar
jcaceres committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
							audioInfo->
							getNumChans (),
							audioInfo->
							getFramesPerBuffer (),
							false, true, this, 
			audioInfo->getJack_alsa_readable_offset ());
			else
				device = new RtAudio (streamID,
						      0,
						      ID,
						      audioInfo->
						      getNumAudioChans (),
						      RTAUDIO_SINT16,
						      audioInfo->
						      getSampleRate (),
						      &(tmpBufferSize),
						      numBuffers,
						      RtAudio::LINUX_ALSA);


			break;
		case PLAYBACK:
			if (jack)
				jdevice =
jcaceres's avatar
jcaceres committed
88
					new JackClient (APP_NAME,
jcaceres's avatar
jcaceres committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
							audioInfo->
							getNumChans (),
							audioInfo->
							getFramesPerBuffer (),
							true, false, this, 
			audioInfo->getJack_alsa_readable_offset ());
			else
				device = new RtAudio (streamID,
						      audioInfo->
						      getNumAudioChans (), ID,
						      0, RTAUDIO_SINT16,
						      audioInfo->
						      getSampleRate (),
						      &(tmpBufferSize),
						      numBuffers,
						      RtAudio::LINUX_ALSA);
			break;
		case DUPLEX:
			if (jack)
				jdevice =
jcaceres's avatar
jcaceres committed
109
					new JackClient (APP_NAME,
jcaceres's avatar
jcaceres committed
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
166
167
168
169
170
171
172
173
174
							audioInfo->
							getNumChans (),
							audioInfo->
							getFramesPerBuffer (),
							true, true, this, 
			audioInfo->getJack_alsa_readable_offset ());
			else
				device = new RtAudio ((streamID),
						      audioInfo->
						      getNumAudioChans (), ID,
						      audioInfo->
						      getNumAudioChans (),
						      RTAUDIO_SINT16,
						      audioInfo->
						      getSampleRate (),
						      &(tmpBufferSize),
						      numBuffers,
						      RtAudio::LINUX_ALSA);
			break;
		}
	}
	catch (RtError (&m))
	{
		cout << "FAILED!" << endl;
		m.printMessage ();
		probeAudioDevices ();
		exit (1);
	}

	cout << "SUCCESS!" << endl;

	if (tmpBufferSize != audioInfo->getFramesPerBuffer ())
	{
		cerr << "RtAudio device could not use requested buffer length of " << audioInfo->getFramesPerBuffer () << " frames.  Using " << tmpBufferSize << endl;
		audioInfo->setFramesPerBuffer (tmpBufferSize);
	}


	if (!jack)
	{
		if (device == NULL)
		{
			cerr << "RtAudio Device is NULL!" << endl;
			probeAudioDevices ();
		}
		buffer = device->getStreamBuffer ();


		if (buffer == NULL)
		{
			cerr << "RtAudio device buffer is NULL!" << endl;
			probeAudioDevices ();
		}
	}

	bytesPerBuffer = audioInfo->getBytesPerBuffer ();

	if (harp == true)
	{
		generateHarpMixMap ();
	}
	else
	{
		readLock = new QSemaphore (1);
		writeLock = new QSemaphore (1);
jcaceres's avatar
jcaceres committed
175
		(*writeLock)++;	// lock out write, to start with read
jcaceres's avatar
jcaceres committed
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
	}

	if (jack)
	{
	}
	else
		device->startStream ();
}

AudioDevice::~AudioDevice ()
{
}

void
AudioDevice::jackStart ()
{
	cout << "jack start" << endl;
	jdevice->start ();
}

void
AudioDevice::jackStop ()
{
	cout << "jack stop" << endl;
	jdevice->stop ();
}

void
AudioDevice::tick ()
{
	try
	{
		device->tickStream (); // blocks until transfer completed
		if (audioInfo->lastTickTime)
		{
			clock_t newTime = clock ();
			fprintf (stderr, "Audio Tick,%d,%d,%.5f\n", newTime,
				 *(audioInfo->lastTickTime),
				 (newTime -
				  *(audioInfo->lastTickTime)) *
				 CLOCKS_PER_SEC);
			*(audioInfo->lastTickTime) = newTime;
		}
	}
	catch (RtError (&m))
	{
		m.printMessage ();
	}
}

void
AudioDevice::jtick () // called by jack process after transfer completed
{
//	jackWait.wakeAll ();
//      cout << "jtick" << endl;
}

void
AudioDevice::bufferPtrs (void *jib, void *job)
{
//	jibuffer = jib;
//	jobuffer = job;
}

void
AudioDevice::unlockRead ()
{
jcaceres's avatar
jcaceres committed
243
	(*readLock)--;		// so audio input thread will unblock when stopped
jcaceres's avatar
jcaceres committed
244
245
246
247
248
249
250
251
252
253
254
}

void
AudioDevice::readBuffer (void *to)
{

	if (mode == PLAYBACK)
	{
		cerr << "ERROR: AudioDevice::readBuffer called on device in PLAYBACK mode!" << endl;
	}
	
jcaceres's avatar
jcaceres committed
255
		(*readLock)++;
jcaceres's avatar
jcaceres committed
256
		memcpy (to, buffer, bytesPerBuffer);
jcaceres's avatar
jcaceres committed
257
		(*writeLock)--;
jcaceres's avatar
jcaceres committed
258
259
260
261
262
263
264
265
266
267
268
269
270
	
}

void
AudioDevice::writeBuffer (void *from)
{
	if (mode == RECORD)
	{
		cerr << "ERROR: AudioDevice::writeBuffer called on device in RECORD mode!" << endl;
	}
	if (harp == false)
	{
		
jcaceres's avatar
jcaceres committed
271
			(*writeLock)++;
jcaceres's avatar
jcaceres committed
272
273
			memcpy (buffer, from, bytesPerBuffer);
			tick (); // calls tickstream, blocks
jcaceres's avatar
jcaceres committed
274
			(*readLock)--;
jcaceres's avatar
jcaceres committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
		
	}
	else
	{
		
			harpTick (buffer, from);
			tick ();
		
	}
}

void
AudioDevice::harpTick (void *toBuffer, void *fromBuffer)
{
	double nextSample;
	int stringIndex = 0;
	int sndChans = audioInfo->getNumAudioChans ();
	int netChans = audioInfo->getNumNetHarpStrings ();
	for (int frameIndex = 0;
	     frameIndex < audioInfo->getFramesPerBuffer (); frameIndex++)
	{
		stringIndex = 0;
		for (int chanIndex = 0; chanIndex < sndChans; chanIndex++)
		{
			nextSample = 0.0;
			for (int sampleIndex = 0;
			     sampleIndex < stringsPerChan[chanIndex];
			     sampleIndex++)
			{
				nextSample +=
					(double) (*
						  ((signed short
						    *) (fromBuffer) +
						   frameIndex *
						   netChans +
						   stringIndex++)) / 32768.0;
			}
			*((signed short *) (toBuffer) +
			  frameIndex * sndChans + chanIndex) =
  (signed short) floor (nextSample * 32768.0);
		}
	}
}

/**
 * @brief Distribute the NetHarp channels across the audio channels.
 */

void
AudioDevice::generateHarpMixMap ()
{
	cerr << "Generating Mixmap for " << audioInfo->getNumNetHarpStrings ()
		<< " NetHarp strings to " << audioInfo->getNumAudioChans ()
		<< " audio channels." << endl;

	stringsPerChan = new int[audioInfo->getNumAudioChans ()];
	// Set all to 0 initially
	for (int i = 0; i < audioInfo->getNumAudioChans (); i++)
	{
		stringsPerChan[i] = 0;
	}
	// Distribute strings among audio channels
	int stringsAllocated (0);
	int i (0);
	while (stringsAllocated != audioInfo->getNumNetHarpStrings ())
	{
		stringsPerChan[i++]++;
		stringsAllocated++;
		if (i == audioInfo->getNumAudioChans ())
			i = 0;
	}
}

/**
 * @brief Probe audio device to see if it supports required functionality.
 */

void
AudioDevice::probeAudioDevices ()
{
	cout << "=====================================================" <<
		endl;
	cout << " Probing audio devices..." << endl;
	cout << " ------------------------" << endl << endl;

	try
	{
		device = new RtAudio ();
	} catch (RtError (&m))
	{
		cerr << "AudioDevice::probeAudioDevice() failed!" << endl;
		m.printMessage ();
		exit (1);
	}

	int numDevices = device->getDeviceCount ();

	RtAudioDeviceInfo info;

	cout << " " << numDevices << " audio devices found.  Probing..." <<
		endl << endl;

	for (int i = 0; i < numDevices; i++)
	{
		try
		{
			info = device->getDeviceInfo (i + 1);
			checkRequirements (i + 1, &info);
		}
		catch (RtError & error)
		{
			error.printMessage ();
			break;
		}
	}
	cout << "Check these values against the streambd default command line settings." << endl;
	cout << "Change the relevant parameters as required.  If duplex is not supported" << endl;
	cout << "you will only be able to run in SNDMIRROR mode." << endl;
	cout << "=====================================================" <<
		endl;
}

void
AudioDevice::checkRequirements (int deviceNum, RtAudioDeviceInfo * info)
{
	cout << "Device " << deviceNum << ": " << info->name << endl;
	cout << "maxOutputChannels =\t" << info->outputChannels << endl;
	cout << "maxInputChannels =\t" << info->inputChannels << endl;
	cout << "maxDuplexChannels =\t" << info->duplexChannels << endl;

	cout << "hasDuplexSupport =\t";
	if (info->duplexChannels > 0)
		cout << "true" << endl;
	else
		cout << "false." << endl;
	cout << "Sample rates supported: " << endl;
	for (int i = 0; i < 99; i++)
		if ((info->
		     sampleRates[i] > 0) && (info->sampleRates[i] < 200000))
			cout << info->sampleRates[i] << endl;
	cout << endl;
}

void
AudioDevice::setThreads (streamThreads t)
{
	inThread = t.audioin;
	outThread = t.audioout;
}