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 ();
101 AudioFileSource::AudioFileSource (Session& s, ustring path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
102 : AudioSource (s, path), _flags (flags),
105 /* constructor used for new internal-to-session files. file cannot exist */
106 _is_embedded = false;
108 if (init (path, false)) {
109 throw failed_constructor ();
113 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
114 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
115 /* _channel is set in set_state() or init() */
117 /* constructor used for existing internal-to-session files. file must exist */
119 if (set_state (node)) {
120 throw failed_constructor ();
125 if (init (foo, must_exist)) {
126 throw failed_constructor ();
130 AudioFileSource::~AudioFileSource ()
133 unlink (_path.c_str());
134 unlink (peakpath.c_str());
139 AudioFileSource::determine_embeddedness (ustring path)
141 return (path.find("/") == 0);
145 AudioFileSource::removable () const
147 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
151 AudioFileSource::init (ustring pathstr, bool must_exist)
154 timeline_position = 0;
155 _peaks_built = false;
157 if (!find (pathstr, must_exist, file_is_new, _channel)) {
158 throw non_existent_source ();
161 if (file_is_new && must_exist) {
170 AudioFileSource::peak_path (ustring audio_path)
174 base = PBD::basename_nosuffix (audio_path);
176 base += (char) ('A' + _channel);
178 return _session.peak_path (base);
182 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
186 /* check for the broken location in use by 2.0 for several months */
188 str = broken_peak_path (audio_path);
190 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
194 /* it would be nice to rename it but the nature of
195 the bug means that we can't reliably use it.
201 /* all native files are mono, so we can just rename
204 ::rename (str.c_str(), peak_path.c_str());
208 /* Nasty band-aid for older sessions that were created before we
209 used libsndfile for all audio files.
213 str = old_peak_path (audio_path);
214 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
223 AudioFileSource::broken_peak_path (ustring audio_path)
225 return Glib::build_filename(_session.peak_dir (), PBD::basename_nosuffix (audio_path) + ".peak");
229 AudioFileSource::old_peak_path (ustring audio_path)
231 /* XXX hardly bombproof! fix me */
233 struct stat stat_file;
234 struct stat stat_mount;
236 ustring mp = mountpoint (audio_path);
238 stat (audio_path.c_str(), &stat_file);
239 stat (mp.c_str(), &stat_mount);
243 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
245 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
248 ustring res = peak_dir;
255 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
257 #ifdef HAVE_COREAUDIO
258 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
261 #endif // HAVE_COREAUDIO
263 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
271 AudioFileSource::get_state ()
273 XMLNode& root (AudioSource::get_state());
275 root.add_property (X_("flags"), enum_2_string (_flags));
276 snprintf (buf, sizeof (buf), "%u", _channel);
277 root.add_property (X_("channel"), buf);
282 AudioFileSource::set_state (const XMLNode& node)
284 const XMLProperty* prop;
286 if (AudioSource::set_state (node)) {
290 if ((prop = node.property (X_("flags"))) != 0) {
291 _flags = Flag (string_2_enum (prop->value(), _flags));
297 if ((prop = node.property (X_("channel"))) != 0) {
298 _channel = atoi (prop->value());
303 if ((prop = node.property (X_("name"))) != 0) {
304 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
306 _is_embedded = false;
309 if ((prop = node.property (X_("destructive"))) != 0) {
310 /* old style, from the period when we had DestructiveFileSource */
311 _flags = Flag (_flags | Destructive);
318 AudioFileSource::mark_for_remove ()
320 // This operation is not allowed for sources for destructive tracks or embedded files.
321 // Fortunately mark_for_remove() is never called for embedded files. This function
322 // must be fixed if that ever happens.
323 if (_flags & Destructive) {
327 _flags = Flag (_flags | Removable | RemoveAtDestroy);
331 AudioFileSource::mark_streaming_write_completed ()
337 /* XXX notice that we're readers of _peaks_built
338 but we must hold a solid lock on PeaksReady.
341 Glib::Mutex::Lock lm (_lock);
344 PeaksReady (); /* EMIT SIGNAL */
349 AudioFileSource::mark_take (ustring id)
357 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
360 cerr << "tried to move an embedded region to trash" << endl;
370 /* don't move the file across filesystems, just
371 stick it in the `trash_dir_name' directory
372 on whichever filesystem it was already on.
375 newpath = Glib::path_get_dirname (_path);
376 newpath = Glib::path_get_dirname (newpath);
378 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
381 newpath += trash_dir_name;
383 newpath += Glib::path_get_basename (_path);
385 if (access (newpath.c_str(), F_OK) == 0) {
387 /* the new path already exists, try versioning */
389 char buf[PATH_MAX+1];
393 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
396 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
397 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
401 if (version == 999) {
402 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
411 /* it doesn't exist, or we can't read it or something */
415 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
416 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
417 _path, newpath, strerror (errno))
422 if (::unlink (peakpath.c_str()) != 0) {
423 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
424 peakpath, _path, strerror (errno))
426 /* try to back out */
427 rename (newpath.c_str(), _path.c_str());
434 /* file can not be removed twice, since the operation is not idempotent */
436 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
442 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew, uint16_t& chan)
444 ustring::size_type pos;
449 if (pathstr[0] != '/') {
451 /* non-absolute pathname: find pathstr in search path */
453 vector<ustring> dirs;
458 if (search_path.length() == 0) {
459 error << _("FileSource: search path not set") << endmsg;
463 split (search_path, dirs, ':');
467 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
470 if (fullpath[fullpath.length()-1] != '/') {
476 /* i (paul) made a nasty design error by using ':' as a special character in
477 Ardour 0.99 .. this hack tries to make things sort of work.
480 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
482 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
484 /* its a real file, no problem */
493 /* might be an older session using file:channel syntax. see if the version
494 without the :suffix exists
497 ustring shorter = pathstr.substr (0, pos);
500 if (fullpath[fullpath.length()-1] != '/') {
506 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
507 chan = atoi (pathstr.substr (pos+1));
515 /* new derived file (e.g. for timefx) being created in a newer session */
522 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
531 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
534 } else if (cnt == 0) {
537 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
550 /* external files and/or very very old style sessions include full paths */
552 /* ugh, handle ':' situation */
554 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
556 ustring shorter = pathstr.substr (0, pos);
558 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
559 chan = atoi (pathstr.substr (pos+1));
569 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
572 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
574 /* file does not exist or we cannot read it */
577 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
581 if (errno != ENOENT) {
582 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
605 AudioFileSource::set_search_path (ustring p)
611 AudioFileSource::set_header_position_offset (nframes_t offset)
613 header_position_offset = offset;
614 HeaderPositionOffsetChanged ();
618 AudioFileSource::set_timeline_position (int64_t pos)
620 timeline_position = pos;
624 AudioFileSource::set_allow_remove_if_empty (bool yn)
631 _flags = Flag (_flags | RemovableIfEmpty);
633 _flags = Flag (_flags & ~RemovableIfEmpty);
638 AudioFileSource::set_name (ustring newname, bool destructive)
640 Glib::Mutex::Lock lm (_lock);
641 ustring oldpath = _path;
642 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
644 if (newpath.empty()) {
645 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
649 // Test whether newpath exists, if yes notify the user but continue.
650 if (access(newpath.c_str(),F_OK) == 0) {
651 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;
655 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
656 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
660 _name = Glib::path_get_basename (newpath);
663 return rename_peakfile (peak_path (_path));
667 AudioFileSource::is_empty (Session& s, ustring path)
672 if (!get_soundfile_info (path, info, err)) {
673 /* dangerous: we can't get info, so assume that its not empty */
677 return info.length == 0;
681 AudioFileSource::setup_peakfile ()
683 if (!(_flags & NoPeakFile)) {
684 return initialize_peakfile (file_is_new, _path);
691 AudioFileSource::safe_file_extension(ustring file)
693 const char* suffixes[] = {
711 #ifdef HAVE_COREAUDIO
715 #endif // HAVE_COREAUDIO
719 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
720 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
729 AudioFileSource::mark_immutable ()
731 /* destructive sources stay writable, and their other flags don't
735 if (!(_flags & Destructive)) {
736 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
742 AudioFileSource::get_interleave_buffer (nframes_t size)
744 SizedSampleBuffer* ssb;
746 if ((ssb = thread_interleave_buffer.get()) == 0) {
747 ssb = new SizedSampleBuffer (size);
748 thread_interleave_buffer.set (ssb);
751 if (ssb->size < size) {
752 ssb = new SizedSampleBuffer (size);
753 thread_interleave_buffer.set (ssb);