forked from Tonejs/Midi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (142 loc) · 3.76 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
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html>
<head>
<title>Tonejs Midi</title>
<script type="text/javascript" src="https://unpkg.com/tone@13.4.9/build/Tone.js"></script>
<script type="text/javascript" src="https://unpkg.com/@tonejs/ui@0.0.8/build/tonejs-ui.js"></script>
<script type="text/javascript" src="https://unpkg.com/@tonejs/midi"></script>
</head>
<body>
<style type="text/css">
#FileDrop{
position: relative;
width: 100%;
height: 100px;
border: 2px dashed black;
color: black;
margin: 20px auto;
}
#FileDrop.Hover{
background-color: black;
color: white;
}
#FileDrop input {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
left: 0px;
top: 0px;
}
#FileDrop #Text {
position: absolute;
width: 100%;
height: 20px;
line-height: 20px;
left: 0px;
top: 50%;
transform: translate(0, -50%);
text-align: center;
}
textarea {
font-family: monospace;
height: 300px;
width: 100%;
display: inline-block;
position: relative;
float: left;
}
#Results {
position: relative;
width: 100%;
margin: 10px auto;
}
#Description {
position: relative;
width: 100%;
text-align: center;
height: 40px;
font-size: 16px;
margin: 10px auto;
font-family: sans-serif;
}
tone-play-toggle {
margin-top: 10px;
}
</style>
<tone-top-bar></tone-top-bar>
<tone-content>
<div id="Description">Parse a MIDI file into a Tone.js-friendly JSON format.</div>
<div id="FileDrop">
<div id="Text">
Drop a midi file here
</div>
<input type="file" accept="audio/midi">
</div>
<div id="Results">
<textarea id="ResultsText" placeholder="json output..."></textarea>
</div>
<tone-play-toggle disabled></tone-play-toggle>
</tone-content>
<script type="text/javascript">
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
document.querySelector("#FileDrop #Text").textContent = "Reading files not supported by this browser";
} else {
const fileDrop = document.querySelector("#FileDrop")
fileDrop.addEventListener("dragenter", () => fileDrop.classList.add("Hover"))
fileDrop.addEventListener("dragleave", () => fileDrop.classList.remove("Hover"))
fileDrop.addEventListener("drop", () => fileDrop.classList.remove("Hover"))
document.querySelector("#FileDrop input").addEventListener("change", e => {
//get the files
const files = e.target.files
if (files.length > 0){
const file = files[0]
document.querySelector("#FileDrop #Text").textContent = file.name
parseFile(file)
}
})
}
let currentMidi = null
function parseFile(file){
//read the file
const reader = new FileReader()
reader.onload = function(e){
const midi = new Midi(e.target.result)
document.querySelector("#ResultsText").value = JSON.stringify(midi, undefined, 2)
document.querySelector('tone-play-toggle').removeAttribute('disabled')
currentMidi = midi
}
reader.readAsArrayBuffer(file)
}
const synths = []
document.querySelector('tone-play-toggle').addEventListener('play', (e) => {
const playing = e.detail
if (playing && currentMidi){
const now = Tone.now() + 0.5
currentMidi.tracks.forEach(track => {
//create a synth for each track
const synth = new Tone.PolySynth(10, Tone.Synth, {
envelope : {
attack : 0.02,
decay : 0.1,
sustain : 0.3,
release : 1
}
}).toMaster()
synths.push(synth)
//schedule all of the events
track.notes.forEach(note => {
synth.triggerAttackRelease(note.name, note.duration, note.time + now, note.velocity)
})
})
} else {
//dispose the synth and make a new one
while(synths.length){
const synth = synths.shift()
synth.dispose()
}
}
})
</script>
</body>
</html>