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.
24 #include <stdio.h> // for rename(), sigh
29 #include "pbd/convert.h"
30 #include "pbd/basename.h"
31 #include "pbd/mountpoint.h"
32 #include "pbd/stl_delete.h"
33 #include "pbd/strsplit.h"
34 #include "pbd/shortpath.h"
35 #include "pbd/enumwriter.h"
39 #include <glibmm/miscutils.h>
40 #include <glibmm/fileutils.h>
41 #include <glibmm/thread.h>
43 #include "ardour/audiofilesource.h"
44 #include "ardour/sndfile_helpers.h"
45 #include "ardour/sndfilesource.h"
46 #include "ardour/session.h"
47 #include "ardour/session_directory.h"
48 #include "ardour/source_factory.h"
49 #include "ardour/filename_extensions.h"
51 // if these headers come before sigc++ is included
52 // the parser throws ObjC++ errors. (nil is a keyword)
54 #include "ardour/coreaudiosource.h"
55 #include <AudioToolbox/ExtendedAudioFile.h>
56 #include <AudioToolbox/AudioFormat.h>
57 #endif // HAVE_COREAUDIO
61 using namespace ARDOUR;
65 ustring AudioFileSource::peak_dir = "";
67 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
68 uint64_t AudioFileSource::header_position_offset = 0;
70 /* XXX maybe this too */
71 char AudioFileSource::bwf_serial_number[13] = "000000000000";
73 struct SizedSampleBuffer {
77 SizedSampleBuffer (nframes_t sz) : size (sz) {
78 buf = new Sample[size];
81 ~SizedSampleBuffer() {
86 Glib::StaticPrivate<SizedSampleBuffer> thread_interleave_buffer = GLIBMM_STATIC_PRIVATE_INIT;
88 /** Constructor used for existing internal-to-session files. */
89 AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded, Source::Flag flags)
90 : Source (s, DataType::AUDIO, path, flags)
91 , AudioSource (s, path)
92 , FileSource (s, DataType::AUDIO, path, embedded, flags)
94 if (init (path, true)) {
95 throw failed_constructor ();
99 /** Constructor used for new internal-to-session files. */
100 AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded, Source::Flag flags,
101 SampleFormat samp_format, HeaderFormat hdr_format)
102 : Source (s, DataType::AUDIO, path, flags)
103 , AudioSource (s, path)
104 , FileSource (s, DataType::AUDIO, path, embedded, flags)
106 _is_embedded = false;
108 if (init (path, false)) {
109 throw failed_constructor ();
113 /** Constructor used for existing internal-to-session files. File must exist. */
114 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
116 , AudioSource (s, node)
117 , FileSource (s, node, must_exist)
119 if (set_state (node)) {
120 throw failed_constructor ();
123 if (init (_name, must_exist)) {
124 throw failed_constructor ();
128 AudioFileSource::~AudioFileSource ()
131 unlink (_path.c_str());
132 unlink (peakpath.c_str());
137 AudioFileSource::init (const ustring& pathstr, bool must_exist)
139 _peaks_built = false;
140 return FileSource::init (pathstr, must_exist);
144 AudioFileSource::peak_path (ustring audio_path)
148 base = PBD::basename_nosuffix (audio_path);
150 base += (char) ('A' + _channel);
152 return _session.peak_path (base);
156 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
160 /* check for the broken location in use by 2.0 for several months */
162 str = broken_peak_path (audio_path);
164 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
168 /* it would be nice to rename it but the nature of
169 the bug means that we can't reliably use it.
175 /* all native files are mono, so we can just rename
178 ::rename (str.c_str(), peak_path.c_str());
182 /* Nasty band-aid for older sessions that were created before we
183 used libsndfile for all audio files.
187 str = old_peak_path (audio_path);
188 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
197 AudioFileSource::broken_peak_path (ustring audio_path)
199 return _session.peak_path (audio_path);
203 AudioFileSource::old_peak_path (ustring audio_path)
205 /* XXX hardly bombproof! fix me */
207 struct stat stat_file;
208 struct stat stat_mount;
210 ustring mp = mountpoint (audio_path);
212 stat (audio_path.c_str(), &stat_file);
213 stat (mp.c_str(), &stat_mount);
217 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
219 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
222 ustring res = peak_dir;
224 res += peakfile_suffix;
230 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
232 #ifdef HAVE_COREAUDIO
233 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
236 #endif // HAVE_COREAUDIO
238 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
246 AudioFileSource::get_state ()
248 XMLNode& root (AudioSource::get_state());
250 snprintf (buf, sizeof (buf), "%u", _channel);
251 root.add_property (X_("channel"), buf);
256 AudioFileSource::set_state (const XMLNode& node)
258 if (Source::set_state (node)) {
262 if (AudioSource::set_state (node)) {
266 if (FileSource::set_state (node)) {
274 AudioFileSource::mark_streaming_write_completed ()
280 /* XXX notice that we're readers of _peaks_built
281 but we must hold a solid lock on PeaksReady.
284 Glib::Mutex::Lock lm (_lock);
287 PeaksReady (); /* EMIT SIGNAL */
292 AudioFileSource::move_dependents_to_trash()
294 return ::unlink (peakpath.c_str());
298 AudioFileSource::set_header_position_offset (nframes_t offset)
300 header_position_offset = offset;
301 HeaderPositionOffsetChanged ();
305 AudioFileSource::is_empty (Session& s, ustring path)
310 if (!get_soundfile_info (path, info, err)) {
311 /* dangerous: we can't get info, so assume that its not empty */
315 return info.length == 0;
319 AudioFileSource::setup_peakfile ()
321 if (!(_flags & NoPeakFile)) {
322 return initialize_peakfile (_file_is_new, _path);
329 AudioFileSource::safe_audio_file_extension(const ustring& file)
331 const char* suffixes[] = {
350 #ifdef HAVE_COREAUDIO
354 #endif // HAVE_COREAUDIO
357 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
358 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
367 AudioFileSource::get_interleave_buffer (nframes_t size)
369 SizedSampleBuffer* ssb;
371 if ((ssb = thread_interleave_buffer.get()) == 0) {
372 ssb = new SizedSampleBuffer (size);
373 thread_interleave_buffer.set (ssb);
376 if (ssb->size < size) {
377 ssb = new SizedSampleBuffer (size);
378 thread_interleave_buffer.set (ssb);