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/stl_delete.h>
30 #include <pbd/strsplit.h>
31 #include <pbd/shortpath.h>
32 #include <pbd/enumwriter.h>
36 #include <glibmm/miscutils.h>
37 #include <glibmm/fileutils.h>
39 #include <ardour/audiofilesource.h>
40 #include <ardour/sndfile_helpers.h>
41 #include <ardour/sndfilesource.h>
42 #include <ardour/session.h>
43 #include <ardour/source_factory.h>
44 #include <ardour/filename_extensions.h>
46 // if these headers come before sigc++ is included
47 // the parser throws ObjC++ errors. (nil is a keyword)
49 #include <ardour/coreaudiosource.h>
50 #include <AudioToolbox/ExtendedAudioFile.h>
51 #include <AudioToolbox/AudioFormat.h>
52 #endif // HAVE_COREAUDIO
56 using namespace ARDOUR;
60 ustring AudioFileSource::peak_dir = "";
61 ustring AudioFileSource::search_path;
63 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
64 uint64_t AudioFileSource::header_position_offset = 0;
66 /* XXX maybe this too */
67 char AudioFileSource::bwf_serial_number[13] = "000000000000";
69 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags)
70 : AudioSource (s, path), _flags (flags),
73 /* constructor used for existing external to session files. file must exist already */
74 _is_embedded = AudioFileSource::determine_embeddedness (path);
76 if (init (path, true)) {
77 throw failed_constructor ();
82 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
83 : AudioSource (s, path), _flags (flags),
86 /* constructor used for new internal-to-session files. file cannot exist */
89 if (init (path, false)) {
90 throw failed_constructor ();
94 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
95 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
96 /* _channel is set in set_state() or init() */
98 /* constructor used for existing internal-to-session files. file must exist */
100 if (set_state (node)) {
101 throw failed_constructor ();
106 if (init (foo, must_exist)) {
107 throw failed_constructor ();
111 AudioFileSource::~AudioFileSource ()
114 unlink (_path.c_str());
115 unlink (peakpath.c_str());
120 AudioFileSource::determine_embeddedness (ustring path)
122 return (path.find("/") == 0);
126 AudioFileSource::removable () const
128 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
132 AudioFileSource::init (ustring pathstr, bool must_exist)
137 timeline_position = 0;
138 _peaks_built = false;
141 if (!find (pathstr, must_exist, is_new, _channel)) {
142 throw non_existent_source ();
145 if (is_new && must_exist) {
154 AudioFileSource::peak_path (ustring audio_path)
156 return _session.peak_path_from_audio_path (audio_path);
160 AudioFileSource::old_peak_path (ustring audio_path)
162 /* XXX hardly bombproof! fix me */
164 struct stat stat_file;
165 struct stat stat_mount;
167 ustring mp = mountpoint (audio_path);
169 stat (audio_path.c_str(), &stat_file);
170 stat (mp.c_str(), &stat_mount);
174 snprintf (buf, sizeof (buf), "%u-%u-%d", stat_mount.st_ino, stat_file.st_ino, _channel);
176 snprintf (buf, sizeof (buf), "%ld-%ld-%d", stat_mount.st_ino, stat_file.st_ino, _channel);
179 ustring res = peak_dir;
181 res += peakfile_suffix;
187 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
189 #ifdef HAVE_COREAUDIO
190 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
193 #endif // HAVE_COREAUDIO
195 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
203 AudioFileSource::get_state ()
205 XMLNode& root (AudioSource::get_state());
207 root.add_property (X_("flags"), enum_2_string (_flags));
208 snprintf (buf, sizeof (buf), "%u", _channel);
209 root.add_property (X_("channel"), buf);
214 AudioFileSource::set_state (const XMLNode& node)
216 const XMLProperty* prop;
218 if (AudioSource::set_state (node)) {
222 if ((prop = node.property (X_("flags"))) != 0) {
223 _flags = Flag (string_2_enum (prop->value(), _flags));
229 if ((prop = node.property (X_("channel"))) != 0) {
230 _channel = atoi (prop->value().c_str());
235 if ((prop = node.property (X_("name"))) != 0) {
236 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
238 _is_embedded = false;
241 if ((prop = node.property (X_("destructive"))) != 0) {
242 /* old style, from the period when we had DestructiveFileSource */
243 _flags = Flag (_flags | Destructive);
250 AudioFileSource::mark_for_remove ()
252 // This operation is not allowed for sources for destructive tracks or embedded files.
253 // Fortunately mark_for_remove() is never called for embedded files. This function
254 // must be fixed if that ever happens.
255 if (_flags & Destructive) {
259 _flags = Flag (_flags | Removable | RemoveAtDestroy);
263 AudioFileSource::mark_streaming_write_completed ()
269 Glib::Mutex::Lock lm (_lock);
272 PeaksReady (); /* EMIT SIGNAL */
277 AudioFileSource::mark_take (ustring id)
285 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
288 cerr << "tried to move an embedded region to trash" << endl;
298 /* don't move the file across filesystems, just
299 stick it in the `trash_dir_name' directory
300 on whichever filesystem it was already on.
303 newpath = Glib::path_get_dirname (_path);
304 newpath = Glib::path_get_dirname (newpath);
306 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
309 newpath += trash_dir_name;
311 newpath += Glib::path_get_basename (_path);
313 if (access (newpath.c_str(), F_OK) == 0) {
315 /* the new path already exists, try versioning */
317 char buf[PATH_MAX+1];
321 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
324 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
325 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
329 if (version == 999) {
330 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
339 /* it doesn't exist, or we can't read it or something */
343 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
344 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
345 _path, newpath, strerror (errno))
350 if (::unlink (peakpath.c_str()) != 0) {
351 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
352 peakpath, _path, strerror (errno))
354 /* try to back out */
355 rename (newpath.c_str(), _path.c_str());
362 /* file can not be removed twice, since the operation is not idempotent */
364 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
370 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew, uint16_t& chan)
372 ustring::size_type pos;
377 if (pathstr[0] != '/') {
379 /* non-absolute pathname: find pathstr in search path */
381 vector<ustring> dirs;
386 if (search_path.length() == 0) {
387 error << _("FileSource: search path not set") << endmsg;
391 split (search_path, dirs, ':');
395 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
398 if (fullpath[fullpath.length()-1] != '/') {
404 /* i (paul) made a nasty design error by using ':' as a special character in
405 Ardour 0.99 .. this hack tries to make things sort of work.
408 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
410 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
412 /* its a real file, no problem */
421 /* might be an older session using file:channel syntax. see if the version
422 without the :suffix exists
425 ustring shorter = pathstr.substr (0, pos);
428 if (fullpath[fullpath.length()-1] != '/') {
434 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
435 chan = atoi (pathstr.substr (pos+1).c_str());
443 /* new derived file (e.g. for timefx) being created in a newer session */
450 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
459 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
462 } else if (cnt == 0) {
465 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
478 /* external files and/or very very old style sessions include full paths */
480 /* ugh, handle ':' situation */
482 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
484 ustring shorter = pathstr.substr (0, pos);
486 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
487 chan = atoi (pathstr.substr (pos+1).c_str());
497 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
500 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
502 /* file does not exist or we cannot read it */
505 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
509 if (errno != ENOENT) {
510 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
533 AudioFileSource::set_search_path (ustring p)
539 AudioFileSource::set_header_position_offset (nframes_t offset)
541 header_position_offset = offset;
542 HeaderPositionOffsetChanged ();
546 AudioFileSource::handle_header_position_change ()
549 set_header_timeline_position ();
555 AudioFileSource::set_timeline_position (int64_t pos)
557 timeline_position = pos;
561 AudioFileSource::set_allow_remove_if_empty (bool yn)
568 _flags = Flag (_flags | RemovableIfEmpty);
570 _flags = Flag (_flags & ~RemovableIfEmpty);
575 AudioFileSource::set_source_name (ustring newname, bool destructive)
577 Glib::Mutex::Lock lm (_lock);
578 ustring oldpath = _path;
579 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
581 if (newpath.empty()) {
582 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
586 // Test whether newpath exists, if yes notify the user but continue.
587 if (access(newpath.c_str(),F_OK) == 0) {
588 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;
592 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
593 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
597 _name = Glib::path_get_basename (newpath);
600 return rename_peakfile (peak_path (_path));
604 AudioFileSource::is_empty (Session& s, ustring path)
608 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (DataType::AUDIO, s, path, 0, NoPeakFile, false));
611 ret = (afs->length() == 0);
618 AudioFileSource::setup_peakfile ()
620 if (!(_flags & NoPeakFile)) {
621 return initialize_peakfile (file_is_new, _path);
628 AudioFileSource::safe_file_extension(ustring file)
630 return !(file.rfind(".wav") == ustring::npos &&
631 file.rfind(".aiff")== ustring::npos &&
632 file.rfind(".aif") == ustring::npos &&
633 file.rfind(".snd") == ustring::npos &&
634 file.rfind(".au") == ustring::npos &&
635 file.rfind(".raw") == ustring::npos &&
636 file.rfind(".sf") == ustring::npos &&
637 file.rfind(".cdr") == ustring::npos &&
638 file.rfind(".smp") == ustring::npos &&
639 file.rfind(".maud")== ustring::npos &&
640 file.rfind(".vwe") == ustring::npos &&
641 file.rfind(".paf") == ustring::npos &&
642 /* protools convention */
643 file.rfind(".L") == ustring::npos &&
644 file.rfind(".R") == ustring::npos &&
646 file.rfind(".flac")== ustring::npos &&
648 #ifdef HAVE_COREAUDIO
649 file.rfind(".mp3") == ustring::npos &&
650 file.rfind(".aac") == ustring::npos &&
651 file.rfind(".mp4") == ustring::npos &&
652 #endif // HAVE_COREAUDIO
653 file.rfind(".voc") == ustring::npos);
657 AudioFileSource::mark_immutable ()
659 /* destructive sources stay writable, and their other flags don't
663 if (!(_flags & Destructive)) {
664 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));