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 <glib/gstdio.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 string AudioFileSource::peak_dir = "";
73 PBD::Signal0<void> AudioFileSource::HeaderPositionOffsetChanged;
74 framecnt_t AudioFileSource::header_position_offset = 0;
76 /* XXX maybe this too */
77 char AudioFileSource::bwf_serial_number[13] = "000000000000";
79 struct SizedSampleBuffer {
83 SizedSampleBuffer (framecnt_t sz) : size (sz) {
84 buf = new Sample[size];
87 ~SizedSampleBuffer() {
92 Glib::Threads::Private<SizedSampleBuffer> thread_interleave_buffer;
94 /** Constructor used for existing external-to-session files. */
95 AudioFileSource::AudioFileSource (Session& s, const string& path, Source::Flag flags)
96 : Source (s, DataType::AUDIO, path, flags)
97 , AudioSource (s, path)
98 /* note that external files have their own path as "origin" */
99 , FileSource (s, DataType::AUDIO, path, path, flags)
101 if (init (_path, true)) {
102 throw failed_constructor ();
106 /** Constructor used for new internal-to-session files. */
107 AudioFileSource::AudioFileSource (Session& s, const string& path, const string& origin, Source::Flag flags,
108 SampleFormat /*samp_format*/, HeaderFormat /*hdr_format*/)
109 : Source (s, DataType::AUDIO, path, flags)
110 , AudioSource (s, path)
111 , FileSource (s, DataType::AUDIO, path, origin, flags)
113 /* note that origin remains empty */
115 if (init (_path, false)) {
116 throw failed_constructor ();
120 /** Constructor used for existing internal-to-session files during crash
121 * recovery. File must exist
123 AudioFileSource::AudioFileSource (Session& s, const string& path, Source::Flag flags, bool /* ignored-exists-for-prototype differentiation */)
124 : Source (s, DataType::AUDIO, path, flags)
125 , AudioSource (s, path)
126 , FileSource (s, DataType::AUDIO, path, string(), flags)
128 /* note that origin remains empty */
130 if (init (_path, true)) {
131 throw failed_constructor ();
136 /** Constructor used for existing internal-to-session files via XML. File must exist. */
137 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
139 , AudioSource (s, node)
140 , FileSource (s, node, must_exist)
142 if (set_state (node, Stateful::loading_state_version)) {
143 throw failed_constructor ();
146 if (init (_path, must_exist)) {
147 throw failed_constructor ();
151 AudioFileSource::~AudioFileSource ()
153 DEBUG_TRACE (DEBUG::Destruction, string_compose ("AudioFileSource destructor %1, removable? %2\n", _path, removable()));
155 ::g_unlink (_path.c_str());
156 ::g_unlink (_peakpath.c_str());
161 AudioFileSource::init (const string& pathstr, bool must_exist)
163 return FileSource::init (pathstr, must_exist);
167 AudioFileSource::construct_peak_filepath (const string& audio_path, bool oldformat) const
171 base = audio_path.substr (0, audio_path.find_last_of ('.'));
176 base += (char) ('A' + _channel);
177 return _session.construct_peak_filepath (base, oldformat);
181 AudioFileSource::get_soundfile_info (const string& path, SoundFileInfo& _info, string& error_msg)
183 /* try sndfile first because it gets timecode info from .wav (BWF) if it exists,
184 which at present, ExtAudioFile from Apple seems unable to do.
187 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
191 #ifdef HAVE_COREAUDIO
192 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
195 #endif // HAVE_COREAUDIO
201 AudioFileSource::get_state ()
203 XMLNode& root (AudioSource::get_state());
205 snprintf (buf, sizeof (buf), "%u", _channel);
206 root.add_property (X_("channel"), buf);
207 root.add_property (X_("origin"), _origin);
212 AudioFileSource::set_state (const XMLNode& node, int version)
214 if (Source::set_state (node, version)) {
218 if (AudioSource::set_state (node, version)) {
222 if (FileSource::set_state (node, version)) {
230 AudioFileSource::mark_streaming_write_completed (const Lock& lock)
236 AudioSource::mark_streaming_write_completed (lock);
240 AudioFileSource::move_dependents_to_trash()
242 return ::g_unlink (_peakpath.c_str());
246 AudioFileSource::set_header_position_offset (framecnt_t offset)
248 header_position_offset = offset;
249 HeaderPositionOffsetChanged ();
253 AudioFileSource::is_empty (Session& /*s*/, string path)
258 if (!get_soundfile_info (path, info, err)) {
259 /* dangerous: we can't get info, so assume that its not empty */
263 return info.length == 0;
267 AudioFileSource::setup_peakfile ()
269 if (_session.deletion_in_progress()) {
272 if (!(_flags & NoPeakFile)) {
273 return initialize_peakfile (_path);
280 AudioFileSource::safe_audio_file_extension(const string& file)
282 const char* suffixes[] = {
306 #ifdef HAVE_COREAUDIO
318 ".sd2", ".SD2", // libsndfile supports sd2 also, but the resource fork is required to open.
319 #endif // HAVE_COREAUDIO
322 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
323 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
332 AudioFileSource::get_interleave_buffer (framecnt_t size)
334 SizedSampleBuffer* ssb;
336 if ((ssb = thread_interleave_buffer.get()) == 0) {
337 ssb = new SizedSampleBuffer (size);
338 thread_interleave_buffer.set (ssb);
341 if (ssb->size < size) {
342 ssb = new SizedSampleBuffer (size);
343 thread_interleave_buffer.set (ssb);