-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (88 loc) · 3.33 KB
/
index.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
'use strict';
window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
const synth = window.speechSynthesis;
const recognition = new SpeechRecognition();
recognition.lang = 'fa-IR';
const icon = document.querySelector('i.fa.fa-microphone');
let paragraph = document.createElement('p');
let container = document.querySelector('.text-box');
container.appendChild(paragraph);
const sound = document.querySelector('.sound');
icon.addEventListener('click', () => {
// sound.play();
dictate();
});
const dictate = () => {
recognition.start();
recognition.onresult = (event) => {
const speechToText = event.results[0][0].transcript;
paragraph.textContent = speechToText;
if (event.results[0].isFinal) {
// if (speechToText.includes('what is the time')) {
if (speechToText.includes('ساعت چنده')) {
speak(getTime);
};
if (speechToText.includes('امروز چه روزيه')) {
speak(getDate);
};
if (speechToText.includes('هوای تهران چطوره')) {
getTheWeather(speechToText);
};
}
};
};
var voiceSelect = document.querySelector('select');
var voices = [];
function populateVoiceList() {
voices = synth.getVoices();
var selectedIndex = voiceSelect.selectedIndex < 0 ? 0 : voiceSelect.selectedIndex;
voiceSelect.innerHTML = '';
for(var i = 0; i < voices.length ; i++) {
var option = document.createElement('option');
option.textContent = voices[i].name + ' (' + voices[i].lang + ')';
if(voices[i].default) {
option.textContent += ' -- DEFAULT';
}
option.setAttribute('data-lang', voices[i].lang);
option.setAttribute('data-name', voices[i].name);
voiceSelect.appendChild(option);
}
voiceSelect.selectedIndex = selectedIndex;
}
populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
const speak = (action) => {
const utterThis = new SpeechSynthesisUtterance(action());
var selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');
for(var i = 0; i < voices.length ; i++) {
if(voices[i].name === selectedOption) {
utterThis.voice = voices[i];
}
}
synth.speak(utterThis);
};
const getTime = () => {
const time = new Date(Date.now());
return `ساعت ${time.toLocaleString('fa-IR', { hour: 'numeric', minute: 'numeric', hour12: true })}`;
};
const getDate = () => {
const time = new Date(Date.now());
return `today is ${time.toLocaleDateString('fa-IR')}`;
};
const getTheWeather = (speech) => {
fetch(`http://api.openweathermap.org/data/2.5/weather?q=${speech.split(' ')[5]}&appid=58b6f7c78582bffab3936dac99c31b25&units=metric`)
.then(function(response){
return response.json();
})
.then(function(weather){
if (weather.cod === '404') {
const utterThis = new SpeechSynthesisUtterance(`هوای. الآن. تهران. خيلی. خوب و نيمه. بارونيه. ${speech.split(' ')[5]}`);
synth.speak(utterThis);
return;
}
const utterThis = new SpeechSynthesisUtterance(`هوای الآن تهران ${weather.name} is mostly full of ${weather.weather[0].description} at a temperature of ${weather.main.temp} degrees Celcius`);
synth.speak(utterThis);
});
};