-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (171 loc) · 5.52 KB
/
index.js
File metadata and controls
200 lines (171 loc) · 5.52 KB
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
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.10.0/firebase-app.js';
import { getDatabase, ref as dbRef, set, onValue } from 'https://www.gstatic.com/firebasejs/10.10.0/firebase-database.js';
import { GoogleGenerativeAI } from "@google/generative-ai";
const API_KEY = "AIzaSyB968fFd0rn8rxgX4WTMiRY-5I7ZmP783A";
const genAI = new GoogleGenerativeAI(API_KEY);
let dot = document.getElementById("DOT")
let line = document.getElementById("LINE")
let space = document.getElementById("SPACE")
let done = document.getElementById("DONE")
let text = document.getElementById("text")
let next = document.getElementById("NEXT")
let currentCodee = document.getElementById("currentCode")
let aud = document.getElementById("AUDIO")
let repeat = document.getElementById("REPEAT")
let words = ""
async function run(prompt) {
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const result = await model.generateContentStream(prompt);
const response = await result.response;
const text = response.text();
words = text.split(" ");
console.log(text);
}
let currentIndex = 0
function playNextWord() {
if (currentIndex < words.length) {
const msg = new SpeechSynthesisUtterance(words[currentIndex]);
window.speechSynthesis.speak(msg);
currentIndex++;
} else {
currentIndex = 0
console.log("All words have been spoken.");
}
}
function repeatLastWord() {
if (currentIndex < words.length) {
const msg = new SpeechSynthesisUtterance(words[currentIndex - 1]);
window.speechSynthesis.speak(msg);
} else {
currentIndex = 0
console.log("All words have been spoken.");
}
}
aud.addEventListener("click", playNextWord);
repeat.addEventListener("click", repeatLastWord);
const firebaseConfig = {
apiKey: "AIzaSyD58mivfUeYefkEimNO94G9j7VgSnSlXJU",
authDomain: "morsecode-69559.firebaseapp.com",
databaseURL: "https://morsecode-69559-default-rtdb.firebaseio.com",
projectId: "morsecode-69559",
storageBucket: "morsecode-69559.appspot.com",
messagingSenderId: "472468964936",
appId: "1:472468964936:web:5e7ce16d6346b2b6abba7d"
};
initializeApp(firebaseConfig);
const db = getDatabase();
const textRef = dbRef(db, "morseText");
let currentText = ""
onValue(textRef, (snapshot) => {
const storedText = snapshot.val(); // Get the value from the snapshot
console.log("Stored text:", storedText);
text.innerText = storedText
});
let currentCode = ""
dot.addEventListener("click", () => {
currentCode += ".";
currentCodee.innerText += "."
console.log(currentCode);
});
line.addEventListener("click", () =>{
currentCode += "-"
currentCodee.innerText += "-"
console.log(currentCode)
})
space.addEventListener("click", () => {
currentText += " ";
set(textRef, currentText)
currentCodee.innerText += "/"
console.log(currentCode);
});
next.addEventListener("click", () =>{
currentText += translateMorseToText(currentCode)
set(textRef, currentText)
currentCodee.innerHTML += " ";
currentCode = ""
console.log(currentCode)
})
done.addEventListener("click", () =>{
onValue(textRef, (snapshot) => {
const storedText = snapshot.val(); // Get the value from the snapshot
console.log("Stored text:", storedText);
run(storedText + " scrie tot Scurt si la Obiect in limba engleza")
});
currentCode = ""
currentCodee.innerText += ""
currentText = ""
set(textRef, currentText)
console.log("SENDING MESSAGE...")
})
const morseToText = {
".-": "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",
".----": "1",
"..---": "2",
"...--": "3",
"....-": "4",
".....": "5",
"-....": "6",
"--...": "7",
"---..": "8",
"----.": "9",
"-----": "0",
"/": " ", // Word separator
};
function translateMorseToText(morseCode) {
const words = morseCode.trim().split(" / ");
let translatedText = "";
for (const word of words) {
const letters = word.split(" ");
for (const letter of letters) {
if (morseToText.hasOwnProperty(letter)) {
translatedText += morseToText[letter];
} else {
vibrateDevice();
if (currentCodee.innerText.endsWith(currentCode)) {
// Remove the common part from currentCodee.innerText
const newInnerText = currentCodee.innerText.slice(0, -currentCode.length);
currentCodee.innerText = newInnerText;
console.log("Removed:", currentCode);
} else {
console.log("No match found.");
}
currentCode = ""
translatedText = ""
}
}
translatedText += " ";
}
return translatedText.trim();
}
function vibrateDevice() {
if ("vibrate" in navigator) {
navigator.vibrate(2000);
} else {
console.log("Vibration not supported on this device.");
}
}