64ee662e681a8b5815295296f9f1ffbc7978845b
[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         void run (BufferSet& /*bufs*/, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
54         void realtime_handle_transport_stopped ();
55         void realtime_locate (bool);
56         bool overwrite_existing_buffers ();
57         void set_pending_overwrite (OverwriteReason);
58         void set_loop (Location *);
59
60         int set_state (const XMLNode&, int version);
61
62         PBD::Signal0<void>            AlignmentStyleChanged;
63
64         float buffer_load() const;
65
66         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<samplepos_t> > const &);
67
68         /* called by the Butler in a non-realtime context */
69
70         int do_refill () {
71                 return refill (_sum_buffer, _mixdown_buffer, _gain_buffer, 0);
72         }
73
74         /** For non-butler contexts (allocates temporary working buffers)
75          *
76          * This accessible method has a default argument; derived classes
77          * must inherit the virtual method that we call which does NOT
78          * have a default argument, to avoid complications with inheritance
79          */
80         int do_refill_with_alloc (bool partial_fill = true) {
81                 return _do_refill_with_alloc (partial_fill);
82         }
83
84         bool pending_overwrite () const;
85
86         // Working buffers for do_refill (butler thread)
87         static void allocate_working_buffers();
88         static void free_working_buffers();
89
90         void adjust_buffering ();
91
92         bool can_internal_playback_seek (sampleoffset_t distance);
93         void internal_playback_seek (sampleoffset_t distance);
94         int seek (samplepos_t sample, bool complete_refill = false);
95
96         static PBD::Signal0<void> Underrun;
97
98         void playlist_modified ();
99         void reset_tracker ();
100
101         bool declick_in_progress () const;
102         void reload_loop ();
103
104         /* inc/dec variants MUST be called as part of the process call tree, before any
105            disk readers are invoked. We use it when the session needs the
106            transport (and thus effective read position for DiskReaders) to keep
107            advancing as part of syncing up with a transport master, but we
108            don't want any actual disk output yet because we are still not
109            synced.
110         */
111
112         static void inc_no_disk_output ();
113         static void dec_no_disk_output();
114         static bool no_disk_output () { return g_atomic_int_get (&_no_disk_output); }
115         static void reset_loop_declick (Location*, samplecnt_t sample_rate);
116         static void alloc_loop_declick (samplecnt_t sample_rate);
117
118         Glib::Threads::Mutex rbuf_lock;
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          overwrite_queued;
190         bool          run_must_resolve;
191         IOChange      input_change_pending;
192         samplepos_t   file_sample[DataType::num_types];
193
194         DeclickAmp     _declick_amp;
195         sampleoffset_t _declick_offs;
196         MidiStateTracker _tracker;
197
198         int _do_refill_with_alloc (bool partial_fill);
199
200         static samplecnt_t _chunk_samples;
201         static gint       _no_disk_output;
202
203         static Declicker loop_declick_in;
204         static Declicker loop_declick_out;
205         static samplecnt_t loop_fade_length;
206
207         samplecnt_t audio_read (Sample* sum_buffer,
208                                 Sample* mixdown_buffer,
209                                 float*  gain_buffer,
210                                 samplepos_t& start, samplecnt_t cnt,
211                                 ReaderChannelInfo* rci,
212                                 int channel,
213                                 bool reversed);
214
215         static Sample* _sum_buffer;
216         static Sample* _mixdown_buffer;
217         static gain_t* _gain_buffer;
218
219         int refill (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level);
220         int refill_audio (Sample* sum_buffer, Sample *mixdown_buffer, float *gain_buffer, samplecnt_t fill_level);
221
222         sampleoffset_t calculate_playback_distance (pframes_t);
223
224         RTMidiBuffer* rt_midibuffer();
225
226         void get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, samplepos_t end_sample, MonitorState, BufferSet&, double speed, samplecnt_t distance);
227         void maybe_xfade_loop (Sample*, samplepos_t read_start, samplepos_t read_end, ReaderChannelInfo*);
228
229         bool overwrite_existing_audio ();
230         bool overwrite_existing_midi ();
231 };
232
233 } // namespace
234
235 #endif /* __ardour_disk_reader_h__ */