enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / coreaudiosource.cc
index bbb0d8ca8987149ccb6e938b582dd3f4f6903d3b..90ab07b86d5df7ac1f5104b7cfea6b44ef9f8131 100644 (file)
 */
 
 #include <algorithm>
-#define __STDC_FORMAT_MACROS
 #include <inttypes.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 "i18n.h"
+#include <glibmm/fileutils.h>
+
+#include "pbd/i18n.h"
 
 #include <AudioToolbox/AudioFormat.h>
 
@@ -37,21 +42,32 @@ 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)
-       /* files created this way are never writable or removable */
        : 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
@@ -90,20 +106,25 @@ CoreAudioSource::init_cafile ()
 
 CoreAudioSource::~CoreAudioSource ()
 {
-       drop_references ();
+}
+
+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;
                }
 
@@ -139,10 +160,10 @@ CoreAudioSource::safe_read (Sample* dst, nframes_t start, nframes_t cnt, AudioBu
 }
 
 
-nframes_t
-CoreAudioSource::read_unlocked (Sample *dst, sframes_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;
@@ -168,7 +189,7 @@ CoreAudioSource::read_unlocked (Sample *dst, sframes_t start, nframes_t cnt) con
        }
 
        if (file_cnt != cnt) {
-               nframes_t delta = cnt - file_cnt;
+               frameoffset_t delta = cnt - file_cnt;
                memset (dst+file_cnt, 0, sizeof (Sample) * delta);
        }
 
@@ -176,7 +197,6 @@ CoreAudioSource::read_unlocked (Sample *dst, sframes_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;
@@ -189,13 +209,11 @@ CoreAudioSource::read_unlocked (Sample *dst, sframes_t start, nframes_t cnt) con
                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;
        }
@@ -219,20 +237,23 @@ CoreAudioSource::sample_rate() const
 }
 
 int
-CoreAudioSource::update_header (sframes_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&)
 {
+#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;
        }
@@ -240,6 +261,15 @@ CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&
        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));
@@ -297,66 +327,66 @@ CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&
        }
 
        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
@@ -364,8 +394,14 @@ CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string&
        _info.timecode = 0;
        ret = 0;
 
-  out:
+out:
        ExtAudioFileDispose (af);
        return ret;
 
 }
+
+void
+CoreAudioSource::set_path (const string& p)
+{
+        FileSource::set_path (p);
+}