summaryrefslogtreecommitdiff
path: root/libs/ardour/coreaudiosource.cc
blob: 90ab07b86d5df7ac1f5104b7cfea6b44ef9f8131 (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
/*
    Copyright (C) 2006 Paul Davis
    Written by Taybin Rutkin

    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 <algorithm>
#include <inttypes.h>

#include "pbd/error.h"
#include "ardour/coreaudiosource.h"
#include "ardour/utils.h"

#ifdef COREAUDIO105
#include "CAAudioFile.h"
#else
#include "CAExtAudioFile.h"
#endif
#include "CAStreamBasicDescription.h"

#include <glibmm/fileutils.h>

#include "pbd/i18n.h"

#include <AudioToolbox/AudioFormat.h>

using namespace std;
using namespace ARDOUR;
using namespace PBD;

/** Create a new CoreAudioSource using session state, which implies that the
 *  file must already exist.
 */
CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
	: Source (s, node)
	, AudioFileSource (s, node)
{
	init_cafile ();

        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
	existence_check ();
}

/** Create a new CoreAudioSource from an existing file. Sources created with this
 *  method are never writable or removable.
 */
CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
	: Source (s, DataType::AUDIO, path, Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy))),
		AudioFileSource (s, path,
			Source::Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
{
	_channel = chn;
	init_cafile ();

        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
	existence_check ();
}

void
CoreAudioSource::init_cafile ()
{
	/* note that we temporarily truncated _id at the colon */
	try {
		af.Open(_path.c_str());

		CAStreamBasicDescription file_format (af.GetFileDataFormat());
		n_channels = file_format.NumberChannels();

		if (_channel >= n_channels) {
			error << string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels, _channel, name()) << endmsg;
			throw failed_constructor();
		}

		_length = af.GetNumberFrames();

		CAStreamBasicDescription client_format (file_format);

		/* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
		   and interleaved (since we plan to deinterleave ourselves)
		*/

		client_format.SetCanonical(client_format.NumberChannels(), true);
		af.SetClientFormat (client_format);

	} catch (CAXException& cax) {

		error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
					_path, (writable() ? "read+write" : "reading")) << endmsg;
		throw failed_constructor ();
	}
}

CoreAudioSource::~CoreAudioSource ()
{
}

void
CoreAudioSource::close ()
{
	af.Close ();
}

int
CoreAudioSource::safe_read (Sample* dst, framepos_t start, framecnt_t cnt, AudioBufferList& abl) const
{
	framecnt_t nread = 0;

	while (nread < cnt) {

		try {
			af.Seek (start+nread);
		} catch (CAXException& cax) {
			error << string_compose("CoreAudioSource: %1 to %2 (%3)", cax.mOperation, start+nread, _name.val().substr (1)) << endmsg;
			return -1;
		}

		UInt32 new_cnt = cnt - nread;

		abl.mBuffers[0].mDataByteSize = new_cnt * n_channels * sizeof(Sample);
		abl.mBuffers[0].mData = dst + nread;

		try {
			af.Read (new_cnt, &abl);
		} catch (CAXException& cax) {
			error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
			return -1;
		}

		if (new_cnt == 0) {
			/* EOF */
			if (start+cnt == _length) {
				/* we really did hit the end */
				nread = cnt;
			}
			break;
		}

		nread += new_cnt;
	}

	if (nread < cnt) {
		return -1;
	} else {
		return 0;
	}
}


framecnt_t
CoreAudioSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) const
{
	framecnt_t file_cnt;
	AudioBufferList abl;

	abl.mNumberBuffers = 1;
	abl.mBuffers[0].mNumberChannels = n_channels;

	if (start > _length) {

		/* read starts beyond end of data, just memset to zero */

		file_cnt = 0;

	} else if (start + cnt > _length) {

		/* read ends beyond end of data, read some, memset the rest */

		file_cnt = _length - start;

	} else {

		/* read is entirely within data */

		file_cnt = cnt;
	}

	if (file_cnt != cnt) {
		frameoffset_t delta = cnt - file_cnt;
		memset (dst+file_cnt, 0, sizeof (Sample) * delta);
	}

	if (file_cnt) {

		if (n_channels == 1) {
			if (safe_read (dst, start, file_cnt, abl) == 0) {
				return cnt;
			}
			return 0;
		}
	}

	Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);

	if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
		return 0;
	}

	Sample *ptr = interleave_buf + _channel;

	/* stride through the interleaved data */

	for (framecnt_t n = 0; n < file_cnt; ++n) {
		dst[n] = *ptr;
		ptr += n_channels;
	}

	return cnt;
}

float
CoreAudioSource::sample_rate() const
{
	CAStreamBasicDescription client_asbd;

	try {
		client_asbd = af.GetClientDataFormat ();
	} catch (CAXException& cax) {
		error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
		return 0.0;
	}

	return client_asbd.mSampleRate;
}

int
CoreAudioSource::update_header (framepos_t, struct tm&, time_t)
{
	return 0;
}

int
CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&)
{
#ifdef COREAUDIO105
	FSRef ref;
#endif
	ExtAudioFileRef af = 0;
	UInt32 size;
	CFStringRef name;
	int ret = -1;

#ifdef COREAUDIO105
	if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
		goto out;
	}

	if (ExtAudioFileOpen(&ref, &af) != noErr) {
		goto out;
	}
#else
	CFURLRef url = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8*)path.c_str (), strlen (path.c_str ()), false);
	OSStatus res = ExtAudioFileOpenURL(url, &af);
	if (url) CFRelease (url);

	if (res != noErr) {
		goto out;
	}
#endif

	AudioStreamBasicDescription absd;
	memset(&absd, 0, sizeof(absd));
	size = sizeof(AudioStreamBasicDescription);
	if (ExtAudioFileGetProperty (af, kExtAudioFileProperty_FileDataFormat, &size, &absd) != noErr) {
		goto out;
	}

	_info.samplerate = absd.mSampleRate;
	_info.channels   = absd.mChannelsPerFrame;

	size = sizeof(_info.length);
	if (ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length) != noErr) {
		goto out;
	}

	size = sizeof(CFStringRef);
	if (AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name) != noErr) {
		goto out;
	}

	_info.format_name = "";

	if (absd.mFormatID == kAudioFormatLinearPCM) {
		if (absd.mFormatFlags & kAudioFormatFlagIsBigEndian) {
			_info.format_name += "big-endian";
		} else {
			_info.format_name += "little-endian";
		}

		char buf[32];
		snprintf (buf, sizeof (buf), " %" PRIu32 " bit", absd.mBitsPerChannel);
		_info.format_name += buf;
		_info.format_name += '\n';

		if (absd.mFormatFlags & kAudioFormatFlagIsFloat) {
			_info.format_name += "float";
		} else {
			if (absd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
				_info.format_name += "signed";
			} else {
				_info.format_name += "unsigned";
			}
			/* integer is typical, do not show it */
		}

		if (_info.channels > 1) {
			if (absd.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
				_info.format_name += " noninterleaved";
			}
			/* interleaved is the normal case, do not show it */
		}

		_info.format_name += ' ';
	}

	switch (absd.mFormatID) {
		case kAudioFormatLinearPCM:
			_info.format_name += "PCM";
			break;

		case kAudioFormatAC3:
			_info.format_name += "AC3";
			break;

		case kAudioFormat60958AC3:
			_info.format_name += "60958 AC3";
			break;

		case kAudioFormatMPEGLayer1:
			_info.format_name += "MPEG-1";
			break;

		case kAudioFormatMPEGLayer2:
			_info.format_name += "MPEG-2";
			break;

		case kAudioFormatMPEGLayer3:
			_info.format_name += "MPEG-3";
			break;

		case kAudioFormatAppleIMA4:
			_info.format_name += "IMA-4";
			break;

		case kAudioFormatMPEG4AAC:
			_info.format_name += "AAC";
			break;

		case kAudioFormatMPEG4CELP:
			_info.format_name += "CELP";
			break;

		case kAudioFormatMPEG4HVXC:
			_info.format_name += "HVXC";
			break;

		case kAudioFormatMPEG4TwinVQ:
			_info.format_name += "TwinVQ";
			break;

			/* these really shouldn't show up, but we should do something
			   somewhere else to make sure that doesn't happen. until
			   that is guaranteed, print something anyway.
			   */

		case kAudioFormatTimeCode:
			_info.format_name += "timecode";
			break;

		case kAudioFormatMIDIStream:
			_info.format_name += "MIDI";
			break;

		case kAudioFormatParameterValueStream:
			_info.format_name += "parameter values";
			break;
	}

	// XXX it would be nice to find a way to get this information if it exists

	_info.timecode = 0;
	ret = 0;

out:
	ExtAudioFileDispose (af);
	return ret;

}

void
CoreAudioSource::set_path (const string& p)
{
        FileSource::set_path (p);
}