-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_api_controls.js
76 lines (57 loc) · 2.06 KB
/
audio_api_controls.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
const audioContext = new ( window.AudioContext || window.webkitAudioContext )();
// Getting html element.
const apiFormControl = document.querySelector('#api-form-controls');
const startWebAudio = document.querySelector('#start-beat-web-audio');
const frequencyTextFeild = document.querySelector('.range-frequency .range-feild-text');
const volumeTextFeild = document.querySelector('.range-volume .range-feild-text');
class AudioApiControls{
constructor( context ) {
this.context = context;
frequencyTextFeild.textContent = `Frequency: ${apiFormControl.frequency.value}`;
volumeTextFeild.textContent = `Volume: ${apiFormControl.volume.value * 100}`
}
playSound = () => {
if( startWebAudio.textContent === 'Start' ) {
// Disabling the button.
start.disabled = true;
startWeb.disabled = true;
startWebAudio.textContent = 'Stop';
this.oscillator = this.context.createOscillator();
this.gainNode = this.context.createGain();
console.log(this.gainNode);
this.oscillator.connect( this.gainNode );
this.gainNode.connect( this.context.destination );
this.oscillator.start( this.context.currentTime );
}
else {
startWebAudio.textContent = 'Start';
this.oscillator.stop();
start.disabled = false;
startWeb.disabled = false;
}
}
frequency = ( e ) => {
let val = e.target.value;
frequencyTextFeild.textContent = `Frequency: ${val}`;
if( this.oscillator ) {
this.oscillator.frequency.value = val;
}
else {
console.log('Please start the web audio with start button');
}
}
volume = ( e ) => {
let val = e.target.value;
volumeTextFeild.textContent = `Volume: ${Math.floor(val * 100)}`;
if( this.oscillator ) {
this.gainNode.gain.value = val;
}
else{
console.log('Please start the web audio with start button');
}
}
}
const audioapicontrols = new AudioApiControls( audioContext );
apiFormControl.frequency.addEventListener( 'input', audioapicontrols.frequency );
apiFormControl.volume.addEventListener( 'input', audioapicontrols.volume );
startWebAudio.addEventListener( 'click', audioapicontrols.playSound );