-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiFilePlay.html
144 lines (109 loc) · 3.19 KB
/
midiFilePlay.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
<!DOCTYPE html>
<html>
<head>
<title>Tonejs Midi</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.26/Tone.js"></script>
<script src="https://unpkg.com/@tonejs/[email protected]/build/Midi.js"></script>
<script type="text/javascript" src="https://unpkg.com/@tonejs/[email protected]"></script>
<script src="p5.js"></script>
</head>
<body>
<style>
#Status {
color: gray;
height: 40px;
}
</style>
<tone-content>
<div id="Description">Load and play DEBUSSY <i>String quartet Op. 10</i></div>
<div id="Status">Loading...</div>
<tone-play-toggle disabled>Loading...</tone-play-toggle>
</tone-content>
<div id="myCanvas" style=" display: flex;
justify-content: center;"></div>
<script type="text/javascript">
let midiLog = [];
let testTracks = [];
let testTicks = [];
var trig = 0;
var nowAnim =1;
var mapRange = function(from, to, s) {
return to[0] + (s - from[0]) * (to[1] - to[0]) / (from[1] - from[0]);
};
Midi.fromUrl("./d.mid").then(midi => {
document.querySelector('tone-play-toggle').removeAttribute('disabled')
document.querySelector('#Status').textContent = ''
midi.tracks.forEach(track => {
track.notes.forEach(note => {
// var mappedrange = mapRange([0,4530],[0,200],track.notes.ticks)
// var mrArray = [];
// mrArray.push(mappedrange)
// testTracks.push(testTicks.push(mappedrange))
})
})
//synth playback
const synths = []
document.querySelector('tone-play-toggle').addEventListener('play', (e) => {
const playing = e.detail
if (playing) {
const now = Tone.now() + 0.5
midi.tracks.forEach(track => {
//create a synth for each track
const synth = new Tone.PolySynth( 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()
}
}
})
})
function setup(){
var canvasDiv = document.getElementById('myCanvas');
var sketchCanvas=createCanvas(400, 400,WEBGL);
sketchCanvas.parent("myCanvas")
Midi.fromUrl("./d.mid").then(midi => {
console.log(midi)
midiLog.push(midi)
for (let i = 0; i <midiLog[0].tracks[0].notes.length; i++) {
testTicks.push(midiLog[0].tracks[0].notes[i].ticks)
}
})
console.log(testTicks)
}
function draw(){
background(0);
for (let i = 0; i <testTicks.length; i++) {
noStroke();
fill(255);
rectMode(CENTER)
rect(testTicks[i]-100,0, .5,40)
// console.log(testTicks[i])
}
// for (let i = 0; i < testTracks.notes.length; i++) {
// if(Tone.now()+ 0.5 == testTracks[0].notes[i].ticks){
// trig = 1;
// }
// else{trig=0}
// }
if (trig ==1){
rect(0,0,100,100)
}
}
</script>
</body>
</html>