Guitarix
gx_convolver.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3  * Copyright (C) 2011 Pete Shorthose
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
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  * --------------------------------------------------------------------------
19  */
20 
21 /* ------- This is the guitarix convolution Engine ------- */
22 
23 #pragma once
24 
25 #ifndef SRC_HEADERS_GX_CONVOLVER_H_
26 #define SRC_HEADERS_GX_CONVOLVER_H_
27 
28 #include <zita-convolver.h>
29 #include <gxwmm/gainline.h>
30 
31 #include <sndfile.hh>
32 
33 namespace gx_engine {
34 
35 /* GxConvolver */
36 
37 class Audiofile {
38 public:
39 
40  enum {
45  };
46 
47  enum {
53  };
54 
55  enum {
56  ERR_NONE = 0,
57  ERR_MODE = -1,
58  ERR_TYPE = -2,
59  ERR_FORM = -3,
60  ERR_OPEN = -4,
61  ERR_SEEK = -5,
62  ERR_DATA = -6,
63  ERR_READ = -7,
64  ERR_WRITE = -8
65  };
66 
67  Audiofile(void);
68  ~Audiofile(void);
69 
70  int type(void) const { return _type; }
71  int form(void) const { return _form; }
72  int rate(void) const { return _rate; }
73  int chan(void) const { return _chan; }
74  unsigned int size(void) const { return _size; }
75 
76  int open_read(string name);
77  int close(void);
78 
79  int seek(unsigned int posit);
80  int read(float *data, unsigned int frames);
81 
82 private:
83 
84  void reset(void);
85 
86  SNDFILE *_sndfile;
87  int _type;
88  int _form;
89  int _rate;
90  int _chan;
91  unsigned int _size;
92 };
93 
94 bool read_audio(const std::string& filename, unsigned int *audio_size, int *audio_chan,
95  int *audio_type, int *audio_form, int *audio_rate, float **buffer);
96 
97 class GxConvolverBase: protected Convproc {
98 protected:
99  volatile bool ready;
100  bool sync;
101  void adjust_values(unsigned int audio_size, unsigned int& count, unsigned int& offset,
102  unsigned int& delay, unsigned int& ldelay, unsigned int& length,
103  unsigned int& size, unsigned int& bufsize);
104  unsigned int buffersize;
105  unsigned int samplerate;
106  GxConvolverBase(): ready(false), sync(false), buffersize(), samplerate() {}
107  ~GxConvolverBase();
108 public:
109  inline void set_buffersize(unsigned int sz) { buffersize = sz; }
110  inline unsigned int get_buffersize() { return buffersize; }
111  inline void set_samplerate(unsigned int sr) { samplerate = sr; }
112  inline unsigned int get_samplerate() { return samplerate; }
113  bool checkstate();
114  using Convproc::state;
115  inline void set_not_runnable() { ready = false; }
116  inline bool is_runnable() { return ready; }
117  bool start(int policy, int priority);
118  using Convproc::stop_process;
119  inline void set_sync(bool val) { sync = val; }
120 };
121 
123 private:
125  bool read_sndfile(Audiofile& audio, int nchan, int samplerate, const float *gain,
126  unsigned int *delay, unsigned int offset, unsigned int length,
127  const Gainline& points);
128 public:
129  GxConvolver(): GxConvolverBase(), resamp() {}
130  bool configure(
131  string fname, float gain, float lgain,
132  unsigned int delay, unsigned int ldelay, unsigned int offset,
133  unsigned int length, unsigned int size, unsigned int bufsize,
134  const Gainline& gainline);
135  bool compute(int count, float* input1, float *input2, float *output1, float *output2);
136  bool configure(string fname, float gain, unsigned int delay, unsigned int offset,
137  unsigned int length, unsigned int size, unsigned int bufsize,
138  const Gainline& gainline);
139  bool compute(int count, float* input, float *output);
140  static void compute_interpolation(double& fct, double& gp, unsigned int& idx,
141  const Gainline& points, int offset);
142 };
143 
145  double& fct, double& gp, unsigned int& idx, const Gainline& points, int offset) {
146  fct = (points[idx+1].g-points[idx].g)/(20*(points[idx+1].i-points[idx].i));
147  gp = points[idx].g/20 + fct * (offset-points[idx].i);
148  idx++;
149 }
150 
151 
153 private:
155 public:
157  : GxConvolverBase(), resamp(resamp_) {}
158  bool configure(int count, float *impresp, unsigned int imprate);
159  bool update(int count, float *impresp, unsigned int imprate);
160  bool compute(int count, float* input, float *output);
161  bool compute(int count, float* buffer) {
162  return is_runnable() ? compute(count, buffer, buffer) : true;
163  }
164 };
165 
166 } /* end of gx_engine namespace */
167 #endif // SRC_HEADERS_GX_CONVOLVER_H_
GxSimpleConvolver(gx_resample::BufferResampler &resamp_)
Definition: gx_convolver.h:156
unsigned int get_buffersize()
Definition: gx_convolver.h:110
CmdConnection::msg_type start
Definition: jsonrpc.cpp:255
int rate(void) const
Definition: gx_convolver.h:72
int open_read(string name)
bool read_audio(const std::string &filename, unsigned int *audio_size, int *audio_chan, int *audio_type, int *audio_form, int *audio_rate, float **buffer)
unsigned int get_samplerate()
Definition: gx_convolver.h:112
int seek(unsigned int posit)
int type(void) const
Definition: gx_convolver.h:70
void compute(int count, float *input0, float *output0)
unsigned int size(void) const
Definition: gx_convolver.h:74
bool compute(int count, float *buffer)
Definition: gx_convolver.h:161
int form(void) const
Definition: gx_convolver.h:71
void set_buffersize(unsigned int sz)
Definition: gx_convolver.h:109
int read(float *data, unsigned int frames)
void set_samplerate(unsigned int sr)
Definition: gx_convolver.h:111
static void compute_interpolation(double &fct, double &gp, unsigned int &idx, const Gainline &points, int offset)
Definition: gx_convolver.h:144
int chan(void) const
Definition: gx_convolver.h:73