Added A/B looping
[songbook.git] / web / songbook.js
1 // Javascript functions for controlling audio
2 starttime = 0;
3 endtime   = 0;
4 song      = null;
5 interval  = null;
6
7 // Keycodes
8 spacebar       = 32;
9 leftarrow      = 37;
10 rightarrow     = 39;
11 seta           = 65;
12 setb           = 66;
13 cleara         = 67;
14 return2start   = 82;
15
16 howmanysecs    = 10;
17
18 function loop() {
19   // If endtime is not set then we can't loop
20   if (endtime == 0) return;
21
22   // if we're past the endtime then it's time to start back at the A marker
23   if (song.currentTime > endtime) song.currentTime = starttime;
24 } // loop
25
26 window.onload = function() {
27   song = document.getElementById('song');
28
29   starttime = song.currentTime;
30   endtime   = song.duration;
31   body      = document.getElementsByTagName('body')[0]
32
33   if (!song.paused) {
34    // Set up loop
35    interval = setInterval(loop, 1000);
36   } // if
37
38   body.onkeydown = 
39     function(e) {
40       var ev = e || event;
41       if (ev.keyCode == spacebar) {
42         if (song.paused) {
43           playing = false;
44         } else {
45           playing = true;
46         } // if
47
48         if (playing) {
49           // Stop loop
50           clearInterval(interval)
51           song.pause();
52           playing = false;
53         } else {
54           if (starttime != 0) {
55             song.currentTime = starttime
56           } // if
57
58           // Set up loop
59           interval = setInterval(loop, 1000);
60
61           song.play();
62           playing = true;
63         } // if
64
65         e.preventDefault();
66         return;
67       } else if (ev.keyCode == return2start) {
68         if (starttime != null) {
69           song.currentTime = starttime;
70         } else {
71           song.currentTime = 0;
72         } // if
73
74         return;
75       } else if (ev.keyCode == leftarrow) {
76         song.currentTime -= howmanysecs;
77         song.play()
78
79         return;
80       } else if (ev.keyCode == rightarrow) {
81         song.currentTime += howmanysecs;
82         song.play();
83
84         return;
85       } else if (ev.keyCode == seta) {
86         // Reset endtime if setting a new A marker
87         if (endtime != song.duration) endtime = song.duration;
88
89         starttime = song.currentTime;
90
91         // Translate seconds to timecode
92         document.getElementById('a').innerHTML = Math.floor(starttime / 60) + ':' + Math.floor(starttime % 60);
93
94         return;
95       } else if (ev.keyCode == setb) {
96         if (song.currentTime > starttime) {
97           endtime = song.currentTime;
98           document.getElementById('b').innerHTML = Math.floor(endtime / 60) + ':' + Math.floor(endtime % 60);
99         } // if
100       } else if (ev.keyCode == cleara) {
101         starttime = 0;
102         endtime   = song.duration;
103
104         document.getElementById('a').innerHTML = '<font color=#666><i>not set</i></font>';
105         document.getElementById('b').innerHTML = '<font color=#666><i>not set</i></font>';
106
107         return;
108       } // if
109     } // function
110   }  // getElementByTagName