-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuneplayer.js
203 lines (168 loc) · 5.74 KB
/
tuneplayer.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
201
202
/* ********************************* tuneplayer.js ************************************* */
function tuneplayer() {
document.getElementById('the_audio').addEventListener('loadedmetadata', function() {
var dur = this.duration;
tune = {
loop: false,
inpoint: 0,
outpoint: dur,
speed: 100,
delay: 2
}
updateView();
function loopy(){ // this is inside here because it can only start once the audio is fully loaded and defined etc...
setTimeout(loopy,20);
var aud = document.getElementById('the_audio');
ct = aud.currentTime;
if (ct >= tune.outpoint) {
aud.currentTime = tune.inpoint;
aud.pause();
tune.loop? playAfterPause(aud) : 1;
}
}
loopy(); // start checking the time for loopyness!!
});
function playAfterPause(aud){
aud.pause();
setTimeout(function(){aud.play()}, tune.delay*1000);
var countdown = tune.delay;
function tic() {
document.getElementById('countdown').innerHTML = countdown;
countdown--;
countdown > -1 ? setTimeout(tic,1000) : document.getElementById('countdown').innerHTML = '';
}
tic();
}
function updateView(){
$('#inpoint').val(tune.inpoint);
$('#outpoint').val(tune.outpoint);
$('#loop').prop("checked",tune.loop); // apparently you have to use prop() instead of attr() for some shit like form elements. awesome.
$('#delayRange').val(tune.delay);
$('#displayDelay').val(tune.delay);
}
var key_listener = window.addEventListener('keypress',keyWasPressed,false);
var speed_listener = document.getElementById('playbackSpeedRange').addEventListener('change',rangeChanged, false);
var range_listener = document.getElementById('delayRange').addEventListener('change',delayChanged, false);
function keyWasPressed(k){
if (k.target.id == "input_tag") { // !!!!!! If they're typing in an input field we don't want to activate the audio
return; // player controls! (like if they type fLute we don't want LOOP to turn on & off)
}
var update = true;
var aud = document.getElementById('the_audio');
console.log("Key was pressed!!! " + k.keyCode); //+ Object.keys(k));
switch (k.keyCode) {
case 105: // i ..... inpoint
case 73: // I
tune.inpoint = aud.currentTime;
tune.outpoint < tune.inpoint ? tune.outpoint = aud.duration : 1;
break;
case 111: // o ..... outpoint
case 79: // O
tune.outpoint = aud.currentTime;
tune.inpoint > tune.outpoint ? tune.inpoint = 0 : 1;
break;
case 32: // space bar
aud.playing || !aud.paused ? aud.pause() : playAfterPause(aud); //aud.play();
break;
case 108:
case 76: // loop
tune.loop == false ? tune.loop = true : tune.loop = false;
aud.loop = tune.loop;
break;
case 13: // enter key
console.log(k.target.id);
if (k.target.id == "displayPlaybackSpeed") {
var spood = $('#displayPlaybackSpeed').val();
spood < 50 ? spood = 50 : spood = spood;
console.log("spood" + spood);
$('#displayPlaybackSpeed').val(spood);
$('#playbackSpeedRange').val(spood);
$('#displayPlaybackSpeed').blur();
aud.playbackRate = spood/100;
}
else if (k.target.id == "inpoint") {
tune.inpoint = $('#inpoint').val();
aud.currentTime < tune.inpoint ? aud.currentTime = tune.inpoint : 1;
$('#inpoint').blur();
} else if (k.target.id == "outpoint") {
tune.outpoint = $('#outpoint').val();
if (aud.currentTime > tune.outpoint) {
aud.pause();
aud.currentTime = tune.outpoint;
}
$('#outpoint').blur();
//aud.currentTime > tune.outpoint ? aud.pause(); aud.currentTime = tune.outpoint : 1;
} else if (k.target.id == "displayDelay") {
var spood = $('#displayDelay').val();
$('#delayRange').val(spood);
$('#displayDelay').blur();
tune.delay = spood;
}
break;
default:
console.log("not a key we care about");
update = false;
}
if (update){
updateView();
}
}
function rangeChanged(){
console.log('range changed');
$('#displayPlaybackSpeed').val($('#playbackSpeedRange').val());
document.getElementById('the_audio').playbackRate = $('#playbackSpeedRange').val()/100;
}
function delayChanged() {
console.log('delay changed');
$('#displayDelay').val($('#delayRange').val());
tune.delay = $('#delayRange').val();
}
}
function sharedFunctions() {
$('.userInput').focus(function() { this.value=''; $(this).removeClass('userInput'); });
}
function typing_in_tags() {
var l = $('#input_tag').val().length;
console.log(l);
if (l > 1) {
$('#add_tag').attr('disabled', false);
$('#search_tags').attr('disabled', false);
}
}
function add_tag() {
var new_tag = $('#input_tag').val();
var old_tags = $('#tags_list').html();
$('#tags_list').html(old_tags + " " + new_tag);
$('#input_tag').val('');
$('#input_tag').focus();
$('#add_tag').attr('disabled', 'disabled');
}
function search_tags() {
var tags_list = $('#tags_list').html();
console.log("AAAAFDSFADSFASDF " + tags_list);
$('#search_results').load("ajax.php", "action=search&tags_list=" + tags_list);
$('#tags_list').html('');
}
function load_metadata(id) {
console.log('load_metadata ' + id);
$('#now_playing_text').load("ajax.php", "action=showTuneMetaData&id=" + id);
}
function load_tune(id) {
console.log("Load Tuuuuuuuuuuuune: " + id);
$('#the_audio').load("ajax.php", "action=loadTune&id=" + id, function() {
delete window.tunePlayer; // delete original instance of this so functions don't get called twice
$('#the_audio').load();
// tuneplayer(); // don't do this, this fucks everything up by adding new event listeners
load_metadata(id);
});
}
function tune_by_date(d,e) {
if (typeof d === 'undefined') {
var data = '';
} else {
var when = e.target.id;
var date = d;
var data = "&d="+date+"&when="+when;
}
$('#recent_tunes').load('ajax.php', 'action=tuneByDate'+data);
}