forked from deskjet/chiptune2.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
157 lines (145 loc) · 4.65 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chiptune Worklet Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content="Chiptune Audio Player"/>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🔊</text></svg>"/>
<link href="./css/style.css" rel="stylesheet"/>
</head>
<body>
<main>
<section>
<fieldset>
<legend>Chiptune3</legend>
This is an updated Chiptune ES6 module version to work with libopenmpt AudioWorklet.<br/>
Drop in your favorite module.<br/>
Usage:<pre>
// Import ES6 module
import {ChiptuneJsPlayer} from 'https://DrSnuggles.github.io/chiptune/chiptune3.js'
// Create instance (wherever you want)
window.chiptune = new ChiptuneJsPlayer()
// if you already have an audioContext you can provide here, but then chiptune does not route to speakers! Connect chiptune.gain
// Wait for ready
chiptune.onInitialized(() => {
// Play awesome music
chiptune.load('https://deskjet.github.io/chiptune2.js/tunes/chipsounds.mod')
})</pre>
</section>
<section>
<fieldset>
<legend>Methods</legend>
<table>
<tr>
<td>chiptune.load(url)</td>
<td><button onclick="chiptune.load('https://deskjet.github.io/chiptune2.js/tunes/chipsounds.mod')">Load</button></td>
</tr>
<tr>
<td>chiptune.play(arrayBuffer)</td>
<td>Play buffer. This is also called by load.</td>
</tr>
<tr>
<td>chiptune.stop()</td>
<td><button onclick="chiptune.stop()">Stop</button></td>
</tr>
<tr>
<td>chiptune.pause()</td>
<td><button onclick="chiptune.pause()">Pause</button></td>
</tr>
<tr>
<td>chiptune.unpause()</td>
<td><button onclick="chiptune.unpause()">Unpause</button></td>
</tr>
<tr>
<td>chiptune.togglePause()</td>
<td><button onclick="chiptune.togglePause()">Toggle Pause</button></td>
</tr>
<tr>
<td>chiptune.setRepeatCount(val)</td>
<td><input onchange="chiptune.setRepeatCount(this.value)" type="number" min="-1" max="99" step="1" value="-1"/>-1 = endless; 0 = just play once</td>
</tr>
<tr>
<td>chiptune.selectSubsong(val)</td>
<td><div id="subsongs"></div></td>
</tr>
<tr>
<td>chiptune.setPos(float)<br/>
chiptune.seek(float)
</td>
<td><input oninput="chiptune.setPos(this.value)" type="range" min="0" max="2" step="0.0001" id="seekbar"/></td>
</tr>
<tr>
<td>chiptune.setVol(float)</td>
<td><input oninput="chiptune.setVol(this.value)" type="range" min="0" max="2" step="0.0001"/></td>
</tr>
<tr>
<td>chiptune.setPitch(float)</td>
<td><input oninput="chiptune.setPitch(this.value)" type="range" min="0" max="2" step="0.0001"/></td>
</tr>
<tr>
<td>chiptune.setTempo(float)</td>
<td><input oninput="chiptune.setTempo(this.value)" type="range" min="0" max="2" step="0.0001"/></td>
</tr>
</table>
</fieldset>
</section>
<section>
<fieldset>
<legend>Events</legend>
chiptune.onInitialized = () => {}<br/>
chiptune.onEnded = () => {}<br/>
chiptune.onError = (err) => {}<br/>
chiptune.onMetadata = (meta) => {}<br/>
chiptune.onProgress = (pos) => {}
</fieldset>
</section>
<section>
<fieldset>
<legend>Metadata</legend>
<div id="metadata"></div>
</section>
<!-- for audio playback policy -->
<div id="audioModal">👉 💻 👂 🎶</div>
</main>
<script type="module">
import {dnd} from './dnd.js'
import {ChiptuneJsPlayer} from './chiptune3.js'
// stupid no audio till user interaction policy thingy
function userInteracted() {
removeEventListener('keydown', userInteracted)
removeEventListener('click', userInteracted)
removeEventListener('touchstart', userInteracted)
audioModal.classList.add('fadeOut')
// init
window.chiptune = new ChiptuneJsPlayer()
// listen events
chiptune.onInitialized(() => {
// play song
chiptune.load('https://deskjet.github.io/chiptune2.js/tunes/chipsounds.mod')
// dnd
dnd(window, (aB) => {
chiptune.play(aB)
})
})
chiptune.onEnded(() => {
console.log('Song ended')
chiptune.stop()
})
chiptune.onError((err) => {
console.log('Song error', err)
})
chiptune.onProgress((pos) => {
seekbar.value = pos
})
chiptune.onMetadata((meta) => {
seekbar.max = meta.dur
meta.song = 'too large to display this way...'
metadata.innerHTML = JSON.stringify(meta).replace(/,/g,'<br/> ')
})
}
addEventListener('keydown', userInteracted)
addEventListener('click', userInteracted)
addEventListener('touchstart', userInteracted)
</script>
</body>
</html>