2 Copyright (C) 2011 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.
20 #include "libardour-config.h"
26 #include <glibmm/fileutils.h>
28 #include "pbd/error.h"
29 #include "pbd/convert.h"
30 #include "pbd/enumwriter.h"
32 #include "ardour/audioplaylist.h"
33 #include "ardour/audio_playlist_source.h"
34 #include "ardour/audioregion.h"
35 #include "ardour/debug.h"
36 #include "ardour/session.h"
37 #include "ardour/session_directory.h"
38 #include "ardour/session_playlists.h"
39 #include "ardour/source_factory.h"
44 using namespace ARDOUR;
47 AudioPlaylistSource::AudioPlaylistSource (Session& s, const std::string& name, boost::shared_ptr<AudioPlaylist> p,
48 uint32_t chn, frameoffset_t begin, framecnt_t len, bool copy, Source::Flag flags)
49 : Source (s, DataType::AUDIO, name)
50 , AudioSource (s, name)
51 , _playlist_channel (chn)
53 /* PlaylistSources are never writable, renameable, removable or destructive */
54 _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
58 _playlist_offset = begin;
59 _playlist_length = len;
61 _playlist.reset (new AudioPlaylist (p, begin, len, "XXXNAMEXXX", true));
63 _playlist_length = len;
66 _peak_path = tempnam (_session.session_directory().peak_path().to_string().c_str(), "apspk");
69 AudioPlaylistSource::AudioPlaylistSource (Session& s, const XMLNode& node)
70 : Source (s, DataType::AUDIO, "toBeRenamed")
71 , AudioSource (s, node)
73 /* PlaylistSources are never writable, renameable, removable or destructive */
74 _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
76 set_state (node, 3000);
79 AudioPlaylistSource::~AudioPlaylistSource ()
84 AudioPlaylistSource::get_state ()
86 XMLNode& node (AudioSource::get_state ());
88 snprintf (buf, sizeof (buf), "%" PRIi64, _playlist_offset);
89 node.add_property ("offset", buf);
90 snprintf (buf, sizeof (buf), "%" PRIu64, _playlist_length);
91 node.add_property ("length", buf);
92 snprintf (buf, sizeof (buf), "%" PRIu32, _playlist_channel);
93 node.add_property ("channel", buf);
94 node.add_property ("name", name());
95 node.add_property ("peak-path", _peak_path);
101 AudioPlaylistSource::set_state (const XMLNode& node, int /* version */)
103 /* get playlist ID */
105 const XMLProperty *prop = node.property (X_("playlist"));
108 throw failed_constructor ();
111 PBD::ID id (prop->value());
115 boost::shared_ptr<Playlist> p = _session.playlists->by_id (id);
118 throw failed_constructor ();
121 /* other properties */
123 if ((prop = node.property (X_("name"))) == 0) {
124 throw failed_constructor ();
127 set_name (prop->value());
129 if ((prop = node.property (X_("offset"))) == 0) {
130 throw failed_constructor ();
132 sscanf (prop->value().c_str(), "%" PRIi64, &_playlist_offset);
134 if ((prop = node.property (X_("length"))) == 0) {
135 throw failed_constructor ();
138 sscanf (prop->value().c_str(), "%" PRIu64, &_playlist_length);
140 if ((prop = node.property (X_("channel"))) == 0) {
141 throw failed_constructor ();
144 sscanf (prop->value().c_str(), "%" PRIu32, &_playlist_channel);
146 if ((prop = node.property (X_("peak-path"))) == 0) {
147 throw failed_constructor ();
150 _peak_path = prop->value ();
156 AudioPlaylistSource::read_unlocked (Sample* dst, framepos_t start, framecnt_t cnt) const
160 pair<framepos_t,framepos_t> extent = _playlist->get_extent();
162 /* we must be careful not to read beyond the end of our "section" of
163 * the playlist, because otherwise we may read data that exists, but
164 * is not supposed be part of our data.
167 if (cnt > _playlist_length - start) {
168 to_read = _playlist_length - start;
169 to_zero = cnt - to_read;
175 _playlist->read (dst, 0, 0, start+_playlist_offset, to_read, _playlist_channel);
178 memset (dst+to_read, 0, sizeof (Sample) * to_zero);
185 AudioPlaylistSource::write_unlocked (Sample *src, framecnt_t cnt)
187 fatal << string_compose (_("programming error: %1"), "AudioPlaylistSource::write() called - should be impossible") << endmsg;
193 AudioPlaylistSource::empty () const
195 return !_playlist || _playlist->empty();
199 AudioPlaylistSource::n_channels () const
201 /* use just the first region to decide */
207 boost::shared_ptr<Region> r = _playlist->region_list().front ();
208 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
210 return ar->audio_source()->n_channels ();
214 AudioPlaylistSource::sample_rate () const
216 /* use just the first region to decide */
219 _session.frame_rate ();
222 boost::shared_ptr<Region> r = _playlist->region_list().front ();
223 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
225 return ar->audio_source()->sample_rate ();
229 AudioPlaylistSource::setup_peakfile ()
231 /* the peak data is setup once and once only
234 if (!Glib::file_test (_peak_path, Glib::FILE_TEST_EXISTS)) {
235 /* the 2nd argument here will be passed
236 in to ::peak_path, and is irrelevant
237 since our peak file path is fixed and
238 not dependent on anything.
241 return initialize_peakfile (true, string());
247 AudioPlaylistSource::peak_path (string /*audio_path*/)