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 path, Flag flags)
67 : AudioSource (s, path), _flags (flags),
70 /* constructor used for existing external to session files. file must exist already */
71 _is_embedded = AudioFileSource::determine_embeddedness (path);
73 if (init (path, true)) {
74 throw failed_constructor ();
79 AudioFileSource::AudioFileSource (Session& s, std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
80 : AudioSource (s, path), _flags (flags),
83 /* constructor used for new internal-to-session files. file cannot exist */
86 if (init (path, false)) {
87 throw failed_constructor ();
91 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node)
92 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
93 /* channel is set in set_state() */
95 /* constructor used for existing internal-to-session files. file must exist */
97 if (set_state (node)) {
98 throw failed_constructor ();
101 if (init (_name, true)) {
102 throw failed_constructor ();
106 AudioFileSource::~AudioFileSource ()
109 unlink (_path.c_str());
110 unlink (peakpath.c_str());
115 AudioFileSource::determine_embeddedness (std::string path)
117 return (path.find("/") == 0);
121 AudioFileSource::removable () const
123 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
127 AudioFileSource::init (string pathstr, bool must_exist)
132 timeline_position = 0;
133 next_peak_clear_should_notify = false;
134 _peaks_built = false;
137 if (!find (pathstr, must_exist, is_new)) {
141 if (is_new && must_exist) {
150 AudioFileSource::peak_path (string audio_path)
152 return _session.peak_path_from_audio_path (audio_path);
156 AudioFileSource::old_peak_path (string audio_path)
158 /* XXX hardly bombproof! fix me */
160 struct stat stat_file;
161 struct stat stat_mount;
163 string mp = mountpoint (audio_path);
165 stat (audio_path.c_str(), &stat_file);
166 stat (mp.c_str(), &stat_mount);
170 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
172 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
175 string res = peak_dir;
182 AudioFileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
184 #ifdef HAVE_COREAUDIO
185 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
188 #endif // HAVE_COREAUDIO
190 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
198 AudioFileSource::get_state ()
200 XMLNode& root (AudioSource::get_state());
202 root.add_property (X_("flags"), enum_2_string (_flags));
203 snprintf (buf, sizeof (buf), "%d", channel);
204 root.add_property (X_("channel"), buf);
209 AudioFileSource::set_state (const XMLNode& node)
211 const XMLProperty* prop;
213 if (AudioSource::set_state (node)) {
217 if ((prop = node.property (X_("flags"))) != 0) {
218 _flags = Flag (string_2_enum (prop->value(), _flags));
224 if ((prop = node.property (X_("channel"))) != 0) {
225 channel = atoi (prop->value());
230 if ((prop = node.property (X_("name"))) != 0) {
231 _is_embedded = AudioFileSource::determine_embeddedness (prop->value());
233 _is_embedded = false;
236 if ((prop = node.property (X_("destructive"))) != 0) {
237 /* old style, from the period when we had DestructiveFileSource */
238 _flags = Flag (_flags | Destructive);
245 AudioFileSource::mark_for_remove ()
251 _flags = Flag (_flags | Removable | RemoveAtDestroy);
255 AudioFileSource::mark_streaming_write_completed ()
261 Glib::Mutex::Lock lm (_lock);
263 next_peak_clear_should_notify = true;
265 if (_peaks_built || pending_peak_builds.empty()) {
267 PeaksReady (); /* EMIT SIGNAL */
272 AudioFileSource::mark_take (string id)
280 AudioFileSource::move_to_trash (const string trash_dir_name)
283 cerr << "tried to move an embedded region to trash" << endl;
293 /* don't move the file across filesystems, just
294 stick it in the `trash_dir_name' directory
295 on whichever filesystem it was already on.
298 newpath = Glib::path_get_dirname (_path);
299 newpath = Glib::path_get_dirname (newpath);
301 cerr << "from " << _path << " dead dir looks like " << newpath << endl;
304 newpath += trash_dir_name;
306 newpath += Glib::path_get_basename (_path);
308 if (access (newpath.c_str(), F_OK) == 0) {
310 /* the new path already exists, try versioning */
312 char buf[PATH_MAX+1];
316 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
319 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
320 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
324 if (version == 999) {
325 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
334 /* it doesn't exist, or we can't read it or something */
338 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
339 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
340 _path, newpath, strerror (errno))
345 if (::unlink (peakpath.c_str()) != 0) {
346 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
347 peakpath, _path, strerror (errno))
349 /* try to back out */
350 rename (newpath.c_str(), _path.c_str());
357 /* file can not be removed twice, since the operation is not idempotent */
359 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
365 AudioFileSource::find (string pathstr, bool must_exist, bool& isnew)
367 string::size_type pos;
372 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
374 if ((pos = pathstr.find_last_of (':')) == string::npos) {
377 pathstr = pathstr.substr (0, pos);
380 if (pathstr[0] != '/') {
382 /* non-absolute pathname: find pathstr in search path */
389 if (search_path.length() == 0) {
390 error << _("FileSource: search path not set") << endmsg;
394 split (search_path, dirs, ':');
398 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
401 if (fullpath[fullpath.length()-1] != '/') {
406 if (access (fullpath.c_str(), R_OK) == 0) {
414 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
417 } else if (cnt == 0) {
420 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
433 /* external files and/or very very old style sessions include full paths */
439 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
442 if (access (_path.c_str(), R_OK) != 0) {
444 /* file does not exist or we cannot read it */
447 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
451 if (errno != ENOENT) {
452 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
474 AudioFileSource::set_search_path (string p)
480 AudioFileSource::set_header_position_offset (nframes_t offset)
482 header_position_offset = offset;
483 HeaderPositionOffsetChanged ();
487 AudioFileSource::set_timeline_position (int64_t pos)
489 timeline_position = pos;
493 AudioFileSource::set_allow_remove_if_empty (bool yn)
500 _flags = Flag (_flags | RemovableIfEmpty);
502 _flags = Flag (_flags & ~RemovableIfEmpty);
507 AudioFileSource::set_name (string newname, bool destructive)
509 Glib::Mutex::Lock lm (_lock);
510 string oldpath = _path;
511 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
513 if (newpath.empty()) {
514 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
518 // Test whether newpath exists, if yes notify the user but continue.
519 if (access(newpath.c_str(),F_OK) == 0) {
520 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;
524 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
525 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
529 _name = Glib::path_get_basename (newpath);
532 return rename_peakfile (peak_path (_path));
536 AudioFileSource::is_empty (Session& s, string path)
539 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (s, path, 0, NoPeakFile, false));
542 ret = (afs->length() == 0);
549 AudioFileSource::setup_peakfile ()
551 if (!(_flags & NoPeakFile)) {
552 return initialize_peakfile (file_is_new, _path);
559 AudioFileSource::safe_file_extension(string file)
561 return !(file.rfind(".wav") == string::npos &&
562 file.rfind(".aiff")== string::npos &&
563 file.rfind(".aif") == string::npos &&
564 file.rfind(".snd") == string::npos &&
565 file.rfind(".au") == string::npos &&
566 file.rfind(".raw") == string::npos &&
567 file.rfind(".sf") == string::npos &&
568 file.rfind(".cdr") == string::npos &&
569 file.rfind(".smp") == string::npos &&
570 file.rfind(".maud")== string::npos &&
571 file.rfind(".vwe") == string::npos &&
572 file.rfind(".paf") == string::npos &&
573 #ifdef HAVE_COREAUDIO
574 file.rfind(".mp3") == string::npos &&
575 file.rfind(".aac") == string::npos &&
576 file.rfind(".mp4") == string::npos &&
577 #endif // HAVE_COREAUDIO
578 file.rfind(".voc") == string::npos);
582 AudioFileSource::mark_immutable ()
584 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));