-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic2arduino.js
168 lines (130 loc) · 5.11 KB
/
music2arduino.js
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
"use strict";
function* enumerate(iterable) {
let i = 0;
for(let item of iterable) {
yield [i, item];
i++;
}
}
class ToneArduinoConv {
static getTrackCumulators = TonePlayer.getTrackCumulators;
static MAX_TICKS = (2 ** 14) - 1;
static NUMS_PER_LINE = 5;
static TRACK_TEMPLATE = null;
static TRACK_TEMPLATE_URL = "MusicPlayerTemplate.txt";
constructor(trackObj, secondsPerTick) {
this.tracks = trackObj;
this.secondsPerTick = secondsPerTick;
this.millisPerTick = secondsPerTick * 1000;
this.locations = TonePlayer.getTrackCumulators(this.tracks);
this._length = Math.max(...Object.values(this.locations).map((a) => a[a.length - 1]));
}
getCode() {
let trackSol = [];
let firstValidTrack = null;
for(let trackName in this.tracks) {
let track = this.tracks[trackName];
if(track.notes == undefined) continue;
firstValidTrack = trackName;
let [noteLst, tickSpeed] = ToneArduinoConv.solveTrack(track, this.length, this.millisPerTick);
let encodedTrack = ToneArduinoConv.encodeTrack(noteLst);
trackSol.push(ToneArduinoConv.toCode(trackName, encodedTrack, tickSpeed));
}
if(trackSol.length == 0) {
return "";
}
else {
let first
let template = ToneArduinoConv.TRACK_TEMPLATE;
if(template != null) {
return template.replaceAll("###MUSICDATA###", trackSol.join("\n")).replaceAll("###TRACKNAME###", firstValidTrack);
}
else {
return trackSol.join("\n");
}
}
}
static async loadTemplate() {
try {
this.TRACK_TEMPLATE = await (new Promise((accept, reject) => {
$.ajax({
url: this.TRACK_TEMPLATE_URL,
success: (d) => accept(d),
error: (j, e) => reject(e),
dataType: "text"
});
}));
} catch (e) {
this.TRACK_TEMPLATE = null;
}
}
static findGCFOfDurations(track) {
let gcfTick = null;
for(let [type, duration] of track.notes) {
gcfTick = (gcfTick == null)? duration: this.gcf(gcfTick, duration);
}
return gcfTick;
}
static to128Midi(num) {
return Math.floor(128 * (69 + 12 * Math.log2(num / 440)));
}
static solveTrack(track, length, millisPerTick) {
// We allow up to millisecond percision on the tick unit max...
let tickUnit = 1 / millisPerTick;
let finalNoteList = [];
for(let [type, arg1, arg2] of track.notes) {
let duration = (type == "play")? arg2: arg1;
let freq = (type == "play")? arg1: undefined;
let durationTicks = Math.floor(duration / tickUnit);
if(type == "play") {
finalNoteList.push(["play", this.to128Midi(freq)]);
type = "wait";
}
for(; durationTicks > this.MAX_TICKS; durationTicks -= this.MAX_TICKS) {
finalNoteList.push([type, this.MAX_TICKS]);
if(type == "rest") type = "wait";
}
finalNoteList.push([type, durationTicks]);
length -= duration;
}
if(length > 0) {
let tickLength = Math.floor(length / tickUnit);
let type = "rest";
for(; tickLength > this.MAX_TICKS; tickLength -= this.MAX_TICKS) {
finalNoteList.push([type, this.MAX_TICKS]);
if(type == "rest") type = "wait";
}
finalNoteList.push([type, tickLength]);
}
return [finalNoteList, tickUnit * millisPerTick];
}
static encodeTrack(solvedTrack) {
let encoded = [];
let to14Bit = (n) => {
return (Math.abs(n) >>> 0).toString(2).padStart(14, "0");
};
let toCommand = {
"wait": "00",
"play": "01",
"rest": "10"
};
for(let [type, arg] of solvedTrack) {
encoded.push(toCommand[type] + to14Bit(arg));
}
return encoded;
}
static toCode(name, encodedTrack, tickSpeed) {
let finalString = "const uint16_t PROGMEM " + name + "[] = {";
for(let [i, binary] of enumerate(encodedTrack)) {
if(i % this.NUMS_PER_LINE == 0) finalString += "\n ";
finalString += "0b" + binary + ((i == encodedTrack.length - 1)? "\n": ", ");
}
finalString += "};\n\n";
finalString += "const size_t " + name + "Len = sizeof(" + name + ") / sizeof(uint16_t);\n";
finalString += "const float " + name + "MillisPerTick = " + tickSpeed + ";\n";
return finalString;
}
get length() {
return this._length;
}
}