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/shortpath.h>
33 #include <pbd/enumwriter.h>
37 #include <glibmm/miscutils.h>
38 #include <glibmm/fileutils.h>
40 #include <ardour/audiofilesource.h>
41 #include <ardour/sndfile_helpers.h>
42 #include <ardour/sndfilesource.h>
43 #include <ardour/session.h>
44 #include <ardour/source_factory.h>
45 #include <ardour/filename_extensions.h>
47 // if these headers come before sigc++ is included
48 // the parser throws ObjC++ errors. (nil is a keyword)
50 #include <ardour/coreaudiosource.h>
51 #include <AudioToolbox/ExtendedAudioFile.h>
52 #include <AudioToolbox/AudioFormat.h>
53 #endif // HAVE_COREAUDIO
57 using namespace ARDOUR;
61 ustring AudioFileSource::peak_dir = "";
62 ustring AudioFileSource::search_path;
64 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
65 uint64_t AudioFileSource::header_position_offset = 0;
67 /* XXX maybe this too */
68 char AudioFileSource::bwf_serial_number[13] = "000000000000";
70 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags)
71 : AudioSource (s, path), _flags (flags),
74 /* constructor used for existing external to session files. file must exist already */
75 _is_embedded = AudioFileSource::determine_embeddedness (path);
77 if (init (path, true)) {
78 throw failed_constructor ();
83 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
84 : AudioSource (s, path), _flags (flags),
87 /* constructor used for new internal-to-session files. file cannot exist */
90 if (init (path, false)) {
91 throw failed_constructor ();
95 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
96 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
97 /* _channel is set in set_state() or init() */
99 /* constructor used for existing internal-to-session files. file must exist */
101 if (set_state (node)) {
102 throw failed_constructor ();
107 if (init (foo, must_exist)) {
108 throw failed_constructor ();
112 AudioFileSource::~AudioFileSource ()
115 unlink (_path.c_str());
116 unlink (peakpath.c_str());
121 AudioFileSource::determine_embeddedness (ustring path)
123 return (path.find("/") == 0);
127 AudioFileSource::removable () const
129 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
133 AudioFileSource::init (ustring pathstr, bool must_exist)
138 timeline_position = 0;
139 _peaks_built = false;
142 if (!find (pathstr, must_exist, is_new, _channel)) {
143 throw non_existent_source ();
146 if (is_new && must_exist) {
155 AudioFileSource::peak_path (ustring audio_path)
157 return _session.peak_path_from_audio_path (audio_path);
161 AudioFileSource::old_peak_path (ustring audio_path)
163 /* XXX hardly bombproof! fix me */
165 struct stat stat_file;
166 struct stat stat_mount;
168 ustring mp = mountpoint (audio_path);
170 stat (audio_path.c_str(), &stat_file);
171 stat (mp.c_str(), &stat_mount);
175 snprintf (buf, sizeof (buf), "%u-%u-%d", stat_mount.st_ino, stat_file.st_ino, _channel);
177 snprintf (buf, sizeof (buf), "%ld-%ld-%d", stat_mount.st_ino, stat_file.st_ino, _channel);
180 ustring res = peak_dir;
182 res += peakfile_suffix;
188 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
190 #ifdef HAVE_COREAUDIO
191 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
194 #endif // HAVE_COREAUDIO
196 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
204 AudioFileSource::get_state ()
206 XMLNode& root (AudioSource::get_state());
208 root.add_property (X_("flags"), enum_2_string (_flags));
209 snprintf (buf, sizeof (buf), "%u", _channel);
210 root.add_property (X_("channel"), buf);
215 AudioFileSource::set_state (const XMLNode& node)
217 const XMLProperty* prop;
219 if (AudioSource::set_state (node)) {
223 if ((prop = node.property (X_("flags"))) != 0) {
224 _flags = Flag (string_2_enum (prop->value(), _flags));
230 if ((prop = node.property (X_("channel"))) != 0) {
231 _channel = atoi (prop->value().c_str());
236 if ((prop = node.property (X_("name"))) != 0) {
237 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
239 _is_embedded = false;
242 if ((prop = node.property (X_("destructive"))) != 0) {
243 /* old style, from the period when we had DestructiveFileSource */
244 _flags = Flag (_flags | Destructive);
251 AudioFileSource::mark_for_remove ()
253 // This operation is not allowed for sources for destructive tracks or embedded files.
254 // Fortunately mark_for_remove() is never called for embedded files. This function
255 // must be fixed if that ever happens.
256 if (_flags & Destructive) {
260 _flags = Flag (_flags | Removable | RemoveAtDestroy);
264 AudioFileSource::mark_streaming_write_completed ()
270 Glib::Mutex::Lock lm (_lock);
273 PeaksReady (); /* EMIT SIGNAL */
278 AudioFileSource::mark_take (ustring id)
286 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
289 cerr << "tried to move an embedded region to trash" << endl;
299 /* don't move the file across filesystems, just
300 stick it in the `trash_dir_name' directory
301 on whichever filesystem it was already on.
304 newpath = Glib::path_get_dirname (_path);
305 newpath = Glib::path_get_dirname (newpath);
307 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
310 newpath += trash_dir_name;
312 newpath += Glib::path_get_basename (_path);
314 if (access (newpath.c_str(), F_OK) == 0) {
316 /* the new path already exists, try versioning */
318 char buf[PATH_MAX+1];
322 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
325 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
326 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
330 if (version == 999) {
331 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
340 /* it doesn't exist, or we can't read it or something */
344 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
345 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
346 _path, newpath, strerror (errno))
351 if (::unlink (peakpath.c_str()) != 0) {
352 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
353 peakpath, _path, strerror (errno))
355 /* try to back out */
356 rename (newpath.c_str(), _path.c_str());
363 /* file can not be removed twice, since the operation is not idempotent */
365 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
371 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew, uint16_t& chan)
373 ustring::size_type pos;
378 if (pathstr[0] != '/') {
380 /* non-absolute pathname: find pathstr in search path */
382 vector<ustring> dirs;
387 if (search_path.length() == 0) {
388 error << _("FileSource: search path not set") << endmsg;
392 split (search_path, dirs, ':');
396 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
399 if (fullpath[fullpath.length()-1] != '/') {
405 /* i (paul) made a nasty design error by using ':' as a special character in
406 Ardour 0.99 .. this hack tries to make things sort of work.
409 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
411 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
413 /* its a real file, no problem */
422 /* might be an older session using file:channel syntax. see if the version
423 without the :suffix exists
426 ustring shorter = pathstr.substr (0, pos);
429 if (fullpath[fullpath.length()-1] != '/') {
435 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
436 chan = atoi (pathstr.substr (pos+1).c_str());
444 /* new derived file (e.g. for timefx) being created in a newer session */
451 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
460 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
463 } else if (cnt == 0) {
466 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
479 /* external files and/or very very old style sessions include full paths */
481 /* ugh, handle ':' situation */
483 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
485 ustring shorter = pathstr.substr (0, pos);
487 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
488 chan = atoi (pathstr.substr (pos+1).c_str());
498 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
501 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
503 /* file does not exist or we cannot read it */
506 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
510 if (errno != ENOENT) {
511 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
534 AudioFileSource::set_search_path (ustring p)
540 AudioFileSource::set_header_position_offset (nframes_t offset)
542 header_position_offset = offset;
543 HeaderPositionOffsetChanged ();
547 AudioFileSource::handle_header_position_change ()
550 set_header_timeline_position ();
556 AudioFileSource::set_timeline_position (int64_t pos)
558 timeline_position = pos;
562 AudioFileSource::set_allow_remove_if_empty (bool yn)
569 _flags = Flag (_flags | RemovableIfEmpty);
571 _flags = Flag (_flags & ~RemovableIfEmpty);
576 AudioFileSource::set_source_name (ustring newname, bool destructive)
578 Glib::Mutex::Lock lm (_lock);
579 ustring oldpath = _path;
580 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
582 if (newpath.empty()) {
583 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
587 // Test whether newpath exists, if yes notify the user but continue.
588 if (access(newpath.c_str(),F_OK) == 0) {
589 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;
593 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
594 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
598 _name = Glib::path_get_basename (newpath);
601 return rename_peakfile (peak_path (_path));
605 AudioFileSource::is_empty (Session& s, ustring path)
609 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (DataType::AUDIO, s, path, 0, NoPeakFile, false));
612 ret = (afs->length() == 0);
619 AudioFileSource::setup_peakfile ()
621 if (!(_flags & NoPeakFile)) {
622 return initialize_peakfile (file_is_new, _path);
629 AudioFileSource::safe_file_extension(ustring file)
631 return !(file.rfind(".wav") == ustring::npos &&
632 file.rfind(".aiff")== ustring::npos &&
633 file.rfind(".aif") == ustring::npos &&
634 file.rfind(".snd") == ustring::npos &&
635 file.rfind(".au") == ustring::npos &&
636 file.rfind(".raw") == ustring::npos &&
637 file.rfind(".sf") == ustring::npos &&
638 file.rfind(".cdr") == ustring::npos &&
639 file.rfind(".smp") == ustring::npos &&
640 file.rfind(".maud")== ustring::npos &&
641 file.rfind(".vwe") == ustring::npos &&
642 file.rfind(".paf") == ustring::npos &&
643 /* protools convention */
644 file.rfind(".L") == ustring::npos &&
645 file.rfind(".R") == ustring::npos &&
647 file.rfind(".flac")== ustring::npos &&
649 #ifdef HAVE_COREAUDIO
650 file.rfind(".mp3") == ustring::npos &&
651 file.rfind(".aac") == ustring::npos &&
652 file.rfind(".mp4") == ustring::npos &&
653 #endif // HAVE_COREAUDIO
654 file.rfind(".voc") == ustring::npos);
658 AudioFileSource::mark_immutable ()
660 /* destructive sources stay writable, and their other flags don't
664 if (!(_flags & Destructive)) {
665 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));