plugin-pin management.. nearly there
[ardour.git] / libs / ardour / coreaudiosource.cc
index 18b9a82ec6acd1113798dee66aaad905ed86c73a..3bb00f731a1c160f951421ccaf9c4e1fb81d2fcb 100644 (file)
 */
 
 #include <algorithm>
-#define __STDC_FORMAT_MACROS
 #include <inttypes.h>
 
-#include <pbd/error.h>
-#include <ardour/coreaudiosource.h>
-#include <ardour/utils.h>
+#include "pbd/error.h"
+#include "ardour/coreaudiosource.h"
+#include "ardour/utils.h"
 
-#include <appleutility/CAAudioFile.h>
-#include <appleutility/CAStreamBasicDescription.h>
+#ifdef COREAUDIO105
+#include "CAAudioFile.h"
+#else
+#include "CAExtAudioFile.h"
+#endif
+#include "CAStreamBasicDescription.h"
+
+#include <glibmm/fileutils.h>
 
 #include "i18n.h"
 
@@ -37,23 +42,36 @@ 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)
-       : AudioFileSource (s, node)
+       : Source (s, node)
+       , AudioFileSource (s, node)
 {
-       init ();
+       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)
-       /* files created this way are never writable or removable */
-       : AudioFileSource (s, path,
+       : 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 ();
+       init_cafile ();
+
+        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
 }
 
-void 
-CoreAudioSource::init ()
+void
+CoreAudioSource::init_cafile ()
 {
        /* note that we temporarily truncated _id at the colon */
        try {
@@ -61,7 +79,7 @@ CoreAudioSource::init ()
 
                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();
@@ -79,8 +97,8 @@ CoreAudioSource::init ()
                af.SetClientFormat (client_format);
 
        } catch (CAXException& cax) {
-               
-               error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"), 
+
+               error << string_compose(_("CoreAudioSource: cannot open file \"%1\" for %2"),
                                        _path, (writable() ? "read+write" : "reading")) << endmsg;
                throw failed_constructor ();
        }
@@ -88,28 +106,33 @@ CoreAudioSource::init ()
 
 CoreAudioSource::~CoreAudioSource ()
 {
-       GoingAway (); /* EMIT SIGNAL */
+}
+
+void
+CoreAudioSource::close ()
+{
+       af.Close ();
 }
 
 int
-CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBufferList& abl) const
+CoreAudioSource::safe_read (Sample* dst, framepos_t start, framecnt_t cnt, AudioBufferList& abl) const
 {
-       nframes_t nread = 0;
+       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.substr (1)) << endmsg;
+                       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) {
@@ -135,12 +158,12 @@ CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBu
                return 0;
        }
 }
-       
 
-nframes_t
-CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) const
+
+framecnt_t
+CoreAudioSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) const
 {
-       nframes_t file_cnt;
+       framecnt_t file_cnt;
        AudioBufferList abl;
 
        abl.mNumberBuffers = 1;
@@ -149,24 +172,24 @@ CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) con
        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) {
-               nframes_t delta = cnt - file_cnt;
+               frameoffset_t delta = cnt - file_cnt;
                memset (dst+file_cnt, 0, sizeof (Sample) * delta);
        }
 
@@ -174,7 +197,6 @@ CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) con
 
                if (n_channels == 1) {
                        if (safe_read (dst, start, file_cnt, abl) == 0) {
-                               _read_data_count = cnt * sizeof (Sample);
                                return cnt;
                        }
                        return 0;
@@ -182,18 +204,16 @@ CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) con
        }
 
        Sample* interleave_buf = get_interleave_buffer (file_cnt * n_channels);
-       
+
        if (safe_read (interleave_buf, start, file_cnt, abl) != 0) {
                return 0;
        }
 
-       _read_data_count = cnt * sizeof(float);
-
        Sample *ptr = interleave_buf + _channel;
-       
+
        /* stride through the interleaved data */
-       
-       for (uint32_t n = 0; n < file_cnt; ++n) {
+
+       for (framecnt_t n = 0; n < file_cnt; ++n) {
                dst[n] = *ptr;
                ptr += n_channels;
        }
@@ -217,35 +237,47 @@ CoreAudioSource::sample_rate() const
 }
 
 int
-CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
+CoreAudioSource::update_header (framepos_t, struct tm&, time_t)
 {
        return 0;
 }
 
 int
-CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
+CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&)
 {
-       FSRef ref; 
+#ifdef COREAUDIO105
+       FSRef ref;
+#endif
        ExtAudioFileRef af = 0;
-       size_t size;
+       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;
 
@@ -253,7 +285,7 @@ CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&
        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;
@@ -267,12 +299,12 @@ CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&
                } 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 {
@@ -283,87 +315,93 @@ CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&
                        }
                        /* 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;
+               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:
+
+out:
        ExtAudioFileDispose (af);
        return ret;
-       
+
+}
+
+void
+CoreAudioSource::set_path (const string& p)
+{
+        FileSource::set_path (p);
 }