Skip to content

Commit e83ef7d

Browse files
Add audio playback functionality
1 parent 621f764 commit e83ef7d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<textarea id="textarea-plain" rows="5" cols="33">Digitale Initiativen</textarea><br />
99
<label for="story">Decode</label><br />
1010
<textarea id="textarea-encoded" rows="5" cols="33">Digitale Initiativen</textarea><br />
11+
<br />
12+
<button onclick="playAudio()">Play</button>
13+
<button onclick="stopAudio()">Stop</button>
1114
<script>
1215
let decode, encode;
1316

@@ -41,6 +44,40 @@
4144
});
4245

4346
init();
47+
48+
const timeouts = [];
49+
const soundMapping = {
50+
" ": undefined,
51+
b: new Audio("./sounds/b-1.m4a"),
52+
z: new Audio("./sounds/z-1.m4a"),
53+
};
54+
const timeMapping = {
55+
" ": 400,
56+
b: 100,
57+
z: 150,
58+
};
59+
60+
const playAudio = () => {
61+
const encoded = encodedTextarea.value;
62+
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+
}
73+
}
74+
};
75+
76+
const stopAudio = () => {
77+
for (const timeoutId of timeouts) {
78+
clearTimeout(timeoutId);
79+
}
80+
};
4481
</script>
4582
<script src="./huffman-coding.js"></script>
4683
</body>

0 commit comments

Comments
 (0)