extend/modify PresentationInfo to make searching for routes via Session::get_remote_n...
[ardour.git] / libs / ardour / ardour / fixed_delay.h
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #ifndef __ardour_fixed_delay_h__
20 #define __ardour_fixed_delay_h__
21
22 #include <vector>
23 #include "ardour/buffer.h"
24
25 namespace ARDOUR {
26
27 class ChanCount;
28
29 /** Multichannel Audio/Midi Delay Line
30  *
31  * This is an efficient delay line operating directly on Ardour buffers.
32  * The drawback is that there is no thread safety:
33  * All calls need to be executed in the same thread.
34  *
35  * After configuration, the delay can be changed safely up to the maximum
36  * configured delay but doing so flushes the buffer. There is no de-clicking
37  * (see ARDOUR::Delayline for those cases).
38  *
39  * Increasing the delay above the max configured or requesting more
40  * buffers will allocate the required space (not realtime safe).
41  *
42  * All buffers part of the set are treated separately.
43  */
44 class LIBARDOUR_API FixedDelay
45 {
46 public:
47         FixedDelay ();
48         ~FixedDelay ();
49
50         /** initial configuration, usually done after instantiation
51          *
52          * @param count Channel Count (audio+midi)
53          * @param max_delay the maximum number of samples to delay
54          * @param shrink when false already allocated buffers are kept if both channel-count and max-delay requirements are satisified
55          */
56         void configure (const ChanCount& count, framecnt_t max_delay, bool shrink = true);
57
58         /** set delay time and update active process buffers
59          *
60          * This calls configure with shrink = false and sets the current delay time
61          * if the delay time mismatches, the buffers are silenced (zeroed).
62          *
63          * @param count channels to be processed
64          * @param delay number of audio samples to delay
65          */
66         void set (const ChanCount& count, framecnt_t delay);
67
68         /** process a channel
69          *
70          * Read N samples from the input buffer, delay them by the configured delay-time and write
71          * the delayed samples to the output buffer at the given offset.
72          *
73          * @param dt datatype
74          * @param id buffer number (starting at 0)
75          * @param out output buffer to write data to
76          * @param in input buffer to read data from
77          * @param n_samples number of samples to process (must be <= 8192)
78          * @param dst_offset offset in output buffer to start writing to
79          * @param src_offset offset in input buffer to start reading from
80          */
81         void delay (ARDOUR::DataType dt, uint32_t id, Buffer& out, const Buffer& in, pframes_t n_samples, framecnt_t dst_offset = 0, framecnt_t src_offset = 0);
82
83         /** zero all buffers */
84         void flush();
85
86 private:
87         framecnt_t _max_delay;
88         framecnt_t _buf_size;
89         framecnt_t _delay;
90         ChanCount  _count;
91
92         struct DelayBuffer {
93                 public:
94                 DelayBuffer () : buf (0), pos (0) {}
95                 DelayBuffer (DataType dt, size_t capacity)
96                         : buf (Buffer::create (dt, capacity)), pos (0) {}
97                 ~DelayBuffer () { delete buf; }
98                 Buffer * buf;
99                 framepos_t pos;
100         };
101
102         typedef std::vector<DelayBuffer*> BufferVec;
103         // Vector of vectors, indexed by DataType
104         std::vector<BufferVec> _buffers;
105
106         void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
107         void clear ();
108 };
109
110 } // namespace ARDOUR
111
112 #endif // __ardour_fixed_delay_h__