2 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2007-2017 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "libardour-config.h"
31 #include <stdio.h> // for rename(), sigh
36 #include "pbd/gstdio_compat.h"
37 #include "pbd/convert.h"
38 #include "pbd/basename.h"
39 #include "pbd/file_utils.h"
40 #include "pbd/mountpoint.h"
41 #include "pbd/stl_delete.h"
42 #include "pbd/strsplit.h"
43 #include "pbd/shortpath.h"
44 #include "pbd/stacktrace.h"
45 #include "pbd/enumwriter.h"
49 #include <glibmm/miscutils.h>
50 #include <glibmm/fileutils.h>
51 #include <glibmm/threads.h>
53 #include "ardour/audiofilesource.h"
54 #include "ardour/debug.h"
55 #include "ardour/sndfilesource.h"
56 #include "ardour/session.h"
57 #include "ardour/filename_extensions.h"
59 // if these headers come before sigc++ is included
60 // the parser throws ObjC++ errors. (nil is a keyword)
62 #include "ardour/coreaudiosource.h"
63 #include <AudioToolbox/ExtendedAudioFile.h>
64 #include <AudioToolbox/AudioFormat.h>
65 #endif // HAVE_COREAUDIO
70 using namespace ARDOUR;
74 PBD::Signal0<void> AudioFileSource::HeaderPositionOffsetChanged;
75 samplecnt_t AudioFileSource::header_position_offset = 0;
77 /* XXX maybe this too */
78 char AudioFileSource::bwf_serial_number[13] = "000000000000";
80 struct SizedSampleBuffer {
84 SizedSampleBuffer (samplecnt_t sz) : size (sz) {
85 buf = new Sample[size];
88 ~SizedSampleBuffer() {
93 Glib::Threads::Private<SizedSampleBuffer> thread_interleave_buffer;
95 /** Constructor used for existing external-to-session files. */
96 AudioFileSource::AudioFileSource (Session& s, const string& path, Source::Flag flags)
97 : Source (s, DataType::AUDIO, path, flags)
98 , AudioSource (s, path)
99 /* note that external files have their own path as "origin" */
100 , FileSource (s, DataType::AUDIO, path, path, flags)
102 if (init (_path, true)) {
103 throw failed_constructor ();
107 /** Constructor used for new internal-to-session files. */
108 AudioFileSource::AudioFileSource (Session& s, const string& path, const string& origin, Source::Flag flags,
109 SampleFormat /*samp_format*/, HeaderFormat /*hdr_format*/)
110 : Source (s, DataType::AUDIO, path, flags)
111 , AudioSource (s, path)
112 , FileSource (s, DataType::AUDIO, path, origin, flags)
114 /* note that origin remains empty */
116 if (init (_path, false)) {
117 throw failed_constructor ();
121 /** Constructor used for existing internal-to-session files during crash
122 * recovery. File must exist
124 AudioFileSource::AudioFileSource (Session& s, const string& path, Source::Flag flags, bool /* ignored-exists-for-prototype differentiation */)
125 : Source (s, DataType::AUDIO, path, flags)
126 , AudioSource (s, path)
127 , FileSource (s, DataType::AUDIO, path, string(), flags)
129 /* note that origin remains empty */
131 if (init (_path, true)) {
132 throw failed_constructor ();
137 /** Constructor used for sources listed in session-files (XML)
138 * and missing sources (SilentFileSource).
140 * If _origin is an absolute path after ::set_state(), then the
141 * file is external to the session.
143 AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exist)
145 , AudioSource (s, node)
146 , FileSource (s, node, must_exist)
148 if (set_state (node, Stateful::loading_state_version)) {
149 throw failed_constructor ();
152 if (Glib::path_is_absolute (_origin)) {
156 if (init (_path, must_exist)) {
157 throw failed_constructor ();
161 AudioFileSource::~AudioFileSource ()
163 DEBUG_TRACE (DEBUG::Destruction, string_compose ("AudioFileSource destructor %1, removable? %2\n", _path, removable()));
165 ::g_unlink (_path.c_str());
166 ::g_unlink (_peakpath.c_str());
171 AudioFileSource::init (const string& pathstr, bool must_exist)
173 return FileSource::init (pathstr, must_exist);
177 AudioFileSource::construct_peak_filepath (const string& audio_path, const bool in_session, const bool old_peak_name) const
181 base = audio_path.substr (0, audio_path.find_last_of ('.'));
186 base += (char) ('A' + _channel);
187 return _session.construct_peak_filepath (base, in_session, old_peak_name);
191 AudioFileSource::get_soundfile_info (const string& path, SoundFileInfo& _info, string& error_msg)
193 /* try sndfile first because it gets timecode info from .wav (BWF) if it exists,
194 which at present, ExtAudioFile from Apple seems unable to do.
197 if (SndFileSource::get_soundfile_info (path, _info, error_msg) != 0) {
201 #ifdef HAVE_COREAUDIO
202 if (CoreAudioSource::get_soundfile_info (path, _info, error_msg) == 0) {
205 #endif // HAVE_COREAUDIO
211 AudioFileSource::get_state ()
213 XMLNode& root (AudioSource::get_state());
214 root.set_property (X_("channel"), _channel);
215 root.set_property (X_("origin"), _origin);
216 root.set_property (X_("gain"), _gain);
221 AudioFileSource::set_state (const XMLNode& node, int version)
223 if (Source::set_state (node, version)) {
227 if (AudioSource::set_state (node, version)) {
231 if (FileSource::set_state (node, version)) {
239 AudioFileSource::mark_streaming_write_completed (const Lock& lock)
245 AudioSource::mark_streaming_write_completed (lock);
249 AudioFileSource::move_dependents_to_trash()
251 return ::g_unlink (_peakpath.c_str());
255 AudioFileSource::set_header_position_offset (samplecnt_t offset)
257 header_position_offset = offset;
258 HeaderPositionOffsetChanged ();
262 AudioFileSource::is_empty (Session& /*s*/, string path)
267 if (!get_soundfile_info (path, info, err)) {
268 /* dangerous: we can't get info, so assume that its not empty */
272 return info.length == 0;
276 AudioFileSource::setup_peakfile ()
278 if (_session.deletion_in_progress()) {
281 if (!(_flags & NoPeakFile)) {
282 return initialize_peakfile (_path, within_session());
289 AudioFileSource::set_gain (float g, bool temporarily)
303 AudioFileSource::safe_audio_file_extension(const string& file)
305 const char* suffixes[] = {
329 #ifdef HAVE_COREAUDIO
341 ".sd2", ".SD2", // libsndfile supports sd2 also, but the resource fork is required to open.
342 #endif // HAVE_COREAUDIO
345 for (size_t n = 0; n < sizeof(suffixes)/sizeof(suffixes[0]); ++n) {
346 if (file.rfind (suffixes[n]) == file.length() - strlen (suffixes[n])) {
355 AudioFileSource::get_interleave_buffer (samplecnt_t size)
357 SizedSampleBuffer* ssb;
359 if ((ssb = thread_interleave_buffer.get()) == 0) {
360 ssb = new SizedSampleBuffer (size);
361 thread_interleave_buffer.set (ssb);
364 if (ssb->size < size) {
365 ssb = new SizedSampleBuffer (size);
366 thread_interleave_buffer.set (ssb);