-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (173 loc) · 4.32 KB
/
index.js
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
let decoder;
const examples =
{
'Without formant preservation':
[
'-q 0 -p 0.5',
'-q 0 -p 0.75',
'-q 0 -p 1.5',
'-q 0 -p 2',
'-q 0 -p 1,1.25,1.5'
],
'With formant preservation':
[
'-q 1 -p 0.5',
'-q 1 -p 0.75',
'-q 1 -p 1.5',
'-q 1 -p 2',
'-q 1 -p 1,1.25,1.5'
],
'Timbre changing':
[
'-q 1 -p 1 -t 0.8',
'-q 1 -p 1 -t 1.2',
'-q 1 -p 1,1.25,1.5 -t 0.8',
'-q 1 -p 1,1.25,1.5 -t 1.2'
]
};
async function download(url)
{
const response = await fetch(url);
const data = response.arrayBuffer();
return data;
}
function decode(wav)
{
decoder = decoder || new AudioContext();
const buffer = decoder.decodeAudioData(wav);
return buffer;
}
function encode(buffer)
{
const samplerate = buffer.sampleRate;
const channels = buffer.numberOfChannels;
const data = Array.from(new Array(channels), (i) => buffer.getChannelData(i));
const encoder = new WavAudioEncoder(samplerate, channels);
encoder.encode(data);
const wav = encoder.finish();
return wav;
}
function prebuild()
{
const tasks = [];
const placeholder = document.getElementById('placeholder');
const container = document.getElementById('container');
placeholder.innerHTML = [
`<div id="example" class="card shadow-sm example trans">`,
` <div class="card-body">`,
` <div class="audio">`,
` <audio id="input" src="" type="audio/wav"></audio>`,
` </div>`,
` </div>`,
` <div class="card-footer">`,
` <p class="card-text text-start text-muted"><samp>original record</samp></p>`,
` </div>`,
`</div>`
].join('');
Object.keys(examples).forEach((category, i) =>
{
const title = document.createElement('h2');
title.classList.add('mb-4');
if (i > 0) {
title.classList.add('mt-5');
}
title.innerText = category;
container.appendChild(title);
const cards = document.createElement('div');
cards.classList.add('row', 'row-cols-1', 'row-cols-sm-1', 'row-cols-md-2', 'row-cols-lg-2', 'row-cols-xl-3', 'g-3');
examples[category].forEach((example, j) =>
{
const card = document.createElement('div');
card.classList.add('col');
card.innerHTML = [
`<div id="example-${i}-${j}" class="card shadow-sm example trans">`,
` <div class="card-body">`,
` <div class="audio">`,
` <audio id="output-${i}-${j}" src="" type="audio/wav"></audio>`,
` </div>`,
` </div>`,
` <div class="card-footer">`,
` <p class="card-text text-start text-muted"><samp>${example}</samp></p>`,
` </div>`,
`</div>`
].join('');
cards.appendChild(card);
tasks.push({
example: example,
i: i,
j: j
});
});
container.appendChild(cards);
});
return tasks;
}
function postbuild(input, tasks)
{
console.debug('Processing', input);
const wav = encode(input);
const parent = document.getElementById('example');
const child = document.getElementById('input');
if (child.src) {
URL.revokeObjectURL(child.src);
}
requestAnimationFrame(() => {
child.src = URL.createObjectURL(wav);
parent.classList.remove('trans');
});
function process(task)
{
const example = task.example;
const i = task.i;
const j = task.j;
console.debug(`Building example "${example}"`);
const output = shiftpitch(input, example);
const wav = encode(output);
const parent = document.getElementById(`example-${i}-${j}`);
const child = document.getElementById(`output-${i}-${j}`);
if (child.src) {
URL.revokeObjectURL(child.src);
}
requestAnimationFrame(() => {
child.src = URL.createObjectURL(wav);
parent.classList.remove('trans');
});
}
function schedule()
{
let task = tasks.shift();
if(!task) {
return;
}
if ('requestIdleCallback' in window)
{
requestIdleCallback(() =>
{
process(task);
if(tasks.length) {
schedule();
}
});
}
else
{
setTimeout(() =>
{
process(task);
if(tasks.length) {
schedule();
}
},
100);
}
}
schedule();
}
function build()
{
const tasks = prebuild();
download('voice.wav').then(decode).then((input) =>
{
postbuild(input, tasks);
});
}