-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (64 loc) · 1.94 KB
/
app.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
/**------------------------------------------------------------------------------
*
* @file app.js
* @brief Web Audio Tuner
*
*----------------------------------------------------------------------------*/
// App
let app = new Vue({
el: '#app',
data: {
state: {
started: false,
},
audio: {
context: null,
stream: null,
source: null,
worklet: null,
processor: null,
},
ui: {
frequency: 0,
cents: 0,
note: 'CLICK',
},
},
methods: {
start: async function () {
if (!this.state.started) {
try {
const workletUrl = new URL('worklet.js', import.meta.url)
this.audio.context = new (window.AudioContext || window.webkitAudioContext)();
this.audio.stream = await navigator.mediaDevices.getUserMedia({ audio: true });
this.audio.source = this.audio.context.createMediaStreamSource(this.audio.stream);
this.audio.worklet = await this.audio.context.audioWorklet.addModule(workletUrl)
this.audio.processor = new AudioWorkletNode(this.audio.context, 'tuner-audio-processor', {
processorOptions: {
sampleRate: this.audio.context.sampleRate
}
})
this.audio.processor.port.onmessage = e => {
Vue.set(app.ui, 'frequency', e.data.frequency);
Vue.set(app.ui, 'note', e.data.note);
Vue.set(app.ui, 'cents', e.data.cents);
}
this.audio.source.connect(this.audio.processor).connect(this.audio.context.destination);
this.state.started = true;
this.ui.note = '-';
} catch (e) {
window.alert(e);
}
} else {
this.audio.stream.getAudioTracks().forEach(t => t.stop());
this.audio.context.close();
this.state.started = false;
}
},
},
computed: {
scale: function () {
return `scale(${1 - this.ui.cents / 100})`;
},
},
});