-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman-migration.js
217 lines (185 loc) · 6.53 KB
/
human-migration.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const csv = require("csv-parser");
const fs = require("fs");
const easymidi = require("easymidi");
const { Note, Scale } = require("@tonaljs/tonal");
const result = [];
const MIDI_CLOCK_PER_QUARTER_NOTE = 24; // From MIDI specification:
const MASTER_TEMPO = 64; // BPM = number of quarter notes per minute
const IS_SIMULATION = false;
fs.createReadStream("tk1423-adsb.csv")
.pipe(csv({ separator: ";" }))
.on("data", (data) => result.push(data))
.on("end", () => {
const parsedFlightData = result.map((data) => ({
altitude: data["metres"] ? parseInt(data["metres"]) : 0,
speed: data["mph"] ? parseInt(data["mph"]) : 0,
}));
main(parsedFlightData);
});
/**
* Randomize an integer between min and max.
* @param {Int} min
* @param {Int} max
* @returns
*/
function getRandomInt(min, max) {
const minimum = Math.ceil(min);
const maximum = (max = Math.floor(max));
return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
}
/**
* Use this function to simulate the sequencer, and debug the musical notes.
*
* @param {Object} data - flight data
* @param {Array} scale - musical scale
*/
function sequencerSimulation(data, scale) {
let speedDifference = 0;
let previousSpeed = 0;
let isSpeedNoteChanged = false;
let previousSpeedNote = "";
let altitudeDifference = 0;
let previousAltitude = 0;
let isAltitudeNoteChanged = false;
let previousAltitudeNote = "";
data.forEach((flight) => {
if (flight.speed > scale.length) {
// Because the speed data is too high, let's do a creative stuff
// to make it fit on a scale length.
const remainder = flight.speed % scale.length;
speedDifference = remainder;
} else {
speedDifference = flight.speed - previousSpeed;
}
if (flight.altitude > scale.length) {
// Because the altitude data is too high, let's do a creative stuff
// to make it fit on a scale length.
const remainder = flight.altitude % scale.length;
altitudeDifference = remainder;
} else {
altitudeDifference = flight.altitude - previousAltitude;
}
const currentSpeedNote = scale[speedDifference] ?? previousSpeedNote;
const currentAltitudeNote =
scale[altitudeDifference] ?? previousAltitudeNote;
if (flight.speed === previousSpeed && !isSpeedNoteChanged) {
console.log(`${currentSpeedNote} is not changed`);
} else {
isSpeedNoteChanged = true;
console.log("---");
console.log(`${previousSpeedNote} retard`);
console.log(`${currentSpeedNote} advance`);
console.log("---");
}
if (flight.altitude === previousAltitude && !isAltitudeNoteChanged) {
console.log(`${currentAltitudeNote} is not changed`);
} else {
isSpeedNoteChanged = true;
console.log("---");
console.log(`${previousAltitudeNote} retard`);
console.log(`${currentAltitudeNote} advance`);
console.log("---");
}
previousSpeed = flight.speed;
previousSpeedNote = currentSpeedNote;
previousAltitude = flight.altitude;
previousAltitudeNote = currentAltitudeNote;
});
}
/**
* The sequencer for Ableton Live. It synchronizes the notes with the MIDI clock.
* @param {Object} data - flight data
* @param {Array} scale - musical scale
*/
function sequencerAbleton(data, scale) {
const virtualInput = new easymidi.Input("Node.js input", true);
const virtualOutput = new easymidi.Output("Node.js output", true);
let speedDifference = 0;
let previousSpeed = 0;
let isSpeedNoteChanged = false;
let previousSpeedNote = "";
let altitudeDifference = 0;
let previousAltitude = 0;
let isAltitudeNoteChanged = false;
let previousAltitudeNote = "";
let totalClockPerMinute = MIDI_CLOCK_PER_QUARTER_NOTE * MASTER_TEMPO;
let clockCounting = 1;
let dataIndex = 0;
virtualInput.on("clock", () => {
if (clockCounting === totalClockPerMinute) {
// Reset the clock counter
clockCounting = 1;
}
if (dataIndex === data.length - 1) {
// Stop the sequencer
virtualOutput.send("stop");
}
// DO THE CALCULATION FOR EACH BEAT ONLY
if (clockCounting % MIDI_CLOCK_PER_QUARTER_NOTE === 0) {
if (data[dataIndex].speed > scale.length) {
// Because the speed data is too high, let's do a creative stuff
// to make it fit on a music scale length.
const remainder = data[dataIndex].speed % scale.length;
speedDifference = remainder;
} else {
speedDifference = data[dataIndex].speed - previousSpeed;
}
if (data[dataIndex].altitude > scale.length) {
// Because the altitude data is too high, let's do a creative stuff
// to make it fit on a music scale length.
const remainder = data[dataIndex].altitude % scale.length;
altitudeDifference = remainder;
} else {
altitudeDifference = data[dataIndex].altitude - previousAltitude;
}
const currentSpeedNote = scale[speedDifference] ?? previousSpeedNote;
const currentAltitudeNote =
scale[altitudeDifference] ?? previousAltitudeNote;
if (data[dataIndex].speed === previousSpeed && !isSpeedNoteChanged) {
console.log(`Speed: ${currentSpeedNote} is not changed`);
} else {
isSpeedNoteChanged = true;
virtualOutput.send("noteoff", {
note: Note.midi(previousSpeedNote),
velocity: 0,
channel: 2,
});
virtualOutput.send("noteon", {
note: Note.midi(currentSpeedNote),
velocity: getRandomInt(100, 127),
channel: 2,
});
}
if (data[dataIndex].altitude === previousAltitude && !isAltitudeNoteChanged) {
console.log(`Altitude: ${currentAltitudeNote} is not changed`);
} else {
isSpeedNoteChanged = true;
virtualOutput.send("noteoff", {
note: Note.midi(previousAltitudeNote),
velocity: 0,
channel: 1,
});
virtualOutput.send("noteon", {
note: Note.midi(currentAltitudeNote),
velocity: getRandomInt(100, 127),
channel: 1,
});
}
previousSpeed = data[dataIndex].speed;
previousSpeedNote = currentSpeedNote;
previousAltitude = data[dataIndex].altitude;
previousAltitudeNote = currentAltitudeNote;
dataIndex++;
}
clockCounting++;
});
}
function main(flightData) {
const musicalRange = Scale.rangeOf("A minor blues");
const musicalScale = musicalRange("A3", "A6");
if (IS_SIMULATION) {
sequencerSimulation(flightData, musicalScale);
} else {
sequencerAbleton(flightData, musicalScale);
}
}