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>
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.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
176 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
179 ustring res = peak_dir;
186 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
188 #ifdef HAVE_COREAUDIO
189 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
192 #endif // HAVE_COREAUDIO
194 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
202 AudioFileSource::get_state ()
204 XMLNode& root (AudioSource::get_state());
206 root.add_property (X_("flags"), enum_2_string (_flags));
207 snprintf (buf, sizeof (buf), "%u", _channel);
208 root.add_property (X_("channel"), buf);
213 AudioFileSource::set_state (const XMLNode& node)
215 const XMLProperty* prop;
217 if (AudioSource::set_state (node)) {
221 if ((prop = node.property (X_("flags"))) != 0) {
222 _flags = Flag (string_2_enum (prop->value(), _flags));
228 if ((prop = node.property (X_("channel"))) != 0) {
229 _channel = atoi (prop->value());
234 if ((prop = node.property (X_("name"))) != 0) {
235 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
237 _is_embedded = false;
240 if ((prop = node.property (X_("destructive"))) != 0) {
241 /* old style, from the period when we had DestructiveFileSource */
242 _flags = Flag (_flags | Destructive);
249 AudioFileSource::mark_for_remove ()
251 // This operation is not allowed for sources for destructive tracks or embedded files.
252 // Fortunately mark_for_remove() is never called for embedded files. This function
253 // must be fixed if that ever happens.
254 if (_flags & Destructive) {
258 _flags = Flag (_flags | Removable | RemoveAtDestroy);
262 AudioFileSource::mark_streaming_write_completed ()
268 Glib::Mutex::Lock lm (_lock);
271 PeaksReady (); /* EMIT SIGNAL */
276 AudioFileSource::mark_take (ustring id)
284 AudioFileSource::move_to_trash (const ustring& trash_dir_name)
287 cerr << "tried to move an embedded region to trash" << endl;
297 /* don't move the file across filesystems, just
298 stick it in the `trash_dir_name' directory
299 on whichever filesystem it was already on.
302 newpath = Glib::path_get_dirname (_path);
303 newpath = Glib::path_get_dirname (newpath);
305 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
308 newpath += trash_dir_name;
310 newpath += Glib::path_get_basename (_path);
312 if (access (newpath.c_str(), F_OK) == 0) {
314 /* the new path already exists, try versioning */
316 char buf[PATH_MAX+1];
320 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
323 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
324 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
328 if (version == 999) {
329 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
338 /* it doesn't exist, or we can't read it or something */
342 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
343 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
344 _path, newpath, strerror (errno))
349 if (::unlink (peakpath.c_str()) != 0) {
350 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
351 peakpath, _path, strerror (errno))
353 /* try to back out */
354 rename (newpath.c_str(), _path.c_str());
361 /* file can not be removed twice, since the operation is not idempotent */
363 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
369 AudioFileSource::find (ustring& pathstr, bool must_exist, bool& isnew, uint16_t& chan)
371 ustring::size_type pos;
376 if (pathstr[0] != '/') {
378 /* non-absolute pathname: find pathstr in search path */
380 vector<ustring> dirs;
385 if (search_path.length() == 0) {
386 error << _("FileSource: search path not set") << endmsg;
390 split (search_path, dirs, ':');
394 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
397 if (fullpath[fullpath.length()-1] != '/') {
403 /* i (paul) made a nasty design error by using ':' as a special character in
404 Ardour 0.99 .. this hack tries to make things sort of work.
407 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
409 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
411 /* its a real file, no problem */
420 /* might be an older session using file:channel syntax. see if the version
421 without the :suffix exists
424 ustring shorter = pathstr.substr (0, pos);
427 if (fullpath[fullpath.length()-1] != '/') {
433 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
434 chan = atoi (pathstr.substr (pos+1));
442 /* new derived file (e.g. for timefx) being created in a newer session */
449 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
458 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
461 } else if (cnt == 0) {
464 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
477 /* external files and/or very very old style sessions include full paths */
479 /* ugh, handle ':' situation */
481 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
483 ustring shorter = pathstr.substr (0, pos);
485 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
486 chan = atoi (pathstr.substr (pos+1));
496 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
499 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
501 /* file does not exist or we cannot read it */
504 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
508 if (errno != ENOENT) {
509 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
532 AudioFileSource::set_search_path (ustring p)
538 AudioFileSource::set_header_position_offset (nframes_t offset)
540 header_position_offset = offset;
541 HeaderPositionOffsetChanged ();
545 AudioFileSource::handle_header_position_change ()
548 set_header_timeline_position ();
554 AudioFileSource::set_timeline_position (int64_t pos)
556 timeline_position = pos;
560 AudioFileSource::set_allow_remove_if_empty (bool yn)
567 _flags = Flag (_flags | RemovableIfEmpty);
569 _flags = Flag (_flags & ~RemovableIfEmpty);
574 AudioFileSource::set_name (ustring newname, bool destructive)
576 Glib::Mutex::Lock lm (_lock);
577 ustring oldpath = _path;
578 ustring newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
580 if (newpath.empty()) {
581 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
585 // Test whether newpath exists, if yes notify the user but continue.
586 if (access(newpath.c_str(),F_OK) == 0) {
587 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;
591 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
592 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
596 _name = Glib::path_get_basename (newpath);
599 return rename_peakfile (peak_path (_path));
603 AudioFileSource::is_empty (Session& s, ustring path)
607 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (DataType::AUDIO, s, path, 0, NoPeakFile, false));
610 ret = (afs->length() == 0);
617 AudioFileSource::setup_peakfile ()
619 if (!(_flags & NoPeakFile)) {
620 return initialize_peakfile (file_is_new, _path);
627 AudioFileSource::safe_file_extension(ustring file)
629 return !(file.rfind(".wav") == ustring::npos &&
630 file.rfind(".aiff")== ustring::npos &&
631 file.rfind(".aif") == ustring::npos &&
632 file.rfind(".snd") == ustring::npos &&
633 file.rfind(".au") == ustring::npos &&
634 file.rfind(".raw") == ustring::npos &&
635 file.rfind(".sf") == ustring::npos &&
636 file.rfind(".cdr") == ustring::npos &&
637 file.rfind(".smp") == ustring::npos &&
638 file.rfind(".maud")== ustring::npos &&
639 file.rfind(".vwe") == ustring::npos &&
640 file.rfind(".paf") == ustring::npos &&
642 file.rfind(".flac")== ustring::npos &&
644 #ifdef HAVE_COREAUDIO
645 file.rfind(".mp3") == ustring::npos &&
646 file.rfind(".aac") == ustring::npos &&
647 file.rfind(".mp4") == ustring::npos &&
648 #endif // HAVE_COREAUDIO
649 file.rfind(".voc") == ustring::npos);
653 AudioFileSource::mark_immutable ()
655 /* destructive sources stay writable, and their other flags don't
659 if (!(_flags & Destructive)) {
660 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));