File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 8
8
< textarea id ="textarea-plain " rows ="5 " cols ="33 "> Digitale Initiativen</ textarea > < br />
9
9
< label for ="story "> Decode</ label > < br />
10
10
< 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 >
11
14
< script >
12
15
let decode , encode ;
13
16
41
44
} ) ;
42
45
43
46
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
+ } ;
44
81
</ script >
45
82
< script src ="./huffman-coding.js "> </ script >
46
83
</ body >
You can’t perform that action at this time.
0 commit comments