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 <boost/optional.hpp> 24 25 #include "pbd/g_atomic_compat.h" 26 27 #include "evoral/Curve.h" 28 29 #include "ardour/disk_io.h" 30 #include "ardour/midi_buffer.h" 31 #include "ardour/midi_state_tracker.h" 32 33 namespace ARDOUR 34 { 35 class Playlist; 36 class AudioPlaylist; 37 class MidiPlaylist; 38 39 template <typename T> class MidiRingBuffer; 40 41 class LIBARDOUR_API DiskReader : public DiskIOProcessor 42 { 43 public: 44 DiskReader (Session&, Track&, std::string const& name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0)); 45 ~DiskReader (); 46 47 bool set_name (std::string const& str); 48 49 std::string display_name () const; 50 chunk_samples()51 static samplecnt_t chunk_samples () 52 { 53 return _chunk_samples; 54 } 55 set_chunk_samples(samplecnt_t n)56 static void set_chunk_samples (samplecnt_t n) 57 { 58 _chunk_samples = n; 59 } 60 61 static samplecnt_t default_chunk_samples (); 62 63 void run (BufferSet& /*bufs*/, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double speed, pframes_t /*nframes*/, bool /*result_required*/); 64 void realtime_handle_transport_stopped (); 65 void realtime_locate (bool); 66 bool overwrite_existing_buffers (); 67 void set_pending_overwrite (OverwriteReason); 68 void set_loop (Location*); 69 70 int set_state (const XMLNode&, int version); 71 72 PBD::Signal0<void> AlignmentStyleChanged; 73 74 float buffer_load () const; 75 76 void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<samplepos_t> > const&); 77 78 /* called by the Butler in a non-realtime context as part of its normal 79 * buffer refill loop (not due to transport-mechanism requests like 80 * locate) 81 */ 82 int do_refill (); 83 84 /** For contexts outside the normal butler refill loop (allocates temporary working buffers) */ 85 int do_refill_with_alloc (bool partial_fill, bool reverse); 86 87 bool pending_overwrite () const; 88 89 /* Working buffers for do_refill (butler thread) */ 90 static void allocate_working_buffers (); 91 static void free_working_buffers (); 92 93 void adjust_buffering (); 94 95 bool can_internal_playback_seek (sampleoffset_t distance); 96 void internal_playback_seek (sampleoffset_t distance); 97 int seek (samplepos_t sample, bool complete_refill = false); 98 99 static PBD::Signal0<void> Underrun; 100 101 void playlist_modified (); 102 void reset_tracker (); 103 104 bool declick_in_progress () const; 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 static void inc_no_disk_output (); 114 static void dec_no_disk_output (); no_disk_output()115 static bool no_disk_output () 116 { 117 return g_atomic_int_get (&_no_disk_output); 118 } 119 static void reset_loop_declick (Location*, samplecnt_t sample_rate); 120 static void alloc_loop_declick (samplecnt_t sample_rate); 121 122 protected: 123 friend class Track; 124 friend class MidiTrack; 125 126 struct ReaderChannelInfo : public DiskIOProcessor::ChannelInfo { ReaderChannelInfoReaderChannelInfo127 ReaderChannelInfo (samplecnt_t buffer_size, samplecnt_t preloop_size) 128 : DiskIOProcessor::ChannelInfo (buffer_size) 129 , pre_loop_buffer (0) 130 , pre_loop_buffer_size (0) 131 , initialized (false) 132 { 133 resize (buffer_size); 134 } 135 ~ReaderChannelInfoReaderChannelInfo136 ~ReaderChannelInfo () 137 { 138 delete[] pre_loop_buffer; 139 } 140 141 void resize (samplecnt_t); 142 void resize_preloop (samplecnt_t); 143 144 Sample* pre_loop_buffer; 145 samplecnt_t pre_loop_buffer_size; 146 bool initialized; 147 }; 148 149 XMLNode& state (); 150 151 void resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time); 152 153 int use_playlist (DataType, boost::shared_ptr<Playlist>); 154 void playlist_ranges_moved (std::list<Evoral::RangeMove<samplepos_t> > const&, bool); 155 156 int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many); 157 158 class DeclickAmp 159 { 160 public: 161 DeclickAmp (samplecnt_t sample_rate); 162 163 void apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target, sampleoffset_t buffer_offset = 0); 164 gain()165 float gain () const 166 { 167 return _g; 168 } set_gain(float g)169 void set_gain (float g) 170 { 171 _g = g; 172 } 173 174 private: 175 float _a; 176 float _l; 177 float _g; 178 }; 179 180 class Declicker 181 { 182 public: 183 Declicker (); 184 ~Declicker (); 185 186 void alloc (samplecnt_t sr, bool fadein, bool linear); 187 188 void run (Sample* buf, samplepos_t start, samplepos_t end); 189 void reset (samplepos_t start, samplepos_t end, bool fadein, samplecnt_t sr); 190 191 samplepos_t fade_start; 192 samplepos_t fade_end; 193 samplecnt_t fade_length; 194 Sample* vec; 195 }; 196 197 private: 198 samplepos_t overwrite_sample; 199 sampleoffset_t overwrite_offset; 200 samplepos_t new_file_sample; 201 bool run_must_resolve; 202 IOChange input_change_pending; 203 samplepos_t file_sample[DataType::num_types]; 204 205 mutable GATOMIC_QUAL gint _pending_overwrite; 206 207 DeclickAmp _declick_amp; 208 sampleoffset_t _declick_offs; 209 bool _declick_enabled; 210 MidiStateTracker _tracker; 211 boost::optional<bool> _last_read_reversed; 212 boost::optional<bool> _last_read_loop; 213 214 static samplecnt_t _chunk_samples; 215 216 static GATOMIC_QUAL gint _no_disk_output; 217 218 static Declicker loop_declick_in; 219 static Declicker loop_declick_out; 220 static samplecnt_t loop_fade_length; 221 222 samplecnt_t audio_read (Sample* sum_buffer, 223 Sample* mixdown_buffer, 224 float* gain_buffer, 225 samplepos_t& start, samplecnt_t cnt, 226 ReaderChannelInfo* rci, 227 int channel, 228 bool reversed); 229 230 static Sample* _sum_buffer; 231 static Sample* _mixdown_buffer; 232 static gain_t* _gain_buffer; 233 234 int refill (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level, bool reversed); 235 int refill_audio (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level, bool reversed); 236 237 sampleoffset_t calculate_playback_distance (pframes_t); 238 239 RTMidiBuffer* rt_midibuffer (); 240 241 void get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, samplepos_t end_sample, MonitorState, BufferSet&, double speed, samplecnt_t distance); 242 void maybe_xfade_loop (Sample*, samplepos_t read_start, samplepos_t read_end, ReaderChannelInfo*); 243 244 void configuration_changed (); 245 246 bool overwrite_existing_audio (); 247 bool overwrite_existing_midi (); 248 249 samplepos_t last_refill_loop_start; 250 void setup_preloop_buffer (); 251 }; 252 253 } // namespace ARDOUR 254 255 #endif /* _ardour_disk_reader_h_ */ 256