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
62 using namespace ARDOUR;
66 ustring AudioFileSource::peak_dir = "";
68 sigc::signal<void> AudioFileSource::HeaderPositionOffsetChanged;
69 uint64_t AudioFileSource::header_position_offset = 0;
71 /* XXX maybe this too */
72 char AudioFileSource::bwf_serial_number[13] = "000000000000";
74 struct SizedSampleBuffer {
78 SizedSampleBuffer (nframes_t sz) : size (sz) {
79 buf = new Sample[size];
82 ~SizedSampleBuffer() {
87 Glib::StaticPrivate<SizedSampleBuffer> thread_interleave_buffer = GLIBMM_STATIC_PRIVATE_INIT;
89 /** Constructor used for existing internal-to-session files. */
90 AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded, Source::Flag flags)
91 : Source (s, DataType::AUDIO, path, flags)
92 , AudioSource (s, path)
93 , FileSource (s, DataType::AUDIO, path, embedded, flags)
95 if (init (path, true)) {
96 throw failed_constructor ();
100 /** Constructor used for new internal-to-session files. */
101 AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded, Source::Flag flags,
102 SampleFormat samp_format, HeaderFormat hdr_format)
103 : Source (s, DataType::AUDIO, path, flags)
104 , AudioSource (s, path)
105 , FileSource (s, DataType::AUDIO, path, embedded, flags)
107 _is_embedded = false;
109 if (init (path, false)) {
110 throw failed_constructor ();
114 /** Constructor used for existing internal-to-session files. File must exist. */
115 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
117 , AudioSource (s, node)
118 , FileSource (s, node, must_exist)
120 if (set_state (node)) {
121 throw failed_constructor ();
124 if (init (_name, must_exist)) {
125 throw failed_constructor ();
129 AudioFileSource::~AudioFileSource ()
132 unlink (_path.c_str());
133 unlink (peakpath.c_str());
138 AudioFileSource::init (const ustring& pathstr, bool must_exist)
140 _peaks_built = false;
141 return FileSource::init (pathstr, must_exist);
145 AudioFileSource::peak_path (ustring audio_path)
149 base = PBD::basename_nosuffix (audio_path);
151 base += (char) ('A' + _channel);
153 return _session.peak_path (base);
157 AudioFileSource::find_broken_peakfile (ustring peak_path, ustring audio_path)
161 /* check for the broken location in use by 2.0 for several months */
163 str = broken_peak_path (audio_path);
165 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
169 /* it would be nice to rename it but the nature of
170 the bug means that we can't reliably use it.
176 /* all native files are mono, so we can just rename
179 ::rename (str.c_str(), peak_path.c_str());
183 /* Nasty band-aid for older sessions that were created before we
184 used libsndfile for all audio files.
188 str = old_peak_path (audio_path);
189 if (Glib::file_test (str, Glib::FILE_TEST_EXISTS)) {
198 AudioFileSource::broken_peak_path (ustring audio_path)
200 return _session.peak_path (audio_path);
204 AudioFileSource::old_peak_path (ustring audio_path)
206 /* XXX hardly bombproof! fix me */
208 struct stat stat_file;
209 struct stat stat_mount;
211 ustring mp = mountpoint (audio_path);
213 stat (audio_path.c_str(), &stat_file);
214 stat (mp.c_str(), &stat_mount);
218 snprintf (buf, sizeof (buf), "%u-%u-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
220 snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, _channel);
223 ustring res = peak_dir;
225 res += peakfile_suffix;
231 AudioFileSource::get_soundfile_info (ustring path, SoundFileInfo& _info, string& error_msg)
233 #ifdef HAVE_COREAUDIO
234 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
237 #endif // HAVE_COREAUDIO
239 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
247 AudioFileSource::get_state ()
249 XMLNode& root (AudioSource::get_state());
251 snprintf (buf, sizeof (buf), "%u", _channel);
252 root.add_property (X_("channel"), buf);
257 AudioFileSource::set_state (const XMLNode& node)
259 if (Source::set_state (node)) {
263 if (AudioSource::set_state (node)) {
267 if (FileSource::set_state (node)) {
275 AudioFileSource::mark_streaming_write_completed ()
281 /* XXX notice that we're readers of _peaks_built
282 but we must hold a solid lock on PeaksReady.
285 Glib::Mutex::Lock lm (_lock);
288 PeaksReady (); /* EMIT SIGNAL */
293 AudioFileSource::move_dependents_to_trash()
295 return ::unlink (peakpath.c_str());
299 AudioFileSource::set_header_position_offset (nframes_t offset)
301 header_position_offset = offset;
302 HeaderPositionOffsetChanged ();
306 AudioFileSource::is_empty (Session& s, ustring path)
311 if (!get_soundfile_info (path, info, err)) {
312 /* dangerous: we can't get info, so assume that its not empty */
316 return info.length == 0;
320 AudioFileSource::setup_peakfile ()
322 if (!(_flags & NoPeakFile)) {
323 return initialize_peakfile (_file_is_new, _path);
330 AudioFileSource::safe_audio_file_extension(const ustring& file)
332 const char* suffixes[] = {
351 #ifdef HAVE_COREAUDIO
355 #endif // HAVE_COREAUDIO
358 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
359 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
368 AudioFileSource::get_interleave_buffer (nframes_t size)
370 SizedSampleBuffer* ssb;
372 if ((ssb = thread_interleave_buffer.get()) == 0) {
373 ssb = new SizedSampleBuffer (size);
374 thread_interleave_buffer.set (ssb);
377 if (ssb->size < size) {
378 ssb = new SizedSampleBuffer (size);
379 thread_interleave_buffer.set (ssb);