fix behavior of DiskReader when moved after an instrument
[ardour.git] / libs / ardour / ardour / disk_reader.h
1 /*
2  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #ifndef __ardour_disk_reader_h__
21 #define __ardour_disk_reader_h__
22
23 #include "pbd/i18n.h"
24
25 #include "evoral/Curve.h"
26
27 #include "ardour/disk_io.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/midi_state_tracker.h"
30
31 namespace ARDOUR
32 {
33
34 class Playlist;
35 class AudioPlaylist;
36 class MidiPlaylist;
37
38 template<typename T> class MidiRingBuffer;
39
40 class LIBARDOUR_API DiskReader : public DiskIOProcessor
41 {
42 public:
43         DiskReader (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
44         ~DiskReader ();
45
46         bool set_name (std::string const & str);
47         std::string display_name() const { return std::string (_("player")); }
48
49         static samplecnt_t chunk_samples() { return _chunk_samples; }
50         static samplecnt_t default_chunk_samples ();
51         static void set_chunk_samples (samplecnt_t n) { _chunk_samples = n; }
52
53         bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
54
55         void run (BufferSet& /*bufs*/, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
56         void realtime_handle_transport_stopped ();
57         void realtime_locate (bool);
58         bool overwrite_existing_buffers ();
59         void set_pending_overwrite (OverwriteReason);
60         void set_loop (Location *);
61
62         int set_state (const XMLNode&, int version);
63
64         PBD::Signal0<void>            AlignmentStyleChanged;
65
66         float buffer_load() const;
67
68         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<samplepos_t> > const &);
69
70         /* called by the Butler in a non-realtime context */
71
72         int do_refill () {
73                 return refill (_sum_buffer, _mixdown_buffer, _gain_buffer, 0);
74         }
75
76         /** For non-butler contexts (allocates temporary working buffers)
77          *
78          * This accessible method has a default argument; derived classes
79          * must inherit the virtual method that we call which does NOT
80          * have a default argument, to avoid complications with inheritance
81          */
82         int do_refill_with_alloc (bool partial_fill = true) {
83                 return _do_refill_with_alloc (partial_fill);
84         }
85
86         bool pending_overwrite () const;
87
88         // Working buffers for do_refill (butler thread)
89         static void allocate_working_buffers();
90         static void free_working_buffers();
91
92         void adjust_buffering ();
93
94         bool can_internal_playback_seek (sampleoffset_t distance);
95         void internal_playback_seek (sampleoffset_t distance);
96         int seek (samplepos_t sample, bool complete_refill = false);
97
98         static PBD::Signal0<void> Underrun;
99
100         void playlist_modified ();
101         void reset_tracker ();
102
103         bool declick_in_progress () const;
104         void reload_loop ();
105
106         /* inc/dec variants MUST be called as part of the process call tree, before any
107            disk readers are invoked. We use it when the session needs the
108            transport (and thus effective read position for DiskReaders) to keep
109            advancing as part of syncing up with a transport master, but we
110            don't want any actual disk output yet because we are still not
111            synced.
112         */
113
114         static void inc_no_disk_output ();
115         static void dec_no_disk_output();
116         static bool no_disk_output () { return g_atomic_int_get (&_no_disk_output); }
117         static void reset_loop_declick (Location*, samplecnt_t sample_rate);
118         static void alloc_loop_declick (samplecnt_t sample_rate);
119
120 protected:
121         friend class Track;
122         friend class MidiTrack;
123
124         struct ReaderChannelInfo : public DiskIOProcessor::ChannelInfo
125         {
126                 ReaderChannelInfo (samplecnt_t buffer_size, samplecnt_t preloop_size)
127                         : DiskIOProcessor::ChannelInfo (buffer_size)
128                         , pre_loop_buffer (0)
129                         , pre_loop_buffer_size (0)
130                 {
131                         resize (buffer_size);
132                         resize_preloop (preloop_size);
133                 }
134                 ~ReaderChannelInfo() { delete [] pre_loop_buffer; }
135
136                 void resize (samplecnt_t);
137                 void resize_preloop (samplecnt_t);
138
139                 Sample* pre_loop_buffer;
140                 samplecnt_t pre_loop_buffer_size;
141         };
142
143         XMLNode& state ();
144
145         void resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time);
146
147         int use_playlist (DataType, boost::shared_ptr<Playlist>);
148         void playlist_ranges_moved (std::list< Evoral::RangeMove<samplepos_t> > const &, bool);
149
150         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
151
152         class DeclickAmp
153         {
154                 public:
155                         DeclickAmp (samplecnt_t sample_rate);
156
157                 void apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target, sampleoffset_t buffer_offset = 0);
158
159                         float gain () const { return _g; }
160                         void set_gain (float g) { _g = g; }
161
162                 private:
163                         float _a;
164                         float _l;
165                         float _g;
166         };
167
168         class Declicker {
169           public:
170                 Declicker ();
171                 ~Declicker ();
172
173                 void alloc (samplecnt_t sr, bool fadein);
174
175                 void run (Sample* buf, samplepos_t start, samplepos_t end);
176                 void reset (samplepos_t start, samplepos_t end, bool fadein, samplecnt_t sr);
177
178                 samplepos_t fade_start;
179                 samplepos_t fade_end;
180                 samplecnt_t fade_length;
181                 Sample* vec;
182         };
183
184 private:
185         samplepos_t   overwrite_sample;
186         sampleoffset_t overwrite_offset;
187         samplepos_t   new_file_sample;
188         mutable gint  _pending_overwrite;
189         bool          run_must_resolve;
190         IOChange      input_change_pending;
191         samplepos_t   file_sample[DataType::num_types];
192
193         DeclickAmp     _declick_amp;
194         sampleoffset_t _declick_offs;
195         MidiStateTracker _tracker;
196
197         int _do_refill_with_alloc (bool partial_fill);
198
199         static samplecnt_t _chunk_samples;
200         static gint       _no_disk_output;
201
202         static Declicker loop_declick_in;
203         static Declicker loop_declick_out;
204         static samplecnt_t loop_fade_length;
205
206         samplecnt_t audio_read (Sample* sum_buffer,
207                                 Sample* mixdown_buffer,
208                                 float*  gain_buffer,
209                                 samplepos_t& start, samplecnt_t cnt,
210                                 ReaderChannelInfo* rci,
211                                 int channel,
212                                 bool reversed);
213
214         static Sample* _sum_buffer;
215         static Sample* _mixdown_buffer;
216         static gain_t* _gain_buffer;
217
218         int refill (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level);
219         int refill_audio (Sample* sum_buffer, Sample *mixdown_buffer, float *gain_buffer, samplecnt_t fill_level);
220
221         sampleoffset_t calculate_playback_distance (pframes_t);
222
223         RTMidiBuffer* rt_midibuffer();
224
225         void get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, samplepos_t end_sample, MonitorState, BufferSet&, double speed, samplecnt_t distance);
226         void maybe_xfade_loop (Sample*, samplepos_t read_start, samplepos_t read_end, ReaderChannelInfo*);
227
228         bool overwrite_existing_audio ();
229         bool overwrite_existing_midi ();
230 };
231
232 } // namespace
233
234 #endif /* __ardour_disk_reader_h__ */