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.
21 #include "libardour-config.h"
28 #include <stdio.h> // for rename(), sigh
33 #include "pbd/convert.h"
34 #include "pbd/basename.h"
35 #include "pbd/mountpoint.h"
36 #include "pbd/stl_delete.h"
37 #include "pbd/strsplit.h"
38 #include "pbd/shortpath.h"
39 #include "pbd/enumwriter.h"
43 #include <glibmm/miscutils.h>
44 #include <glibmm/fileutils.h>
45 #include <glibmm/thread.h>
47 #include "ardour/audiofilesource.h"
48 #include "ardour/debug.h"
49 #include "ardour/sndfile_helpers.h"
50 #include "ardour/sndfilesource.h"
51 #include "ardour/session.h"
52 #include "ardour/session_directory.h"
53 #include "ardour/source_factory.h"
54 #include "ardour/filename_extensions.h"
56 // if these headers come before sigc++ is included
57 // the parser throws ObjC++ errors. (nil is a keyword)
59 #include "ardour/coreaudiosource.h"
60 #include <AudioToolbox/ExtendedAudioFile.h>
61 #include <AudioToolbox/AudioFormat.h>
62 #endif // HAVE_COREAUDIO
67 using namespace ARDOUR;
71 ustring AudioFileSource::peak_dir = "";
73 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
74 uint64_t AudioFileSource::header_position_offset = 0;
76 /* XXX maybe this too */
77 char AudioFileSource::bwf_serial_number[13] = "000000000000";
79 struct SizedSampleBuffer {
83 SizedSampleBuffer (nframes_t sz) : size (sz) {
84 buf = new Sample[size];
87 ~SizedSampleBuffer() {
92 Glib::StaticPrivate<SizedSampleBuffer> thread_interleave_buffer = GLIBMM_STATIC_PRIVATE_INIT;
94 /** Constructor used for existing internal-to-session files. */
95 AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded, Source::Flag flags)
96 : Source (s, DataType::AUDIO, path, flags)
97 , AudioSource (s, path)
98 , FileSource (s, DataType::AUDIO, path, embedded, flags)
100 if (init (path, true)) {
101 throw failed_constructor ();
105 /** Constructor used for new internal-to-session files. */
106 AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded, Source::Flag flags,
107 SampleFormat /*samp_format*/, HeaderFormat /*hdr_format*/)
108 : Source (s, DataType::AUDIO, path, flags)
109 , AudioSource (s, path)
110 , FileSource (s, DataType::AUDIO, path, embedded, flags)
112 _is_embedded = false;
114 if (init (path, false)) {
115 throw failed_constructor ();
119 /** Constructor used for existing internal-to-session files. File must exist. */
120 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
122 , AudioSource (s, node)
123 , FileSource (s, node, must_exist)
125 if (set_state (node, Stateful::loading_state_version)) {
126 throw failed_constructor ();
129 if (init (_name, must_exist)) {
130 throw failed_constructor ();
134 AudioFileSource::~AudioFileSource ()
136 DEBUG_TRACE (DEBUG::Destruction, string_compose ("AudioFileSource destructor %1, removable? %2\n", _path, removable()));
138 unlink (_path.c_str());
139 unlink (peakpath.c_str());
144 AudioFileSource::init (const ustring& pathstr, bool must_exist)
146 _peaks_built = false;
147 return FileSource::init (pathstr, must_exist);
151 AudioFileSource::peak_path (ustring audio_path)
155 base = PBD::basename_nosuffix (audio_path);
157 base += (char) ('A' + _channel);
159 return _session.peak_path (base);
163 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
167 /* check for the broken location in use by 2.0 for several months */
169 str = broken_peak_path (audio_path);
171 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
175 /* it would be nice to rename it but the nature of
176 the bug means that we can't reliably use it.
182 /* all native files are mono, so we can just rename
185 ::rename (str.c_str(), peak_path.c_str());
189 /* Nasty band-aid for older sessions that were created before we
190 used libsndfile for all audio files.
194 str = old_peak_path (audio_path);
195 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
204 AudioFileSource::broken_peak_path (ustring audio_path)
206 return _session.peak_path (audio_path);
210 AudioFileSource::old_peak_path (ustring audio_path)
212 /* XXX hardly bombproof! fix me */
214 struct stat stat_file;
215 struct stat stat_mount;
217 ustring mp = mountpoint (audio_path);
219 stat (audio_path.c_str(), &stat_file);
220 stat (mp.c_str(), &stat_mount);
224 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
226 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
229 ustring res = peak_dir;
231 res += peakfile_suffix;
237 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
239 #ifdef HAVE_COREAUDIO
240 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
243 #endif // HAVE_COREAUDIO
245 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
253 AudioFileSource::get_state ()
255 XMLNode& root (AudioSource::get_state());
257 snprintf (buf, sizeof (buf), "%u", _channel);
258 root.add_property (X_("channel"), buf);
263 AudioFileSource::set_state (const XMLNode& node, int version)
265 if (Source::set_state (node, version)) {
269 if (AudioSource::set_state (node, version)) {
273 if (FileSource::set_state (node, version)) {
281 AudioFileSource::mark_streaming_write_completed ()
287 /* XXX notice that we're readers of _peaks_built
288 but we must hold a solid lock on PeaksReady.
291 Glib::Mutex::Lock lm (_lock);
294 PeaksReady (); /* EMIT SIGNAL */
299 AudioFileSource::move_dependents_to_trash()
301 return ::unlink (peakpath.c_str());
305 AudioFileSource::set_header_position_offset (nframes_t offset)
307 header_position_offset = offset;
308 HeaderPositionOffsetChanged ();
312 AudioFileSource::is_empty (Session& /*s*/, ustring path)
317 if (!get_soundfile_info (path, info, err)) {
318 /* dangerous: we can't get info, so assume that its not empty */
322 return info.length == 0;
326 AudioFileSource::setup_peakfile ()
328 if (!(_flags & NoPeakFile)) {
329 return initialize_peakfile (_file_is_new, _path);
336 AudioFileSource::safe_audio_file_extension(const ustring& file)
338 const char* suffixes[] = {
356 #ifdef HAVE_COREAUDIO
360 #endif // HAVE_COREAUDIO
363 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
364 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
373 AudioFileSource::get_interleave_buffer (nframes_t size)
375 SizedSampleBuffer* ssb;
377 if ((ssb = thread_interleave_buffer.get()) == 0) {
378 ssb = new SizedSampleBuffer (size);
379 thread_interleave_buffer.set (ssb);
382 if (ssb->size < size) {
383 ssb = new SizedSampleBuffer (size);
384 thread_interleave_buffer.set (ssb);