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>
41 #include <glibmm/thread.h>
43 #include <ardour/audiofilesource.h>
44 #include <ardour/sndfile_helpers.h>
45 #include <ardour/sndfilesource.h>
46 #include <ardour/session.h>
47 #include <ardour/session_directory.h>
48 #include <ardour/source_factory.h>
49 #include <ardour/filename_extensions.h>
51 // if these headers come before sigc++ is included
52 // the parser throws ObjC++ errors. (nil is a keyword)
54 #include <ardour/coreaudiosource.h>
55 #include <AudioToolbox/ExtendedAudioFile.h>
56 #include <AudioToolbox/AudioFormat.h>
57 #endif // HAVE_COREAUDIO
61 using namespace ARDOUR;
65 ustring AudioFileSource::peak_dir = "";
66 ustring AudioFileSource::search_path;
68 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
69 uint64_t AudioFileSource::header_position_offset = 0;
71 /* XXX maybe this too */
72 char AudioFileSource::bwf_serial_number[13] = "000000000000";
74 struct SizedSampleBuffer {
78 SizedSampleBuffer (nframes_t sz) : size (sz) {
79 buf = new Sample[size];
82 ~SizedSampleBuffer() {
87 Glib::StaticPrivate<SizedSampleBuffer> thread_interleave_buffer = GLIBMM_STATIC_PRIVATE_INIT;
89 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags)
90 : AudioSource (s, path), _flags (flags),
93 /* constructor used for existing external to session files. file must exist already */
94 _is_embedded = AudioFileSource::determine_embeddedness (path);
96 if (init (path, true)) {
97 throw failed_constructor ();
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 ();
114 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
115 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
116 /* _channel is set in set_state() or init() */
118 /* constructor used for existing internal-to-session files. file must exist */
120 if (set_state (node)) {
121 throw failed_constructor ();
126 if (init (foo, must_exist)) {
127 throw failed_constructor ();
131 AudioFileSource::~AudioFileSource ()
134 unlink (_path.c_str());
135 unlink (peakpath.c_str());
140 AudioFileSource::determine_embeddedness (ustring path)
142 return (path.find("/") == 0);
146 AudioFileSource::removable () const
148 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
152 AudioFileSource::init (ustring pathstr, bool must_exist)
155 timeline_position = 0;
156 _peaks_built = false;
158 if (!find (pathstr, must_exist, file_is_new, _channel)) {
159 throw non_existent_source ();
162 if (file_is_new && must_exist) {
171 AudioFileSource::peak_path (ustring audio_path)
175 base = PBD::basename_nosuffix (audio_path);
177 base += (char) ('A' + _channel);
179 return _session.peak_path (base);
183 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
187 /* check for the broken location in use by 2.0 for several months */
189 str = broken_peak_path (audio_path);
191 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
195 /* it would be nice to rename it but the nature of
196 the bug means that we can't reliably use it.
202 /* all native files are mono, so we can just rename
205 ::rename (str.c_str(), peak_path.c_str());
209 /* Nasty band-aid for older sessions that were created before we
210 used libsndfile for all audio files.
214 str = old_peak_path (audio_path);
215 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
224 AudioFileSource::broken_peak_path (ustring audio_path)
226 return _session.peak_path (audio_path);
230 AudioFileSource::old_peak_path (ustring audio_path)
232 /* XXX hardly bombproof! fix me */
234 struct stat stat_file;
235 struct stat stat_mount;
237 ustring mp = mountpoint (audio_path);
239 stat (audio_path.c_str(), &stat_file);
240 stat (mp.c_str(), &stat_mount);
244 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
246 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
249 ustring res = peak_dir;
251 res += peakfile_suffix;
257 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
259 #ifdef HAVE_COREAUDIO
260 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
263 #endif // HAVE_COREAUDIO
265 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
273 AudioFileSource::get_state ()
275 XMLNode& root (AudioSource::get_state());
277 root.add_property (X_("flags"), enum_2_string (_flags));
278 snprintf (buf, sizeof (buf), "%u", _channel);
279 root.add_property (X_("channel"), buf);
284 AudioFileSource::set_state (const XMLNode& node)
286 const XMLProperty* prop;
288 if (AudioSource::set_state (node)) {
292 if ((prop = node.property (X_("flags"))) != 0) {
293 _flags = Flag (string_2_enum (prop->value(), _flags));
299 if ((prop = node.property (X_("channel"))) != 0) {
300 _channel = atoi (prop->value());
305 if ((prop = node.property (X_("name"))) != 0) {
306 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
308 _is_embedded = false;
311 if ((prop = node.property (X_("destructive"))) != 0) {
312 /* old style, from the period when we had DestructiveFileSource */
313 _flags = Flag (_flags | Destructive);
320 AudioFileSource::mark_for_remove ()
322 // This operation is not allowed for sources for destructive tracks or embedded files.
323 // Fortunately mark_for_remove() is never called for embedded files. This function
324 // must be fixed if that ever happens.
325 if (_flags & Destructive) {
329 _flags = Flag (_flags | Removable | RemoveAtDestroy);
333 AudioFileSource::mark_streaming_write_completed ()
339 /* XXX notice that we're readers of _peaks_built
340 but we must hold a solid lock on PeaksReady.
343 Glib::Mutex::Lock lm (_lock);
346 PeaksReady (); /* EMIT SIGNAL */
351 AudioFileSource::mark_take (ustring id)
359 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
362 cerr << "tried to move an embedded region to trash" << endl;
372 /* don't move the file across filesystems, just
373 stick it in the `trash_dir_name' directory
374 on whichever filesystem it was already on.
377 newpath = Glib::path_get_dirname (_path);
378 newpath = Glib::path_get_dirname (newpath);
380 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
383 newpath += trash_dir_name;
385 newpath += Glib::path_get_basename (_path);
387 if (access (newpath.c_str(), F_OK) == 0) {
389 /* the new path already exists, try versioning */
391 char buf[PATH_MAX+1];
395 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
398 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
399 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
403 if (version == 999) {
404 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
413 /* it doesn't exist, or we can't read it or something */
417 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
418 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
419 _path, newpath, strerror (errno))
424 if (::unlink (peakpath.c_str()) != 0) {
425 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
426 peakpath, _path, strerror (errno))
428 /* try to back out */
429 rename (newpath.c_str(), _path.c_str());
436 /* file can not be removed twice, since the operation is not idempotent */
438 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
444 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew, uint16_t& chan)
446 ustring::size_type pos;
451 if (pathstr[0] != '/') {
453 /* non-absolute pathname: find pathstr in search path */
455 vector<ustring> dirs;
460 if (search_path.length() == 0) {
461 error << _("FileSource: search path not set") << endmsg;
465 split (search_path, dirs, ':');
469 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
472 if (fullpath[fullpath.length()-1] != '/') {
478 /* i (paul) made a nasty design error by using ':' as a special character in
479 Ardour 0.99 .. this hack tries to make things sort of work.
482 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
484 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
486 /* its a real file, no problem */
495 /* might be an older session using file:channel syntax. see if the version
496 without the :suffix exists
499 ustring shorter = pathstr.substr (0, pos);
502 if (fullpath[fullpath.length()-1] != '/') {
508 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
509 chan = atoi (pathstr.substr (pos+1));
517 /* new derived file (e.g. for timefx) being created in a newer session */
524 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
533 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
536 } else if (cnt == 0) {
539 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
546 /* Current find() is unable to parse relative path names to yet non-existant
547 sources. QuickFix(tm) */
548 if (keeppath == "") {
550 error << "AudioFileSource::find(), keeppath = \"\", but the file must exist" << endl;
563 /* external files and/or very very old style sessions include full paths */
565 /* ugh, handle ':' situation */
567 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
569 ustring shorter = pathstr.substr (0, pos);
571 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
572 chan = atoi (pathstr.substr (pos+1));
582 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
585 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
587 /* file does not exist or we cannot read it */
590 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
594 if (errno != ENOENT) {
595 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
618 AudioFileSource::set_search_path (ustring p)
624 AudioFileSource::set_header_position_offset (nframes_t offset)
626 header_position_offset = offset;
627 HeaderPositionOffsetChanged ();
631 AudioFileSource::set_timeline_position (int64_t pos)
633 timeline_position = pos;
637 AudioFileSource::set_allow_remove_if_empty (bool yn)
644 _flags = Flag (_flags | RemovableIfEmpty);
646 _flags = Flag (_flags & ~RemovableIfEmpty);
651 AudioFileSource::set_source_name (ustring newname, bool destructive)
653 Glib::Mutex::Lock lm (_lock);
654 ustring oldpath = _path;
655 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
657 if (newpath.empty()) {
658 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
662 // Test whether newpath exists, if yes notify the user but continue.
663 if (access(newpath.c_str(),F_OK) == 0) {
664 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;
668 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
669 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
673 _name = Glib::path_get_basename (newpath);
676 return rename_peakfile (peak_path (_path));
680 AudioFileSource::is_empty (Session& s, ustring path)
685 if (!get_soundfile_info (path, info, err)) {
686 /* dangerous: we can't get info, so assume that its not empty */
690 return info.length == 0;
694 AudioFileSource::setup_peakfile ()
696 if (!(_flags & NoPeakFile)) {
697 return initialize_peakfile (file_is_new, _path);
704 AudioFileSource::safe_file_extension(ustring file)
706 const char* suffixes[] = {
725 #ifdef HAVE_COREAUDIO
729 #endif // HAVE_COREAUDIO
732 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
733 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
742 AudioFileSource::mark_immutable ()
744 /* destructive sources stay writable, and their other flags don't
748 if (!(_flags & Destructive)) {
749 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
755 AudioFileSource::get_interleave_buffer (nframes_t size)
757 SizedSampleBuffer* ssb;
759 if ((ssb = thread_interleave_buffer.get()) == 0) {
760 ssb = new SizedSampleBuffer (size);
761 thread_interleave_buffer.set (ssb);
764 if (ssb->size < size) {
765 ssb = new SizedSampleBuffer (size);
766 thread_interleave_buffer.set (ssb);