-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (123 loc) · 4.82 KB
/
script.js
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
import * as howler from '/howler.js';
const key = document.querySelector(".Key");
const replayKey = document.querySelector(".Replay");
const morseText = document.querySelector(".MorseText");
const textDiv = document.querySelector(".TextDiv");
const buttonTranslate = document.querySelector(".ButtonTranslate");
const buttonPlayTranslation = document.querySelector(".PlayTranslation");
const inputTranslateField = document.querySelector(".InputField");
const translationText = document.querySelector(".Translation");
const array = [];
const arrayDelays = [];
let timeStart;
let timeEnd;
let betweenTimeStart;
let betweenTimeEnd;
const keySound = new Howl({
src: ["/audio/440.wav"],
loop: true,
});
// Timing and logging the time between clicks and the length of each click.
key.addEventListener("mousedown", function () {
replayKey.style.opacity = "100%";
replayKey.style.transform = "translateY(-10px)";
//Stores the length between clicks.
betweenTimeEnd = new Date;
arrayDelays.push(betweenTimeEnd - betweenTimeStart);
// Starts timing the length of a click.
timeStart = new Date();
keySound.play();
});
// Timing and logging the time between clicks and the length of each click.
key.addEventListener("mouseup", function () {
keySound.stop();
// Stores a dot/dash with a timestamp of the length.
timeEnd = new Date();
if (timeEnd - timeStart > 130) array.push(`-${timeEnd - timeStart}`)
else array.push(`.${timeEnd - timeStart}`);
// Starts timing of length between clicks.
betweenTimeStart = new Date();
});
// Stop sounds in case mouse leaves the button while clicked.
key.addEventListener("mouseleave", function () {
keySound.stop();
});
// A promise used as a timer.
const timer = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
// Replays whatever was pressed on the button.
const replay = async function () {
console.log(...array);
console.log(...arrayDelays);
// Displays dots/dashes of replay.
morseText.textContent = morseCode();
textDiv.style.opacity = "100%";
// Plays from an array that stored dots/dashes and their respective duration.
for (let i = 0; i < array.length; i++) {
keySound.play();
await timer(array[i].slice(1));
keySound.stop();
await timer(arrayDelays[i + 1]);
};
};
replayKey.addEventListener("click", replay);
// Creates a dot/dash string of the stored clicks.
let morseCodeString = [];
const morseCode = function () {
for (let i = 0; i < array.length; i++) {
morseCodeString.push(array[i][0]);
}
return morseCodeString.join("")
};
// Translation button.
buttonTranslate.addEventListener("click", function () {
translationText.textContent = "";
const message = inputTranslateField.value.toLowerCase();
translationText.textContent = translateIntoMorse(message);
translationText.style.opacity = "100%";
});
// Starts audio reproduction of morse that was translated.
buttonPlayTranslation.addEventListener("click", function () {
playTranslation();
});
//////////// Used for translating into morse. ////////////////
const alphabetArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', " "];
const morseArray = ['.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..', "/"];
//////////////////////////////////////////////////////////////
// Returns a string of morse code that was translated from the user input.
const translateIntoMorse = function (message) {
const criptedMessage = [];
for (let i = 0; i < message.length; i++) {
let letterNumber = alphabetArray.indexOf(message[i]);
//console.log(letterNumber);
criptedMessage.push(morseArray[letterNumber])
}
// console.log(criptedMessage.join(" "));
playMessage = criptedMessage.join(" ");
return criptedMessage.join(" ");
};
/// Plays the audio created from text translated into morse code.
let playMessage;
const playTranslation = async function () {
for (let i = 0; i < playMessage.length; i++) {
if (playMessage[i] == ".") {
keySound.play();
await timer(65);
keySound.stop();
await timer(75);
};
if (playMessage[i] == "-") {
keySound.play();
await timer(200);
keySound.stop();
await timer(75);
};
if (playMessage[i] == " ") {
await timer(150);
}
if (playMessage[i] == "/") {
await timer(300);
};
};
};