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.
24 #include <stdio.h> // for rename(), sigh
29 #include <pbd/convert.h>
30 #include <pbd/basename.h>
31 #include <pbd/mountpoint.h>
32 #include <pbd/stl_delete.h>
33 #include <pbd/strsplit.h>
34 #include <pbd/shortpath.h>
35 #include <pbd/enumwriter.h>
39 #include <glibmm/miscutils.h>
40 #include <glibmm/fileutils.h>
42 #include <ardour/audiofilesource.h>
43 #include <ardour/sndfile_helpers.h>
44 #include <ardour/sndfilesource.h>
45 #include <ardour/session.h>
46 #include <ardour/session_directory.h>
47 #include <ardour/source_factory.h>
48 #include <ardour/filename_extensions.h>
50 // if these headers come before sigc++ is included
51 // the parser throws ObjC++ errors. (nil is a keyword)
53 #include <ardour/coreaudiosource.h>
54 #include <AudioToolbox/ExtendedAudioFile.h>
55 #include <AudioToolbox/AudioFormat.h>
56 #endif // HAVE_COREAUDIO
60 using namespace ARDOUR;
64 ustring AudioFileSource::peak_dir = "";
65 ustring AudioFileSource::search_path;
67 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
68 uint64_t AudioFileSource::header_position_offset = 0;
70 /* XXX maybe this too */
71 char AudioFileSource::bwf_serial_number[13] = "000000000000";
73 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags)
74 : AudioSource (s, path), _flags (flags),
77 /* constructor used for existing external to session files. file must exist already */
78 _is_embedded = AudioFileSource::determine_embeddedness (path);
80 if (init (path, true)) {
81 throw failed_constructor ();
86 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
87 : AudioSource (s, path), _flags (flags),
90 /* constructor used for new internal-to-session files. file cannot exist */
93 if (init (path, false)) {
94 throw failed_constructor ();
98 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
99 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
100 /* _channel is set in set_state() or init() */
102 /* constructor used for existing internal-to-session files. file must exist */
104 if (set_state (node)) {
105 throw failed_constructor ();
110 if (init (foo, must_exist)) {
111 throw failed_constructor ();
115 AudioFileSource::~AudioFileSource ()
118 unlink (_path.c_str());
119 unlink (peakpath.c_str());
124 AudioFileSource::determine_embeddedness (ustring path)
126 return (path.find("/") == 0);
130 AudioFileSource::removable () const
132 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
136 AudioFileSource::init (ustring pathstr, bool must_exist)
141 timeline_position = 0;
142 _peaks_built = false;
145 if (!find (pathstr, must_exist, is_new, _channel)) {
146 throw non_existent_source ();
149 if (is_new && must_exist) {
158 AudioFileSource::peak_path (ustring audio_path)
162 base = PBD::basename_nosuffix (audio_path);
164 base += (char) ('A' + _channel);
166 return _session.peak_path (base);
170 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
174 /* check for the broken location in use by 2.0 for several months */
176 str = broken_peak_path (audio_path);
178 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
182 /* it would be nice to rename it but the nature of
183 the bug means that we can't reliably use it.
189 /* all native files are mono, so we can just rename
192 ::rename (str.c_str(), peak_path.c_str());
196 /* Nasty band-aid for older sessions that were created before we
197 used libsndfile for all audio files.
201 str = old_peak_path (audio_path);
202 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
211 AudioFileSource::broken_peak_path (ustring audio_path)
213 return _session.peak_path (audio_path);
217 AudioFileSource::old_peak_path (ustring audio_path)
219 /* XXX hardly bombproof! fix me */
221 struct stat stat_file;
222 struct stat stat_mount;
224 ustring mp = mountpoint (audio_path);
226 stat (audio_path.c_str(), &stat_file);
227 stat (mp.c_str(), &stat_mount);
231 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
233 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
236 ustring res = peak_dir;
238 res += peakfile_suffix;
244 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
246 #ifdef HAVE_COREAUDIO
247 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
250 #endif // HAVE_COREAUDIO
252 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
260 AudioFileSource::get_state ()
262 XMLNode& root (AudioSource::get_state());
264 root.add_property (X_("flags"), enum_2_string (_flags));
265 snprintf (buf, sizeof (buf), "%u", _channel);
266 root.add_property (X_("channel"), buf);
271 AudioFileSource::set_state (const XMLNode& node)
273 const XMLProperty* prop;
275 if (AudioSource::set_state (node)) {
279 if ((prop = node.property (X_("flags"))) != 0) {
280 _flags = Flag (string_2_enum (prop->value(), _flags));
286 if ((prop = node.property (X_("channel"))) != 0) {
287 _channel = atoi (prop->value());
292 if ((prop = node.property (X_("name"))) != 0) {
293 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
295 _is_embedded = false;
298 if ((prop = node.property (X_("destructive"))) != 0) {
299 /* old style, from the period when we had DestructiveFileSource */
300 _flags = Flag (_flags | Destructive);
307 AudioFileSource::mark_for_remove ()
309 // This operation is not allowed for sources for destructive tracks or embedded files.
310 // Fortunately mark_for_remove() is never called for embedded files. This function
311 // must be fixed if that ever happens.
312 if (_flags & Destructive) {
316 _flags = Flag (_flags | Removable | RemoveAtDestroy);
320 AudioFileSource::mark_streaming_write_completed ()
326 /* XXX notice that we're readers of _peaks_built
327 but we must hold a solid lock on PeaksReady.
330 Glib::Mutex::Lock lm (_lock);
333 PeaksReady (); /* EMIT SIGNAL */
338 AudioFileSource::mark_take (ustring id)
346 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
349 cerr << "tried to move an embedded region to trash" << endl;
359 /* don't move the file across filesystems, just
360 stick it in the `trash_dir_name' directory
361 on whichever filesystem it was already on.
364 newpath = Glib::path_get_dirname (_path);
365 newpath = Glib::path_get_dirname (newpath);
367 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
370 newpath += trash_dir_name;
372 newpath += Glib::path_get_basename (_path);
374 if (access (newpath.c_str(), F_OK) == 0) {
376 /* the new path already exists, try versioning */
378 char buf[PATH_MAX+1];
382 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
385 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
386 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
390 if (version == 999) {
391 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
400 /* it doesn't exist, or we can't read it or something */
404 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
405 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
406 _path, newpath, strerror (errno))
411 if (::unlink (peakpath.c_str()) != 0) {
412 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
413 peakpath, _path, strerror (errno))
415 /* try to back out */
416 rename (newpath.c_str(), _path.c_str());
423 /* file can not be removed twice, since the operation is not idempotent */
425 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
431 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew, uint16_t& chan)
433 ustring::size_type pos;
438 if (pathstr[0] != '/') {
440 /* non-absolute pathname: find pathstr in search path */
442 vector<ustring> dirs;
447 if (search_path.length() == 0) {
448 error << _("FileSource: search path not set") << endmsg;
452 split (search_path, dirs, ':');
456 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
459 if (fullpath[fullpath.length()-1] != '/') {
465 /* i (paul) made a nasty design error by using ':' as a special character in
466 Ardour 0.99 .. this hack tries to make things sort of work.
469 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
471 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
473 /* its a real file, no problem */
482 /* might be an older session using file:channel syntax. see if the version
483 without the :suffix exists
486 ustring shorter = pathstr.substr (0, pos);
489 if (fullpath[fullpath.length()-1] != '/') {
495 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
496 chan = atoi (pathstr.substr (pos+1));
504 /* new derived file (e.g. for timefx) being created in a newer session */
511 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
520 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
523 } else if (cnt == 0) {
526 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
539 /* external files and/or very very old style sessions include full paths */
541 /* ugh, handle ':' situation */
543 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
545 ustring shorter = pathstr.substr (0, pos);
547 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
548 chan = atoi (pathstr.substr (pos+1));
558 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
561 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
563 /* file does not exist or we cannot read it */
566 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
570 if (errno != ENOENT) {
571 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
594 AudioFileSource::set_search_path (ustring p)
600 AudioFileSource::set_header_position_offset (nframes_t offset)
602 header_position_offset = offset;
603 HeaderPositionOffsetChanged ();
607 AudioFileSource::set_timeline_position (int64_t pos)
609 timeline_position = pos;
613 AudioFileSource::set_allow_remove_if_empty (bool yn)
620 _flags = Flag (_flags | RemovableIfEmpty);
622 _flags = Flag (_flags & ~RemovableIfEmpty);
627 AudioFileSource::set_source_name (ustring newname, bool destructive)
629 Glib::Mutex::Lock lm (_lock);
630 ustring oldpath = _path;
631 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
633 if (newpath.empty()) {
634 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
638 // Test whether newpath exists, if yes notify the user but continue.
639 if (access(newpath.c_str(),F_OK) == 0) {
640 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;
644 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
645 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
649 _name = Glib::path_get_basename (newpath);
652 return rename_peakfile (peak_path (_path));
656 AudioFileSource::is_empty (Session& s, ustring path)
661 if (!get_soundfile_info (path, info, err)) {
662 /* dangerous: we can't get info, so assume that its not empty */
666 return info.length == 0;
670 AudioFileSource::setup_peakfile ()
672 if (!(_flags & NoPeakFile)) {
673 return initialize_peakfile (file_is_new, _path);
680 AudioFileSource::safe_file_extension(ustring file)
682 return !(file.rfind(".wav") == ustring::npos &&
683 file.rfind(".aiff")== ustring::npos &&
684 file.rfind(".aif") == ustring::npos &&
685 file.rfind(".snd") == ustring::npos &&
686 file.rfind(".au") == ustring::npos &&
687 file.rfind(".raw") == ustring::npos &&
688 file.rfind(".sf") == ustring::npos &&
689 file.rfind(".cdr") == ustring::npos &&
690 file.rfind(".smp") == ustring::npos &&
691 file.rfind(".maud")== ustring::npos &&
692 file.rfind(".vwe") == ustring::npos &&
693 file.rfind(".paf") == ustring::npos &&
694 /* protools convention */
695 file.rfind(".L") == ustring::npos &&
696 file.rfind(".R") == ustring::npos &&
698 file.rfind(".flac")== ustring::npos &&
700 #ifdef HAVE_COREAUDIO
701 file.rfind(".mp3") == ustring::npos &&
702 file.rfind(".aac") == ustring::npos &&
703 file.rfind(".mp4") == ustring::npos &&
704 #endif // HAVE_COREAUDIO
705 file.rfind(".voc") == ustring::npos);
709 AudioFileSource::mark_immutable ()
711 /* destructive sources stay writable, and their other flags don't
715 if (!(_flags & Destructive)) {
716 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));