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/pathscanner.h>
33 #include <pbd/stl_delete.h>
34 #include <pbd/strsplit.h>
35 #include <pbd/shortpath.h>
36 #include <pbd/enumwriter.h>
40 #include <glibmm/miscutils.h>
41 #include <glibmm/fileutils.h>
42 #include <glibmm/thread.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/sndfile_helpers.h>
46 #include <ardour/sndfilesource.h>
47 #include <ardour/session.h>
48 #include <ardour/source_factory.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 struct SizedSampleBuffer {
77 SizedSampleBuffer (nframes_t sz) : size (sz) {
78 buf = new Sample[size];
81 ~SizedSampleBuffer() {
86 Glib::StaticPrivate<SizedSampleBuffer> thread_interleave_buffer = GLIBMM_STATIC_PRIVATE_INIT;
88 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags)
89 : AudioSource (s, path), _flags (flags),
92 /* constructor used for existing external to session files. file must exist already */
93 _is_embedded = AudioFileSource::determine_embeddedness (path);
95 if (init (path, true)) {
96 throw failed_constructor ();
99 fix_writable_flags ();
102 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
103 : AudioSource (s, path), _flags (flags),
106 /* constructor used for new internal-to-session files. file cannot exist */
107 _is_embedded = false;
109 if (init (path, false)) {
110 throw failed_constructor ();
113 fix_writable_flags ();
116 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
117 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
118 /* _channel is set in set_state() or init() */
120 /* constructor used for existing internal-to-session files. file must exist */
122 if (set_state (node)) {
123 throw failed_constructor ();
128 if (init (foo, must_exist)) {
129 throw failed_constructor ();
132 fix_writable_flags ();
135 AudioFileSource::~AudioFileSource ()
138 unlink (_path.c_str());
139 unlink (peakpath.c_str());
144 AudioFileSource::fix_writable_flags ()
146 if (!_session.writable()) {
147 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
152 AudioFileSource::determine_embeddedness (ustring path)
154 return (path.find("/") == 0);
158 AudioFileSource::removable () const
160 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
164 AudioFileSource::writable() const
166 return (_flags & Writable);
170 AudioFileSource::init (ustring pathstr, bool must_exist)
173 timeline_position = 0;
174 _peaks_built = false;
176 /* is_embedded() can't work yet, because our _path is not set */
178 bool embedded = determine_embeddedness (pathstr);
180 if (!find (pathstr, must_exist, embedded, file_is_new, _channel, _path, _name)) {
181 throw non_existent_source ();
184 if (file_is_new && must_exist) {
193 AudioFileSource::peak_path (ustring audio_path)
197 base = PBD::basename_nosuffix (audio_path);
199 base += (char) ('A' + _channel);
201 return _session.peak_path (base);
205 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
209 /* check for the broken location in use by 2.0 for several months */
211 str = broken_peak_path (audio_path);
213 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
217 /* it would be nice to rename it but the nature of
218 the bug means that we can't reliably use it.
224 /* all native files are mono, so we can just rename
227 ::rename (str.c_str(), peak_path.c_str());
231 /* Nasty band-aid for older sessions that were created before we
232 used libsndfile for all audio files.
236 str = old_peak_path (audio_path);
237 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
246 AudioFileSource::broken_peak_path (ustring audio_path)
248 return Glib::build_filename(_session.peak_dir (), PBD::basename_nosuffix (audio_path) + ".peak");
252 AudioFileSource::old_peak_path (ustring audio_path)
254 /* XXX hardly bombproof! fix me */
256 struct stat stat_file;
257 struct stat stat_mount;
259 ustring mp = mountpoint (audio_path);
261 stat (audio_path.c_str(), &stat_file);
262 stat (mp.c_str(), &stat_mount);
266 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
268 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
271 ustring res = peak_dir;
278 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
280 #ifdef HAVE_COREAUDIO
281 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
284 #endif // HAVE_COREAUDIO
286 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
294 AudioFileSource::get_state ()
296 XMLNode& root (AudioSource::get_state());
298 root.add_property (X_("flags"), enum_2_string (_flags));
299 snprintf (buf, sizeof (buf), "%u", _channel);
300 root.add_property (X_("channel"), buf);
305 AudioFileSource::set_state (const XMLNode& node)
307 const XMLProperty* prop;
309 if (AudioSource::set_state (node)) {
313 if ((prop = node.property (X_("flags"))) != 0) {
314 _flags = Flag (string_2_enum (prop->value(), _flags));
320 fix_writable_flags ();
322 if ((prop = node.property (X_("channel"))) != 0) {
323 _channel = atoi (prop->value());
328 if ((prop = node.property (X_("name"))) != 0) {
329 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
331 _is_embedded = false;
334 if ((prop = node.property (X_("destructive"))) != 0) {
335 /* old style, from the period when we had DestructiveFileSource */
336 _flags = Flag (_flags | Destructive);
343 AudioFileSource::mark_for_remove ()
345 // This operation is not allowed for sources for destructive tracks or embedded files.
346 // Fortunately mark_for_remove() is never called for embedded files. This function
347 // must be fixed if that ever happens.
348 if (!_session.writable() || (_flags & Destructive)) {
352 _flags = Flag (_flags | Removable | RemoveAtDestroy);
356 AudioFileSource::mark_streaming_write_completed ()
362 /* XXX notice that we're readers of _peaks_built
363 but we must hold a solid lock on PeaksReady.
366 Glib::Mutex::Lock lm (_lock);
369 PeaksReady (); /* EMIT SIGNAL */
374 AudioFileSource::mark_take (ustring id)
382 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
385 cerr << "tried to move an embedded region to trash" << endl;
395 /* don't move the file across filesystems, just
396 stick it in the `trash_dir_name' directory
397 on whichever filesystem it was already on.
400 newpath = Glib::path_get_dirname (_path);
401 newpath = Glib::path_get_dirname (newpath);
403 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
406 newpath += trash_dir_name;
408 newpath += Glib::path_get_basename (_path);
410 if (access (newpath.c_str(), F_OK) == 0) {
412 /* the new path already exists, try versioning */
414 char buf[PATH_MAX+1];
418 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
421 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
422 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
426 if (version == 999) {
427 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
436 /* it doesn't exist, or we can't read it or something */
440 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
441 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
442 _path, newpath, strerror (errno))
447 if (::unlink (peakpath.c_str()) != 0) {
448 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
449 peakpath, _path, strerror (errno))
451 /* try to back out */
452 rename (newpath.c_str(), _path.c_str());
459 /* file can not be removed twice, since the operation is not idempotent */
461 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
467 AudioFileSource::find (ustring pathstr, bool must_exist, bool embedded,
468 bool& isnew, uint16_t& chan,
469 ustring& path, std::string& name)
471 ustring::size_type pos;
476 if (pathstr[0] != '/') {
478 /* non-absolute pathname: find pathstr in search path */
480 vector<ustring> dirs;
485 if (search_path.length() == 0) {
486 error << _("FileSource: search path not set") << endmsg;
490 split (search_path, dirs, ':');
494 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
497 if (fullpath[fullpath.length()-1] != '/') {
503 /* i (paul) made a nasty design error by using ':' as a special character in
504 Ardour 0.99 .. this hack tries to make things sort of work.
507 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
509 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
511 /* its a real file, no problem */
520 /* might be an older session using file:channel syntax. see if the version
521 without the :suffix exists
524 ustring shorter = pathstr.substr (0, pos);
527 if (fullpath[fullpath.length()-1] != '/') {
533 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
534 chan = atoi (pathstr.substr (pos+1));
542 /* new derived file (e.g. for timefx) being created in a newer session */
549 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
558 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
561 } else if (cnt == 0) {
564 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
577 /* external files and/or very very old style sessions include full paths */
579 /* ugh, handle ':' situation */
581 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
583 ustring shorter = pathstr.substr (0, pos);
585 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
586 chan = atoi (pathstr.substr (pos+1));
596 name = Glib::path_get_basename (pathstr);
599 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
601 /* file does not exist or we cannot read it */
604 error << string_compose(_("Filesource: cannot find required file (%1): %2"), pathstr, strerror (errno)) << endmsg;
608 if (errno != ENOENT) {
609 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), pathstr, strerror (errno)) << endmsg;
632 AudioFileSource::set_search_path (ustring p)
638 AudioFileSource::set_header_position_offset (nframes_t offset)
640 header_position_offset = offset;
641 HeaderPositionOffsetChanged ();
645 AudioFileSource::set_timeline_position (int64_t pos)
647 timeline_position = pos;
651 AudioFileSource::set_allow_remove_if_empty (bool yn)
658 _flags = Flag (_flags | RemovableIfEmpty);
660 _flags = Flag (_flags & ~RemovableIfEmpty);
663 fix_writable_flags ();
667 AudioFileSource::set_name (ustring newname, bool destructive)
669 Glib::Mutex::Lock lm (_lock);
670 ustring oldpath = _path;
671 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
673 if (newpath.empty()) {
674 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
678 // Test whether newpath exists, if yes notify the user but continue.
679 if (access(newpath.c_str(),F_OK) == 0) {
680 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;
684 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
685 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
689 _name = Glib::path_get_basename (newpath);
692 return rename_peakfile (peak_path (_path));
696 AudioFileSource::is_empty (Session& s, ustring path)
701 if (!get_soundfile_info (path, info, err)) {
702 /* dangerous: we can't get info, so assume that its not empty */
706 return info.length == 0;
710 AudioFileSource::setup_peakfile ()
712 if (!(_flags & NoPeakFile)) {
713 return initialize_peakfile (file_is_new, _path);
720 AudioFileSource::safe_file_extension(ustring file)
722 const char* suffixes[] = {
739 #ifdef HAVE_COREAUDIO
743 #endif // HAVE_COREAUDIO
747 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
748 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
757 AudioFileSource::mark_immutable ()
759 /* destructive sources stay writable, and their other flags don't
763 if (!(_flags & Destructive)) {
764 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
770 AudioFileSource::get_interleave_buffer (nframes_t size)
772 SizedSampleBuffer* ssb;
774 if ((ssb = thread_interleave_buffer.get()) == 0) {
775 ssb = new SizedSampleBuffer (size);
776 thread_interleave_buffer.set (ssb);
779 if (ssb->size < size) {
780 ssb = new SizedSampleBuffer (size);
781 thread_interleave_buffer.set (ssb);