-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
139 lines (116 loc) · 3.97 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Javascript Oscillator Control</title>
</head>
<body>
<div class="wrapper">
<div class="input container"></div>
<input type="text" size="4" id="freq" value="440" /><label for="hz"
>Hz</label
>
</div>
<div class="play button">
<button id="playButton" class="playbutton" onclick="start()">play</button>
</div>
<div class="stop button">
<button class="playbutton" onclick="stop()">stop</button>
</div>
<div class="volum">
<span>volume</span>
<input
type="range"
min="0.0"
max="1.0"
step="0.01"
name="volume"
id="volumeControl"
/>
</div>
</div>
<script type="text/javascript">
let context = new AudioContext() //create audio container
//Create OscillatorNode
let oscillator = context.createOscillator() //Create sound source
oscillator.type = "sine"
oscillator.frequency.setValueAtTime(440, audioCtx.currenTime) // Value in hertz
oscillator.connect(contex.destination) //connect sound source to output
let gainNode = context.createGain() //create gain node
gainNode.connect(context.destination) //connect sound to gain node
gainNode.gain.value = 0.1 //Set gain to 10 percent volume
oscillator.start() //play sound source
let finish = context.createMediaStreamDestination()
//Declare global variable
let context = null
let playButton = null
let volumeControl = null
let oscNode1 = null
let oscNode2 = null
let oscNode3 = null
let constantNode = null
let gainNode1 = null
let gainNode2 = null
let gainNode3 = null
let playing = false
function setup() {
context = new (window.AudioContext || window.webkitAudioContext)()
playButton = document.querySelector("#playButton")
volumeControl = document.querySelector("#volumeControl")
playButton.addEventListener("click", togglePlay, false)
volumeControl.addEventListener("input", changeVolume, false)
gainNode1 = context.createGain()
gainNode1.gain.value = 0.5
gainNode2 = context.createGain()
gainNode3 = context.createGain()
gainNode2.gain.value = gainNode1.gain.value
gainNode3.gain.value = gainNode1.gain.value
volumeControl.value = gainNode1.gain.value
constantNode = context.createConstantSource()
constantNode.connect(gainNode2.gain)
constantNode.connect(gainNode3.gain)
constantNode.start()
gainNode1.connect(context.destination)
gainNode2.connect(context.destination)
gainNode3.connect(context.destination)
}
window.addEventListener("load", setup, false)
function togglePlay(event) {
if (playing) {
playButton.innerHTML = "▶️"
stopOscillators()
} else {
playButton.innerHTML = "⏸"
startOscillators()
}
}
function changeVolume(event) {
constantNode.offset.value = volumeControl.value
}
// Starting the oscillators
function startOscillators() {
oscNode1 = context.createOscillator()
oscNode1.type = "sine"
oscNode1.frequency.value = 261.625565300598634 // middle C
oscNode1.connect(gainNode1)
oscNode2 = context.createOscillator()
oscNode2.type = "sine"
oscNode2.frequency.value = 329.627556912869929 // E
oscNode2.connect(gainNode2)
oscNode3 = context.createOscillator()
oscNode3.type = "sine"
oscNode3.frequency.value = 391.995435981749294 // G
oscNode3.connect(gainNode3)
oscNode1.start()
oscNode2.start()
oscNode3.start()
playing = true
}
function stopOscillators() {
oscNode1.stop()
oscNode2.stop()
oscNode3.stop()
playing = false
}
</script>
</body>
</html>