Skip to content

Commit 83e53c0

Browse files
Add download audio
1 parent dbdf9ef commit 83e53c0

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

index.html

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
<label for="story">Decode</label><br />
1010
<textarea id="textarea-encoded" rows="5" cols="33">Digitale Initiativen</textarea><br />
1111
<br />
12-
<button onclick="playAudio()">Play</button>
13-
<button onclick="stopAudio()">Stop</button>
14-
<script>
12+
<button id="button-play">Play</button>
13+
<button id="button-stop">Stop</button>
14+
<button id="button-download">Download</button>
15+
<script type="module">
16+
import Crunker from "https://unpkg.com/crunker@latest/dist/crunker.esm.js";
17+
1518
let decode, encode;
1619

1720
const getJsonFromUrl = async (url) => {
@@ -40,36 +43,38 @@
4043
});
4144

4245
addChangeListenerToTextarea(encodedTextarea, () => {
43-
plainTextarea.value = decode(encodedTextarea.value);
46+
plainTextarea.value = decode(encodedTextarea.value.toLowerCase());
4447
});
4548

4649
init();
4750

4851
const timeouts = [];
49-
const soundMapping = {
50-
" ": undefined,
51-
b: () => new Audio("./sounds/b-1.m4a"),
52-
z: () => new Audio("./sounds/z-1.m4a"),
52+
const soundPathMapping = {
53+
" ": "./sounds/space-1.m4a",
54+
b: "./sounds/b-1.m4a",
55+
z: "./sounds/z-1.m4a",
5356
};
5457
const timeMapping = {
5558
" ": 500,
5659
b: 200,
5760
z: 250,
5861
};
5962

63+
const getEncodedSymbolArray = () => {
64+
return encodedTextarea.value
65+
.toLowerCase()
66+
.split("")
67+
.filter((letter) => [" ", "b", "z"].includes(letter));
68+
};
69+
6070
const playAudio = () => {
61-
const encoded = encodedTextarea.value;
6271
let timeOffset = 0;
63-
for (const letter of encoded.split("")) {
64-
if ([" ", "b", "z"].includes(letter)) {
65-
const timeoutId = setTimeout(() => {
66-
if (soundMapping[letter]) {
67-
soundMapping[letter]().play();
68-
}
69-
}, timeOffset);
70-
timeouts.push(timeoutId);
71-
timeOffset += timeMapping[letter];
72-
}
72+
for (const letter of getEncodedSymbolArray()) {
73+
const timeoutId = setTimeout(() => {
74+
new Audio(soundPathMapping[letter]).play();
75+
}, timeOffset);
76+
timeouts.push(timeoutId);
77+
timeOffset += timeMapping[letter];
7378
}
7479
};
7580

@@ -78,6 +83,23 @@
7883
clearTimeout(timeoutId);
7984
}
8085
};
86+
const downloadAudio = async () => {
87+
let crunker = new Crunker();
88+
89+
const symbols = getEncodedSymbolArray();
90+
crunker
91+
.fetchAudio(...symbols.map((symbol) => soundPathMapping[symbol]))
92+
.then((buffers) => crunker.concatAudio(buffers))
93+
.then((merged) => crunker.export(merged, "audio/mp3"))
94+
.then((output) => crunker.download(output.blob, "bz"))
95+
.catch((error) => {
96+
throw new Error(error);
97+
});
98+
};
99+
100+
document.getElementById("button-play").addEventListener("click", playAudio);
101+
document.getElementById("button-stop").addEventListener("click", stopAudio);
102+
document.getElementById("button-download").addEventListener("click", downloadAudio);
81103
</script>
82104
<script src="./huffman-coding.js"></script>
83105
</body>

sounds/space-1.m4a

5.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)