This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
200 lines (171 loc) · 4.78 KB
/
app.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
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
wavesurfer = Object.create(WaveSurfer);
audioTrack = new AudioTrack('demo.mp3');
function AudioTrack(nameOrUrl) {
this.nameOrUrl = nameOrUrl;
}
function CueMarker() {
this.startAt = wavesurfer.getCurrentTime();
this.audioTrack = audioTrack.nameOrUrl;
this.text = getText(this.startAt);
function getText(startAt) {
var text = '';
text += Math.floor(startAt) + ' s ';
text += Number(String(startAt % 1).split('.')[1] || 0) + ' ms';
return text;
}
}
CueMarker.findAll = function() {
cuemarkers = store.getAll();
console.log('Loading markers for song ' + audioTrack.nameOrUrl)
for (var key in cuemarkers) {
if (cuemarkers[key].audioTrack != audioTrack.nameOrUrl) {
delete cuemarkers[key];
}
};
return cuemarkers;
};
CueMarker.pop = function() {
var allCueMarkers = store.getAll();
var cueMarkerStartAt = Object.keys(allCueMarkers)[Object.keys(allCueMarkers).length - 1];
var cueMarker = store.get(cueMarkerStartAt);
store.remove(cueMarkerStartAt);
console.log('Popped cuemarker');
console.log(cueMarker);
};
CueMarker.remove = function(startAt) {
var cueMarker = store.get(startAt)
store.remove(startAt);
console.log('Removed cuemarker');
console.log(cueMarker);
};
CueMarker.add = function() {
var cueMarker = new CueMarker();
if (store.get(cueMarker.startAt)) {
console.log('Warning: Cue marker already exists at that position');
} else {
store.set(cueMarker.startAt, cueMarker);
console.log('Stored cuemarker');
console.log(cueMarker);
}
return cueMarker;
};
CueMarker.getByPosition = function(position) {
var allCueMarkers = store.getAll();
var cueMarkerStartAt = Object.keys(allCueMarkers)[position - 1];
var cueMarker = store.get(cueMarkerStartAt);
console.log('Cuemarker at position ' + position);
console.log(cueMarker);
return cueMarker;
};
$(function() {
var options = {
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple',
loaderColor: 'purple',
cursorColor: 'navy'
};
wavesurfer.init(options);
wavesurfer.load(audioTrack.nameOrUrl);
renderCueMarkers();
});
function renderCueMarkers() {
cuemarkers = CueMarker.findAll();
var templateScript = $('#cueMarkersTemplate').html();
var template = Handlebars.compile(templateScript);
var context = {
'cueMarkers': cuemarkers
};
var html = template(context);
$('#cueMarkers').html(html);
}
$(document).on('click', '#playPause', function() {
playPause();
});
$(document).on('click', '#addCue', function() {
addCue();
});
$(document).on('click', '.removeCue', function() {
var startAt = $(this).data('cuemarker');
removeCue(startAt);
});
$(document).on('click', '.playCue', function() {
var startAt = $(this).data('cuemarker');
playCue(startAt);
});
$(document).on('click', '#loadTrack', function() {
loadTrack();
});
$(document).keydown(function(e) {
// Spacebar
if (e.keyCode == 32) {
playPause();
}
// +
if (e.keyCode == 107 || e.keyCode == 187) {
addCue();
}
// -
if (e.keyCode == 109 || e.keyCode == 189) {
popCue();
}
// 1-9
if (e.keyCode >= 49 && e.keyCode <= 57) {
position = e.keyCode - 48;
playCueByPosition(position);
}
if (e.keyCode >= 96 && e.keyCode <= 105) {
position = e.keyCode - 95;
playCueByPosition(position);
}
// O
if (e.keyCode == 79) {
loadTrack();
}
});
function playPause() {
wavesurfer.playPause();
$('#playPause').toggleClass('btn-success');
$('#playPause').toggleClass('btn-warning');
$('#playPauseIcon').toggleClass('fa-play');
$('#playPauseIcon').toggleClass('fa-pause');
if ($('#playPause').hasClass('btn-success')) {
$('#playPauseText').text('Play');
} else {
$('#playPauseText').text('Pause');
}
}
function addCue() {
CueMarker.add();
renderCueMarkers();
}
function removeCue(cueMarker) {
CueMarker.remove(cueMarker);
renderCueMarkers();
}
function popCue() {
CueMarker.pop();
renderCueMarkers();
}
function playCue(cueMarker) {
wavesurfer.play(cueMarker);
}
function playCueByPosition(position) {
var cueMarker = CueMarker.getByPosition(position);
if (cueMarker)
wavesurfer.play(cueMarker.startAt);
}
function loadTrack() {
var fd = $.FileDialog({
multiple: false,
accept: 'audio/mpeg3',
readAs: 'ArrayBuffer'
});
fd.on('files.bs.filedialog', function(ev) {
var file = ev.files.pop();
wavesurfer.loadBlob(file);
audioTrack = new AudioTrack(file.name);
$('#trackName').html(audioTrack.nameOrUrl);
renderCueMarkers();
});
}