Ardour  8.7-15-gadf511264b
convolver.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018-2021 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #ifndef _ardour_convolver_h_
19 #define _ardour_convolver_h_
20 
21 #include <vector>
22 
24 
26 
27 #include "ardour/buffer_set.h"
28 #include "ardour/chan_mapping.h"
29 #include "ardour/readable.h"
30 
31 namespace ARDOUR { namespace DSP {
32 
34 {
35 public:
36  Convolution (Session&, uint32_t n_in, uint32_t n_out);
37  virtual ~Convolution () {}
38 
39  bool add_impdata (
40  uint32_t c_in,
41  uint32_t c_out,
42  std::shared_ptr<AudioReadable> r,
43  float gain = 1.0,
44  uint32_t pre_delay = 0,
45  sampleoffset_t offset = 0,
46  samplecnt_t length = 0,
47  uint32_t channel = 0);
48 
49  bool ready () const;
50  uint32_t latency () const { return _n_samples; }
51  uint32_t n_inputs () const { return _n_inputs; }
52  uint32_t n_outputs () const { return _n_outputs; }
53 
54  void clear_impdata ();
55  void restart ();
57 
58  void run_mono_buffered (float*, uint32_t);
59  void run_mono_no_latency (float*, uint32_t);
60 
61 protected:
63 
64  uint32_t _n_samples;
65  uint32_t _max_size;
66  uint32_t _offset;
68  bool _threaded;
69 
70 private:
71  class ImpData : public AudioReadable
72  {
73  public:
74  ImpData (uint32_t ci, uint32_t co, std::shared_ptr<AudioReadable> r, float g, float d, sampleoffset_t s = 0, samplecnt_t l = 0, uint32_t c = 0)
75  : c_in (ci)
76  , c_out (co)
77  , gain (g)
78  , delay (d)
79  , _readable (r)
80  , _offset (s)
81  , _length (l)
82  , _channel (c)
83  {}
84 
85  uint32_t c_in;
86  uint32_t c_out;
87  float gain;
88  uint32_t delay;
89 
90  samplecnt_t read (Sample* s, samplepos_t pos, samplecnt_t cnt, int c = -1) const {
91  return _readable->read (s, pos + _offset, cnt, _channel);
92  }
93 
95  samplecnt_t rl = _readable->readable_length_samples ();
96  if (rl < _offset) {
97  return 0;
98  } else if (_length > 0) {
99  return std::min (rl - _offset, _length);
100  } else {
101  return rl - _offset;
102  }
103  }
104 
105  uint32_t n_channels () const {
106  return _readable->n_channels ();
107  }
108 
109  private:
110  std::shared_ptr<AudioReadable> _readable;
111 
114  uint32_t _channel;
115  };
116 
117  std::vector<ImpData> _impdata;
118  uint32_t _n_inputs;
119  uint32_t _n_outputs;
120 };
121 
123 {
124 public:
129  };
130 
131  static uint32_t ircc_in (IRChannelConfig irc) {
132  return irc < Stereo ? 1 : 2;
133  }
134 
135  static uint32_t ircc_out (IRChannelConfig irc) {
136  return irc == Mono ? 1 : 2;
137  }
138 
139  struct IRSettings {
141  {
142  gain = 1.0;
143  pre_delay = 0.0;
144  channel_gain[0] = channel_gain[1] = channel_gain[2] = channel_gain[3] = 1.0;
145  channel_delay[0] = channel_delay[1] = channel_delay[2] = channel_delay[3] = 0;
146  };
147 
148  float gain;
149  uint32_t pre_delay;
150  float channel_gain[4];
151  uint32_t channel_delay[4];
152 
153  /* convenient array accessors for Lua bindings */
154  float get_channel_gain (unsigned i) const {
155  if (i < 4) { return channel_gain[i]; }
156  return 0;
157  }
158  void set_channel_gain (unsigned i, float g) {
159  if (i < 4) { channel_gain[i] = g; }
160  }
161  uint32_t get_channel_delay (unsigned i) const {
162  if (i < 4) { return channel_delay[i]; }
163  return 0;
164  }
165  void set_channel_delay (unsigned i, uint32_t d) {
166  if (i < 4) { channel_delay[i] = d; }
167  }
168  };
169 
170  Convolver (Session&, std::string const&, IRChannelConfig irc = Mono, IRSettings irs = IRSettings ());
171 
172  void run_mono_buffered (float*, uint32_t);
173  void run_stereo_buffered (float* L, float* R, uint32_t);
174 
175  void run_mono_no_latency (float*, uint32_t);
176  void run_stereo_no_latency (float* L, float* R, uint32_t);
177 
178 private:
179  std::vector<std::shared_ptr<AudioReadable> > _readables;
180 
183 };
184 
185 } } /* namespace */
186 #endif
samplecnt_t read(Sample *s, samplepos_t pos, samplecnt_t cnt, int c=-1) const
Definition: convolver.h:90
samplecnt_t readable_length_samples() const
Definition: convolver.h:94
ImpData(uint32_t ci, uint32_t co, std::shared_ptr< AudioReadable > r, float g, float d, sampleoffset_t s=0, samplecnt_t l=0, uint32_t c=0)
Definition: convolver.h:74
std::shared_ptr< AudioReadable > _readable
Definition: convolver.h:110
void run_mono_buffered(float *, uint32_t)
bool add_impdata(uint32_t c_in, uint32_t c_out, std::shared_ptr< AudioReadable > r, float gain=1.0, uint32_t pre_delay=0, sampleoffset_t offset=0, samplecnt_t length=0, uint32_t channel=0)
uint32_t n_outputs() const
Definition: convolver.h:52
void run_mono_no_latency(float *, uint32_t)
uint32_t n_inputs() const
Definition: convolver.h:51
uint32_t latency() const
Definition: convolver.h:50
ArdourZita::Convproc _convproc
Definition: convolver.h:62
Convolution(Session &, uint32_t n_in, uint32_t n_out)
std::vector< ImpData > _impdata
Definition: convolver.h:117
void run(BufferSet &, ChanMapping const &, ChanMapping const &, pframes_t, samplecnt_t)
IRSettings _ir_settings
Definition: convolver.h:182
@ MonoToStereo
1 in, 2 out, stereo IR M -> L, M -> R
Definition: convolver.h:127
@ Stereo
2 in, 2 out, stereo IR L -> L, R -> R || 4 chan IR L -> L, L -> R, R -> R, R -> L
Definition: convolver.h:128
@ Mono
1 in, 1 out; 1ch IR
Definition: convolver.h:126
void run_mono_no_latency(float *, uint32_t)
void run_mono_buffered(float *, uint32_t)
static uint32_t ircc_in(IRChannelConfig irc)
Definition: convolver.h:131
void run_stereo_no_latency(float *L, float *R, uint32_t)
static uint32_t ircc_out(IRChannelConfig irc)
Definition: convolver.h:135
std::vector< std::shared_ptr< AudioReadable > > _readables
Definition: convolver.h:179
void run_stereo_buffered(float *L, float *R, uint32_t)
IRChannelConfig _irc
Definition: convolver.h:181
Convolver(Session &, std::string const &, IRChannelConfig irc=Mono, IRSettings irs=IRSettings())
#define LIBARDOUR_API
PBD::PropertyDescriptor< gain_t > gain
PBD::PropertyDescriptor< timecnt_t > length
uint32_t pframes_t
Temporal::samplecnt_t samplecnt_t
Temporal::sampleoffset_t sampleoffset_t
Temporal::samplepos_t samplepos_t
void set_channel_delay(unsigned i, uint32_t d)
Definition: convolver.h:165
float get_channel_gain(unsigned i) const
Definition: convolver.h:154
void set_channel_gain(unsigned i, float g)
Definition: convolver.h:158
uint32_t get_channel_delay(unsigned i) const
Definition: convolver.h:161