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 /* XXX maybe this too */
63 char AudioFileSource::bwf_serial_number[13] = "000000000000";
65 AudioFileSource::AudioFileSource (Session& s, string idstr, Flag flags)
66 : AudioSource (s, idstr), _flags (flags)
68 /* constructor used for existing external to session files. file must exist already */
70 if (init (idstr, true)) {
71 throw failed_constructor ();
76 AudioFileSource::AudioFileSource (Session& s, std::string path, Flag flags, SampleFormat samp_format, HeaderFormat hdr_format)
77 : AudioSource (s, path), _flags (flags)
79 /* constructor used for new internal-to-session files. file cannot exist */
81 if (init (path, false)) {
82 throw failed_constructor ();
86 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node)
87 : AudioSource (s, node), _flags (Flag (Writable|CanRename))
89 /* constructor used for existing internal-to-session files. file must exist */
91 if (set_state (node)) {
92 throw failed_constructor ();
95 if (init (_name, true)) {
96 throw failed_constructor ();
100 AudioFileSource::~AudioFileSource ()
103 unlink (_path.c_str());
104 unlink (peakpath.c_str());
109 AudioFileSource::removable () const
111 return (_flags & Removable) && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && length() == 0));
115 AudioFileSource::init (string pathstr, bool must_exist)
120 timeline_position = 0;
121 next_peak_clear_should_notify = false;
122 _peaks_built = false;
125 if (!find (pathstr, must_exist, is_new)) {
129 if (is_new && must_exist) {
138 AudioFileSource::peak_path (string audio_path)
140 return _session.peak_path_from_audio_path (audio_path);
144 AudioFileSource::old_peak_path (string audio_path)
146 /* XXX hardly bombproof! fix me */
148 struct stat stat_file;
149 struct stat stat_mount;
151 string mp = mountpoint (audio_path);
153 stat (audio_path.c_str(), &stat_file);
154 stat (mp.c_str(), &stat_mount);
158 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
160 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);
163 string res = peak_dir;
170 AudioFileSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
172 #ifdef HAVE_COREAUDIO
173 OSStatus err = noErr;
175 ExtAudioFileRef af = 0;
179 err = FSPathMakeRef ((UInt8*)path.c_str(), &ref, 0);
181 ExtAudioFileDispose (af);
185 err = ExtAudioFileOpen(&ref, &af);
187 ExtAudioFileDispose (af);
191 AudioStreamBasicDescription absd;
192 memset(&absd, 0, sizeof(absd));
193 size = sizeof(AudioStreamBasicDescription);
194 err = ExtAudioFileGetProperty(af,
195 kExtAudioFileProperty_FileDataFormat, &size, &absd);
197 ExtAudioFileDispose (af);
201 _info.samplerate = absd.mSampleRate;
202 _info.channels = absd.mChannelsPerFrame;
204 size = sizeof(_info.length);
205 err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length);
207 ExtAudioFileDispose (af);
211 size = sizeof(CFStringRef);
212 err = AudioFormatGetProperty(
213 kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name);
215 ExtAudioFileDispose (af);
219 _info.format_name = CFStringRefToStdString(name);
221 ExtAudioFileDispose (af);
225 #endif // HAVE_COREAUDIO
230 sf_info.format = 0; // libsndfile says to clear this before sf_open().
232 if ((sf = sf_open ((char*) path.c_str(), SFM_READ, &sf_info)) == 0) {
234 error_msg = sf_error_str (0, errbuf, sizeof (errbuf) - 1);
240 _info.samplerate = sf_info.samplerate;
241 _info.channels = sf_info.channels;
242 _info.length = sf_info.frames;
243 _info.format_name = string_compose("Format: %1, %2",
244 sndfile_major_format(sf_info.format),
245 sndfile_minor_format(sf_info.format));
250 AudioFileSource::get_state ()
252 XMLNode& root (AudioSource::get_state());
254 snprintf (buf, sizeof (buf), "0x%x", (int)_flags);
255 root.add_property ("flags", buf);
260 AudioFileSource::set_state (const XMLNode& node)
262 const XMLProperty* prop;
264 if (AudioSource::set_state (node)) {
268 if ((prop = node.property (X_("flags"))) != 0) {
271 sscanf (prop->value().c_str(), "0x%x", &ival);
272 _flags = Flag (ival);
284 AudioFileSource::mark_for_remove ()
290 _flags = Flag (_flags | Removable | RemoveAtDestroy);
294 AudioFileSource::mark_streaming_write_completed ()
300 Glib::Mutex::Lock lm (_lock);
302 next_peak_clear_should_notify = true;
304 if (_peaks_built || pending_peak_builds.empty()) {
306 PeaksReady (); /* EMIT SIGNAL */
311 AudioFileSource::mark_take (string id)
319 AudioFileSource::move_to_trash (const string trash_dir_name)
327 /* don't move the file across filesystems, just
328 stick it in the `trash_dir_name' directory
329 on whichever filesystem it was already on.
332 newpath = Glib::path_get_dirname (_path);
333 newpath = Glib::path_get_dirname (newpath);
336 newpath += trash_dir_name;
338 newpath += Glib::path_get_basename (_path);
340 if (access (newpath.c_str(), F_OK) == 0) {
342 /* the new path already exists, try versioning */
344 char buf[PATH_MAX+1];
348 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
351 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
352 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
356 if (version == 999) {
357 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
366 /* it doesn't exist, or we can't read it or something */
370 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
371 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
372 _path, newpath, strerror (errno))
377 if (::unlink (peakpath.c_str()) != 0) {
378 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
379 peakpath, _path, strerror (errno))
381 /* try to back out */
382 rename (newpath.c_str(), _path.c_str());
389 /* file can not be removed twice, since the operation is not idempotent */
391 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
397 AudioFileSource::find (string pathstr, bool must_exist, bool& isnew)
399 string::size_type pos;
404 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
406 if ((pos = pathstr.find_last_of (':')) == string::npos) {
409 pathstr = pathstr.substr (0, pos);
412 if (pathstr[0] != '/') {
414 /* non-absolute pathname: find pathstr in search path */
421 if (search_path.length() == 0) {
422 error << _("FileSource: search path not set") << endmsg;
426 split (search_path, dirs, ':');
430 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
433 if (fullpath[fullpath.length()-1] != '/') {
438 if (access (fullpath.c_str(), R_OK) == 0) {
446 error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path) << endmsg;
449 } else if (cnt == 0) {
452 error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, search_path) << endmsg;
465 /* external files and/or very very old style sessions include full paths */
468 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
470 if (access (_path.c_str(), R_OK) != 0) {
472 /* file does not exist or we cannot read it */
475 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
479 if (errno != ENOENT) {
480 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
502 AudioFileSource::set_search_path (string p)
508 AudioFileSource::set_header_position_offset (nframes_t offset)
510 header_position_offset = offset;
511 HeaderPositionOffsetChanged ();
515 AudioFileSource::set_timeline_position (nframes_t pos)
517 timeline_position = pos;
521 AudioFileSource::set_allow_remove_if_empty (bool yn)
528 _flags = Flag (_flags | RemovableIfEmpty);
530 _flags = Flag (_flags & ~RemovableIfEmpty);
535 AudioFileSource::set_name (string newname, bool destructive)
537 Glib::Mutex::Lock lm (_lock);
538 string oldpath = _path;
539 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
541 if (newpath.empty()) {
542 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
546 // Test whether newpath exists, if yes notify the user but continue.
547 if (access(newpath.c_str(),F_OK) == 0) {
548 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;
552 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
553 error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
557 _name = Glib::path_get_basename (newpath);
560 return rename_peakfile (peak_path (_path));
564 AudioFileSource::is_empty (Session& s, string path)
567 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (s, path, NoPeakFile, false));
570 ret = (afs->length() == 0);
577 AudioFileSource::setup_peakfile ()
579 if (!(_flags & NoPeakFile)) {
580 return initialize_peakfile (file_is_new, _path);