-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
240 lines (208 loc) · 9.61 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Nokia Composer (RTTTL Player)</title>
<link rel="stylesheet" href="https://fonts.xz.style/serve/inter.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@exampledev/new.css@1.1.2/new.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/themes/prism-okaidia.min.css" />
<link rel="stylesheet" href="https://fonts.cdnfonts.com/css/nokia-cellphone-fc" />
<style>
body { max-width: 900px; }
pre { font-size: .9em !important }
#js-composer select { float: right; }
#js-composer textarea {
width: 100%;
height: 100px;
line-height: 1.5em;
font-family: "Nokia Cellphone FC", sans-serif;
}
</style>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/react@16/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.22.0/prism.min.js" data-manual></script>
<script src="https://cdn.jsdelivr.net/gh/mohayonao/web-audio-api-shim@master/build/web-audio-api-shim-light.min.js"></script>
</head>
<body>
<header>
<h1>Nokia Composer</h1>
</header>
<p>
The accompanying <a href="https://eddmann.com/posts/building-a-nokia-composer-rtttl-player-and-wav-file-generator-in-the-browser/">article</a> which highlights building this application can be found on my website.
</p>
<div id="js-composer"></div>
<details>
<summary>View RTTTL parser source</summary>
<pre id="js-view-parser-source" class="language-js"></pre>
</details>
<details>
<summary>View WAV generation source</summary>
<pre id="js-view-wav-generation-source" class="language-js"></pre>
</details>
<details>
<summary>View player source</summary>
<pre id="js-view-player-source" class="language-js"></pre>
</details>
<script>
window.MELODIES = [
"Super Mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6",
"A-Team:d=8,o=5,b=125:4d#6,a#,2d#6,16p,g#,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p,c#.6,16c6,16a#,g#.,2a#",
"Mission Impossible:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d",
"The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6",
"Indiana Jones:d=4,o=5,b=250:e,8p,8f,8g,8p,1c6,8p.,d,8p,8e,1f,p.,g,8p,8a,8b,8p,1f6,p,a,8p,8b,2c6,2d6,2e6,e,8p,8f,8g,8p,1c6,p,d6,8p,8e6,1f.6,g,8p,8g,e.6,8p,d6,8p,8g,e.6,8p,d6,8p,8g,f.6,8p,e6,8p,8d6,2c6",
"James Bond:d=4,o=5,b=320:c,8d,8d,d,2d,c,c,c,c,8d#,8d#,2d#,d,d,d,c,8d,8d,d,2d,c,c,c,c,8d#,8d#,d#,2d#,d,c#,c,c6,1b.,g,f,1g.",
"Star Wars:d=4,o=5,b=45:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6",
"Flinstones:d=4,o=5,b=40:32p,16f6,16a#,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,d6,16f6,16a#.,16a#6,32g6,16f6,16a#.,32f6,32f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,a#,16a6,16d.6,16a#6,32a6,32a6,32g6,32f#6,32a6,8g6,16g6,16c.6,32a6,32a6,32g6,32g6,32f6,32e6,32g6,8f6,16f6,16a#.,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#6,16c7,8a#.6",
];
</script>
<script type="text/babel" id="js-parser-source">
const toDefaults = unparsedDefaults =>
unparsedDefaults.split(",").reduce(
(defaults, option) => {
const [key, value] = option.split("=");
switch (key) {
case "d":
return { ...defaults, duration: value };
case "o":
return { ...defaults, octave: value };
case "b":
return { ...defaults, beat: value };
default:
return defaults;
}
},
{ duration: 4, octave: 6, beat: 63 }
);
const toMelody = (melody, defaults) => {
const notes = ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"];
const middleC = 261.63;
return melody.split(",").map(unparsedNote => {
const { groups: parsed } = unparsedNote.match(
/(?<duration>1|2|4|8|16|32|64)?(?<note>(?:[a-g]|p)#?){1}(?<dot>\.?)(?<octave>4|5|6|7)?/
);
const { duration, note, dot, octave, beat } = Object.keys(parsed).reduce(
(xs, x) => (parsed[x] ? { ...xs, [x]: parsed[x] } : xs),
defaults
);
return {
duration: (240 / beat / duration) * (dot ? 1.5 : 1),
frequency:
note === "p" ? 0 : middleC * 2 ** (octave - 4 + notes.indexOf(note) / 12),
};
});
};
const parse = rtttl => {
const [_, unparsedDefaults, unparsedMelody] = rtttl.split(":", 3);
return toMelody(unparsedMelody, toDefaults(unparsedDefaults));
};
</script>
<script type="text/babel" id="js-wav-generation-source">
const generateAudioBuffer = async rtttl => {
const melody = parse(rtttl);
const totalDuration = melody.reduce((total, { duration }) => total + duration, 0);
const audio = new OfflineAudioContext(1, totalDuration * 44100, 44100);
const oscillator = audio.createOscillator();
oscillator.type = "square";
oscillator.connect(audio.destination);
let time = 0;
oscillator.start();
melody.forEach(({ duration, frequency }) => {
oscillator.frequency.setValueAtTime(frequency, time);
time += duration;
});
oscillator.stop(time);
return audio.startRendering();
};
const generateWavBlob = audioBuffer => {
const writeString = (view, offset, string) => {
string.split("").forEach((character, index) => {
view.setUint8(offset + index, character.charCodeAt());
});
};
const samples = audioBuffer.getChannelData(0);
const bytesPerSample = 4;
const buffer = new ArrayBuffer(44 + samples.length * bytesPerSample);
const view = new DataView(buffer);
writeString(view, 0, "RIFF"); // ChunkID
view.setUint32(4, 36 + samples.length * bytesPerSample, true); // ChunkSize
writeString(view, 8, "WAVE"); // Format
writeString(view, 12, "fmt "); // Subchunk1ID
view.setUint32(16, 16, true); // Subchunk1Size
view.setUint16(20, 3, true); // AudioFormat (IEEE float)
view.setUint16(22, 1, true); // NumChannels
view.setUint32(24, audioBuffer.sampleRate, true); // SampleRate
view.setUint32(28, audioBuffer.sampleRate * bytesPerSample, true); // ByteRate
view.setUint16(32, bytesPerSample, true); // BlockAlign
view.setUint16(34, 32, true); // BitsPerSample
writeString(view, 36, "data"); // Subchunk2ID
view.setUint32(40, samples.length * bytesPerSample, true); // Subchunk2Size
samples.forEach((sample, index) => {
view.setFloat32(44 + index * bytesPerSample, sample, true); // Data
});
return new Blob([view], { type: "audio/wav" });
};
</script>
<script type="text/babel" id="js-player-source">
function NokiaComposer({ melodies }) {
const [isPlaying, setPlaying] = React.useState(false);
const [composition, setComposition] = React.useState(melodies[0] || "");
React.useEffect(() => {
if (!isPlaying) return;
const audio = new AudioContext();
const oscillator = audio.createOscillator();
oscillator.type = "square";
oscillator.connect(audio.destination);
oscillator.onended = () => setPlaying(false);
let time = 0;
oscillator.start();
parse(composition).forEach(({ duration, frequency }) => {
oscillator.frequency.setValueAtTime(frequency, time);
time += duration;
});
oscillator.stop(time);
return async () => {
await audio.close();
};
}, [isPlaying]);
const handleDownload = async e => {
const blob = generateWavBlob(await generateAudioBuffer(composition));
const autoDownloadLink = document.createElement("a");
autoDownloadLink.href = window.URL.createObjectURL(blob);
autoDownloadLink.download = composition.split(":")[0];
autoDownloadLink.click();
};
return (
<div>
<button onClick={e => setPlaying(!isPlaying)} disabled={!composition}>
{isPlaying ? "Stop" : "Play"}
</button>{" "}
<button onClick={handleDownload} disabled={!composition}>
Download
</button>
<select onChange={e => setComposition(e.target.value)}>
{melodies.map(melody => (
<option value={melody}>{melody.split(":")[0]}</option>
))}
</select>
<textarea
value={composition}
disabled={isPlaying}
onChange={e => setComposition(e.target.value)}
></textarea>
</div>
);
}
ReactDOM.render(
<NokiaComposer melodies={window.MELODIES} />,
document.getElementById("js-composer")
);
</script>
<script type="text/babel">
["parser", "wav-generation", "player"].forEach(name => {
document.getElementById(`js-view-${name}-source`).innerHTML = Prism.highlight(document.getElementById(`js-${name}-source`).innerHTML.trim(), Prism.languages.javascript, 'javascript');
});
</script>
</body>
</html>