-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
103 lines (85 loc) · 2.8 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
const gapikey = 'AIzaSyCKMpw2nmPnon_gkh4EIXnbiAmrZNw-v4M';
var listening = false;
const initTitle = "Try 'Play tum hi ho'";
// new instance of speech recognition
var recognition = new webkitSpeechRecognition();
// set params
recognition.continuous = false;
recognition.interimResults = true;
function startListening(){
recognition.start();
recognition.onresult = function(event){
var saidWord = event.results[0][0].transcript.substr(4);
console.log(saidWord);
$('.text').text(saidWord);
if(event.results[0].isFinal){
$('.ring1, .ring2').removeClass('mic-active');
$('.text').addClass('white');
listening = 0;
playMusic(saidWord);
console.log(saidWord);
var msg = new SpeechSynthesisUtterance("playing, " + saidWord);
window.speechSynthesis.speak(msg);
noteFall();
}
}
// speech error handling
recognition.onerror = function(event){
console.log('Error occured');
console.log(event);
}
}
function playMusic(song){
$.get(
"https://www.googleapis.com/youtube/v3/search", {
part: 'snippet, id',
q: song + ' song',
type: 'video',
key: gapikey
}, function(data) {
var id = data.items[0].id.videoId;
var url = 'https://www.youtube.com/embed/'+id+'?enablejsapi=1&autoplay=1';
console.log(data.items[0].id.videoId);
$('iframe').attr('src', url);
}
)}
$('.mic').on('click', function(){
if(listening){
listening = 0;
recognition.abort();
$('.ring1, .ring2').removeClass('mic-active');
}
else {
listening = 1;
$('.text').removeClass('white').text(initTitle);
$('iframe').attr('src', '');
$('.ring1, .ring2').toggleClass('mic-active');
$('.notefall').fadeOut();
var msg = new SpeechSynthesisUtterance("yo");
window.speechSynthesis.speak(msg);
startListening();
}
});
//animated notes
function noteFall(){
var notes = document.querySelectorAll('.note');
for (var i = 0, len = notes.length; i < len; ++i) {
var note = notes[i];
var scale = Math.random() * .8 + .2;
var player = note.animate([
{ transform: 'translate3d(' + (i/len*100) + 'vw,0,0) scale(' + scale + ')', opacity: scale*0.6 },
{ transform: 'translate3d(' + (i/len*100 + 10) + 'vw,50vh,0) scale(' + scale + ')', opacity: 0 }
], {
duration: Math.random() * 3000 + 2000,
iterations: Infinity,
delay: -(Math.random() * 5000)
});
}
$('.notefall').fadeIn();
}
$(document).on('change', 'iframe', function(){
if(this.attr('src')) noteFall();
else {
$('.notefall').fadeOut();
}
});