-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitshift.cpp
230 lines (187 loc) · 5.6 KB
/
pitshift.cpp
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
#include "stdafx.h"
#include "dsp_def.h"
#include "dsp_util.h"
#include "delay.h"
#include "pitshift.h"
#include <math.h>
#define DEF_CH 1
#define DEF_BIT 16
#define DEF_SAMRATE 22050
#define DEF_FADER 1
#define DEF_LOW 24
#define DEF_MID 1000
#define DEF_UPPER 2024
#define DEF_SHIFT 0
#define DEF_VOL 1
#define DEF_DELAYLEN 2048
#define DEF_DELAYGAP 2000
#define DEF_NOTCHGAIN 0.001
#define DEF_SHIFTGAIN 1024
int Compute(PITCHSHIFT *pShift)
{
pShift->Delay[0] += pShift->Shift[0];
while (pShift->Delay[0] > DEF_UPPER)
{
pShift->Delay[0] -= DEF_DELAYGAP;
if (pShift->Shift[0] != pShift->Shift[1])
pShift->Shift[0] = pShift->Shift[1];
}
while (pShift->Delay[0] < DEF_LOW)
{
pShift->Delay[0] += DEF_DELAYGAP;
if (pShift->Shift[0] != pShift->Shift[1])
pShift->Shift[0] = pShift->Shift[1];
}
pShift->Delay[1] = pShift->Delay[0] + DEF_MID;
while (pShift->Delay[1] > DEF_UPPER)
{
pShift->Delay[1] -= DEF_DELAYGAP;
if (pShift->Shift[0] != pShift->Shift[1])
pShift->Shift[0] = pShift->Shift[1];
}
while (pShift->Delay[1] < DEF_LOW)
{
pShift->Delay[1] += DEF_DELAYGAP;
if (pShift->Shift[0] != pShift->Shift[1])
pShift->Shift[0] = pShift->Shift[1];
}
pShift->Fader[1] = float(fabs(pShift->Delay[0] - DEF_SHIFTGAIN) * DEF_NOTCHGAIN);
pShift->Fader[0] = 1 - pShift->Fader[1];
return TRUE;
}
int InitPitchShift(PITCHSHIFT *pShift)
{
InitDelay(&(pShift->Buffer[0]));
DoMsgProc();
InitDelay(&(pShift->Buffer[1]));
DoMsgProc();
pShift->nCh = DEF_CH;
pShift->nBit = DEF_BIT;
pShift->nSamplePerSec = DEF_SAMRATE;
pShift->Fader[0] = DEF_FADER;
pShift->Fader[1] = DEF_FADER;
pShift->Delay[0] = DEF_LOW;
pShift->Delay[1] = DEF_UPPER;
pShift->Shift[0] = DEF_SHIFT;
pShift->Shift[1] = DEF_SHIFT;
pShift->Vol[0] = DEF_VOL;
pShift->Vol[1] = DEF_VOL;
pShift->isProc = false;
return TRUE;
}
int MakePitchShift(PITCHSHIFT *pShift)
{
pShift->Buffer[0].nCh = pShift->nCh;
pShift->Buffer[1].nCh = pShift->nCh;
pShift->Buffer[0].nBit = pShift->nBit;
pShift->Buffer[1].nBit = pShift->nBit;
pShift->Buffer[0].nSamplePerSec = pShift->nSamplePerSec;
pShift->Buffer[1].nSamplePerSec = pShift->nSamplePerSec;
pShift->Buffer[0].WetGain = 1;
pShift->Buffer[1].WetGain = 1;
pShift->Buffer[0].DryGain = 1;
pShift->Buffer[1].DryGain = 1;
pShift->isProc = false;
MakeDelayLine(&(pShift->Buffer[0]), 1, DEF_DELAYLEN);
DoMsgProc();
MakeDelayLine(&(pShift->Buffer[1]), 1, DEF_DELAYLEN);
DoMsgProc();
return TRUE;
}
int SetShiftAmount(PITCHSHIFT *pShift, float amt)
{
pShift->Shift[1] = amt;
pShift->Shift[0] = pShift->Shift[1];
return TRUE;
}
int ShiftPitch(PITCHSHIFT *pShift, char *pData, int nSam)
{
int i;
INT16 *p16BitBuff, Tmp16M1,Tmp16M2, s16M,
Tmp16S1[2], Tmp16S2[2],s16S[2];
INT8 *p8BitBuff, Tmp8M1, Tmp8M2, s8M,
Tmp8S1[2], Tmp8S2[2], s8S[2];
while(pShift->isProc == true)
DoMsgProc();
pShift->isProc = true;
p8BitBuff = pData;
p16BitBuff = (INT16*)pData;
switch(pShift->nCh)
{
case 1:
if (pShift->nBit==8)
{
for(i = 0 ; i < nSam ; i++)
{
Compute(pShift);
PutDelay(&(pShift->Buffer[0]),&p8BitBuff[i],1);
PutDelay(&(pShift->Buffer[1]),&p8BitBuff[i],1);
GetFracDelay(&(pShift->Buffer[0]),&Tmp8M1,1,pShift->Delay[0]);
GetFracDelay(&(pShift->Buffer[1]),&Tmp8M2,1,pShift->Delay[1]);
s8M = Return8Bit(Set8Bit(Tmp8M1)*pShift->Fader[0] +
Set8Bit(Tmp8M2)*pShift->Fader[1]);
p8BitBuff[i] = s8M;
}
}else if (pShift->nBit==16)
{
for(i = 0 ; i < nSam ; i++)
{
Compute(pShift);
PutDelay(&(pShift->Buffer[0]),(char*)(&p16BitBuff[i]),1);
PutDelay(&(pShift->Buffer[1]),(char*)(&p16BitBuff[i]),1);
GetFracDelay(&(pShift->Buffer[0]),(char*)(&Tmp16M1),1,pShift->Delay[0]);
GetFracDelay(&(pShift->Buffer[1]),(char*)(&Tmp16M2),1,pShift->Delay[1]);
s16M =Return16Bit((Tmp16M1*pShift->Fader[0]) + (Tmp16M2*pShift->Fader[1]));
p16BitBuff[i] = s16M;
}
}else{
pShift->isProc = false;
return FALSE;
}
break;
case 2:
if (pShift->nBit==8)
{
for(i = 0 ; i < nSam ; i++)
{
Compute(pShift);
Tmp8S1[0] = p8BitBuff[i*2];
Tmp8S1[1] = p8BitBuff[i*2+1];
PutDelay(&(pShift->Buffer[0]),(char*)Tmp8S1,1);
PutDelay(&(pShift->Buffer[1]),(char*)Tmp8S1,1);
GetFracDelay(&(pShift->Buffer[0]),(char*)Tmp8S1,1,pShift->Delay[0]);
GetFracDelay(&(pShift->Buffer[1]),(char*)Tmp8S2,1,pShift->Delay[1]);
s8S[0] = Return8Bit(Set8Bit(Tmp8S1[0])*pShift->Fader[0] +
Set8Bit(Tmp8S2[0])*pShift->Fader[1]);
s8S[1] = Return8Bit(Set8Bit(Tmp8S1[1])*pShift->Fader[0] +
Set8Bit(Tmp8S2[1])*pShift->Fader[1]);
p8BitBuff[i*2] = s8S[0];
p8BitBuff[i*2+1] = s8S[1];
}
}else if (pShift->nBit==16)
{
for(i = 0 ; i < nSam ; i++)
{
Compute(pShift);
Tmp16S1[0] = p16BitBuff[i*2];
Tmp16S1[1] = p16BitBuff[i*2+1];
PutDelay(&(pShift->Buffer[0]),(char*)Tmp16S1,1);
PutDelay(&(pShift->Buffer[1]),(char*)Tmp16S1,1);
GetFracDelay(&(pShift->Buffer[0]),(char*)Tmp16S1,1,pShift->Delay[0]);
GetFracDelay(&(pShift->Buffer[1]),(char*)Tmp16S2,1,pShift->Delay[1]);
s16S[0] = Return16Bit(Tmp16S1[0]*pShift->Fader[0] +
Tmp16S2[0]*pShift->Fader[1]);
s16S[1] = Return16Bit(Tmp16S1[1]*pShift->Fader[0] +
Tmp16S2[1]*pShift->Fader[1]);
p16BitBuff[i*2] = s16S[0];
p16BitBuff[i*2+1] = s16S[1];
}
}else{
pShift->isProc = false;
return FALSE;
}
break;
}
pShift->isProc = false;
return TRUE;
}