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/enumwriter.h>
36 #include <glibmm/miscutils.h>
38 #include <ardour/audiofilesource.h>
39 #include <ardour/sndfile_helpers.h>
40 #include <ardour/sndfilesource.h>
41 #include <ardour/session.h>
42 #include <ardour/source_factory.h>
44 // if these headers come before sigc++ is included
45 // the parser throws ObjC++ errors. (nil is a keyword)
47 #include <ardour/coreaudiosource.h>
48 #include <AudioToolbox/ExtendedAudioFile.h>
49 #include <AudioToolbox/AudioFormat.h>
50 #endif // HAVE_COREAUDIO
54 using namespace ARDOUR;
57 string AudioFileSource::peak_dir = "";
58 string AudioFileSource::search_path;
60 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
61 uint64_t AudioFileSource::header_position_offset = 0;
63 /* XXX maybe this too */
64 char AudioFileSource::bwf_serial_number[13] = "000000000000";
66 AudioFileSource::AudioFileSource (Session& s, string idstr, Flag flags)
67 : AudioSource (s, idstr), _flags (flags)
69 /* constructor used for existing external to session files. file must exist already */
70 _is_embedded = AudioFileSource::determine_embeddedness (idstr);
72 if (init (idstr, true)) {
73 throw failed_constructor ();
78 AudioFileSource::AudioFileSource (Session& s, std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
79 : AudioSource (s, path), _flags (flags)
81 /* constructor used for new internal-to-session files. file cannot exist */
84 if (init (path, false)) {
85 throw failed_constructor ();
89 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node)
90 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
92 /* constructor used for existing internal-to-session files. file must exist */
94 if (set_state (node)) {
95 throw failed_constructor ();
98 if (init (_name, true)) {
99 throw failed_constructor ();
103 AudioFileSource::~AudioFileSource ()
106 unlink (_path.c_str());
107 unlink (peakpath.c_str());
112 AudioFileSource::determine_embeddedness (std::string path)
114 return (path.find("/") == 0);
118 AudioFileSource::removable () const
120 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
124 AudioFileSource::init (string pathstr, bool must_exist)
129 timeline_position = 0;
130 next_peak_clear_should_notify = false;
131 _peaks_built = false;
134 if (!find (pathstr, must_exist, is_new)) {
138 if (is_new && must_exist) {
147 AudioFileSource::peak_path (string audio_path)
149 return _session.peak_path_from_audio_path (audio_path);
153 AudioFileSource::old_peak_path (string audio_path)
155 /* XXX hardly bombproof! fix me */
157 struct stat stat_file;
158 struct stat stat_mount;
160 string mp = mountpoint (audio_path);
162 stat (audio_path.c_str(), &stat_file);
163 stat (mp.c_str(), &stat_mount);
167 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
169 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
172 string res = peak_dir;
179 AudioFileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
181 #ifdef HAVE_COREAUDIO
182 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
185 #endif // HAVE_COREAUDIO
187 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
195 AudioFileSource::get_state ()
197 XMLNode& root (AudioSource::get_state());
198 root.add_property ("flags", enum_2_string (_flags));
203 AudioFileSource::set_state (const XMLNode& node)
205 const XMLProperty* prop;
207 if (AudioSource::set_state (node)) {
211 if ((prop = node.property (X_("flags"))) != 0) {
213 _flags = Flag (string_2_enum (prop->value(), _flags));
221 if ((prop = node.property (X_("name"))) != 0) {
222 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
224 _is_embedded = false;
227 if ((prop = node.property (X_("destructive"))) != 0) {
228 /* old style, from the period when we had DestructiveFileSource */
229 _flags = Flag (_flags | Destructive);
236 AudioFileSource::mark_for_remove ()
242 _flags = Flag (_flags | Removable | RemoveAtDestroy);
246 AudioFileSource::mark_streaming_write_completed ()
252 Glib::Mutex::Lock lm (_lock);
254 next_peak_clear_should_notify = true;
256 if (_peaks_built || pending_peak_builds.empty()) {
258 PeaksReady (); /* EMIT SIGNAL */
263 AudioFileSource::mark_take (string id)
271 AudioFileSource::move_to_trash (const string trash_dir_name)
274 cerr << "tried to move an embedded region to trash" << endl;
284 /* don't move the file across filesystems, just
285 stick it in the `trash_dir_name' directory
286 on whichever filesystem it was already on.
289 newpath = Glib::path_get_dirname (_path);
290 newpath = Glib::path_get_dirname (newpath);
293 newpath += trash_dir_name;
295 newpath += Glib::path_get_basename (_path);
297 if (access (newpath.c_str(), F_OK) == 0) {
299 /* the new path already exists, try versioning */
301 char buf[PATH_MAX+1];
305 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
308 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
309 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
313 if (version == 999) {
314 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
323 /* it doesn't exist, or we can't read it or something */
327 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
328 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
329 _path, newpath, strerror (errno))
334 if (::unlink (peakpath.c_str()) != 0) {
335 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
336 peakpath, _path, strerror (errno))
338 /* try to back out */
339 rename (newpath.c_str(), _path.c_str());
346 /* file can not be removed twice, since the operation is not idempotent */
348 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
354 AudioFileSource::find (string pathstr, bool must_exist, bool& isnew)
356 string::size_type pos;
361 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
363 if ((pos = pathstr.find_last_of (':')) == string::npos) {
366 pathstr = pathstr.substr (0, pos);
369 if (pathstr[0] != '/') {
371 /* non-absolute pathname: find pathstr in search path */
378 if (search_path.length() == 0) {
379 error << _("FileSource: search path not set") << endmsg;
383 split (search_path, dirs, ':');
387 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
390 if (fullpath[fullpath.length()-1] != '/') {
395 if (access (fullpath.c_str(), R_OK) == 0) {
403 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
406 } else if (cnt == 0) {
409 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
422 /* external files and/or very very old style sessions include full paths */
428 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
431 if (access (_path.c_str(), R_OK) != 0) {
433 /* file does not exist or we cannot read it */
436 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
440 if (errno != ENOENT) {
441 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
463 AudioFileSource::set_search_path (string p)
469 AudioFileSource::set_header_position_offset (nframes_t offset)
471 header_position_offset = offset;
472 HeaderPositionOffsetChanged ();
476 AudioFileSource::set_timeline_position (int64_t pos)
478 timeline_position = pos;
482 AudioFileSource::set_allow_remove_if_empty (bool yn)
489 _flags = Flag (_flags | RemovableIfEmpty);
491 _flags = Flag (_flags & ~RemovableIfEmpty);
496 AudioFileSource::set_name (string newname, bool destructive)
498 Glib::Mutex::Lock lm (_lock);
499 string oldpath = _path;
500 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
502 if (newpath.empty()) {
503 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
507 // Test whether newpath exists, if yes notify the user but continue.
508 if (access(newpath.c_str(),F_OK) == 0) {
509 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;
513 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
514 error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
518 _name = Glib::path_get_basename (newpath);
521 return rename_peakfile (peak_path (_path));
525 AudioFileSource::is_empty (Session& s, string path)
528 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (s, path, NoPeakFile, false));
531 ret = (afs->length() == 0);
538 AudioFileSource::setup_peakfile ()
540 if (!(_flags & NoPeakFile)) {
541 return initialize_peakfile (file_is_new, _path);
548 AudioFileSource::safe_file_extension(string file)
550 return !(file.rfind(".wav") == string::npos &&
551 file.rfind(".aiff")== string::npos &&
552 file.rfind(".aif") == string::npos &&
553 file.rfind(".snd") == string::npos &&
554 file.rfind(".au") == string::npos &&
555 file.rfind(".raw") == string::npos &&
556 file.rfind(".sf") == string::npos &&
557 file.rfind(".cdr") == string::npos &&
558 file.rfind(".smp") == string::npos &&
559 file.rfind(".maud")== string::npos &&
560 file.rfind(".vwe") == string::npos &&
561 file.rfind(".paf") == string::npos &&
562 #ifdef HAVE_COREAUDIO
563 file.rfind(".mp3") == string::npos &&
564 file.rfind(".aac") == string::npos &&
565 file.rfind(".mp4") == string::npos &&
566 #endif // HAVE_COREAUDIO
567 file.rfind(".voc") == string::npos);