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.
28 #include <pbd/mountpoint.h>
29 #include <pbd/pathscanner.h>
30 #include <pbd/stl_delete.h>
31 #include <pbd/strsplit.h>
32 #include <pbd/enumwriter.h>
36 #include <glibmm/miscutils.h>
38 #include <ardour/audiofilesource.h>
39 #include <ardour/sndfile_helpers.h>
40 #include <ardour/sndfilesource.h>
41 #include <ardour/session.h>
42 #include <ardour/source_factory.h>
44 // if these headers come before sigc++ is included
45 // the parser throws ObjC++ errors. (nil is a keyword)
47 #include <ardour/coreaudiosource.h>
48 #include <AudioToolbox/ExtendedAudioFile.h>
49 #include <AudioToolbox/AudioFormat.h>
50 #endif // HAVE_COREAUDIO
54 using namespace ARDOUR;
58 ustring AudioFileSource::peak_dir = "";
59 ustring AudioFileSource::search_path;
61 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
62 uint64_t AudioFileSource::header_position_offset = 0;
64 /* XXX maybe this too */
65 char AudioFileSource::bwf_serial_number[13] = "000000000000";
67 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags)
68 : AudioSource (s, path), _flags (flags),
71 /* constructor used for existing external to session files. file must exist already */
72 _is_embedded = AudioFileSource::determine_embeddedness (path);
74 if (init (path, true)) {
75 throw failed_constructor ();
80 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
81 : AudioSource (s, path), _flags (flags),
84 /* constructor used for new internal-to-session files. file cannot exist */
87 if (init (path, false)) {
88 throw failed_constructor ();
92 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
93 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
94 /* _channel is set in set_state() */
96 /* constructor used for existing internal-to-session files. file must exist */
98 if (set_state (node)) {
99 throw failed_constructor ();
102 if (init (_name, must_exist)) {
103 throw failed_constructor ();
107 AudioFileSource::~AudioFileSource ()
110 unlink (_path.c_str());
111 unlink (peakpath.c_str());
116 AudioFileSource::determine_embeddedness (ustring path)
118 return (path.find("/") == 0);
122 AudioFileSource::removable () const
124 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
128 AudioFileSource::init (ustring pathstr, bool must_exist)
133 timeline_position = 0;
134 _peaks_built = false;
137 if (!find (pathstr, must_exist, is_new)) {
138 throw non_existent_source ();
141 if (is_new && must_exist) {
150 AudioFileSource::peak_path (ustring audio_path)
152 return _session.peak_path_from_audio_path (audio_path);
156 AudioFileSource::old_peak_path (ustring audio_path)
158 /* XXX hardly bombproof! fix me */
160 struct stat stat_file;
161 struct stat stat_mount;
163 ustring mp = mountpoint (audio_path);
165 stat (audio_path.c_str(), &stat_file);
166 stat (mp.c_str(), &stat_mount);
170 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
172 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
175 ustring res = peak_dir;
182 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
184 #ifdef HAVE_COREAUDIO
185 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
188 #endif // HAVE_COREAUDIO
190 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
198 AudioFileSource::get_state ()
200 XMLNode& root (AudioSource::get_state());
202 root.add_property (X_("flags"), enum_2_string (_flags));
203 snprintf (buf, sizeof (buf), "%u", _channel);
204 root.add_property (X_("channel"), buf);
209 AudioFileSource::set_state (const XMLNode& node)
211 const XMLProperty* prop;
213 if (AudioSource::set_state (node)) {
217 if ((prop = node.property (X_("flags"))) != 0) {
218 _flags = Flag (string_2_enum (prop->value(), _flags));
224 if ((prop = node.property (X_("channel"))) != 0) {
225 _channel = atoi (prop->value());
230 if ((prop = node.property (X_("name"))) != 0) {
231 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
233 _is_embedded = false;
236 if ((prop = node.property (X_("destructive"))) != 0) {
237 /* old style, from the period when we had DestructiveFileSource */
238 _flags = Flag (_flags | Destructive);
245 AudioFileSource::mark_for_remove ()
251 _flags = Flag (_flags | Removable | RemoveAtDestroy);
255 AudioFileSource::mark_streaming_write_completed ()
261 Glib::Mutex::Lock lm (_lock);
264 PeaksReady (); /* EMIT SIGNAL */
269 AudioFileSource::mark_take (ustring id)
277 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
280 cerr << "tried to move an embedded region to trash" << endl;
290 /* don't move the file across filesystems, just
291 stick it in the `trash_dir_name' directory
292 on whichever filesystem it was already on.
295 newpath = Glib::path_get_dirname (_path);
296 newpath = Glib::path_get_dirname (newpath);
298 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
301 newpath += trash_dir_name;
303 newpath += Glib::path_get_basename (_path);
305 if (access (newpath.c_str(), F_OK) == 0) {
307 /* the new path already exists, try versioning */
309 char buf[PATH_MAX+1];
313 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
316 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
317 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
321 if (version == 999) {
322 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
331 /* it doesn't exist, or we can't read it or something */
335 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
336 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
337 _path, newpath, strerror (errno))
342 if (::unlink (peakpath.c_str()) != 0) {
343 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
344 peakpath, _path, strerror (errno))
346 /* try to back out */
347 rename (newpath.c_str(), _path.c_str());
354 /* file can not be removed twice, since the operation is not idempotent */
356 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
362 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew)
364 ustring::size_type pos;
369 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
371 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
372 pathstr = pathstr.substr (0, pos);
375 if (pathstr[0] != '/') {
377 /* non-absolute pathname: find pathstr in search path */
379 vector<ustring> dirs;
384 if (search_path.length() == 0) {
385 error << _("FileSource: search path not set") << endmsg;
389 split (search_path, dirs, ':');
393 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
396 if (fullpath[fullpath.length()-1] != '/') {
401 if (access (fullpath.c_str(), R_OK) == 0) {
409 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
412 } else if (cnt == 0) {
415 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
428 /* external files and/or very very old style sessions include full paths */
434 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
437 if (access (_path.c_str(), R_OK) != 0) {
439 /* file does not exist or we cannot read it */
442 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
446 if (errno != ENOENT) {
447 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
469 AudioFileSource::set_search_path (ustring p)
475 AudioFileSource::set_header_position_offset (nframes_t offset)
477 header_position_offset = offset;
478 HeaderPositionOffsetChanged ();
482 AudioFileSource::set_timeline_position (int64_t pos)
484 timeline_position = pos;
488 AudioFileSource::set_allow_remove_if_empty (bool yn)
495 _flags = Flag (_flags | RemovableIfEmpty);
497 _flags = Flag (_flags & ~RemovableIfEmpty);
502 AudioFileSource::set_name (ustring newname, bool destructive)
504 Glib::Mutex::Lock lm (_lock);
505 ustring oldpath = _path;
506 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
508 if (newpath.empty()) {
509 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
513 // Test whether newpath exists, if yes notify the user but continue.
514 if (access(newpath.c_str(),F_OK) == 0) {
515 error << _("Programming error! Ardour tried to rename a file over another file! It's safe to continue working, but please report this to the developers.") << endmsg;
519 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
520 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
524 _name = Glib::path_get_basename (newpath);
527 return rename_peakfile (peak_path (_path));
531 AudioFileSource::is_empty (Session& s, ustring path)
534 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (s, path, 0, NoPeakFile, false));
537 ret = (afs->length() == 0);
544 AudioFileSource::setup_peakfile ()
546 if (!(_flags & NoPeakFile)) {
547 return initialize_peakfile (file_is_new, _path);
554 AudioFileSource::safe_file_extension(ustring file)
556 return !(file.rfind(".wav") == ustring::npos &&
557 file.rfind(".aiff")== ustring::npos &&
558 file.rfind(".aif") == ustring::npos &&
559 file.rfind(".snd") == ustring::npos &&
560 file.rfind(".au") == ustring::npos &&
561 file.rfind(".raw") == ustring::npos &&
562 file.rfind(".sf") == ustring::npos &&
563 file.rfind(".cdr") == ustring::npos &&
564 file.rfind(".smp") == ustring::npos &&
565 file.rfind(".maud")== ustring::npos &&
566 file.rfind(".vwe") == ustring::npos &&
567 file.rfind(".paf") == ustring::npos &&
569 file.rfind(".flac")== ustring::npos &&
571 #ifdef HAVE_COREAUDIO
572 file.rfind(".mp3") == ustring::npos &&
573 file.rfind(".aac") == ustring::npos &&
574 file.rfind(".mp4") == ustring::npos &&
575 #endif // HAVE_COREAUDIO
576 file.rfind(".voc") == ustring::npos);
580 AudioFileSource::mark_immutable ()
582 /* destructive sources stay writable, and their other flags don't
586 if (!(_flags & Destructive)) {
587 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));