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>
27 #include <glibmm/miscutils.h>
29 #include "pbd/error.h"
30 #include "pbd/convert.h"
31 #include "pbd/enumwriter.h"
33 #include "ardour/audioplaylist.h"
34 #include "ardour/audio_playlist_source.h"
35 #include "ardour/audioregion.h"
36 #include "ardour/debug.h"
37 #include "ardour/session.h"
38 #include "ardour/session_directory.h"
39 #include "ardour/session_playlists.h"
40 #include "ardour/source_factory.h"
45 using namespace ARDOUR;
48 AudioPlaylistSource::AudioPlaylistSource (Session& s, const std::string& name, boost::shared_ptr<AudioPlaylist> p,
49 uint32_t chn, frameoffset_t begin, framecnt_t len, bool copy, Source::Flag flags)
50 : Source (s, DataType::AUDIO, name)
51 , AudioSource (s, name)
53 , _playlist_channel (chn)
55 /* PlaylistSources are never writable, renameable, removable or destructive */
56 _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
60 _playlist_offset = begin;
61 _playlist_length = len;
63 _playlist.reset (new AudioPlaylist (p, begin, len, "XXXNAMEXXX", true));
65 _playlist_length = len;
69 _peak_path = Glib::build_filename (_session.session_directory().peak_path().to_string(), name);
70 _level = _playlist->max_source_level () + 1;
71 ensure_buffers_for_level (_level);
74 AudioPlaylistSource::AudioPlaylistSource (Session& s, const XMLNode& node)
75 : Source (s, DataType::AUDIO, "toBeRenamed")
76 , AudioSource (s, node)
78 /* PlaylistSources are never writable, renameable, removable or destructive */
79 _flags = Flag (_flags & ~(Writable|CanRename|Removable|RemovableIfEmpty|RemoveAtDestroy|Destructive));
81 set_state (node, 3000);
84 AudioPlaylistSource::~AudioPlaylistSource ()
89 AudioPlaylistSource::get_state ()
91 XMLNode& node (AudioSource::get_state ());
93 snprintf (buf, sizeof (buf), "%" PRIi64, _playlist_offset);
94 node.add_property ("offset", buf);
95 snprintf (buf, sizeof (buf), "%" PRIu64, _playlist_length);
96 node.add_property ("length", buf);
97 snprintf (buf, sizeof (buf), "%" PRIu32, _playlist_channel);
98 node.add_property ("channel", buf);
99 node.add_property ("name", name());
100 node.add_property ("peak-path", _peak_path);
106 AudioPlaylistSource::set_state (const XMLNode& node, int /* version */)
108 /* get playlist ID */
110 const XMLProperty *prop = node.property (X_("playlist"));
113 throw failed_constructor ();
116 PBD::ID id (prop->value());
120 boost::shared_ptr<Playlist> p = _session.playlists->by_id (id);
121 _playlist = boost::dynamic_pointer_cast<AudioPlaylist>(p);
124 throw failed_constructor ();
127 pair<framepos_t,framepos_t> extent = _playlist->get_extent();
128 _length = extent.second - extent.first;
130 /* other properties */
132 if ((prop = node.property (X_("name"))) == 0) {
133 throw failed_constructor ();
136 set_name (prop->value());
138 if ((prop = node.property (X_("offset"))) == 0) {
139 throw failed_constructor ();
141 sscanf (prop->value().c_str(), "%" PRIi64, &_playlist_offset);
143 if ((prop = node.property (X_("length"))) == 0) {
144 throw failed_constructor ();
147 sscanf (prop->value().c_str(), "%" PRIu64, &_playlist_length);
149 if ((prop = node.property (X_("channel"))) == 0) {
150 throw failed_constructor ();
153 sscanf (prop->value().c_str(), "%" PRIu32, &_playlist_channel);
155 if ((prop = node.property (X_("peak-path"))) == 0) {
156 throw failed_constructor ();
159 _peak_path = prop->value ();
161 _level = _playlist->max_source_level ();
162 ensure_buffers_for_level (_level);
168 AudioPlaylistSource::read_unlocked (Sample* dst, framepos_t start, framecnt_t cnt) const
172 pair<framepos_t,framepos_t> extent = _playlist->get_extent();
174 /* we must be careful not to read beyond the end of our "section" of
175 * the playlist, because otherwise we may read data that exists, but
176 * is not supposed be part of our data.
179 if (cnt > _playlist_length - start) {
180 to_read = _playlist_length - start;
181 to_zero = cnt - to_read;
188 Glib::Mutex::Lock lm (_level_buffer_lock);
189 _playlist->read (dst, _mixdown_buffers[_level-1], _gain_buffers[_level-1], start+_playlist_offset, to_read, _playlist_channel);
193 memset (dst+to_read, 0, sizeof (Sample) * to_zero);
200 AudioPlaylistSource::write_unlocked (Sample *src, framecnt_t cnt)
202 fatal << string_compose (_("programming error: %1"), "AudioPlaylistSource::write() called - should be impossible") << endmsg;
208 AudioPlaylistSource::empty () const
210 return !_playlist || _playlist->empty();
214 AudioPlaylistSource::n_channels () const
216 /* use just the first region to decide */
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()->n_channels ();
229 AudioPlaylistSource::sample_rate () const
231 /* use just the first region to decide */
234 _session.frame_rate ();
237 boost::shared_ptr<Region> r = _playlist->region_list().front ();
238 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
240 return ar->audio_source()->sample_rate ();
244 AudioPlaylistSource::setup_peakfile ()
246 /* the peak data is setup once and once only
249 cerr << "looking for peakfile " << _peak_path << endl;
252 if (!Glib::file_test (_peak_path, Glib::FILE_TEST_EXISTS)) {
253 /* the 2nd argument here will be passed
254 in to ::peak_path, and is irrelevant
255 since our peak file path is fixed and
256 not dependent on anything.
258 cerr << "build it!\n";
259 return initialize_peakfile (false, string());
268 AudioPlaylistSource::peak_path (string /*audio_path*/)