2 Copyright (C) 2006 Paul Davis
3 Written by Taybin Rutkin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define __STDC_FORMAT_MACROS
25 #include "pbd/error.h"
26 #include "ardour/coreaudiosource.h"
27 #include "ardour/utils.h"
29 #include <appleutility/CAAudioFile.h>
30 #include <appleutility/CAStreamBasicDescription.h>
34 #include <AudioToolbox/AudioFormat.h>
37 using namespace ARDOUR
;
40 CoreAudioSource::CoreAudioSource (Session
& s
, const XMLNode
& node
)
42 , AudioFileSource (s
, node
)
47 CoreAudioSource::CoreAudioSource (Session
& s
, const string
& path
, int chn
, Flag flags
)
48 /* files created this way are never writable or removable */
49 : Source (s
, DataType::AUDIO
, path
, Source::Flag (flags
& ~(Writable
|Removable
|RemovableIfEmpty
|RemoveAtDestroy
))),
50 AudioFileSource (s
, path
,
51 Source::Flag (flags
& ~(Writable
|Removable
|RemovableIfEmpty
|RemoveAtDestroy
)))
58 CoreAudioSource::init_cafile ()
60 /* note that we temporarily truncated _id at the colon */
62 af
.Open(_path
.c_str());
64 CAStreamBasicDescription
file_format (af
.GetFileDataFormat());
65 n_channels
= file_format
.NumberChannels();
67 if (_channel
>= n_channels
) {
68 error
<< string_compose("CoreAudioSource: file only contains %1 channels; %2 is invalid as a channel number (%3)", n_channels
, _channel
, name()) << endmsg
;
69 throw failed_constructor();
72 _length
= af
.GetNumberFrames();
74 CAStreamBasicDescription
client_format (file_format
);
76 /* set canonial form (PCM, native float packed, 32 bit, with the correct number of channels
77 and interleaved (since we plan to deinterleave ourselves)
80 client_format
.SetCanonical(client_format
.NumberChannels(), true);
81 af
.SetClientFormat (client_format
);
83 } catch (CAXException
& cax
) {
85 error
<< string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
86 _path
, (writable() ? "read+write" : "reading")) << endmsg
;
87 throw failed_constructor ();
91 CoreAudioSource::~CoreAudioSource ()
96 CoreAudioSource::safe_read (Sample
* dst
, nframes_t start
, nframes_t cnt
, AudioBufferList
& abl
) const
100 while (nread
< cnt
) {
103 af
.Seek (start
+nread
);
104 } catch (CAXException
& cax
) {
105 error
<< string_compose("CoreAudioSource: %1 to %2 (%3)", cax
.mOperation
, start
+nread
, _name
.substr (1)) << endmsg
;
109 UInt32 new_cnt
= cnt
- nread
;
111 abl
.mBuffers
[0].mDataByteSize
= new_cnt
* n_channels
* sizeof(Sample
);
112 abl
.mBuffers
[0].mData
= dst
+ nread
;
115 af
.Read (new_cnt
, &abl
);
116 } catch (CAXException
& cax
) {
117 error
<< string_compose("CoreAudioSource: %1 (%2)", cax
.mOperation
, _name
);
123 if (start
+cnt
== _length
) {
124 /* we really did hit the end */
142 CoreAudioSource::read_unlocked (Sample
*dst
, sframes_t start
, nframes_t cnt
) const
147 abl
.mNumberBuffers
= 1;
148 abl
.mBuffers
[0].mNumberChannels
= n_channels
;
150 if (start
> _length
) {
152 /* read starts beyond end of data, just memset to zero */
156 } else if (start
+ cnt
> _length
) {
158 /* read ends beyond end of data, read some, memset the rest */
160 file_cnt
= _length
- start
;
164 /* read is entirely within data */
169 if (file_cnt
!= cnt
) {
170 nframes_t delta
= cnt
- file_cnt
;
171 memset (dst
+file_cnt
, 0, sizeof (Sample
) * delta
);
176 if (n_channels
== 1) {
177 if (safe_read (dst
, start
, file_cnt
, abl
) == 0) {
178 _read_data_count
= cnt
* sizeof (Sample
);
185 Sample
* interleave_buf
= get_interleave_buffer (file_cnt
* n_channels
);
187 if (safe_read (interleave_buf
, start
, file_cnt
, abl
) != 0) {
191 _read_data_count
= cnt
* sizeof(float);
193 Sample
*ptr
= interleave_buf
+ _channel
;
195 /* stride through the interleaved data */
197 for (uint32_t n
= 0; n
< file_cnt
; ++n
) {
206 CoreAudioSource::sample_rate() const
208 CAStreamBasicDescription client_asbd
;
211 client_asbd
= af
.GetClientDataFormat ();
212 } catch (CAXException
& cax
) {
213 error
<< string_compose("CoreAudioSource: %1 (%2)", cax
.mOperation
, _name
);
217 return client_asbd
.mSampleRate
;
221 CoreAudioSource::update_header (sframes_t when
, struct tm
&, time_t)
227 CoreAudioSource::get_soundfile_info (string path
, SoundFileInfo
& _info
, string
& error_msg
)
230 ExtAudioFileRef af
= 0;
235 if (FSPathMakeRef ((UInt8
*)path
.c_str(), &ref
, 0) != noErr
) {
239 if (ExtAudioFileOpen(&ref
, &af
) != noErr
) {
243 AudioStreamBasicDescription absd
;
244 memset(&absd
, 0, sizeof(absd
));
245 size
= sizeof(AudioStreamBasicDescription
);
246 if (ExtAudioFileGetProperty (af
, kExtAudioFileProperty_FileDataFormat
, &size
, &absd
) != noErr
) {
250 _info
.samplerate
= absd
.mSampleRate
;
251 _info
.channels
= absd
.mChannelsPerFrame
;
253 size
= sizeof(_info
.length
);
254 if (ExtAudioFileGetProperty(af
, kExtAudioFileProperty_FileLengthFrames
, &size
, &_info
.length
) != noErr
) {
258 size
= sizeof(CFStringRef
);
259 if (AudioFormatGetProperty(kAudioFormatProperty_FormatName
, sizeof(absd
), &absd
, &size
, &name
) != noErr
) {
263 _info
.format_name
= "";
265 if (absd
.mFormatID
== kAudioFormatLinearPCM
) {
266 if (absd
.mFormatFlags
& kAudioFormatFlagIsBigEndian
) {
267 _info
.format_name
+= "big-endian";
269 _info
.format_name
+= "little-endian";
273 snprintf (buf
, sizeof (buf
), " %" PRIu32
" bit", absd
.mBitsPerChannel
);
274 _info
.format_name
+= buf
;
275 _info
.format_name
+= '\n';
277 if (absd
.mFormatFlags
& kAudioFormatFlagIsFloat
) {
278 _info
.format_name
+= "float";
280 if (absd
.mFormatFlags
& kAudioFormatFlagIsSignedInteger
) {
281 _info
.format_name
+= "signed";
283 _info
.format_name
+= "unsigned";
285 /* integer is typical, do not show it */
288 if (_info
.channels
> 1) {
289 if (absd
.mFormatFlags
& kAudioFormatFlagIsNonInterleaved
) {
290 _info
.format_name
+= " noninterleaved";
292 /* interleaved is the normal case, do not show it */
295 _info
.format_name
+= ' ';
298 switch (absd
.mFormatID
) {
299 case kAudioFormatLinearPCM
:
300 _info
.format_name
+= "PCM";
303 case kAudioFormatAC3
:
304 _info
.format_name
+= "AC3";
307 case kAudioFormat60958AC3
:
308 _info
.format_name
+= "60958 AC3";
311 case kAudioFormatMPEGLayer1
:
312 _info
.format_name
+= "MPEG-1";
315 case kAudioFormatMPEGLayer2
:
316 _info
.format_name
+= "MPEG-2";
319 case kAudioFormatMPEGLayer3
:
320 _info
.format_name
+= "MPEG-3";
323 case kAudioFormatAppleIMA4
:
324 _info
.format_name
+= "IMA-4";
327 case kAudioFormatMPEG4AAC
:
328 _info
.format_name
+= "AAC";
331 case kAudioFormatMPEG4CELP
:
332 _info
.format_name
+= "CELP";
335 case kAudioFormatMPEG4HVXC
:
336 _info
.format_name
+= "HVXC";
339 case kAudioFormatMPEG4TwinVQ
:
340 _info
.format_name
+= "TwinVQ";
343 /* these really shouldn't show up, but we should do something
344 somewhere else to make sure that doesn't happen. until
345 that is guaranteed, print something anyway.
348 case kAudioFormatTimeCode
:
349 _info
.format_name
+= "timecode";
352 case kAudioFormatMIDIStream
:
353 _info
.format_name
+= "MIDI";
356 case kAudioFormatParameterValueStream
:
357 _info
.format_name
+= "parameter values";
361 // XXX it would be nice to find a way to get this information if it exists
367 ExtAudioFileDispose (af
);
373 CoreAudioSource::set_path (const string
& p
)
375 FileSource::set_path (p
);