Use sys::basename instead of PBD::basename_nosuffix in Session::import_audiofile
[ardour.git] / libs / ardour / coreaudiosource.cc
index c8cbb7a40db9cb2c283100d3946b59b47b15e2cd..703225e502eaa953eee5c5d40a9afe9f828c1796 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <pbd/error.h>
 #include <ardour/coreaudiosource.h>
+#include <ardour/utils.h>
 
 #include <appleutility/CAAudioFile.h>
 #include <appleutility/CAStreamBasicDescription.h>
@@ -33,33 +34,23 @@ using namespace PBD;
 CoreAudioSource::CoreAudioSource (Session& s, const XMLNode& node)
        : AudioFileSource (s, node)
 {
-       init (_name);
+       init ();
 }
 
-CoreAudioSource::CoreAudioSource (Session& s, const string& idstr, Flag flags)
-       : AudioFileSource(s, idstr, flags)
+CoreAudioSource::CoreAudioSource (Session& s, const string& path, int chn, Flag flags)
+       /* files created this way are never writable or removable */
+       : AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
 {
-       init (idstr);
+       _channel = chn;
+       init ();
 }
 
 void 
-CoreAudioSource::init (const string& idstr)
+CoreAudioSource::init ()
 {
-       string::size_type pos;
-
        tmpbuf = 0;
        tmpbufsize = 0;
 
-       _name = idstr;
-
-       if ((pos = idstr.find_last_of (':')) == string::npos) {
-               channel = 0;
-               _path = idstr;
-       } else {
-               channel = atoi (idstr.substr (pos+1).c_str());
-               _path = idstr.substr (0, pos);
-       }
-
        cerr << "CoreAudioSource::init() " << name() << endl;
        
        /* note that we temporarily truncated _id at the colon */
@@ -70,8 +61,8 @@ CoreAudioSource::init (const string& idstr)
                n_channels = file_asbd.NumberChannels();
                cerr << "number of channels: " << n_channels << endl;
                
-               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;
+               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();
                }
 
@@ -84,10 +75,6 @@ CoreAudioSource::init (const string& idstr)
                error << string_compose ("CoreAudioSource: %1 (%2)", cax.mOperation, name()) << endmsg;
                throw failed_constructor ();
        }
-       
-       if (_build_peakfiles) {
-               _need_peakfile = true;
-       }
 }
 
 CoreAudioSource::~CoreAudioSource ()
@@ -146,14 +133,14 @@ CoreAudioSource::read_unlocked (Sample *dst, nframes_t start, nframes_t cnt) con
                abl.mBuffers[0].mDataByteSize = tmpbufsize * sizeof(Sample);
                abl.mBuffers[0].mData = tmpbuf;
 
-               cerr << "channel: " << channel << endl;
+               cerr << "channel: " << _channel << endl;
                
                try {
                        af.Read (real_cnt, &abl);
                } catch (CAXException& cax) {
                        error << string_compose("CoreAudioSource: %1 (%2)", cax.mOperation, _name);
                }
-               float *ptr = tmpbuf + channel;
+               float *ptr = tmpbuf + _channel;
                real_cnt /= n_channels;
                
                /* stride through the interleaved data */
@@ -189,3 +176,53 @@ CoreAudioSource::update_header (nframes_t when, struct tm&, time_t)
 {
        return 0;
 }
+
+int
+CoreAudioSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
+{
+       FSRef ref; 
+       ExtAudioFileRef af = 0;
+       size_t size;
+       CFStringRef name;
+       int ret = -1;
+
+       if (FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0) != noErr) {
+               goto out;
+       }
+       
+       if (ExtAudioFileOpen(&ref, &af) != noErr) {
+               goto out;
+       }
+       
+       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 = CFStringRefToStdString(name);
+
+       // 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;
+       
+}