Skip to content

Commit 18bec9b

Browse files
author
bryc
committed
various fixes + new song
- fix deprecated methods - implemented Just Temperment (experimental) - removed needless alias - add pause/resume ability - clean up UI - no crash if no waveforms defined - no crash if no channel volumes defined - strip dashes from note values (e.g. c-5 becomes c5)
1 parent 33971d5 commit 18bec9b

File tree

3 files changed

+170
-14
lines changed

3 files changed

+170
-14
lines changed

bsp/bsp.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
(function(){
22
/* ------------------------ */
33

4-
54
var schedule = function() {
65
var SONG = BSP.SONG;
76
var fix = function(n){return Math.round(n*1000)/1000;};
87

98
for(var n = 0.000501, j = 0; j < SONG.seq.length; j++) {
109
for(var i = 0, tick = BSP.time; i < SONG.seq[j].length;) {
1110
var step = SONG.seq[j][i];
11+
if(step && step[0] && step[0][1]==='-') step[0] = step[0].replace("-","");
1212
if(step && BSP.freq[step[0]]) {
1313
BSP.osc[j].frequency.setValueAtTime((BSP.freq[step[0]]/(SONG.trans||2)), tick);
1414
if(tick > 0) {
@@ -45,16 +45,18 @@ var startSong = function() {
4545
var waves = ["sine", "square", "triangle", "sawtooth"];
4646
for(var j = 0; j < SONG.seq.length; j++) {
4747
BSP.osc[j] = BSP.ctx.createOscillator();
48-
if(SONG.wave[j].constructor === Array) {
48+
if(SONG.wave && SONG.wave[j] && SONG.wave[j].constructor === Array) {
4949
var waveform = BSP.ctx.createPeriodicWave(SONG.wave[j][0], SONG.wave[j][1]);
5050
BSP.osc[j].setPeriodicWave(waveform);
51-
} else {
51+
} else if(SONG.wave) {
5252
BSP.osc[j].type = waves[SONG.wave[j]] || waves[1];
53+
} else {
54+
BSP.osc[j].type = waves[1];
5355
}
5456
BSP.amp[0][j] = BSP.ctx.createGain(); // Osc Channel Volume
5557
BSP.amp[1][j] = BSP.ctx.createGain(); // Osc Note Volume
56-
BSP.amp[1][j].gain.value = 0;
57-
BSP.amp[0][j].gain.value = SONG.cVol[j];
58+
BSP.amp[1][j].gain.setValueAtTime(0, 0);
59+
BSP.amp[0][j].gain.setValueAtTime(SONG.cVol&&SONG.cVol[j]?SONG.cVol[j]:1, 0);
5860
BSP.osc[j].connect(BSP.amp[1][j]);
5961
BSP.amp[1][j].connect(BSP.amp[0][j]);
6062
BSP.amp[0][j].connect(BSP.ctx.destination);
@@ -74,8 +76,15 @@ var startSong = function() {
7476
---------------------------- */
7577
var init = function() {
7678
BSP.freq = (function() {
79+
var JT=[1, 25/24, 9/8, 6/5, 5/4, 4/3, 45/32, 3/2, 8/5, 5/3, 9/5, 15/8];
7780
var freq={},i=-57,o="c,c#,d,d#,e,f,f#,g,g#,a,a#,b".split(",");
78-
for(var n=0,p=0;63>i;i++)freq[o[n++]+p]=440*Math.pow(Math.pow(2,1/12),i),12==n&&(p++,n=0);
81+
for(var n=0,p=0;63>i;i++){
82+
var Q = Math.floor((i+9)/12);
83+
var freq1 = 440*Math.pow(Math.pow(2,1/12),i);
84+
var freq2 = 261.625*((Q==0)?JT[n]:Math.pow(2,Q)*JT[n]);
85+
freq[o[n++]+p] = freq1;
86+
12==n&&(p++,n=0);
87+
}
7988
return freq;
8089
})();
8190

@@ -96,9 +105,8 @@ var init = function() {
96105
}
97106
};
98107

99-
BSP.play = startSong;
100108
window.addEventListener("load", function() {
101-
BSP.play();
109+
startSong();
102110
});
103111
};
104112

bsp/index.html

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<div id=z7i></div>
2+
<div id=z8i></div>
23
<style>
3-
a{text-decoration:none;font-size:24px;color:#000;background:#FFF;display:inline-block;padding:16px;outline:2px solid #DDD;}a:hover{outline:none;color:#777;background:#DDD;}
4-
body{width:640px;margin:auto;margin-top:64px;
4+
h2{margin:0;border-bottom:1px solid #888;}
5+
#z7i{padding:8px;margin-top:48px;margin-bottom:8px;}
6+
#z8i{padding:8px;border-top:2px solid #888;color:#EEE;}
7+
#wat{text-decoration:none;font-size:18px;text-align:center;width:85px;padding:8px;color:#999;background:#333;font-weight:bold;margin-right:16px;outline:2px solid #444;}#wat:hover{color:#CCC;outline:2px solid #777;background:#444;}
8+
a#z{text-decoration:none;font-size:18px;color:#000;background:#FFF;display:inline-block;padding:8px 12px;outline:2px solid #DDD;}a#z:hover{outline:none;color:#777;background:#DDD;}
9+
body{width:640px;margin:auto;
510
background:#333;font-family:-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Ubuntu,Roboto,"Helvetica Neue",sans-serif}
611
</style>
712
<script>
@@ -13,10 +18,11 @@
1318
"mono",
1419
"cool",
1520
"old",
21+
"stormlord2",
1622
];
1723

18-
for(var s='',i = 0; i < songs.length; i++) {
19-
s+=`<a href=?${songs[i]}>${songs[i]}</a>`;
24+
for(var s='<a href=# id=wat>Stop</a>',i = 0; i < songs.length; i++) {
25+
s+=`<a id=z href=?${songs[i]}>${songs[i]}</a>`;
2026
}
2127
document.querySelector("div#z7i").innerHTML = s;
2228

@@ -25,5 +31,27 @@
2531
s.src = "bsp.js";
2632
s2.src = "songs/"+location.search.slice(1)+".js";
2733
document.head.append(s), document.head.append(s2);
28-
}
29-
</script>
34+
}
35+
36+
window.onload = function() {
37+
document.querySelector("#z8i").innerHTML = `
38+
<b>Title</b> = ${BSP.SONG.title || "<i>none</i>"}<br>
39+
<b>Date</b> = ${BSP.SONG.date || "<i>none</i>"}<br>
40+
<b>Author</b> = ${BSP.SONG.author || "<i>none</i>"}<br>
41+
<b>BPM</b> = ${BSP.SONG.bpm} ${BSP.SONG.divide? `(${BSP.SONG.divide} steps per beat)`:``}<br>
42+
<h2>Comment</h2>${BSP.SONG.comment || "<i>none</i>"}<br>
43+
`;
44+
}
45+
46+
document.querySelector("#wat").onclick = function () {
47+
if(BSP.paused) {
48+
BSP.ctx.resume();
49+
BSP.paused = false;
50+
this.innerHTML = "Stop";
51+
} else {
52+
BSP.ctx.suspend();
53+
BSP.paused = true;
54+
this.innerHTML = "Resume";
55+
}
56+
}
57+
</script>

bsp/songs/stormlord2.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
BSP.SONG = (function(SONG = {}) {
2+
Array.prototype.rotate=function(t){return this.slice(t,this.length).concat(this.slice(0,t))}
3+
var w0=new Float32Array([0,1,.5,.3,.5,.1]),
4+
w1=new Float32Array([0,1,0,0,1,0,.1,.25,.1,.6]),
5+
w2=new Float32Array([0,.2,.1,.1,.3]);
6+
var A=[w0,w0],B=[w1,w1],C=[w2,w2];
7+
/* ----------------------------- */
8+
SONG.title = "stormlord 2"
9+
SONG.author = "bryc"
10+
SONG.date = "jan-14-2018"
11+
SONG.comment = "pattern variables, rotated patterns, periodic waves.<br>remake of a remake of a remake (https://soundcloud.com/bryc/stormlord-two)"
12+
SONG.bpm = 81
13+
SONG.divide = 6
14+
SONG.cVol = [.25,.19,.08,.045,.06,.05,.05]
15+
SONG.wave = [A,B,B,B, C,C,C]
16+
17+
var bass=[
18+
['d-3'], , , , ,['d-3'],
19+
-1 , ,['d-3'], , ,['a#2'],
20+
, , , , ,['a#3'],
21+
-1 , ,['a#2'], , ,['a#2'],
22+
]
23+
var bass2=[
24+
['a-2'], , , , ,['a-2'],
25+
-1 , ,['a-2'], , ,['d#3'],
26+
, , , , ,['d#4'],
27+
-1 , ,['d#3'], , , ,
28+
]
29+
var bass3=[
30+
['a-2'], , , , ,['a-2'],
31+
-1 , ,['a-2'], , ,['g#2'],
32+
, , , , ,['g#3'],
33+
-1 , ,['g#2'], , , ,
34+
]
35+
var pad0_0=[
36+
['c-5'], , , , , ,
37+
, , , , -1 ,['d-5'],
38+
, , , , , ,
39+
, , , , , -1 ,
40+
]
41+
var pad0_1=[
42+
['c-5'], , , , , ,
43+
, , , , -1 ,['d-5'],
44+
, , , , , ,
45+
, , , , , -1 ,
46+
]
47+
var pad0_2=[
48+
['c-5'], , , , , ,
49+
, , , , -1 ,['c-5'],
50+
, , , , , ,
51+
, , , , , -1 ,
52+
]
53+
var pad1_0=[
54+
['f-5'], , , , , ,
55+
, , , , -1 ,['f-5'],
56+
, , , , , ,
57+
, , , , , -1 ,
58+
]
59+
var pad1_1=[
60+
['e-5'], , , , , ,
61+
, , , , -1 ,['f-5'],
62+
, , , , , ,
63+
, , , , , -1 ,
64+
]
65+
var pad1_2=[
66+
['e-5'], , , , , ,
67+
, , , , -1 ,['d#5'],
68+
, , , , , ,
69+
, , , , , -1 ,
70+
]
71+
var pad2_0=[
72+
['a-5'], , , , , ,
73+
, , , , -1 ,['a-5'],
74+
, , , , , ,
75+
, , , , , -1 ,
76+
]
77+
var pad2_1=[
78+
['g-5'], , , , , ,
79+
, , , , -1 ,['a#5'],
80+
, , , , , ,
81+
, , , , , -1 ,
82+
]
83+
var pad2_2=[
84+
['g-5'], , , , , ,
85+
, , , , -1 ,['g-5'],
86+
, , , , , ,
87+
, , , , , -1 ,
88+
]
89+
90+
var melody1=[
91+
, , ,['a-5'], -1 ,['c-6'],
92+
-1 , ,['c-6'], -1 , ,['c-6'],
93+
, , ,['d-6'], -1 , ,
94+
, , ,['a-5'], , ,
95+
['g-5'], , , , -1 ,['f-5'],
96+
, ,['e-5'], , ,['f-5'],
97+
, , , , , ,
98+
['a#5'], , , , , ,
99+
['c-6'], , ,['a-5'], ,['g-5'],
100+
['f-5'], ,['d-5'], -1 , ,['c-5'],
101+
, , ,['d-5'], -1 , ,
102+
['c-5'],['d-5'],['f-5'],['g-5'],['a-5'],['c-6'],
103+
['d-6'], , , , , ,
104+
, -1 ,['f-6'], -1 , ,['g-6'],
105+
, , , , , ,
106+
, , , , , ,
107+
]
108+
109+
SONG.seq = [
110+
bass.concat(bass2,bass,bass3),
111+
melody1, melody1.rotate(-3), melody1.rotate(-6),
112+
pad0_0.concat(pad0_1,pad0_0,pad0_2),
113+
pad1_0.concat(pad1_1,pad1_0,pad1_2),
114+
pad2_0.concat(pad2_1,pad2_0,pad2_2),
115+
]
116+
117+
/* ----------------------------- */
118+
return SONG;
119+
/* ----------------------------- */
120+
})();

0 commit comments

Comments
 (0)