-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
178 lines (148 loc) · 4.37 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebMod Test</title>
<script src="dist/webmod.min.js"></script>
<script>
var track = null;
var ctx = new AudioContext();
async function loadModule(data) {
if(track) {
track.stop();
}
track = await webmod.createModuleFromBuffer(ctx, data);
track.connect(ctx.destination);
track.start();
document.getElementById('sample-number').value = 1;
renderSample(1);
}
function main() {
var request = new XMLHttpRequest();
request.open('GET', 'dino.mod', true);
request.responseType = 'arraybuffer';
request.onload = function() {
loadModule(new Uint8Array(request.response));
};
request.send();
}
function loadFile(file) {
if(!file) return;
var reader = new FileReader();
reader.onload = function(data) {
loadModule(reader.result);
};
reader.readAsArrayBuffer(file);
}
setInterval(function() {
if(!track) return;
var str = '';
for(var i = -16; i <= 16; ++i) {
var rowNum = track.currentRow + i;
if(rowNum >= 0 && rowNum < 64) {
if(rowNum.toString().length == 1) rowNum = ' ' + rowNum;
str += rowNum + ' ';
}
if(i == 0) str += '<mark>> ' + track.rowToString(i) + '</mark>\n';
else str += ' ' + track.rowToString(i) + '\n';
}
document.getElementById('track').innerHTML = str;
}, 50);
setInterval(function() {
if(!track) return;
var div = document.getElementById('position');
div.innerHTML = '';
for(var i = 0; i < track.positionCount; ++i) {
var span = document.createElement('span');
if(i == track.currentPos) span.className = 'active';
span.innerHTML = track.patternTable[i];
div.appendChild(span);
}
}, 100);
function playSample(number) {
if(!track) return;
if(number - 1 >= track.samples.length) return;
if(!track.samples[number - 1].buffer) return;
var node = ctx.createBufferSource();
node.buffer = track.samples[number - 1].buffer;
node.connect(ctx.destination);
node.start();
}
function renderSample(number) {
var canvas = document.getElementById('sample-preview');
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
if(!track) return;
if(number - 1 >= track.samples.length) return;
var sample = track.samples[number - 1];
if(!sample.buffer) return;
var data = sample.buffer.getChannelData(0);
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(0, canvas.height / 2);
if(canvas.width < data.length) {
for(var x = 0; x <= canvas.width; ++x) {
var level = -data[Math.round(x / canvas.width * data.length)];
ctx.lineTo(x, canvas.height / 2 + level * canvas.height / 2);
}
} else {
for(var i = 0; i < data.length; ++i) {
var level = -data[i];
var x = i / (data.length - 1) * canvas.width;
ctx.lineTo(x, canvas.height / 2 + level * canvas.height / 2);
}
}
ctx.stroke();
ctx.strokeStyle = '#666';
ctx.beginPath();
ctx.moveTo(0, canvas.height / 2);
ctx.lineTo(canvas.width, canvas.height / 2);
ctx.stroke();
if(sample.repeatLength) {
ctx.beginPath();
ctx.moveTo(sample.repeat / sample.length * canvas.width, 0);
ctx.lineTo(sample.repeat / sample.length * canvas.width, canvas.height);
ctx.moveTo((sample.repeat + sample.repeatLength) / sample.length * canvas.width, 0);
ctx.lineTo((sample.repeat + sample.repeatLength) / sample.length * canvas.width, canvas.height);
ctx.stroke();
}
}
</script>
<style>
#track
{
white-space:pre;
font-family:Monospace;
border:2px #008 inset;
}
#position span
{
display:inline-block;
width:24px;
/* height:24px; */
padding:4px;
text-align:center;
border:1px solid black;
margin:2px;
font-family:Sans-Serif;
font-size:10pt;
}
#position span.active
{
background-color:rgb(121, 199, 229);
font-weight:bold;
}
</style>
</head>
<body>
<h1>WebMod Test</h1>
<input type="file" onchange="loadFile(this.files[0]);">
<button type="button" onclick="if(track) { track.stop(); track = null; }">Stop</button>
<div id="position"></div>
<div id="track" style="white-space:pre; font-family:Monospace;"></div>
<input id="sample-number" type="number" min="1" max="256" value="1" onchange="renderSample(this.value);">
<button type="button" onclick="playSample(document.getElementById('sample-number').value);">Play Sample</button><br>
<canvas id="sample-preview" width="800" height="320">
</body>
</html>