Load audition synth on demand
[ardour.git] / libs / ardour / disk_io.cc
1 /*
2     Copyright (C) 2009-2016 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "pbd/debug.h"
21 #include "pbd/error.h"
22
23 #include "ardour/audioplaylist.h"
24 #include "ardour/butler.h"
25 #include "ardour/debug.h"
26 #include "ardour/disk_io.h"
27 #include "ardour/disk_reader.h"
28 #include "ardour/disk_writer.h"
29 #include "ardour/location.h"
30 #include "ardour/midi_ring_buffer.h"
31 #include "ardour/midi_playlist.h"
32 #include "ardour/playlist.h"
33 #include "ardour/playlist_factory.h"
34 #include "ardour/rc_configuration.h"
35 #include "ardour/session.h"
36 #include "ardour/session_playlists.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace ARDOUR;
41 using namespace PBD;
42 using namespace std;
43
44 const string DiskIOProcessor::state_node_name = X_("DiskIOProcessor");
45
46 // PBD::Signal0<void> DiskIOProcessor::DiskOverrun;
47 // PBD::Signal0<void>  DiskIOProcessor::DiskUnderrun;
48
49 DiskIOProcessor::DiskIOProcessor (Session& s, string const & str, Flag f)
50         : Processor (s, str)
51         , _flags (f)
52         , i_am_the_modifier (false)
53         , _slaved (false)
54         , in_set_state (false)
55         , playback_sample (0)
56         , _need_butler (false)
57         , channels (new ChannelList)
58         , _midi_buf (new MidiRingBuffer<samplepos_t> (s.butler()->midi_diskstream_buffer_size()))
59         , _samples_written_to_ringbuffer (0)
60         , _samples_read_from_ringbuffer (0)
61 {
62         set_display_to_user (false);
63 }
64
65 DiskIOProcessor::~DiskIOProcessor ()
66 {
67         {
68                 RCUWriter<ChannelList> writer (channels);
69                 boost::shared_ptr<ChannelList> c = writer.get_copy();
70
71                 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
72                         delete *chan;
73                 }
74
75                 c->clear();
76         }
77
78         channels.flush ();
79 }
80
81
82 void
83 DiskIOProcessor::init ()
84 {
85         set_block_size (_session.get_block_size());
86 }
87
88 void
89 DiskIOProcessor::set_buffering_parameters (BufferingPreset bp)
90 {
91         samplecnt_t read_chunk_size;
92         samplecnt_t read_buffer_size;
93         samplecnt_t write_chunk_size;
94         samplecnt_t write_buffer_size;
95
96         if (!get_buffering_presets (bp, read_chunk_size, read_buffer_size, write_chunk_size, write_buffer_size)) {
97                 return;
98         }
99
100         DiskReader::set_chunk_samples (read_chunk_size);
101         DiskWriter::set_chunk_samples (write_chunk_size);
102
103         Config->set_audio_capture_buffer_seconds (write_buffer_size);
104         Config->set_audio_playback_buffer_seconds (read_buffer_size);
105 }
106
107 bool
108 DiskIOProcessor::get_buffering_presets (BufferingPreset bp,
109                                         samplecnt_t& read_chunk_size,
110                                         samplecnt_t& read_buffer_size,
111                                         samplecnt_t& write_chunk_size,
112                                         samplecnt_t& write_buffer_size)
113 {
114         switch (bp) {
115         case Small:
116                 read_chunk_size = 65536;  /* samples */
117                 write_chunk_size = 65536; /* samples */
118                 read_buffer_size = 5;  /* seconds */
119                 write_buffer_size = 5; /* seconds */
120                 break;
121
122         case Medium:
123                 read_chunk_size = 262144;  /* samples */
124                 write_chunk_size = 131072; /* samples */
125                 read_buffer_size = 10;  /* seconds */
126                 write_buffer_size = 10; /* seconds */
127                 break;
128
129         case Large:
130                 read_chunk_size = 524288; /* samples */
131                 write_chunk_size = 131072; /* samples */
132                 read_buffer_size = 20; /* seconds */
133                 write_buffer_size = 20; /* seconds */
134                 break;
135
136         default:
137                 return false;
138         }
139
140         return true;
141 }
142
143 bool
144 DiskIOProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out)
145 {
146         if (in.n_midi() != 0 && in.n_midi() != 1) {
147                 /* we only support zero or 1 MIDI stream */
148                 return false;
149         }
150
151         /* currently no way to deliver different channels that we receive */
152         out = in;
153
154         return true;
155 }
156
157 bool
158 DiskIOProcessor::configure_io (ChanCount in, ChanCount out)
159 {
160         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("Configuring %1 for in:%2 out:%3\n", name(), in, out));
161
162         bool changed = false;
163
164         {
165                 RCUWriter<ChannelList> writer (channels);
166                 boost::shared_ptr<ChannelList> c = writer.get_copy();
167
168                 uint32_t n_audio = in.n_audio();
169
170                 if (n_audio > c->size()) {
171                         add_channel_to (c, n_audio - c->size());
172                         changed = true;
173                 } else if (n_audio < c->size()) {
174                         remove_channel_from (c, c->size() - n_audio);
175                         changed = true;
176                 }
177
178                 /* writer leaves scope, actual channel list is updated */
179         }
180
181         if (in.n_midi() > 0 && !_midi_buf) {
182                 const size_t size = _session.butler()->midi_diskstream_buffer_size();
183                 _midi_buf = new MidiRingBuffer<samplepos_t>(size);
184                 changed = true;
185         }
186
187         if (changed) {
188                 seek (_session.transport_sample());
189         }
190
191         return Processor::configure_io (in, out);
192 }
193
194 int
195 DiskIOProcessor::set_block_size (pframes_t nframes)
196 {
197         return 0;
198 }
199
200 void
201 DiskIOProcessor::non_realtime_locate (samplepos_t location)
202 {
203         /* now refill channel buffers */
204
205         seek (location, true);
206 }
207
208 int
209 DiskIOProcessor::set_state (const XMLNode& node, int version)
210 {
211         XMLProperty const * prop;
212
213         Processor::set_state (node, version);
214
215         if ((prop = node.property ("flags")) != 0) {
216                 _flags = Flag (string_2_enum (prop->value(), _flags));
217         }
218
219         return 0;
220 }
221
222 int
223 DiskIOProcessor::add_channel (uint32_t how_many)
224 {
225         RCUWriter<ChannelList> writer (channels);
226         boost::shared_ptr<ChannelList> c = writer.get_copy();
227
228         return add_channel_to (c, how_many);
229 }
230
231 int
232 DiskIOProcessor::remove_channel_from (boost::shared_ptr<ChannelList> c, uint32_t how_many)
233 {
234         while (how_many-- && !c->empty()) {
235                 delete c->back();
236                 c->pop_back();
237         }
238
239         return 0;
240 }
241
242 int
243 DiskIOProcessor::remove_channel (uint32_t how_many)
244 {
245         RCUWriter<ChannelList> writer (channels);
246         boost::shared_ptr<ChannelList> c = writer.get_copy();
247
248         return remove_channel_from (c, how_many);
249 }
250
251 void
252 DiskIOProcessor::playlist_deleted (boost::weak_ptr<Playlist> wpl)
253 {
254         boost::shared_ptr<Playlist> pl (wpl.lock());
255
256         if (!pl) {
257                 return;
258         }
259
260         for (uint32_t n = 0; n < DataType::num_types; ++n) {
261                 if (pl == _playlists[n]) {
262
263                         /* this catches an ordering issue with session destruction. playlists
264                            are destroyed before disk readers. we have to invalidate any handles
265                            we have to the playlist.
266                         */
267                         _playlists[n].reset ();
268                         break;
269                 }
270         }
271 }
272
273 boost::shared_ptr<AudioPlaylist>
274 DiskIOProcessor::audio_playlist () const
275 {
276         return boost::dynamic_pointer_cast<AudioPlaylist> (_playlists[DataType::AUDIO]);
277 }
278
279 boost::shared_ptr<MidiPlaylist>
280 DiskIOProcessor::midi_playlist () const
281 {
282         return boost::dynamic_pointer_cast<MidiPlaylist> (_playlists[DataType::MIDI]);
283 }
284
285 int
286 DiskIOProcessor::use_playlist (DataType dt, boost::shared_ptr<Playlist> playlist)
287 {
288         if (!playlist) {
289                 return 0;
290         }
291
292         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: set to use playlist %2 (%3)\n", name(), playlist->name(), dt.to_string()));
293
294         if (playlist == _playlists[dt]) {
295                 DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1: already using that playlist\n", name()));
296                 return 0;
297         }
298
299         playlist_connections.drop_connections ();
300
301         if (_playlists[dt]) {
302                 _playlists[dt]->release();
303         }
304
305         _playlists[dt] = playlist;
306         playlist->use();
307
308         playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&DiskIOProcessor::playlist_modified, this));
309         playlist->LayeringChanged.connect_same_thread (playlist_connections, boost::bind (&DiskIOProcessor::playlist_modified, this));
310         playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&DiskIOProcessor::playlist_deleted, this, boost::weak_ptr<Playlist>(playlist)));
311         playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&DiskIOProcessor::playlist_ranges_moved, this, _1, _2));
312
313         DEBUG_TRACE (DEBUG::DiskIO, string_compose ("%1 now using playlist %1 (%2)\n", name(), playlist->name(), playlist->id()));
314
315         return 0;
316 }
317
318 DiskIOProcessor::ChannelInfo::ChannelInfo (samplecnt_t bufsize)
319         : rbuf (0)
320         , wbuf (0)
321         , capture_transition_buf (0)
322         , curr_capture_cnt (0)
323 {
324 }
325
326 DiskIOProcessor::ChannelInfo::~ChannelInfo ()
327 {
328         delete rbuf;
329         delete wbuf;
330         delete capture_transition_buf;
331         rbuf = 0;
332         wbuf = 0;
333         capture_transition_buf = 0;
334 }
335
336 void
337 DiskIOProcessor::drop_route ()
338 {
339         _route.reset ();
340 }
341
342 void
343 DiskIOProcessor::set_route (boost::shared_ptr<Route> r)
344 {
345         _route = r;
346
347         if (_route) {
348                 _route->DropReferences.connect_same_thread (*this, boost::bind (&DiskIOProcessor::drop_route, this));
349         }
350 }
351
352 /** Get the start, end, and length of a location "atomically".
353  *
354  * Note: Locations don't get deleted, so all we care about when I say "atomic"
355  * is that we are always pointing to the same one and using start/length values
356  * obtained just once.  Use this function to achieve this since location being
357  * a parameter achieves this.
358  */
359 void
360 DiskIOProcessor::get_location_times(const Location* location,
361                    samplepos_t*     start,
362                    samplepos_t*     end,
363                    samplepos_t*     length)
364 {
365         if (location) {
366                 *start  = location->start();
367                 *end    = location->end();
368                 *length = *end - *start;
369         }
370 }
371