-
Notifications
You must be signed in to change notification settings - Fork 0
/
karaoke.php
263 lines (238 loc) · 8.41 KB
/
karaoke.php
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
use MagicObject\Request\InputGet;
use MusicProductionManager\Data\Dto\Karaoke;
use MusicProductionManager\Data\Entity\EntitySong;
use MusicProductionManager\Data\Entity\Song;
use MusicProductionManager\Data\Entity\UserProfile;
require_once "inc/auth-with-login-form.php";
$song = new Song(null, $database);
$inputGet = new InputGet();
$delayStr = $inputGet->getDelay();
if($delayStr == null || empty($delayStr))
{
$delay = 0;
}
else
{
$delay = intval($delayStr);
}
$lyric = array('lyric' => '', 'start'=>0, 'duration'=>0, 'song_id'=>'');
if($inputGet->getSongId() != null)
{
$song->findOneBySongId($inputGet->getSongId());
$lyric['subtitle'] = $song->getSubtitle();
$lyric['duration'] = $song->getDuration() * 1000;
$lyric['start'] = (time() * 1000) + $delay;
$lyric['song_id'] = $song->getSongId();
}
require_once "inc/header.php";
$userProfile = new UserProfile(null, $database);
try
{
$userProfile->findOneByUserIdAndProfileName($currentLoggedInUser->getUserId(), "vocal-guide-intrument");
}
catch(Exception $e)
{
// do nothing
}
$vocalGuideInstrument = $userProfile->hasValueProfileValue() ? $userProfile->getProfileValue() : $cfg->getDefaultVocalGuideInstrument();
?>
<script>
let baseSoundFontUrl = "assets/soundfont/";
let instrumentName = '<?php echo $vocalGuideInstrument;?>';
</script>
<div class="filter-container">
<link rel="stylesheet" href="assets/css/karaoke.css">
<link rel="stylesheet" href="assets/css/piano.css">
<script src="assets/js/karaoke.min.js"></script>
<script src="assets/js/piano.min.js"></script>
<script src="assets/soundfont/soundfont-player.min.js"></script>
<script src="assets/soundfont/soundfont-midi-player.min.js"></script>
<form action="" method="get">
<?php
$inputGet = new InputGet();
$sql = "select song.song_id, song.name, song.title, song.track_number, album.album_id, album.name as album_name
from song
inner join(album) on(album.album_id = song.album_id)
where song.active = true and album.active = true and album.as_draft = false
order by album.sort_order asc, song.track_number asc
";
$album_list = array();
try
{
$res = $database->executeQuery($sql);
$rows = $res->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
if(!isset($album_list[$row['album_id']]))
{
$album_list[$row['album_id']] = array();
}
$album_list[$row['album_id']][] = $row;
}
$arr1 = array();
foreach($album_list as $albumItem)
{
$arr2 = array();
if(!empty($albumItem))
{
$arr2[] = '<optgroup label="'.$albumItem[0]['album_name'].'">';
foreach($albumItem as $songItem)
{
if($inputGet->getSongId() != null && $songItem['song_id'] == $inputGet->getSongId())
{
$selected = " selected";
}
else
{
$selected = "";
}
$arr2[] = '<option value="'.$songItem['song_id'].'"'.$selected.'>'.sprintf("%02d — %s", $songItem['track_number'], $songItem['name']).'</option>';
}
$arr2[] = '</optgroup>';
}
$arr1[] = implode("\r\n", $arr2);
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
<style>
.song-control{
padding: 0px 0px 10px 0px;
vertical-align: middle;
white-space: nowrap;
}
.song-select{
width: 145px !important;
margin-right: 2px;
}
.btn.solo{
width:90px;
}
.teleprompter {
height: calc(100vh - 420px);
width: calc(100% + 40px);
min-height: 180px;
}
</style>
<div class="song-control">
<select class="form-control song-select" name="song_id" id="song_id">
<?php
echo implode("\r\n", $arr1);
?>
</select>
<input class="btn btn-primary open" type="submit" name="open" value="Open">
<input class="btn btn-success solo" type="button" value="Solo Off">
</div>
</form>
</div>
<div class="control">
<?php
if($inputGet->getSongId() != null)
{
try
{
$song = new EntitySong(null, $database);
$song->findOneBySongId($inputGet->getSongId());
$songDto = Karaoke::valueOf($song);
?>
<audio class="player" src="<?php echo $cfg->getSongBaseUrl()."/".$song->getSongId()."/".basename($song->getFilePath());?>?hash=<?php echo str_replace(array(' ', '-', ':'), '', $song->getLastUploadTime());?>" controls></audio>
<script>
let piano = null;
let karaoke = null;
let data = <?php echo $songDto;?>;
let hasMidiSong = <?php echo $song->hasValueVocalGuide() ? 'true' : 'false';?>;
let midiSong = <?php echo $song->hasValueVocalGuide() ? $song->getVocalGuide() : '[]';?>;
let playInterval = setInterval('', 1000000);
let timeInterval = 2;
let audioContent = null;
let active = false;
let trimmer = 0.05;
function toggleSolo()
{
if(active)
{
document.querySelector('.solo').value = 'Solo Off';
active = false;
document.querySelector('.player').muted = false;
}
else
{
document.querySelector('.solo').value = 'Solo On';
active = true;
document.querySelector('.player').muted = true;
}
}
$(document).ready(function(){
if(hasMidiSong)
{
piano = new Piano(document.querySelector('.piano'));
piano.setSong(midiSong);
}
if(typeof data.subtitle != 'undefined' && data.subtitle != '')
{
karaoke = new Karaoke(data, '.teleprompter-container');
animate();
}
let midiPlayer = new SoundfontMidiPlayer();
audioContent = new AudioContext();
/*
let source = audioContent.createBufferSource();
var gainNode = audioContent.createGain()
gainNode.gain.value = 1;
gainNode.connect(audioContent.destination)
source.connect(gainNode)
*/
midiPlayer.setAudioContext(audioContent);
midiPlayer.loadInstrument(baseSoundFontUrl, instrumentName);
midiPlayer.loadNote(midiSong);
document.querySelector('.solo').addEventListener('click', function(){
toggleSolo();
})
document.querySelector('.player').addEventListener('play', function(){
clearInterval(playInterval);
playInterval = setInterval(function(){
let pos = document.querySelector('.player').currentTime + trimmer;
midiPlayer.play(instrumentName, pos, active);
})
});
document.querySelector('.player').addEventListener('pause', function(){
clearInterval(playInterval);
});
document.querySelector('.player').addEventListener('ended', function(){
clearInterval(playInterval);
});
});
function animate()
{
let pos = document.querySelector('.player').currentTime;
if(hasMidiSong)
{
piano.setTime(pos);
piano.draw();
}
karaoke.updatePosition(pos * 1000);
requestAnimationFrame(animate);
}
</script>
<?php
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
?>
</div>
<div class="piano-container">
<div class="piano"></div>
</div>
<div class="teleprompter">
<div class="teleprompter-container"></div>
</div>
<?php
require_once "inc/footer.php";
?>