Ardour  9.0-pre0-582-g084a23a80d
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 #include "ardour/session_handle.h"
31 
32 namespace ARDOUR { namespace DSP {
33 
35 {
36 public:
37  Convolution (Session&, uint32_t n_in, uint32_t n_out);
38  virtual ~Convolution () {}
39 
40  bool add_impdata (
41  uint32_t c_in,
42  uint32_t c_out,
43  std::shared_ptr<AudioReadable> r,
44  float gain = 1.0,
45  uint32_t pre_delay = 0,
46  sampleoffset_t offset = 0,
47  samplecnt_t length = 0,
48  uint32_t channel = 0);
49 
50  bool ready () const;
51  uint32_t latency () const { return _n_samples; }
52  uint32_t n_inputs () const { return _n_inputs; }
53  uint32_t n_outputs () const { return _n_outputs; }
54 
55  void clear_impdata ();
56  void restart ();
58 
59  void run_mono_buffered (float*, uint32_t);
60  void run_mono_no_latency (float*, uint32_t);
61 
62 protected:
64 
65  uint32_t _n_samples;
66  uint32_t _max_size;
67  uint32_t _offset;
69  bool _threaded;
70 
71 private:
72  class ImpData : public AudioReadable
73  {
74  public:
75  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)
76  : c_in (ci)
77  , c_out (co)
78  , gain (g)
79  , delay (d)
80  , _readable (r)
81  , _offset (s)
82  , _length (l)
83  , _channel (c)
84  {}
85 
86  uint32_t c_in;
87  uint32_t c_out;
88  float gain;
89  uint32_t delay;
90 
91  samplecnt_t read (Sample* s, samplepos_t pos, samplecnt_t cnt, int c = -1) const {
92  return _readable->read (s, pos + _offset, cnt, _channel);
93  }
94 
96  samplecnt_t rl = _readable->readable_length_samples ();
97  if (rl < _offset) {
98  return 0;
99  } else if (_length > 0) {
100  return std::min (rl - _offset, _length);
101  } else {
102  return rl - _offset;
103  }
104  }
105 
106  uint32_t n_channels () const {
107  return _readable->n_channels ();
108  }
109 
110  private:
111  std::shared_ptr<AudioReadable> _readable;
112 
115  uint32_t _channel;
116  };
117 
118  std::vector<ImpData> _impdata;
119  uint32_t _n_inputs;
120  uint32_t _n_outputs;
121 };
122 
124 {
125 public:
130  };
131 
132  static uint32_t ircc_in (IRChannelConfig irc) {
133  return irc < Stereo ? 1 : 2;
134  }
135 
136  static uint32_t ircc_out (IRChannelConfig irc) {
137  return irc == Mono ? 1 : 2;
138  }
139 
140  struct IRSettings {
142  {
143  gain = 1.0;
144  pre_delay = 0.0;
145  channel_gain[0] = channel_gain[1] = channel_gain[2] = channel_gain[3] = 1.0;
146  channel_delay[0] = channel_delay[1] = channel_delay[2] = channel_delay[3] = 0;
147  };
148 
149  float gain;
150  uint32_t pre_delay;
151  float channel_gain[4];
152  uint32_t channel_delay[4];
153 
154  /* convenient array accessors for Lua bindings */
155  float get_channel_gain (unsigned i) const {
156  if (i < 4) { return channel_gain[i]; }
157  return 0;
158  }
159  void set_channel_gain (unsigned i, float g) {
160  if (i < 4) { channel_gain[i] = g; }
161  }
162  uint32_t get_channel_delay (unsigned i) const {
163  if (i < 4) { return channel_delay[i]; }
164  return 0;
165  }
166  void set_channel_delay (unsigned i, uint32_t d) {
167  if (i < 4) { channel_delay[i] = d; }
168  }
169  };
170 
171  Convolver (Session&, std::string const&, IRChannelConfig irc = Mono, IRSettings irs = IRSettings ());
172 
173  void run_mono_buffered (float*, uint32_t);
174  void run_stereo_buffered (float* L, float* R, uint32_t);
175 
176  void run_mono_no_latency (float*, uint32_t);
177  void run_stereo_no_latency (float* L, float* R, uint32_t);
178 
179 private:
180  std::vector<std::shared_ptr<AudioReadable> > _readables;
181 
184 };
185 
186 } } /* namespace */
187 #endif
samplecnt_t read(Sample *s, samplepos_t pos, samplecnt_t cnt, int c=-1) const
Definition: convolver.h:91
samplecnt_t readable_length_samples() const
Definition: convolver.h:95
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:75
std::shared_ptr< AudioReadable > _readable
Definition: convolver.h:111
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:53
void run_mono_no_latency(float *, uint32_t)
uint32_t n_inputs() const
Definition: convolver.h:52
uint32_t latency() const
Definition: convolver.h:51
ArdourZita::Convproc _convproc
Definition: convolver.h:63
Convolution(Session &, uint32_t n_in, uint32_t n_out)
std::vector< ImpData > _impdata
Definition: convolver.h:118
void run(BufferSet &, ChanMapping const &, ChanMapping const &, pframes_t, samplecnt_t)
IRSettings _ir_settings
Definition: convolver.h:183
@ MonoToStereo
1 in, 2 out, stereo IR M -> L, M -> R
Definition: convolver.h:128
@ 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:129
@ Mono
1 in, 1 out; 1ch IR
Definition: convolver.h:127
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:132
void run_stereo_no_latency(float *L, float *R, uint32_t)
static uint32_t ircc_out(IRChannelConfig irc)
Definition: convolver.h:136
std::vector< std::shared_ptr< AudioReadable > > _readables
Definition: convolver.h:180
void run_stereo_buffered(float *L, float *R, uint32_t)
IRChannelConfig _irc
Definition: convolver.h:182
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:166
float get_channel_gain(unsigned i) const
Definition: convolver.h:155
void set_channel_gain(unsigned i, float g)
Definition: convolver.h:159
uint32_t get_channel_delay(unsigned i) const
Definition: convolver.h:162