forked from cwilso/WebAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·174 lines (142 loc) · 4.8 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Audio Playground</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="shortcut icon" href="Speaker.ico">
<!-- Visual design for nodes in the page -->
<link rel="stylesheet" href="nodes.css" type="text/css">
<!-- Script functions for dragging nodes around the page and connecting -->
<script type="text/javascript" src="dragging.js"></script>
<script type="text/javascript">
var audioContext = null;
var tempx=100, tempy=100;
function createNewNode( nodeType, input, output ) {
var e=document.createElement("div");
e.className="node " + nodeType;
e.style.left="" + tempx + "px";
if (tempx > 500)
tempx = 100;
else
tempx += 50;
e.style.top="" + tempy + "px";
if (tempy > 1000)
tempy = 100;
else
tempy += 50;
e.setAttribute("audioNodeType", nodeType );
e.onmousedown=startDraggingNode;
e.appendChild(document.createTextNode(nodeType));
if (input) {
var i=document.createElement("div");
i.className="inputconnector";
i.onmousedown=startDraggingConnector;
e.appendChild(i);
}
if (output) {
var i=document.createElement("div");
i.className="outputconnector";
i.onmousedown=startDraggingConnector;
e.appendChild(i);
}
// add the node into the soundfield
document.getElementById("soundField").appendChild(e);
return(e);
}
function hitplay(e) {
e = e.target.parentNode;
if (e.bufferSource)
e.bufferSource.noteOff(0);
e.bufferSource = audioContext.createBufferSource();
e.bufferSource.loop = e.getElementsByTagName("input")[0].checked;
e.bufferSource.connect( audioContext.destination );
e.bufferSource.buffer = e.buffer;
e.bufferSource.noteOn(0);
}
function hitstop(e) {
e.target.parentNode.bufferSource.noteOff(0);
}
function flipLoop(e) {
e.target.checked = !e.target.checked;
if (e.target.parentNode.bufferSource)
e.target.parentNode.bufferSource.loop = e.target.checked;
}
function createAudioNodeFromBuffer( buffer ) {
var e=createNewNode("audioBufferSource", false, true );
e.buffer=buffer;
var ctl=document.createElement("button");
ctl.appendChild(document.createTextNode("play"));
ctl.onclick = hitplay;
e.appendChild(ctl);
ctl=document.createElement("button");
ctl.appendChild(document.createTextNode("stop"));
ctl.onclick = hitstop;
e.appendChild(ctl);
ctl=document.createElement("input");
ctl.type="checkbox";
ctl.name="loop";
ctl.value="loop";
ctl.addEventListener( "onclick", flipLoop );
e.appendChild(ctl);
e.appendChild(document.createTextNode("loop"));
}
function downloadAudioFromURL( url ){
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
// Decode asynchronously
request.onload = function() {
audioContext.decodeAudioData( request.response, function(buffer) {
createAudioNodeFromBuffer(buffer);
}, function(){alert("error loading!");});
}
request.send();
}
// Set up the page as a drop site for audio files. When an audio file is
// dropped on the page, it will be auto-loaded as an AudioBufferSourceNode.
function initDragDropOfAudioFiles() {
window.ondragover = function () { this.className = 'hover'; return false; };
window.ondragend = function () { this.className = ''; return false; };
window.ondrop = function (e) {
this.className = '';
e.preventDefault();
var reader = new FileReader();
reader.onload = function (event) {
audioContext.decodeAudioData( event.target.result, function(buffer) {
createAudioNodeFromBuffer(buffer);
}, function(){alert("error loading!");} );
};
reader.onerror = function (event) {
alert("Error: " + reader.error );
};
reader.readAsArrayBuffer(e.dataTransfer.files[0]);
return false;
};
}
// Initialization function for the page.
function init() {
try {
audioContext = new webkitAudioContext();
}
catch(e) {
alert('Web Audio API is not supported in this browser');
}
initDragDropOfAudioFiles(); // set up page as a drop site for audio files
// create the one-and-only destination node for the context
var dest = createNewNode("destination", true, false );
dest.style.backgroundImage = "url(Speaker_Icon_gray.svg)";
dest.style.backgroundSize = "contain";
}
window.addEventListener('load', init, false);
</script>
<style>
</style>
</head>
<body>
<div id="soundField">
<div>Drop a sound file onto the page in order to create an AudioBufferSource with that file</div>
<button onclick="downloadAudioFromURL('440Hz.mp3'); ">Sine Wave AudioBufferSource</button>
<button onclick="var uri=prompt('Enter a URL to a sound file'); if (uri) downloadAudioFromURL(uri); ">AudioBufferSource from URL</button>
</div>
</body>
</html>