-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPitchShifter.ts
199 lines (150 loc) · 6.15 KB
/
PitchShifter.ts
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
class PitchShifter {
private previousPitch: number = -1;
private delayTime: number = 0.50;
public static get FADE_TIME(): number {
return 0.25;
}
public static get BUFFER_TIME(): number {
return 0.50;
}
private shiftDownBuffer: AudioBuffer;
private shiftUpBuffer: AudioBuffer;
private context: AudioContext;
public input: GainNode;
public output: GainNode;
private bufferSources: AudioBufferSourceNode[] = [];
private modGains: GainNode[] = [];
private delayGains: GainNode[] = [];
private delays: any[] = []; //Should be type DelayNode but typescript doesn't like it
private fadeBufferSources: AudioBufferSourceNode[] = [];
private mixGains: any[] = []; //Should be type GainNode but typescript doesn't like it
constructor(context){
this.context = context;
// Create nodes for the input and output of this "module".
this.input = this.context.createGain();
this.output = this.context.createGain();
for (let i = 0; i < 4; i++){
this.bufferSources.push(this.context.createBufferSource());
}
this.shiftDownBuffer = this.createDelayTimeBuffer(false);
this.shiftUpBuffer = this.createDelayTimeBuffer(true);
//todo: for loop!
this.bufferSources.forEach((bufferSource: AudioBufferSourceNode, i: number) => {
bufferSource.buffer = i < 2 ? this.shiftDownBuffer : this.shiftUpBuffer;
bufferSource.loop = true;
});
// for switching between oct-up and oct-down //todo FOR LOOP!
for (let i = 0; i < 4; i++){
this.modGains.push(this.context.createGain());
this.bufferSources[i].connect(this.modGains[i])
}
this.modGains.forEach((modGain: GainNode, i: number) => {
modGain.gain.value = i < 2 ? 1 : 0;
});
for (let i = 0; i < 2; i++){
this.delayGains.push(this.context.createGain());
this.delays.push(this.context.createDelay());
this.delayGains[i].connect(this.delays[i].delayTime);
}
for (let i = 0; i < 4; i++) {
if (i % 2 === 0){
this.modGains[i].connect(this.delayGains[0])
} else {
this.modGains[i].connect(this.delayGains[1])
}
}
var fadeBuffer: AudioBuffer = this.createFadeBuffer();
// Connect processing graph.
for (let i = 0; i < 2; i++){
this.fadeBufferSources.push(this.context.createBufferSource());
this.fadeBufferSources[i].buffer = fadeBuffer;
this.fadeBufferSources[i].loop = true;
this.mixGains.push(this.context.createGain());
this.mixGains[i].gain.value = 0;
this.fadeBufferSources[i].connect(this.mixGains[i].gain);
this.input.connect(this.delays[i]);
this.delays[i].connect(this.mixGains[i]);
this.mixGains[i].connect(this.output);
}
// Start
var t = this.context.currentTime + 0.050;
var t2 = t + PitchShifter.BUFFER_TIME - PitchShifter.FADE_TIME;
this.bufferSources[0].start(t);
this.bufferSources[1].start(t2);
this.bufferSources[2].start(t);
this.bufferSources[3].start(t2);
this.fadeBufferSources[0].start(t);
this.fadeBufferSources[1].start(t2);
this.setDelay(this.delayTime);
}
private setDelay(delayTime): void {
this.delayGains.forEach((delayGain: GainNode) => {
delayGain.gain.setTargetAtTime(0.5*delayTime, 0, 0.010);
});
}
public get PitchOffset(): number {
return this.previousPitch;
}
public set PitchOffset(multiplier: number) {
if (multiplier>0) { // pitch up
this.modGains.forEach((modGain: GainNode, i: number) => {
modGain.gain.value = i < 2 ? 0 : 1;
});
} else { // pitch down
this.modGains.forEach((modGain: GainNode, i: number) => {
modGain.gain.value = i < 2 ? 1 : 0;
});
}
this.setDelay(this.delayTime * Math.abs(multiplier));
this.previousPitch = multiplier;
}
private createFadeBuffer(): AudioBuffer {
const length1: number = PitchShifter.BUFFER_TIME * this.context.sampleRate;
const length2: number = (PitchShifter.BUFFER_TIME - 2 * PitchShifter.FADE_TIME) * this.context.sampleRate;
const length: number = length1 + length2;
var buffer: AudioBuffer = this.context.createBuffer(1, length, this.context.sampleRate);
var p: any = buffer.getChannelData(0);
var fadeLength = PitchShifter.FADE_TIME * this.context.sampleRate;
var fadeIndex1 = fadeLength;
var fadeIndex2 = length1 - fadeLength;
// 1st part of cycle
for (var i = 0; i < length1; ++i) {
var value;
if (i < fadeIndex1) {
value = Math.sqrt(i / fadeLength);
} else if (i >= fadeIndex2) {
value = Math.sqrt(1 - (i - fadeIndex2) / fadeLength);
} else {
value = 1;
}
p[i] = value;
}
// 2nd part
for (var i = length1; i < length; ++i) {
p[i] = 0;
}
return buffer;
}
private createDelayTimeBuffer(shiftUp): AudioBuffer {
const length1: number = PitchShifter.BUFFER_TIME * this.context.sampleRate;
const length2: number = (PitchShifter.BUFFER_TIME - 2 * PitchShifter.FADE_TIME) * this.context.sampleRate;
const length: number = length1 + length2;
const buffer: AudioBuffer = this.context.createBuffer(1, length, this.context.sampleRate);
let p: any = buffer.getChannelData(0);
// 1st part of cycle
for (let i = 0; i < length1; ++i) {
if (shiftUp)
// This line does shift-up transpose
p[i] = (length1-i)/length;
else
// This line does shift-down transpose
p[i] = i / length1;
}
// 2nd part
for (var i = length1; i < length; ++i) {
p[i] = 0;
}
return buffer;
}
}
export = PitchShifter;