summaryrefslogtreecommitdiff
path: root/libs/backends/coreaudio/coreaudio_pcmio.cc
blob: a1615d8583eebd2bffaf49122e5125aa03619edb (plain)
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
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
156
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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "coreaudio_pcmio.h"
#include <string>


static OSStatus hardwarePropertyChangeCallback (AudioHardwarePropertyID inPropertyID, void* arg) {
	if (inPropertyID == kAudioHardwarePropertyDevices) {
		CoreAudioPCM * self = static_cast<CoreAudioPCM*>(arg);
		self->hwPropertyChange();
	}
	return noErr;
}

CoreAudioPCM::CoreAudioPCM ()
	: _auhal (0)
	, _deviceIDs (0)
	, _inputAudioBufferList (0)
	, _state (-1)
	, _capture_channels (0)
	, _playback_channels (0)
	, _in_process (false)
	, _numDevices (0)
	, _process_callback (0)
	, _error_callback (0)
	, _device_ins (0)
	, _device_outs (0)
{
#ifdef COREAUDIO_108 // TODO
	CFRunLoopRef theRunLoop = NULL;
	AudioObjectPropertyAddress property = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioHardwarePropertyDevices };
	AudioObjectSetPropertyData (kAudioObjectSystemObject, &property, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
#endif
	AudioHardwareAddPropertyListener (kAudioHardwarePropertyDevices, hardwarePropertyChangeCallback, this);
}

CoreAudioPCM::~CoreAudioPCM ()
{
	if (_state == 0) {
		pcm_stop();
	}
	delete _deviceIDs;
	free(_device_ins);
	free(_device_outs);
	AudioHardwareRemovePropertyListener(kAudioHardwarePropertyDevices, hardwarePropertyChangeCallback);
	free(_inputAudioBufferList);
}


void
CoreAudioPCM::hwPropertyChange() {
	printf("hardwarePropertyChangeCallback\n");
	discover();
}

void
CoreAudioPCM::discover() {
	OSStatus err;
	UInt32 propSize = 0;

	// TODO trymutex lock.

	if (_deviceIDs) {
		delete _deviceIDs; _deviceIDs = 0;
		free(_device_ins); _device_ins = 0;
		free(_device_outs); _device_outs = 0;
	}
	_devices.clear();

#ifdef COREAUDIO_108
	AudioObjectPropertyAddress propertyAddress;
	propertyAddress.mSelector = kAudioHardwarePropertyDevices;
	propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
	propertyAddress.mElement = kAudioObjectPropertyElementMaster;
	err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propSize);
#else
	err = AudioHardwareGetPropertyInfo (kAudioHardwarePropertyDevices, &propSize, NULL);
#endif

	_numDevices = propSize / sizeof (AudioDeviceID);
	propSize = _numDevices * sizeof (AudioDeviceID);

	_deviceIDs = new AudioDeviceID[_numDevices];
	_device_ins = (uint32_t*) calloc(_numDevices, sizeof(uint32_t));
	_device_outs = (uint32_t*) calloc(_numDevices, sizeof(uint32_t));

#ifdef COREAUDIO_108
	propertyAddress.mSelector = kAudioHardwarePropertyDevices;
	err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propSize, _deviceIDs);
#else
	err = AudioHardwareGetProperty (kAudioHardwarePropertyDevices, &propSize, _deviceIDs);
#endif

	for (size_t deviceIndex = 0; deviceIndex < _numDevices; deviceIndex++) {
		propSize = 64;
		char deviceName[64];
#ifdef COREAUDIO_108
		propertyAddress.mSelector = kAudioDevicePropertyDeviceName;
		propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
		err = AudioObjectGetPropertyData(_deviceIDs[deviceIndex], &propertyAddress, 0, NULL, &propSize, deviceName);
#else
		err = AudioDeviceGetProperty(_deviceIDs[deviceIndex], 0, 0, kAudioDevicePropertyDeviceName, &propSize, deviceName);
#endif

		if (kAudioHardwareNoError != err) {
			fprintf(stderr, "device name query failed: %i\n", err);
			continue;
		}

		UInt32 size;
		UInt32 outputChannelCount = 0;
		UInt32 inputChannelCount = 0;
		AudioBufferList *bufferList = NULL;

		/* query number of inputs */
#ifdef COREAUDIO_108
		size = 0;
		propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
		propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
		err = AudioObjectGetPropertyDataSize(_deviceIDs[deviceIndex], &propertyAddress, 0, NULL, &size);
		if (kAudioHardwareNoError != err) {
			fprintf(stderr, "kAudioDevicePropertyStreamConfiguration failed: %i\n", err);
			continue;
		}

		bufferList = (AudioBufferList *)(malloc(size));
		assert(bufferList);
		if (!bufferList) { fprintf(stderr, "OUT OF MEMORY\n"); break; }

		err = AudioObjectGetPropertyData(_deviceIDs[deviceIndex], &propertyAddress, 0, NULL, &size, bufferList);

#else
		err = AudioDeviceGetPropertyInfo (_deviceIDs[deviceIndex], 0, AUHAL_OUTPUT_ELEMENT, kAudioDevicePropertyStreamConfiguration, &propSize, NULL);
		if (kAudioHardwareNoError != err) {
			fprintf(stderr, "kAudioDevicePropertyStreamConfiguration failed: %i\n", err);
			continue;
		}
		bufferList = (AudioBufferList *)(malloc(size));
		assert(bufferList);
		if (!bufferList) { fprintf(stderr, "OUT OF MEMORY\n"); break; }

		bufferList->mNumberBuffers = 0;
		err = AudioDeviceGetProperty(_deviceIDs[deviceIndex], 0, AUHAL_OUTPUT_ELEMENT, kAudioDevicePropertyStreamConfiguration, &size, bufferList);
#endif
		if(kAudioHardwareNoError != err) {
			fprintf(stderr, "kAudioDevicePropertyStreamConfiguration failed: %i\n", err);
			free(bufferList);
			continue;
		}

		for(UInt32 j = 0; j < bufferList->mNumberBuffers; ++j) {
			outputChannelCount += bufferList->mBuffers[j].mNumberChannels;
		}
		free(bufferList);


		/* query number of inputs */
#ifdef COREAUDIO_108
		size = 0;
		propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
		propertyAddress.mScope = kAudioDevicePropertyScopeInput;
		err = AudioObjectGetPropertyDataSize(_deviceIDs[deviceIndex], &propertyAddress, 0, NULL, &size);
		if (kAudioHardwareNoError != err) {
			fprintf(stderr, "kAudioDevicePropertyStreamConfiguration failed: %i\n", err);
			continue;
		}

		bufferList = (AudioBufferList *)(malloc(size));
		assert(bufferList);
		if (!bufferList) { fprintf(stderr, "OUT OF MEMORY\n"); break; }

		err = AudioObjectGetPropertyData(_deviceIDs[deviceIndex], &propertyAddress, 0, NULL, &size, bufferList);
#else
		err = AudioDeviceGetPropertyInfo (_deviceIDs[deviceIndex], 0, AUHAL_INPUT_ELEMENT, kAudioDevicePropertyStreamConfiguration, &propSize, NULL);
		if (kAudioHardwareNoError != err) {
			fprintf(stderr, "kAudioDevicePropertyStreamConfiguration failed: %i\n", err);
			continue;
		}
		bufferList = (AudioBufferList *)(malloc(size));
		assert(bufferList);
		if (!bufferList) { fprintf(stderr, "OUT OF MEMORY\n"); break; }

		bufferList->mNumberBuffers = 0;
		err = AudioDeviceGetProperty(_deviceIDs[deviceIndex], 0, AUHAL_INPUT_ELEMENT, kAudioDevicePropertyStreamConfiguration, &size, bufferList);
#endif
		if(kAudioHardwareNoError != err) {
			fprintf(stderr, "kAudioDevicePropertyStreamConfiguration failed: %i\n", err);
			free(bufferList);
			continue;
		}

		for(UInt32 j = 0; j < bufferList->mNumberBuffers; ++j) {
			inputChannelCount += bufferList->mBuffers[j].mNumberChannels;
		}
		free(bufferList);



		{
			std::string dn = deviceName;
			_device_ins[deviceIndex] = inputChannelCount;
			_device_outs[deviceIndex] = outputChannelCount;
			printf("CoreAudio Device: #%ld '%s' in:%d out:%d\n", deviceIndex, deviceName, inputChannelCount, outputChannelCount);
			if (outputChannelCount > 0 && inputChannelCount > 0) {
				_devices.insert (std::pair<size_t, std::string> (deviceIndex, dn));
			}
		}
	}
}

void
CoreAudioPCM::pcm_stop ()
{
	printf("CoreAudioPCM::pcm_stop\n");
	if (!_auhal) return;

	AudioOutputUnitStop(_auhal);
	AudioUnitUninitialize(_auhal);
#ifdef COREAUDIO_108
	AudioComponentInstanceDispose(_auhal);
#else
	CloseComponent(_auhal);
#endif
	_auhal = 0;
	_state = -1;
	_capture_channels = 0;
	_playback_channels = 0;

	free(_inputAudioBufferList);
	_inputAudioBufferList = 0;

	_error_callback = 0;
	_process_callback = 0;
}

#ifndef NDEBUG
static void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
{
    printf ("- - - - - - - - - - - - - - - - - - - -\n");
    printf ("  Sample Rate:%f", inDesc->mSampleRate);
    printf ("  Format ID:%.*s\n", (int)sizeof(inDesc->mFormatID), (char*)&inDesc->mFormatID);
    printf ("  Format Flags:%X\n", inDesc->mFormatFlags);
    printf ("  Bytes per Packet:%d\n", inDesc->mBytesPerPacket);
    printf ("  Frames per Packet:%d\n", inDesc->mFramesPerPacket);
    printf ("  Bytes per Frame:%d\n", inDesc->mBytesPerFrame);
    printf ("  Channels per Frame:%d\n", inDesc->mChannelsPerFrame);
    printf ("  Bits per Channel:%d\n", inDesc->mBitsPerChannel);
    printf ("- - - - - - - - - - - - - - - - - - - -\n");
}
#endif

static OSStatus render_callback_ptr (
		void* inRefCon,
		AudioUnitRenderActionFlags* ioActionFlags,
		const AudioTimeStamp* inTimeStamp,
		UInt32 inBusNumber,
		UInt32 inNumberFrames,
		AudioBufferList* ioData)
{
	CoreAudioPCM * d = static_cast<CoreAudioPCM*> (inRefCon);
	return d->render_callback(ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
}



int
CoreAudioPCM::pcm_start (
		uint32_t device_id_in, uint32_t device_id_out,
		uint32_t sample_rate, uint32_t samples_per_period,
		int (process_callback (void*)), void *process_arg)
{

	assert(_deviceIDs);
	_state = -2;

	if (device_id_out >= _numDevices || device_id_in >= _numDevices) {
		return -1;
	}

	_process_callback = process_callback;
	_process_arg = process_arg;
	_max_samples_per_period = samples_per_period;
	_cur_samples_per_period = 0;

	ComponentResult err;
	UInt32 enableIO;
	AudioStreamBasicDescription srcFormat, dstFormat;
	
	AudioComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
	AudioComponent HALOutput = AudioComponentFindNext(NULL, &cd);
	if (!HALOutput) { goto error; }

	err = AudioComponentInstanceNew(HALOutput, &_auhal);
	if (err != noErr) { goto error; }

	err = AudioUnitInitialize(_auhal);
	if (err != noErr) { goto error; }

	enableIO = 1;
	err = AudioUnitSetProperty(_auhal, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, AUHAL_INPUT_ELEMENT, &enableIO, sizeof(enableIO));
	if (err != noErr) { goto error; }
	enableIO = 1;
	err = AudioUnitSetProperty(_auhal, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, AUHAL_OUTPUT_ELEMENT, &enableIO, sizeof(enableIO));
	if (err != noErr) { goto error; }

	err = AudioUnitSetProperty(_auhal, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, AUHAL_OUTPUT_ELEMENT, &_deviceIDs[device_id_out], sizeof(AudioDeviceID));
	if (err != noErr) { goto error; }

	err = AudioUnitSetProperty(_auhal, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, AUHAL_INPUT_ELEMENT, &_deviceIDs[device_id_in], sizeof(AudioDeviceID));
	if (err != noErr) { goto error; }

	// Set buffer size
	err = AudioUnitSetProperty(_auhal, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, AUHAL_INPUT_ELEMENT, (UInt32*)&_max_samples_per_period, sizeof(UInt32));
	if (err != noErr) { goto error; }
	err = AudioUnitSetProperty(_auhal, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, AUHAL_OUTPUT_ELEMENT, (UInt32*)&_max_samples_per_period, sizeof(UInt32));
	if (err != noErr) { goto error; }


	// set sample format
	srcFormat.mSampleRate = sample_rate;
	srcFormat.mFormatID = kAudioFormatLinearPCM;
	srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
	srcFormat.mBytesPerPacket = sizeof(float);
	srcFormat.mFramesPerPacket = 1;
	srcFormat.mBytesPerFrame = sizeof(float);
	srcFormat.mChannelsPerFrame = _device_ins[device_id_in];
	srcFormat.mBitsPerChannel = 32;

	err = AudioUnitSetProperty(_auhal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, AUHAL_INPUT_ELEMENT, &srcFormat, sizeof(AudioStreamBasicDescription));
	if (err != noErr) { goto error; }

	dstFormat.mSampleRate = sample_rate;
	dstFormat.mFormatID = kAudioFormatLinearPCM;
	dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
	dstFormat.mBytesPerPacket = sizeof(float);
	dstFormat.mFramesPerPacket = 1;
	dstFormat.mBytesPerFrame = sizeof(float);
	dstFormat.mChannelsPerFrame = _device_outs[device_id_out];
	dstFormat.mBitsPerChannel = 32;

	err = AudioUnitSetProperty(_auhal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, AUHAL_OUTPUT_ELEMENT, &dstFormat, sizeof(AudioStreamBasicDescription));
	if (err != noErr) { goto error; }

	UInt32 size;
	size = sizeof(AudioStreamBasicDescription);
	err = AudioUnitGetProperty(_auhal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, AUHAL_INPUT_ELEMENT, &srcFormat, &size);
	if (err != noErr) { goto error; }
	_capture_channels = srcFormat.mChannelsPerFrame;
#ifndef NDEBUG
	PrintStreamDesc(&srcFormat);
#endif

	size = sizeof(AudioStreamBasicDescription);
	err = AudioUnitGetProperty(_auhal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, AUHAL_OUTPUT_ELEMENT, &dstFormat, &size);
	if (err != noErr) { goto error; }
	_playback_channels = dstFormat.mChannelsPerFrame;

#ifndef NDEBUG
	PrintStreamDesc(&dstFormat);
#endif

	_inputAudioBufferList = (AudioBufferList*)malloc(sizeof(UInt32) + _capture_channels * sizeof(AudioBuffer));

	// Setup callbacks
	AURenderCallbackStruct renderCallback;
	memset (&renderCallback, 0, sizeof (renderCallback));
	renderCallback.inputProc = render_callback_ptr;
	renderCallback.inputProcRefCon = this;
	err = AudioUnitSetProperty(_auhal,
			kAudioUnitProperty_SetRenderCallback,
			kAudioUnitScope_Output, AUHAL_OUTPUT_ELEMENT,
			&renderCallback, sizeof (renderCallback));
	if (err != noErr) { goto error; }

	printf("SETUP OK..\n");

	if (AudioOutputUnitStart(_auhal) == noErr) {
		printf("Coreaudio Started..\n");
		_state = 0;
		return 0;
	}

error:
	pcm_stop();
	_state = -3;
	return -1;
}


OSStatus
CoreAudioPCM::render_callback (
		AudioUnitRenderActionFlags* ioActionFlags,
		const AudioTimeStamp* inTimeStamp,
		UInt32 inBusNumber,
		UInt32 inNumberFrames,
		AudioBufferList* ioData)
{
	OSStatus retVal = 0;

	assert(_max_samples_per_period >= inNumberFrames);
	assert(ioData->mNumberBuffers = _playback_channels);

	_cur_samples_per_period = inNumberFrames;


        _inputAudioBufferList->mNumberBuffers = _capture_channels;
	for (int i = 0; i < _capture_channels; ++i) {
		_inputAudioBufferList->mBuffers[i].mNumberChannels = 1;
		_inputAudioBufferList->mBuffers[i].mDataByteSize = inNumberFrames * sizeof(float);
		_inputAudioBufferList->mBuffers[i].mData = NULL;
	}

        retVal = AudioUnitRender(_auhal, ioActionFlags, inTimeStamp, AUHAL_INPUT_ELEMENT, inNumberFrames, _inputAudioBufferList);

        if (retVal != kAudioHardwareNoError) {
		char *rv = (char*)&retVal;
		printf("ERR %c%c%c%c\n", rv[0], rv[1], rv[2], rv[3]);
		if (_error_callback) {
			_error_callback(_error_arg);
		}
		return retVal;
	}

	_outputAudioBufferList = ioData;

	_in_process = true;

	int rv = -1;

	if (_process_callback) {
		rv = _process_callback(_process_arg);
	}

	_in_process = false;

	if (rv != 0) {
		// clear output
		for (int i = 0; i < ioData->mNumberBuffers; ++i) {
			float* ob = (float*) ioData->mBuffers[i].mData;
			memset(ob, 0, sizeof(float) * inNumberFrames);
		}
	}
	return noErr;
}

int
CoreAudioPCM::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
{
	if (!_in_process || chn > _capture_channels || n_samples > _cur_samples_per_period) {
		return -1;
	}
	assert(_inputAudioBufferList->mNumberBuffers > chn);
	memcpy((void*)input, (void*)_inputAudioBufferList->mBuffers[chn].mData, sizeof(float) * n_samples);
	return 0;

}
int
CoreAudioPCM::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
{
	if (!_in_process || chn > _playback_channels || n_samples > _cur_samples_per_period) {
		return -1;
	}

	assert(_outputAudioBufferList->mNumberBuffers > chn);
	memcpy((void*)_outputAudioBufferList->mBuffers[chn].mData, (void*)output, sizeof(float) * n_samples);
	return 0;
}