-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeat.js
139 lines (119 loc) · 2.8 KB
/
beat.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
let sounds = [
{
"keepPlaying": false,
"sound": 'aa aa',
"voice": "es-ES",
"icon": "❖"
},
{
"keepPlaying": false,
"sound": 'ксж',
"voice": "ru-RU",
"icon": "✶"
},
{
"keepPlaying": false,
"sound": 'iao',
"voice": "it-IT",
"icon": "△"
},
{
"keepPlaying": false,
"sound": 'rrr',
"voice": "ro-RO",
"icon": "✽"
},
{
"keepPlaying": false,
"sound": 'ввв',
"voice": "ru-RU",
"icon": "◎"
},
{
"keepPlaying": false,
"sound": '我',
"voice": "zh-HK",
"icon": "❀"
},
{
"keepPlaying": false,
"sound": '브',
"voice": "ko-KR",
"icon": "⦿"
},
{
"keepPlaying": false,
"sound": 'th',
"voice": "ja-JA",
"icon": "□",
},
{
"keepPlaying": false,
"sound": 'κκ',
"voice": "el-GR",
"icon": "✸"
},
]
const soundsPerRow = 3;
const delay = ms => new Promise(res => setTimeout(res, ms));
let speed = 1000;
$('#speedSlider').change(function (e) {
speed = e.target.value;
})
buildAll();
function play(soundIndex){
playByText(
sounds[soundIndex]["voice"],
sounds[soundIndex]["sound"],
sounds[soundIndex]["volume"]
)
}
const hold = async (soundIndex) => {
// Check if already playing
if (sounds[soundIndex]["keepPlaying"]){
return;
}
sounds[soundIndex]["keepPlaying"] = true;
while(sounds[soundIndex]["keepPlaying"]){
play(soundIndex);
await delay(speed);
}
};
function stopPlaying(soundIndex){
sounds[soundIndex]["keepPlaying"] = false;
}
function buildAll() {
var container = $('#buttons');
for (var i = 0; i < sounds.length; i+=soundsPerRow){
container.append(buildSoundRow(i, i+soundsPerRow));
}
}
function buildSoundRow(startIndex, endIndex){
let row = $('<div></div>');
row.addClass("row blacks");
for (var i = startIndex; i < endIndex; i++){
row.append(buildSoundButton(i));
}
return row;
}
function buildSoundButton(index) {
let container = $('<div></div>');
let ball = $('<a><p>'+ sounds[index]["icon"] +'</p></a>');
ball.click(function() { play(index); })
let controls = $('<div></div>');
controls.addClass("row controls");
controls.click(function () {
if (controls.hasClass("on")){
controls.removeClass('on');
stopPlaying(index);
} else {
controls.addClass('on');
hold(index);
}
});
let holdControl = $('<div>REPEAT</div>');
controls.append(holdControl);
container.append(ball);
container.append(controls);
return container;
}