2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <pbd/mountpoint.h>
28 #include <pbd/pathscanner.h>
29 #include <pbd/stl_delete.h>
30 #include <pbd/strsplit.h>
34 #include <glibmm/miscutils.h>
36 #include <ardour/audiofilesource.h>
37 #include <ardour/sndfile_helpers.h>
38 #include <ardour/sndfilesource.h>
39 #include <ardour/destructive_filesource.h>
40 #include <ardour/session.h>
42 // if these headers come before sigc++ is included
43 // the parser throws ObjC++ errors. (nil is a keyword)
45 #include <ardour/coreaudiosource.h>
46 #include <AudioToolbox/ExtendedAudioFile.h>
47 #include <AudioToolbox/AudioFormat.h>
48 #endif // HAVE_COREAUDIO
52 using namespace ARDOUR;
55 string AudioFileSource::peak_dir = "";
56 string AudioFileSource::search_path;
58 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
59 uint64_t AudioFileSource::header_position_offset = 0;
61 char AudioFileSource::bwf_country_code[3] = "US";
62 char AudioFileSource::bwf_organization_code[4] = "LAS";
63 char AudioFileSource::bwf_serial_number[13] = "000000000000";
65 AudioFileSource::AudioFileSource (string idstr, Flag flags)
66 : AudioSource (idstr), _flags (flags)
68 /* constructor used for existing external to session files. file must exist already */
70 if (init (idstr, true)) {
71 throw failed_constructor ();
76 AudioFileSource::AudioFileSource (std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
77 : AudioSource (path), _flags (flags)
79 /* constructor used for new internal-to-session files. file cannot exist */
81 if (init (path, false)) {
82 throw failed_constructor ();
86 AudioFileSource::AudioFileSource (const XMLNode& node)
87 : AudioSource (node), _flags (Flag (Writable|CanRename))
89 /* constructor used for existing internal-to-session files. file must exist */
91 if (set_state (node)) {
92 throw failed_constructor ();
95 if (init (_name, true)) {
96 throw failed_constructor ();
100 AudioFileSource::~AudioFileSource ()
103 unlink (_path.c_str());
104 unlink (peakpath.c_str());
109 AudioFileSource::removable () const
111 return (_flags & Removable) && ((_flags & RemoveAtDestroy) ||
112 ((_flags & RemovableIfEmpty) && is_empty (_path)));
116 AudioFileSource::init (string pathstr, bool must_exist)
121 next_peak_clear_should_notify = false;
123 if (!find (pathstr, must_exist, is_new)) {
127 if (is_new && must_exist) {
136 AudioFileSource::peak_path (string audio_path)
138 return Session::peak_path_from_audio_path (audio_path);
142 AudioFileSource::old_peak_path (string audio_path)
144 /* XXX hardly bombproof! fix me */
146 struct stat stat_file;
147 struct stat stat_mount;
149 string mp = mountpoint (audio_path);
151 stat (audio_path.c_str(), &stat_file);
152 stat (mp.c_str(), &stat_mount);
156 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
158 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
161 string res = peak_dir;
167 #ifdef HAVE_COREAUDIO
170 AudioFileSource::create (const XMLNode& node)
172 AudioFileSource* es = 0;
174 if (node.property (X_("destructive")) != 0) {
176 es = new DestructiveFileSource (node);
181 es = new CoreAudioSource (node);
185 catch (failed_constructor& err) {
186 es = new SndFileSource (node);
196 AudioFileSource::create (const XMLNode& node)
198 if (node.property (X_("destructive")) != 0) {
200 return new DestructiveFileSource (node);
204 return new SndFileSource (node);
208 #endif // HAVE_COREAUDIO
210 #ifdef HAVE_COREAUDIO
212 AudioFileSource::create (const string& idstr, Flag flags)
214 AudioFileSource* es = 0;
216 if (flags & Destructive) {
217 return new DestructiveFileSource (idstr, flags);
221 es = new CoreAudioSource (idstr, flags);
224 catch (failed_constructor& err) {
225 es = new SndFileSource (idstr, flags);
234 AudioFileSource::create (const string& idstr, Flag flags)
236 return new SndFileSource (idstr, flags);
239 #endif // HAVE_COREAUDIO
241 #ifdef HAVE_COREAUDIO
243 CFStringRefToStdString(CFStringRef stringRef)
246 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
247 kCFStringEncodingASCII);
248 char *buf = new char[size];
252 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingASCII)) {
258 #endif // HAVE_COREAUDIO
261 AudioFileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
263 #ifdef HAVE_COREAUDIO
264 OSStatus err = noErr;
266 ExtAudioFileRef af = 0;
270 err = FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0);
272 ExtAudioFileDispose (af);
276 err = ExtAudioFileOpen(&ref, &af);
278 ExtAudioFileDispose (af);
282 AudioStreamBasicDescription absd;
283 memset(&absd, 0, sizeof(absd));
284 size = sizeof(AudioStreamBasicDescription);
285 err = ExtAudioFileGetProperty(af,
286 kExtAudioFileProperty_FileDataFormat, &size, &absd);
288 ExtAudioFileDispose (af);
292 _info.samplerate = absd.mSampleRate;
293 _info.channels = absd.mChannelsPerFrame;
295 size = sizeof(_info.length);
296 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length);
298 ExtAudioFileDispose (af);
302 size = sizeof(CFStringRef);
303 err = AudioFormatGetProperty(
304 kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name);
306 ExtAudioFileDispose (af);
310 _info.format_name = CFStringRefToStdString(name);
312 ExtAudioFileDispose (af);
316 #endif // HAVE_COREAUDIO
321 sf_info.format = 0; // libsndfile says to clear this before sf_open().
323 if ((sf = sf_open ((char*) path.c_str(), SFM_READ, &sf_info)) == 0) {
325 error_msg = sf_error_str (0, errbuf, sizeof (errbuf) - 1);
331 _info.samplerate = sf_info.samplerate;
332 _info.channels = sf_info.channels;
333 _info.length = sf_info.frames;
334 _info.format_name = string_compose("Format: %1, %2",
335 sndfile_major_format(sf_info.format),
336 sndfile_minor_format(sf_info.format));
341 AudioFileSource::get_state ()
343 XMLNode& root (AudioSource::get_state());
345 snprintf (buf, sizeof (buf), "0x%x", (int)_flags);
346 root.add_property ("flags", buf);
351 AudioFileSource::set_state (const XMLNode& node)
353 const XMLProperty* prop;
355 if (AudioSource::set_state (node)) {
359 if ((prop = node.property (X_("flags"))) != 0) {
362 sscanf (prop->value().c_str(), "0x%x", &ival);
363 _flags = Flag (ival);
375 AudioFileSource::mark_for_remove ()
380 _flags = Flag (_flags | RemoveAtDestroy);
384 AudioFileSource::mark_streaming_write_completed ()
390 Glib::Mutex::Lock lm (_lock);
392 next_peak_clear_should_notify = true;
394 if (_peaks_built || pending_peak_builds.empty()) {
396 PeaksReady (); /* EMIT SIGNAL */
401 AudioFileSource::mark_take (string id)
409 AudioFileSource::move_to_trash (const string trash_dir_name)
417 /* don't move the file across filesystems, just
418 stick it in the `trash_dir_name' directory
419 on whichever filesystem it was already on.
422 newpath = Glib::path_get_dirname (_path);
423 newpath = Glib::path_get_dirname (newpath);
426 newpath += trash_dir_name;
428 newpath += Glib::path_get_basename (_path);
430 if (access (newpath.c_str(), F_OK) == 0) {
432 /* the new path already exists, try versioning */
434 char buf[PATH_MAX+1];
438 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
441 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
442 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
446 if (version == 999) {
447 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
456 /* it doesn't exist, or we can't read it or something */
460 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
461 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
462 _path, newpath, strerror (errno))
467 if (::unlink (peakpath.c_str()) != 0) {
468 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
469 peakpath, _path, strerror (errno))
471 /* try to back out */
472 rename (newpath.c_str(), _path.c_str());
479 /* file can not be removed twice, since the operation is not idempotent */
481 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
487 AudioFileSource::find (string pathstr, bool must_exist, bool& isnew)
489 string::size_type pos;
494 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
496 if ((pos = pathstr.find_last_of (':')) == string::npos) {
499 pathstr = pathstr.substr (0, pos);
502 if (pathstr[0] != '/') {
504 /* non-absolute pathname: find pathstr in search path */
511 if (search_path.length() == 0) {
512 error << _("FileSource: search path not set") << endmsg;
516 split (search_path, dirs, ':');
520 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
523 if (fullpath[fullpath.length()-1] != '/') {
528 if (access (fullpath.c_str(), R_OK) == 0) {
536 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
539 } else if (cnt == 0) {
542 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
555 /* external files and/or very very old style sessions include full paths */
558 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
560 if (access (_path.c_str(), R_OK) != 0) {
562 /* file does not exist or we cannot read it */
565 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
569 if (errno != ENOENT) {
570 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
592 AudioFileSource::set_search_path (string p)
598 AudioFileSource::set_header_position_offset (jack_nframes_t offset)
600 header_position_offset = offset;
601 cerr << "hpo set to " << offset << endl;
602 HeaderPositionOffsetChanged ();
606 AudioFileSource::handle_header_position_change ()
609 set_header_timeline_position ();
615 AudioFileSource::set_timeline_position (jack_nframes_t pos)
617 timeline_position = pos;
621 AudioFileSource::set_allow_remove_if_empty (bool yn)
624 allow_remove_if_empty = yn;
629 AudioFileSource::set_name (string newname, bool destructive)
631 Glib::Mutex::Lock lm (_lock);
632 string oldpath = _path;
633 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
635 if (newpath.empty()) {
636 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
640 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
641 error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
645 _name = Glib::path_get_basename (newpath);
648 return rename_peakfile (peak_path (_path));
652 AudioFileSource::is_empty (string path)
655 AudioFileSource* afs = create (path, NoPeakFile);
658 ret = (afs->length() == 0);