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/file_utils.h"
36 #include "pbd/mountpoint.h"
37 #include "pbd/stl_delete.h"
38 #include "pbd/strsplit.h"
39 #include "pbd/shortpath.h"
40 #include "pbd/stacktrace.h"
41 #include "pbd/enumwriter.h"
45 #include <pbd/gstdio_compat.h>
46 #include <glibmm/miscutils.h>
47 #include <glibmm/fileutils.h>
48 #include <glibmm/threads.h>
50 #include "ardour/audiofilesource.h"
51 #include "ardour/debug.h"
52 #include "ardour/sndfilesource.h"
53 #include "ardour/session.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 PBD::Signal0<void> AudioFileSource::HeaderPositionOffsetChanged;
72 framecnt_t AudioFileSource::header_position_offset = 0;
74 /* XXX maybe this too */
75 char AudioFileSource::bwf_serial_number[13] = "000000000000";
77 struct SizedSampleBuffer {
81 SizedSampleBuffer (framecnt_t sz) : size (sz) {
82 buf = new Sample[size];
85 ~SizedSampleBuffer() {
90 Glib::Threads::Private<SizedSampleBuffer> thread_interleave_buffer;
92 /** Constructor used for existing external-to-session files. */
93 AudioFileSource::AudioFileSource (Session& s, const string& path, Source::Flag flags)
94 : Source (s, DataType::AUDIO, path, flags)
95 , AudioSource (s, path)
96 /* note that external files have their own path as "origin" */
97 , FileSource (s, DataType::AUDIO, path, path, flags)
99 if (init (_path, true)) {
100 throw failed_constructor ();
104 /** Constructor used for new internal-to-session files. */
105 AudioFileSource::AudioFileSource (Session& s, const string& path, const string& origin, Source::Flag flags,
106 SampleFormat /*samp_format*/, HeaderFormat /*hdr_format*/)
107 : Source (s, DataType::AUDIO, path, flags)
108 , AudioSource (s, path)
109 , FileSource (s, DataType::AUDIO, path, origin, flags)
111 /* note that origin remains empty */
113 if (init (_path, false)) {
114 throw failed_constructor ();
118 /** Constructor used for existing internal-to-session files during crash
119 * recovery. File must exist
121 AudioFileSource::AudioFileSource (Session& s, const string& path, Source::Flag flags, bool /* ignored-exists-for-prototype differentiation */)
122 : Source (s, DataType::AUDIO, path, flags)
123 , AudioSource (s, path)
124 , FileSource (s, DataType::AUDIO, path, string(), flags)
126 /* note that origin remains empty */
128 if (init (_path, true)) {
129 throw failed_constructor ();
134 /** Constructor used for existing internal-to-session files via XML. File must exist. */
135 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
137 , AudioSource (s, node)
138 , FileSource (s, node, must_exist)
140 if (set_state (node, Stateful::loading_state_version)) {
141 throw failed_constructor ();
144 if (init (_path, must_exist)) {
145 throw failed_constructor ();
149 AudioFileSource::~AudioFileSource ()
151 DEBUG_TRACE (DEBUG::Destruction, string_compose ("AudioFileSource destructor %1, removable? %2\n", _path, removable()));
153 ::g_unlink (_path.c_str());
154 ::g_unlink (_peakpath.c_str());
159 AudioFileSource::init (const string& pathstr, bool must_exist)
161 return FileSource::init (pathstr, must_exist);
165 AudioFileSource::construct_peak_filepath (const string& audio_path, const bool in_session, const bool old_peak_name) const
169 base = audio_path.substr (0, audio_path.find_last_of ('.'));
174 base += (char) ('A' + _channel);
175 return _session.construct_peak_filepath (base, in_session, old_peak_name);
179 AudioFileSource::get_soundfile_info (const string& path, SoundFileInfo& _info, string& error_msg)
181 /* try sndfile first because it gets timecode info from .wav (BWF) if it exists,
182 which at present, ExtAudioFile from Apple seems unable to do.
185 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
189 #ifdef HAVE_COREAUDIO
190 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
193 #endif // HAVE_COREAUDIO
199 AudioFileSource::get_state ()
201 XMLNode& root (AudioSource::get_state());
203 snprintf (buf, sizeof (buf), "%u", _channel);
204 root.add_property (X_("channel"), buf);
205 root.add_property (X_("origin"), _origin);
210 AudioFileSource::set_state (const XMLNode& node, int version)
212 if (Source::set_state (node, version)) {
216 if (AudioSource::set_state (node, version)) {
220 if (FileSource::set_state (node, version)) {
228 AudioFileSource::mark_streaming_write_completed (const Lock& lock)
234 AudioSource::mark_streaming_write_completed (lock);
238 AudioFileSource::move_dependents_to_trash()
240 return ::g_unlink (_peakpath.c_str());
244 AudioFileSource::set_header_position_offset (framecnt_t offset)
246 header_position_offset = offset;
247 HeaderPositionOffsetChanged ();
251 AudioFileSource::is_empty (Session& /*s*/, string path)
256 if (!get_soundfile_info (path, info, err)) {
257 /* dangerous: we can't get info, so assume that its not empty */
261 return info.length == 0;
265 AudioFileSource::setup_peakfile ()
267 if (_session.deletion_in_progress()) {
270 if (!(_flags & NoPeakFile)) {
271 return initialize_peakfile (_path, within_session());
278 AudioFileSource::safe_audio_file_extension(const string& file)
280 const char* suffixes[] = {
304 #ifdef HAVE_COREAUDIO
316 ".sd2", ".SD2", // libsndfile supports sd2 also, but the resource fork is required to open.
317 #endif // HAVE_COREAUDIO
320 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
321 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
330 AudioFileSource::get_interleave_buffer (framecnt_t size)
332 SizedSampleBuffer* ssb;
334 if ((ssb = thread_interleave_buffer.get()) == 0) {
335 ssb = new SizedSampleBuffer (size);
336 thread_interleave_buffer.set (ssb);
339 if (ssb->size < size) {
340 ssb = new SizedSampleBuffer (size);
341 thread_interleave_buffer.set (ssb);