-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
113 lines (81 loc) · 2.99 KB
/
index.html
File metadata and controls
113 lines (81 loc) · 2.99 KB
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
<html>
<head>
<title>.midi to Arduino</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@tonejs/midi"></script>
</head>
<body>
<div id="song"></div>
<script type="text/javascript">
const $song = $("#song")
async function parseMidi(songFile) {
let completed = false
let notes = []
const midi = await Midi.fromUrl(songFile)
for(let trackId = 0; trackId < midi.tracks.length; trackId++) {
const track = midi.tracks[trackId]
const trackNotes = track.notes
trackNotes.forEach(note => {
let stripped_note = {}
// console.log(note)
stripped_note.name = note.name
stripped_note.time = note.time
stripped_note.duration = note.duration
stripped_note.trackId = trackId
notes.push(stripped_note)
})
if(trackId + 1 == midi.tracks.length) {
notes.sort(sortByTime)
displayNotes(notes)
}
}
}
function displayNotes(notes) {
let arduinoCommands = [];
let previousCommand = -1;
let previousTime = 0;
for(let noteIndex = 0; noteIndex < notes.length; noteIndex++) {
const note = notes[noteIndex]
const delay = Math.floor((note.time - previousTime) * 1000)
if(noteIndex > 0 && delay > 0) {
arduinoCommands.push(`delay(${ delay });`)
previousCommand = -1
}
if(previousCommand == note.trackId) {
continue
// arduinoCommands.pop()
}
arduinoCommands.push(`freq${ note.trackId }.play(NOTE_${ note.name.replace("#", "S") }, ${ Math.floor(note.duration * 1000) });`)
previousCommand = note.trackId;
previousTime = note.time
}
for(let commandIndex = 0; commandIndex < arduinoCommands.length; commandIndex++) {
$song.append(arduinoCommands[commandIndex] + '<br>')
}
}
function sortByTime(a, b) {
if (a.time < b.time){
return -1;
}
if (a.time > b.time){
return 1;
}
return 0;
}
parseMidi("song.mid");
// let previousTime = 0
// for(const trackIndex in song.tracks) {
// console.log(trackIndex)
// const notes = song.tracks[trackIndex].notes
// for(const noteIndex in notes) {
// const note = notes[noteIndex];
// const delay = Math.floor((note.time - previousTime) * 1000)
// if(noteIndex > 0 && delay > 0) {$song.append(`delay(${ delay });<br>`)
// }
// $song.append(`freq1.play(NOTE_${ note.name.replace("#", "S") }, ${ Math.floor(note.duration * 1000) });<br>`)
// previousTime = note.time
// }
// }
</script>
</body>
</html>