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.
27 #include <pbd/mountpoint.h>
28 #include <pbd/pathscanner.h>
29 #include <pbd/stl_delete.h>
30 #include <pbd/strsplit.h>
34 #include <glibmm/miscutils.h>
36 #include <ardour/audiofilesource.h>
37 #include <ardour/sndfile_helpers.h>
38 #include <ardour/sndfilesource.h>
39 #include <ardour/destructive_filesource.h>
40 #include <ardour/session.h>
41 #include <ardour/source_factory.h>
43 // if these headers come before sigc++ is included
44 // the parser throws ObjC++ errors. (nil is a keyword)
46 #include <ardour/coreaudiosource.h>
47 #include <AudioToolbox/ExtendedAudioFile.h>
48 #include <AudioToolbox/AudioFormat.h>
49 #endif // HAVE_COREAUDIO
53 using namespace ARDOUR;
56 string AudioFileSource::peak_dir = "";
57 string AudioFileSource::search_path;
59 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
60 uint64_t AudioFileSource::header_position_offset = 0;
62 char AudioFileSource::bwf_country_code[3] = "US";
63 char AudioFileSource::bwf_organization_code[4] = "LAS";
64 char AudioFileSource::bwf_serial_number[13] = "000000000000";
66 AudioFileSource::AudioFileSource (string idstr, Flag flags)
67 : AudioSource (idstr), _flags (flags)
69 /* constructor used for existing external to session files. file must exist already */
71 if (init (idstr, true)) {
72 throw failed_constructor ();
77 AudioFileSource::AudioFileSource (std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
78 : AudioSource (path), _flags (flags)
80 /* constructor used for new internal-to-session files. file cannot exist */
82 if (init (path, false)) {
83 throw failed_constructor ();
87 AudioFileSource::AudioFileSource (const XMLNode& node)
88 : AudioSource (node), _flags (Flag (Writable|CanRename))
90 /* constructor used for existing internal-to-session files. file must exist */
92 if (set_state (node)) {
93 throw failed_constructor ();
96 if (init (_name, true)) {
97 throw failed_constructor ();
101 AudioFileSource::~AudioFileSource ()
104 unlink (_path.c_str());
105 unlink (peakpath.c_str());
110 AudioFileSource::removable () const
112 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && is_empty (_path)));
116 AudioFileSource::init (string pathstr, bool must_exist)
121 next_peak_clear_should_notify = false;
123 if (!find (pathstr, must_exist, is_new)) {
127 if (is_new && must_exist) {
136 AudioFileSource::peak_path (string audio_path)
138 return Session::peak_path_from_audio_path (audio_path);
142 AudioFileSource::old_peak_path (string audio_path)
144 /* XXX hardly bombproof! fix me */
146 struct stat stat_file;
147 struct stat stat_mount;
149 string mp = mountpoint (audio_path);
151 stat (audio_path.c_str(), &stat_file);
152 stat (mp.c_str(), &stat_mount);
156 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
158 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
161 string res = peak_dir;
168 AudioFileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
170 #ifdef HAVE_COREAUDIO
171 OSStatus err = noErr;
173 ExtAudioFileRef af = 0;
177 err = FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0);
179 ExtAudioFileDispose (af);
183 err = ExtAudioFileOpen(&ref, &af);
185 ExtAudioFileDispose (af);
189 AudioStreamBasicDescription absd;
190 memset(&absd, 0, sizeof(absd));
191 size = sizeof(AudioStreamBasicDescription);
192 err = ExtAudioFileGetProperty(af,
193 kExtAudioFileProperty_FileDataFormat, &size, &absd);
195 ExtAudioFileDispose (af);
199 _info.samplerate = absd.mSampleRate;
200 _info.channels = absd.mChannelsPerFrame;
202 size = sizeof(_info.length);
203 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length);
205 ExtAudioFileDispose (af);
209 size = sizeof(CFStringRef);
210 err = AudioFormatGetProperty(
211 kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name);
213 ExtAudioFileDispose (af);
217 _info.format_name = CFStringRefToStdString(name);
219 ExtAudioFileDispose (af);
223 #endif // HAVE_COREAUDIO
228 sf_info.format = 0; // libsndfile says to clear this before sf_open().
230 if ((sf = sf_open ((char*) path.c_str(), SFM_READ, &sf_info)) == 0) {
232 error_msg = sf_error_str (0, errbuf, sizeof (errbuf) - 1);
238 _info.samplerate = sf_info.samplerate;
239 _info.channels = sf_info.channels;
240 _info.length = sf_info.frames;
241 _info.format_name = string_compose("Format: %1, %2",
242 sndfile_major_format(sf_info.format),
243 sndfile_minor_format(sf_info.format));
248 AudioFileSource::get_state ()
250 XMLNode& root (AudioSource::get_state());
252 snprintf (buf, sizeof (buf), "0x%x", (int)_flags);
253 root.add_property ("flags", buf);
258 AudioFileSource::set_state (const XMLNode& node)
260 const XMLProperty* prop;
262 if (AudioSource::set_state (node)) {
266 if ((prop = node.property (X_("flags"))) != 0) {
269 sscanf (prop->value().c_str(), "0x%x", &ival);
270 _flags = Flag (ival);
282 AudioFileSource::mark_for_remove ()
287 _flags = Flag (_flags | RemoveAtDestroy);
291 AudioFileSource::mark_streaming_write_completed ()
297 Glib::Mutex::Lock lm (_lock);
299 next_peak_clear_should_notify = true;
301 if (_peaks_built || pending_peak_builds.empty()) {
303 PeaksReady (); /* EMIT SIGNAL */
308 AudioFileSource::mark_take (string id)
316 AudioFileSource::move_to_trash (const string trash_dir_name)
324 /* don't move the file across filesystems, just
325 stick it in the `trash_dir_name' directory
326 on whichever filesystem it was already on.
329 newpath = Glib::path_get_dirname (_path);
330 newpath = Glib::path_get_dirname (newpath);
333 newpath += trash_dir_name;
335 newpath += Glib::path_get_basename (_path);
337 if (access (newpath.c_str(), F_OK) == 0) {
339 /* the new path already exists, try versioning */
341 char buf[PATH_MAX+1];
345 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
348 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
349 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
353 if (version == 999) {
354 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
363 /* it doesn't exist, or we can't read it or something */
367 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
368 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
369 _path, newpath, strerror (errno))
374 if (::unlink (peakpath.c_str()) != 0) {
375 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
376 peakpath, _path, strerror (errno))
378 /* try to back out */
379 rename (newpath.c_str(), _path.c_str());
386 /* file can not be removed twice, since the operation is not idempotent */
388 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
394 AudioFileSource::find (string pathstr, bool must_exist, bool& isnew)
396 string::size_type pos;
401 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
403 if ((pos = pathstr.find_last_of (':')) == string::npos) {
406 pathstr = pathstr.substr (0, pos);
409 if (pathstr[0] != '/') {
411 /* non-absolute pathname: find pathstr in search path */
418 if (search_path.length() == 0) {
419 error << _("FileSource: search path not set") << endmsg;
423 split (search_path, dirs, ':');
427 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
430 if (fullpath[fullpath.length()-1] != '/') {
435 if (access (fullpath.c_str(), R_OK) == 0) {
443 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
446 } else if (cnt == 0) {
449 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
462 /* external files and/or very very old style sessions include full paths */
465 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
467 if (access (_path.c_str(), R_OK) != 0) {
469 /* file does not exist or we cannot read it */
472 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
476 if (errno != ENOENT) {
477 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
499 AudioFileSource::set_search_path (string p)
505 AudioFileSource::set_header_position_offset (jack_nframes_t offset)
507 header_position_offset = offset;
508 HeaderPositionOffsetChanged ();
512 AudioFileSource::set_timeline_position (jack_nframes_t pos)
514 timeline_position = pos;
518 AudioFileSource::set_allow_remove_if_empty (bool yn)
521 allow_remove_if_empty = yn;
526 AudioFileSource::set_name (string newname, bool destructive)
528 Glib::Mutex::Lock lm (_lock);
529 string oldpath = _path;
530 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
532 if (newpath.empty()) {
533 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
537 // Test whether newpath exists, if yes notify the user but continue.
538 if (access(newpath.c_str(),F_OK) == 0) {
539 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;
543 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
544 error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
548 _name = Glib::path_get_basename (newpath);
551 return rename_peakfile (peak_path (_path));
555 AudioFileSource::is_empty (string path)
558 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (path, NoPeakFile, false));
561 ret = (afs->length() == 0);