X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=web%2Fsongbook.js;h=036a4a2bd4f621ab7f9ed0b0ba7503a1f362d684;hb=e1d95d5d2a9ad5be494be17ec335618cb8f5d946;hp=85a4b18abd6202c354dd2af9411dfa8b3760d3f5;hpb=4b532d27ad4ad7d71bb190f7974b3129ee114b22;p=songbook.git
diff --git a/web/songbook.js b/web/songbook.js
index 85a4b18..036a4a2 100644
--- a/web/songbook.js
+++ b/web/songbook.js
@@ -1,15 +1,28 @@
-// Javascript functions for controling audio
-starttime = null;
-endtime = null;
+// Javascript functions for controlling audio
+starttime = 0;
+endtime = 0;
+song = null;
+interval = null;
+// Keycodes
spacebar = 32;
-return2start = 82;
-backfewsecs = 66;
-forwardfewsecs = 70;
+leftarrow = 37;
+rightarrow = 39;
seta = 65;
+setb = 66;
cleara = 67;
+return2start = 82;
+
howmanysecs = 10;
+function loop() {
+ // If endtime is not set then we can't loop
+ if (endtime == 0) return;
+
+ // if we're past the endtime then it's time to start back at the A marker
+ if (song.currentTime > endtime) song.currentTime = starttime;
+} // loop
+
window.onload = function() {
song = document.getElementById('song');
@@ -17,6 +30,11 @@ window.onload = function() {
endtime = song.duration;
body = document.getElementsByTagName('body')[0]
+ if (!song.paused) {
+ // Set up loop
+ interval = setInterval(loop, 1000);
+ } // if
+
body.onkeydown =
function(e) {
var ev = e || event;
@@ -28,6 +46,8 @@ window.onload = function() {
} // if
if (playing) {
+ // Stop loop
+ clearInterval(interval)
song.pause();
playing = false;
} else {
@@ -35,6 +55,9 @@ window.onload = function() {
song.currentTime = starttime
} // if
+ // Set up loop
+ interval = setInterval(loop, 1000);
+
song.play();
playing = true;
} // if
@@ -49,22 +72,37 @@ window.onload = function() {
} // if
return;
- } else if (ev.keyCode == backfewsecs) {
+ } else if (ev.keyCode == leftarrow) {
song.currentTime -= howmanysecs;
song.play()
return;
- } else if (ev.keyCode == forwardfewsecs) {
+ } else if (ev.keyCode == rightarrow) {
song.currentTime += howmanysecs;
song.play();
return;
} else if (ev.keyCode == seta) {
+ // Reset endtime if setting a new A marker
+ if (endtime != song.duration) endtime = song.duration;
+
starttime = song.currentTime;
+ // Translate seconds to timecode
+ document.getElementById('a').innerHTML = Math.floor(starttime / 60) + ':' + Math.floor(starttime % 60);
+
return;
+ } else if (ev.keyCode == setb) {
+ if (song.currentTime > starttime) {
+ endtime = song.currentTime;
+ document.getElementById('b').innerHTML = Math.floor(endtime / 60) + ':' + Math.floor(endtime % 60);
+ } // if
} else if (ev.keyCode == cleara) {
starttime = 0;
+ endtime = song.duration;
+
+ document.getElementById('a').innerHTML = 'not set';
+ document.getElementById('b').innerHTML = 'not set';
return;
} // if