summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/disk_reader.h
blob: f762935da04954eecb86c37e90253754807b8f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
 * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _ardour_disk_reader_h_
#define _ardour_disk_reader_h_

#include <boost/optional.hpp>

#include "pbd/i18n.h"

#include "evoral/Curve.h"

#include "ardour/disk_io.h"
#include "ardour/midi_buffer.h"
#include "ardour/midi_state_tracker.h"

namespace ARDOUR
{
class Playlist;
class AudioPlaylist;
class MidiPlaylist;

template <typename T> class MidiRingBuffer;

class LIBARDOUR_API DiskReader : public DiskIOProcessor
{
public:
	DiskReader (Session&, std::string const& name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
	~DiskReader ();

	bool set_name (std::string const& str);

	std::string display_name () const
	{
		return std::string (_("player"));
	}

	static samplecnt_t chunk_samples ()
	{
		return _chunk_samples;
	}

	static void set_chunk_samples (samplecnt_t n)
	{
		_chunk_samples = n;
	}

	static samplecnt_t default_chunk_samples ();

	void run (BufferSet& /*bufs*/, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
	void realtime_handle_transport_stopped ();
	void realtime_locate (bool);
	bool overwrite_existing_buffers ();
	void set_pending_overwrite (OverwriteReason);
	void set_loop (Location*);

	int set_state (const XMLNode&, int version);

	PBD::Signal0<void> AlignmentStyleChanged;

	float buffer_load () const;

	void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<samplepos_t> > const&);

	/* called by the Butler in a non-realtime context as part of its normal
	 * buffer refill loop (not due to transport-mechanism requests like
	 * locate)
	 */
	int do_refill ();

	/** For contexts outside the normal butler refill loop (allocates temporary working buffers) */
	int do_refill_with_alloc (bool partial_fill, bool reverse);

	bool pending_overwrite () const;

	/* Working buffers for do_refill (butler thread) */
	static void allocate_working_buffers ();
	static void free_working_buffers ();

	void adjust_buffering ();

	bool can_internal_playback_seek (sampleoffset_t distance);
	void internal_playback_seek (sampleoffset_t distance);
	int  seek (samplepos_t sample, bool complete_refill = false);

	static PBD::Signal0<void> Underrun;

	void playlist_modified ();
	void reset_tracker ();

	bool declick_in_progress () const;
	void reload_loop ();

	/* inc/dec variants MUST be called as part of the process call tree, before any
	 * disk readers are invoked. We use it when the session needs the
	 * transport (and thus effective read position for DiskReaders) to keep
	 * advancing as part of syncing up with a transport master, but we
	 * don't want any actual disk output yet because we are still not
	 * synced.
	 */
	static void inc_no_disk_output ();
	static void dec_no_disk_output ();
	static bool no_disk_output ()
	{
		return g_atomic_int_get (&_no_disk_output);
	}
	static void reset_loop_declick (Location*, samplecnt_t sample_rate);
	static void alloc_loop_declick (samplecnt_t sample_rate);

protected:
	friend class Track;
	friend class MidiTrack;

	struct ReaderChannelInfo : public DiskIOProcessor::ChannelInfo {
		ReaderChannelInfo (samplecnt_t buffer_size, samplecnt_t preloop_size)
			: DiskIOProcessor::ChannelInfo (buffer_size)
			, pre_loop_buffer (0)
			, pre_loop_buffer_size (0)
			, initialized (false)
		{
			resize (buffer_size);
			resize_preloop (preloop_size);
		}

		~ReaderChannelInfo ()
		{
			delete[] pre_loop_buffer;
		}

		void resize (samplecnt_t);
		void resize_preloop (samplecnt_t);

		Sample*     pre_loop_buffer;
		samplecnt_t pre_loop_buffer_size;
		bool        initialized;
	};

	XMLNode& state ();

	void resolve_tracker (Evoral::EventSink<samplepos_t>& buffer, samplepos_t time);

	int  use_playlist (DataType, boost::shared_ptr<Playlist>);
	void playlist_ranges_moved (std::list<Evoral::RangeMove<samplepos_t> > const&, bool);

	int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);

	class DeclickAmp
	{
	public:
		DeclickAmp (samplecnt_t sample_rate);

		void apply_gain (AudioBuffer& buf, samplecnt_t n_samples, const float target, sampleoffset_t buffer_offset = 0);

		float gain () const
		{
			return _g;
		}
		void set_gain (float g)
		{
			_g = g;
		}

	private:
		float _a;
		float _l;
		float _g;
	};

	class Declicker
	{
	public:
		Declicker ();
		~Declicker ();

		void alloc (samplecnt_t sr, bool fadein);

		void run (Sample* buf, samplepos_t start, samplepos_t end);
		void reset (samplepos_t start, samplepos_t end, bool fadein, samplecnt_t sr);

		samplepos_t fade_start;
		samplepos_t fade_end;
		samplecnt_t fade_length;
		Sample*     vec;
	};

private:
	samplepos_t    overwrite_sample;
	sampleoffset_t overwrite_offset;
	samplepos_t    new_file_sample;
	mutable gint   _pending_overwrite;
	bool           run_must_resolve;
	IOChange       input_change_pending;
	samplepos_t    file_sample[DataType::num_types];

	DeclickAmp            _declick_amp;
	sampleoffset_t        _declick_offs;
	MidiStateTracker      _tracker;
	boost::optional<bool> _last_read_reversed;
	boost::optional<bool> _last_read_loop;

	static samplecnt_t _chunk_samples;
	static gint        _no_disk_output;

	static Declicker   loop_declick_in;
	static Declicker   loop_declick_out;
	static samplecnt_t loop_fade_length;

	samplecnt_t audio_read (Sample*      sum_buffer,
	                        Sample*      mixdown_buffer,
	                        float*       gain_buffer,
	                        samplepos_t& start, samplecnt_t cnt,
	                        ReaderChannelInfo* rci,
	                        int                channel,
	                        bool               reversed);

	static Sample* _sum_buffer;
	static Sample* _mixdown_buffer;
	static gain_t* _gain_buffer;

	int refill (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level, bool reversed);
	int refill_audio (Sample* sum_buffer, Sample* mixdown_buffer, float* gain_buffer, samplecnt_t fill_level, bool reversed);

	sampleoffset_t calculate_playback_distance (pframes_t);

	RTMidiBuffer* rt_midibuffer ();

	void get_midi_playback (MidiBuffer& dst, samplepos_t start_sample, samplepos_t end_sample, MonitorState, BufferSet&, double speed, samplecnt_t distance);
	void maybe_xfade_loop (Sample*, samplepos_t read_start, samplepos_t read_end, ReaderChannelInfo*);

	void configuration_changed ();

	bool overwrite_existing_audio ();
	bool overwrite_existing_midi ();
};

} // namespace ARDOUR

#endif /* _ardour_disk_reader_h_ */