-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
111 lines (103 loc) · 2.48 KB
/
test.ts
File metadata and controls
111 lines (103 loc) · 2.48 KB
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
declare class Chart {
constructor(ctx: any, options: any);
}
async function main() {
const audio_ctx = new AudioContext();
await audio_ctx.audioWorklet.addModule('librnnoise.worker.js');
const wasm = await WebAssembly.instantiateStreaming(fetch('librnnoise.wasm'), {});
console.log(wasm);
const constraints = {
audio: {
channelCount: {
min: 1, max: 1
},
sampleRate: {
min: 48000,
max: 48000,
},
noiseSuppression: false,
autoGainControl: true,
echoCancellation: false,
},
video: false,
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
const tracks = stream.getAudioTracks();
if (tracks.length == 0)
throw 'not found audio track';
const audio_track = tracks[0];
console.log(audio_track.id, audio_track.label);
console.log(audio_track.getConstraints());
console.log(audio_track.getSettings());
const src_node =
audio_ctx.createMediaStreamTrackSource ?
audio_ctx.createMediaStreamTrackSource(audio_track) :
audio_ctx.createMediaStreamSource(stream);
const worklet_node = new AudioWorkletNode(audio_ctx, 'rnnoise-processor');
src_node.connect(worklet_node);
worklet_node.port.postMessage(wasm.module);
audio_ctx.resume();
const zero_filled: number[] = [];
for (let i = 0; i < 1000; i++)
zero_filled.push(0);
const chart: any = new Chart(document.getElementById('chart'), {
type: 'line',
data: {
labels: zero_filled.map((_, i) => i),
datasets: [{
data: zero_filled,
}]
},
options: {
aspectRatio: 5,
tooltips: {
enabled: false,
},
hover: {
mode: null,
},
events: [],
animation: {
duration: 0,
},
responsiveAnimationDuration: 0,
elements: {
line: {
tension: 0,
}
},
scales: {
xAxes: [{
display: false,
}],
yAxes: [{
ticks: {
beginAtZero: true,
min: 0,
max: 1.0
}
}]
},
legend: {
display: false,
},
},
});
worklet_node.port.onmessage = (m) => {
const data = chart.data.datasets[0].data;
data.shift();
data.push(m.data);
};
const update = () => {
chart.update();
window.setTimeout(() => {
window.requestAnimationFrame(() => {
update();
});
}, 1);
};
update();
}
window.addEventListener('DOMContentLoaded', _ => {
main();
});