-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
223 lines (194 loc) · 6.86 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html><body>
<form id="form">
Play
<input id="code0" type="text" size="40" value="t & t>>8"> and optionally
<input id="code1" type="text" size="40" value=""><br>for
<input id="duration" type="text" size="2" value="30"> seconds at
<input name="samplerate" type="radio" value="8000" checked>8 kHz
<input name="samplerate" type="radio" value="11025">11 kHz
<input name="samplerate" type="radio" value="22050">22 kHz
<input name="samplerate" type="radio" value="32000">32 kHz
<input name="samplerate" type="radio" value="44100">44 kHz.
The expressions are in
<input name="notation" type="radio" value="JavaScript" checked>JavaScript
<input name="notation" type="radio" value="RPN">RPN.
<br>Call this tune
<input id="title" type="text" size="40">.
<br>
<input id="play" type="submit" value="Hit it."
style="width: 938px; font-size: 120%">
</form>
<audio id="player" autoplay="true" controls></audio>
<p>
<canvas id="viz" height="256" width="938"></canvas>
<p id="error" style="color: red">
<p>Once you've made a new tune, you can save or link to it: <a id="permalink">Untitled</a>.
<span id="elsewhere"></span>
<span id="source"></span>
<hr>
Try some <a href="examples.html">examples</a>.
In RPN mode, <a
href="http://madgarden.net/apps/glitch-machine/">Glitch Machine</a>
expressions should work. For example, <a
href="http://wry.me/toys/bytebeat/?code0=t%2010%20%3E%3E%2042%20*%20t%20*&code1=&duration=30&samplerate=8000¬ation=RPN&title="><code>t
10 >> 42 * t * </code></a>.
<p>See <a href="http://canonical.org/~kragen/bytebeat/">Kragen Sitaker's
overview of bytebeat</a> for an idea of what to do with this web toy
by <a href="http://wry.me/blog">Darius Bacon</a>.
<a href="https://github.com/darius/bytebeat">Source code</a> on GitHub.
Based on <a href="http://www.bemmu.com/music/index.html">Bemmu and
viznut's</a>
and <a href="http://wurstcaptures.untergrund.net/music/">rarefluid's</a>.
<script src="bytebeat.js"></script>
<script src="rpn.js"></script>
<script>
//'use strict';
//'
// Utilities for URLs & DOM, etc.
function dbg(msg) {
if (window.console)
console.log(msg);
}
function byId(id) {
return document.getElementById(id);
}
function decodeParams(url) {
var result = {};
var qmark = url.indexOf('?');
if (0 <= qmark) {
var pairs = url.substring(qmark+1, url.length).split('&');
for (var i = 0; i < pairs.length; ++i) {
var kv = pairs[i].split('=', 2);
result[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
}
}
return result;
}
function replaceParams(url, params) {
var encoded = [];
for (var k in params)
if (params.hasOwnProperty(k))
encoded.push(encodeURIComponent(k)
+ '=' + encodeURIComponent(params[k]));
var qmark = url.indexOf('?');
var base = qmark < 0 ? url : url.substring(0, qmark);
return base + "?" + encoded.join('&');
}
function getCheckedValue(buttons) {
for (var i = 0; i < buttons.length; ++i)
if (buttons[i].checked)
return buttons[i].value;
}
function setCheckedValue(buttons, value) {
for (var i = 0; i < buttons.length; ++i)
if (buttons[i].value == value)
buttons[i].checked = true;
}
// Actual page logic
var code0 = byId('code0');
var code1 = byId('code1');
var duration = byId('duration');
var samplerate = byId('form').samplerate;
var notation = byId('form').notation;
var title = byId('title');
var player = byId('player');
var viz = byId('viz');
var error = byId('error');
var permalink = byId('permalink');
var elsewhere = byId('elsewhere');
var source = byId('source');
function getFormValues() {
return {
code0: code0.value,
code1: code1.value,
duration: duration.value,
samplerate: getCheckedValue(samplerate),
notation: getCheckedValue(notation),
title: title.value,
};
}
var defaults = getFormValues();
var params = decodeParams(document.URL);
if (params.code0) code0.value = params.code0;
if (params.code1) code1.value = params.code1;
if (params.duration) duration.value = params.duration;
if (params.samplerate) setCheckedValue(samplerate, params.samplerate);
if (params.notation) setCheckedValue(notation, params.notation);
if (params.title) title.value = params.title;
if (params.source) {
source.innerHTML =
'This tune is said to be from <a id="sourcehref" rel="nofollow">here</a>.';
// Setting the href separately to avoid XSS issues (I hope) --
// presumably there's a better way:
byId('sourcehref').href = params.source;
}
function updatePermalink() {
permalink.href = makePermalink();
permalink.replaceChild(document.createTextNode(getTitle()),
permalink.childNodes[0]);
elsewhere.innerHTML = (
getCheckedValue(notation) === 'JavaScript'
? 'Or play it at <a href="'
+ replaceParams("http://wurstcaptures.untergrund.net/music/",
{oneliner: code0.value,
oneliner2: code1.value,
duration: duration.value,
rate: getCheckedValue(samplerate)})
+ '">wurstcaptures</a>.'
: '');
}
function makePermalink() {
return replaceParams(document.URL, keepChanges(defaults, getFormValues()));
}
// Return an object with those properties of obj that aren't the same
// in ref.
function keepChanges(ref, obj) {
var result = {};
for (var k in obj)
if (obj.hasOwnProperty(k))
if (ref[k] !== obj[k])
result[k] = obj[k];
return result;
}
function getTitle() {
return trim(title.value) || 'Untitled';
}
function trim(string) {
return string.replace(/^\s+|\s+$/g, '');
}
function updateTitle() {
document.title = 'bytebeat.js | ' + getTitle();
}
if (typeof(player.play) === 'undefined')
alert("Your browser doesn't seem to support HTML5 audio. Sorry.");
function play() {
error.innerHTML = '';
try {
function asJS(field) {
var text = nonempty(field) ? field.value : '0';
return getCheckedValue(notation) === 'RPN' ? jsFromRPN(text) : text;
}
var composers = [compileComposer(asJS(code0))];
if (nonempty(code1)) composers.push(compileComposer(asJS(code1)));
var seconds = parseFloat(duration.value);
if (seconds === 0) seconds = 15;
var rate = parseInt(getCheckedValue(samplerate));
showAudioVisual(makeSound(composers, seconds, rate, 1),
player, viz);
updatePermalink();
updateTitle();
} catch (err) {
error.innerHTML = '' + err;
}
return false;
}
function nonempty(field) {
return field.value.match(/\S/);
}
code0.focus();
byId('play').onclick = play;
updatePermalink();
updateTitle();
</script>
</body></html>