2 * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
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.
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.
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.
20 #ifndef __ardour_disk_reader_h__
21 #define __ardour_disk_reader_h__
25 #include "evoral/Curve.h"
27 #include "ardour/disk_io.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/midi_state_tracker.h"
38 template<typename T> class MidiRingBuffer;
40 class LIBARDOUR_API DiskReader : public DiskIOProcessor
43 DiskReader (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
46 bool set_name (std::string const & str);
47 std::string display_name() const { return std::string (_("player")); }
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; }
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 *);
60 int set_state (const XMLNode&, int version);
62 PBD::Signal0<void> AlignmentStyleChanged;
64 float buffer_load() const;
66 void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<samplepos_t> > const &);
68 /* called by the Butler in a non-realtime context */
71 return refill (_sum_buffer, _mixdown_buffer, _gain_buffer, 0);
74 /** For non-butler contexts (allocates temporary working buffers)
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
80 int do_refill_with_alloc (bool partial_fill = true) {
81 return _do_refill_with_alloc (partial_fill);
84 bool pending_overwrite () const;
86 // Working buffers for do_refill (butler thread)
87 static void allocate_working_buffers();
88 static void free_working_buffers();
90 void adjust_buffering ();
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);
96 static PBD::Signal0<void> Underrun;
98 void playlist_modified ();
99 void reset_tracker ();
101 bool declick_in_progress () const;
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
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);
118 Glib::Threads::Mutex rbuf_lock;
122 friend class MidiTrack;
124 struct ReaderChannelInfo : public DiskIOProcessor::ChannelInfo
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)
131 resize (buffer_size);
132 resize_preloop (preloop_size);
134 ~ReaderChannelInfo() { delete [] pre_loop_buffer; }
136 void resize (samplecnt_t);
137 void resize_preloop (samplecnt_t);
139 Sample* pre_loop_buffer;
140 samplecnt_t pre_loop_buffer_size;
145 void resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time);
147 int use_playlist (DataType, boost::shared_ptr<Playlist>);
148 void playlist_ranges_moved (std::list< Evoral::RangeMove<samplepos_t> > const &, bool);
150 int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
155 DeclickAmp (samplecnt_t sample_rate);
157 void apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target, sampleoffset_t buffer_offset = 0);
159 float gain () const { return _g; }
160 void set_gain (float g) { _g = g; }
173 void alloc (samplecnt_t sr, bool fadein);
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);
178 samplepos_t fade_start;
179 samplepos_t fade_end;
180 samplecnt_t fade_length;
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];
194 DeclickAmp _declick_amp;
195 sampleoffset_t _declick_offs;
196 MidiStateTracker _tracker;
198 int _do_refill_with_alloc (bool partial_fill);
200 static samplecnt_t _chunk_samples;
201 static gint _no_disk_output;
203 static Declicker loop_declick_in;
204 static Declicker loop_declick_out;
205 static samplecnt_t loop_fade_length;
207 samplecnt_t audio_read (Sample* sum_buffer,
208 Sample* mixdown_buffer,
210 samplepos_t& start, samplecnt_t cnt,
211 ReaderChannelInfo* rci,
215 static Sample* _sum_buffer;
216 static Sample* _mixdown_buffer;
217 static gain_t* _gain_buffer;
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);
222 sampleoffset_t calculate_playback_distance (pframes_t);
224 RTMidiBuffer* rt_midibuffer();
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*);
229 bool overwrite_existing_audio ();
230 bool overwrite_existing_midi ();
235 #endif /* __ardour_disk_reader_h__ */