-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_pitchshift2.h
315 lines (248 loc) · 8.71 KB
/
simple_pitchshift2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#ifndef _WDL_SIMPLEPITCHSHIFT_H_
#define _WDL_SIMPLEPITCHSHIFT_H_
#include "queue.h"
#ifndef WDL_SIMPLEPITCHSHIFT_SAMPLETYPE
#define WDL_SIMPLEPITCHSHIFT_SAMPLETYPE double
#endif
// this one isnt done yet, but stretches, then (optionally) resamples
#ifdef WDL_SIMPLEPITCHSHIFT_PARENTCLASS
class WDL_SimplePitchShifter2 : public WDL_SIMPLEPITCHSHIFT_PARENTCLASS
#else
class WDL_SimplePitchShifter2
#endif
{
public:
WDL_SimplePitchShifter2()
{
m_last_nch=1;
m_srate=44100.0;
m_last_tempo=1.0;
m_last_shift=1.0;
m_qual=0;
Reset();
}
~WDL_SimplePitchShifter2() { }
void Reset()
{
m_hadinput=0;
m_pspos=0.0;
m_pswritepos=0;
m_tempo_fracpos=0.0;
m_queue.Clear();
m_rsbuf.Resize(0,false);
m_psbuf.Resize(0,false);
}
bool IsReset()
{
return !m_queue.Available() && !m_hadinput;
}
void set_srate(double srate) { m_srate=srate; }
void set_nch(int nch) { if (m_last_nch!=nch) { m_queue.Clear(); m_last_nch=nch; m_tempo_fracpos=0.0; } }
void set_shift(double shift) { m_last_shift=shift; }
void set_tempo(double tempo) { m_last_tempo=tempo; }
void set_formant_shift(double shift)
{
}
WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *GetBuffer(int size)
{
return m_inbuf.Resize(size*m_last_nch);
}
void BufferDone(int input_filled);
void FlushSamples() {}
static char *enumQual(int q);
static bool GetSizes(int qv, int *ws, int *os);
int GetSamples(int requested_output, WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *buffer);
void SetQualityParameter(int parm)
{
m_qual=parm;
}
int Stretch(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *inputs, WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *outputs, int nch, int length, int maxoutlen, double stretch, double srate, int ws_ms, int os_ms);
private:
int StretchBlock(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *inputs, WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *outputs, int nch, int length, int maxoutlen, double stretch, int bsize, int olsize, double srate);
private:
double m_pspos WDL_FIXALIGN;
double m_tempo_fracpos;
double m_srate,m_last_tempo,m_last_shift;
WDL_TypedBuf<WDL_SIMPLEPITCHSHIFT_SAMPLETYPE> m_psbuf;
WDL_Queue m_queue;
WDL_TypedBuf<WDL_SIMPLEPITCHSHIFT_SAMPLETYPE> m_inbuf;
WDL_TypedBuf<WDL_SIMPLEPITCHSHIFT_SAMPLETYPE> m_rsbuf;
int m_pswritepos;
int m_last_nch;
int m_qual;
int m_hadinput;
};
#ifdef WDL_SIMPLEPITCHSHIFT_IMPLEMENT
void WDL_SimplePitchShifter2::BufferDone(int input_filled)
{
if (input_filled>0)
{
m_hadinput=1;
int ws,os;
GetSizes(m_qual,&ws,&os);
int max_outputlen=(int) (input_filled * m_last_shift / m_last_tempo * 1.1f + 32.0f);
if (fabs(m_last_shift-1.0)<0.0000000001)
{
int valid_amt = Stretch(m_inbuf.Get(),(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *)m_queue.Add(NULL,max_outputlen*m_last_nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE)),m_last_nch,
input_filled,max_outputlen,1.0/m_last_tempo,m_srate,ws,os);
if (valid_amt < max_outputlen)
m_queue.Add(NULL,(valid_amt-max_outputlen)*m_last_nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE));
}
else
{
int needclear=m_rsbuf.GetSize()<m_last_nch;
WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *bufi=m_rsbuf.Resize((max_outputlen+1)*m_last_nch,false);
if (needclear)
memset(bufi,0,m_last_nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE));
int valid_amt = Stretch(m_inbuf.Get(),bufi+m_last_nch,m_last_nch,input_filled,max_outputlen,m_last_shift/m_last_tempo,m_srate,ws,os);
double adv=m_last_shift;
double fp=m_tempo_fracpos;
int out_max = (int) (input_filled / m_last_tempo * 1.1f + 32.0f);
WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *bufo = (WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *)m_queue.Add(NULL,out_max*m_last_nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE));
// resample bufi to bufo
int i,nch=m_last_nch;
for (i = 0; i < out_max; i ++)
{
double rdpos=floor(fp);
int idx=((int)rdpos);
if (idx>=valid_amt)
{
// un-add any missing samples
m_queue.Add(NULL,(i-out_max)*m_last_nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE));
break;
}
rdpos = (fp-rdpos);
int a;
idx*=nch;
for (a = 0; a < nch; a ++)
{
*bufo++ = bufi[idx+a]*(1.0-rdpos)+bufi[idx+nch+a]*rdpos;
}
fp += adv;
}
memcpy(bufi,bufi+m_last_nch*valid_amt,m_last_nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE)); // save last sample for interpolation later
//
m_tempo_fracpos=fp-floor(fp);
}
}
}
char *WDL_SimplePitchShifter2::enumQual(int q)
{
int ws,os;
if (!GetSizes(q,&ws,&os)) return NULL;
static char buf[128];
sprintf(buf,"%dms window, %dms fade",ws,os);
return buf;
}
bool WDL_SimplePitchShifter2::GetSizes(int qv, int *ws, int *os)
{
int windows[]={50,75,100,150,225,300,40,30,20,10,5,3};
int divs[]={2,3,5,7};
int wd=qv/(sizeof(divs)/sizeof(divs[0]));
if (wd >= sizeof(windows)/sizeof(windows[0])) wd=-1;
*ws=windows[wd>=0?wd:0];
*os = *ws / divs[qv%(sizeof(divs)/sizeof(divs[0]))];
if (*os<1) *os=1;
return wd>=0;
}
int WDL_SimplePitchShifter2::GetSamples(int requested_output, WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *buffer)
{
if (!m_last_nch||requested_output<1) return 0;
int l=m_queue.Available()/sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE)/m_last_nch;
if (requested_output>l) requested_output=l;
int sz=requested_output*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE)*m_last_nch;
memcpy(buffer,m_queue.Get(),sz);
m_queue.Advance(sz);
m_queue.Compact();
return requested_output;
}
int WDL_SimplePitchShifter2::Stretch(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *inputs, WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *outputs, int nch, int length, int maxoutlen, double stretch, double srate, int ws_ms, int os_ms)
{
int bsize=(int) (ws_ms * 0.001 * srate);
if (bsize<16) bsize=16;
else if (bsize>128*1024)bsize=128*1024;
int olsize=(int) (os_ms * 0.001 * srate);
if (olsize > bsize/2) olsize=bsize/2;
if (olsize<1)olsize=1;
if (m_psbuf.GetSize() != bsize*nch)
{
memset(m_psbuf.Resize(bsize*nch,false),0,sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE)*bsize*nch);
m_pspos=(double) (bsize/2);
m_pswritepos=0;
}
return StretchBlock(inputs,outputs,nch,length,maxoutlen,stretch,bsize,olsize,srate);
}
int WDL_SimplePitchShifter2::StretchBlock(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *inputs, WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *outputs, int nch, int length, int maxoutlen, double stretch, int bsize, int olsize, double srate)
{
double iolsize=1.0/olsize;
WDL_SIMPLEPITCHSHIFT_SAMPLETYPE *psbuf=m_psbuf.Get();
double pspos=m_pspos;
int writepos=m_pswritepos;
int writeposnch = writepos*nch;
int bsizench = bsize*nch;
int olsizench = olsize*nch;
int output_used=0;
int i=length;
int chunksize = nch*sizeof(WDL_SIMPLEPITCHSHIFT_SAMPLETYPE);
while (i--)
{
int cnt = (int) (pspos + stretch) - (int) pspos;
double pspos_fracadd=stretch - cnt;
while (cnt-- > 0)
{
if (output_used>=maxoutlen) return maxoutlen;
int ipos1=(int)pspos;
ipos1*=nch;
memcpy(outputs,psbuf+ipos1,chunksize);
double tv=pspos;
if (stretch >= 1.0)
{
if (tv > writepos) tv-=bsize;
if (tv >= writepos-olsize && tv < writepos)
{
double tfrac=(writepos-tv)*iolsize;
int tmp=ipos1+olsizench;
if (tmp>=bsizench) tmp-=bsizench;
int a;
for(a=0;a<nch;a++) outputs[a]= outputs[a]*tfrac + (1-tfrac)*psbuf[tmp+a];
if (tv+stretch >= writepos)
{
pspos+=olsize;
}
}
}
else
{
if (tv<writepos) tv+=bsize;
if (tv >= writepos && tv < writepos+olsize)
{
double tfrac=(tv-writepos)*iolsize;
int tmp=ipos1+olsizench;
if (tmp>=bsizench) tmp -= bsizench;
int a;
for(a=0;a<nch;a++) outputs[a] = outputs[a]*tfrac + (1-tfrac)*psbuf[tmp+a];
// this is wrong, but blehhh?
if (tv+stretch < writepos+1)
{
// pspos += olsize;
}
if (tv+stretch >= writepos+olsize) pspos += olsize;
}
}
if ((pspos+=1) >= bsize) pspos -= bsize;
outputs += nch;
output_used++;
}
pspos += pspos_fracadd;
if (pspos>=bsize) pspos-=bsize;
memcpy(psbuf+writeposnch,inputs,chunksize);
writeposnch += nch;
if (++writepos >= bsize) writeposnch = writepos=0;
inputs += nch;
} // sample loop
m_pspos=pspos;
m_pswritepos=writepos;
return output_used;
}
#endif
#endif