gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
WSOLA.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream 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  GTK+ IOStream 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 have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef WSOLA_H_
18 #define WSOLA_H_
19 
20 #include "config.h"
21 #ifdef HAVE_EMSCRIPTEN
22 #include <emscripten/bind.h>
23 #endif
24 
25 #ifdef _MSC_VER
26 #define MS_ROUND
27 #ifdef _MSC_VER
28 #define _USE_MATH_DEFINES
29 #endif
30 #include <math.h>
31 
33 double round(double number) {
34  return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
35 }
36 #endif
37 
38 #include <Debug.H>
39 
40 #define WSOLA_MOD2_ERROR -10+WSOLA_ERROR_OFFSET
41 #define WSOLA_NFRAMES_JACK_ERROR -11+WSOLA_ERROR_OFFSET
42 #define WSOLA_ROWS_ERROR -12+WSOLA_ERROR_OFFSET
43 #define WSOLA_COLS_ERROR -13+WSOLA_ERROR_OFFSET
44 
45 
47 class WSOLADebug : virtual public Debug {
48 public:
52 #ifndef NDEBUG
53  errors[WSOLA_MOD2_ERROR]=std::string("Developer error : BUFF_SIZE must be divisible by 2. ");
54  errors[WSOLA_NFRAMES_JACK_ERROR]=std::string("Jack nframes request error : Jack wants to process a number of frames which WSOLA can't handle. ");
55  errors[WSOLA_ROWS_ERROR]=std::string("Row request error : You are trying to access beyond the end of the array. ");
56  errors[WSOLA_COLS_ERROR]=std::string("Col request error : You are trying to access beyond the end of the array. ");
57 #endif
58  }
59 
61  virtual ~WSOLADebug() {}
62 };
63 
64 #include <Eigen/Dense>
65 using namespace Eigen;
66 
67 typedef float FP_TYPE;
68 
69 #define FS_DEFAULT 48000.
70 #define TAU 0.02
71 
72 #define DEFAULT_CH_CNT 1
73 
74 #define M_DEFAULT 3;
75 
76 
82 class WSOLA {
83 
84  float fs;
85 
86  int N;
87  int NO2;
88  int M;
89  int m;
90  double rem;
91 
93 
95 
97 
99 
104  template<typename Derived>
105  int findSimilarityInBuffer(const DenseBase<Derived> &buffer);
106 
111  template<typename Derived>
112  FP_TYPE findSimilarity(const ArrayBase<Derived> &outputIn) {
113  simComp=outputIn;
114  simComp*=wnd;
115  return rms(nextOutput-simComp);
116  }
117 
123  template<typename Derived>
124  FP_TYPE rms(const ArrayBase<Derived> &s) {
125  //r=sqrt(mean(s.^2));
126 // cout<<"norm"<<endl;
127 // cout<<s.matrix().norm()<<endl;
128  return s.matrix().norm();
129  }
130 
132  void OLAWnd(void);
133 
135 
138  void init(void);
139 public:
140 
143 
147  WSOLA();
148 
152  WSOLA(int chCnt);
153 
154  virtual ~WSOLA();
155 
156 #ifdef HAVE_EMSCRIPTEN
157  int processOurInput(FP_TYPE timeScale) {
158  return process(timeScale, input);
159  }
160 #endif
161 
168  template<typename Derived>
169  int process(FP_TYPE timeScale, const DenseBase<Derived> &input) {
170 // cout<<"input : "<<endl;
171 // cout<<input<<endl;
172 // cout<<"buffer pre shift : "<<endl;
173 // cout<<buffer<<endl;
174  // shift the buffer on
175  int chCnt=buffer.rows();
176  buffer.block(0,0,chCnt,buffer.cols()-inputSamplesRequired)=buffer.block(0,inputSamplesRequired,chCnt,buffer.cols()-inputSamplesRequired);
177 // cout<<"buffer post shift : "<<endl;
178 // cout<<buffer<<endl;
179  // load the required input samples into the buffer
180  buffer.block(0,buffer.cols()-inputSamplesRequired,chCnt,inputSamplesRequired)=input.block(0,0,chCnt,inputSamplesRequired);
181 // cout<<"buffer with input : "<<endl;
182 // cout<<buffer<<endl;
183 
184  processInner(); // do the inner processing
185 
186  // shift on by the ratio of N/2
187 // cout<<"rem "<<rem<<endl;
188  double shiftOn=timeScale*NO2+rem;
189 // cout<<"shiftOn "<<shiftOn<<endl;
190  inputSamplesRequired=static_cast<int>(round(shiftOn));
191 // cout<<"inputSamplesRequired "<<inputSamplesRequired<<endl;
192  rem=shiftOn-(double)inputSamplesRequired;
193 // cout<<"rem "<<rem<<endl;
194  return inputSamplesRequired;
195  }
196 
200  void processInner(void);
201 
202 // template<typename Derived>
203 // int loadBuffer(, const DenseBase<Derived> &input);
204 
208  return NO2*(M+2);
209  }
210 
214  int getSamplesRequired(void) {
215  return inputSamplesRequired;
216  }
217 
220  int getOutputSize(void){
221  return NO2;
222  }
223 
227  void reset(int chCnt);
228 
233  int noMoreAudio(){
234  return inputSamplesRequired-=NO2;
235  }
236 
244  int loadInput(int n, int m, FP_TYPE val);
245 
252  FP_TYPE unloadOutput(int n, int m);
253 
257  void setFS(float fsIn);
258 
259 };
260 
261 #endif // WSOLA_H_
int inputSamplesRequired
The number of audio samples required for the next call to process.
Definition: WSOLA.H:134
int NO2
Half the window size.
Definition: WSOLA.H:87
int N
The size of the window.
Definition: WSOLA.H:86
#define WSOLA_COLS_ERROR
Occurs when trying to access a col > the input or output Array cols.
Definition: WSOLA.H:43
Array< FP_TYPE, Dynamic, Dynamic > nextOutput
The output vector to search for, each channel per row.
Definition: WSOLA.H:96
WSOLADebug()
Definition: WSOLA.H:51
double rem
The remainder fraction of a sample to remember for next time (can&#39;t move on by fractions of a sample)...
Definition: WSOLA.H:90
int m
The current row index into the buffer.
Definition: WSOLA.H:89
std::map< int, std::string > errors
This will contain a map between error numbers and descriptive std::strings for the errors...
Definition: Debug.H:115
Array< FP_TYPE, Dynamic, Dynamic > simComp
Temporary vector used for computation.
Definition: WSOLA.H:98
FP_TYPE rms(const ArrayBase< Derived > &s)
Definition: WSOLA.H:124
#define WSOLA_ROWS_ERROR
Occurs when trying to access a row > the input or output Array rows.
Definition: WSOLA.H:42
int noMoreAudio()
Definition: WSOLA.H:233
Array< FP_TYPE, Dynamic, Dynamic > wnd
The overlap add window.
Definition: WSOLA.H:94
FP_TYPE findSimilarity(const ArrayBase< Derived > &outputIn)
Definition: WSOLA.H:112
int getSamplesRequired(void)
Definition: WSOLA.H:214
float FP_TYPE
The floating point type to use if not previously declared.
Definition: WSOLA.H:67
Array< FP_TYPE, Dynamic, Dynamic > input
The input vector, each row is a channel.
Definition: WSOLA.H:142
Array< FP_TYPE, Dynamic, Dynamic > output
The output vector, each row is a channel.
Definition: WSOLA.H:141
double precision function round(x)
Definition: round.f:2
#define WSOLA_MOD2_ERROR
< Provided by GTKIOStream on sf.net
Definition: WSOLA.H:40
#define WSOLA_NFRAMES_JACK_ERROR
Occurs when jack wants to process nframes which is not divisible by N/2.
Definition: WSOLA.H:41
Definition: Debug.H:112
int process(FP_TYPE timeScale, const DenseBase< Derived > &input)
Definition: WSOLA.H:169
virtual ~WSOLADebug()
Destructor.
Definition: WSOLA.H:61
int getMaxInputSamplesRequired(void)
Definition: WSOLA.H:207
int M
The number of windows in the search buffer.
Definition: WSOLA.H:88
Definition: WSOLA.H:82
int getOutputSize(void)
Definition: WSOLA.H:220
float fs
The sample rate in Hz.
Definition: WSOLA.H:84
Array< FP_TYPE, Dynamic, Dynamic > buffer
The buffer of audio, each channel on its own row.
Definition: WSOLA.H:92
gtkIOStream: /tmp/gtkiostream/include/WSOLA.H Source File
GTK+ IOStream  Beta