-
Notifications
You must be signed in to change notification settings - Fork 0
/
looper.js
116 lines (101 loc) · 3.07 KB
/
looper.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
import { autoDetect, colorHelpers } from './launchpad.js/index.js';
import { findDevice, onExit } from './launchpad.js/utils.js';
import { MidiLooper } from './launchpad.js/launchpads/MK3/MidiLooper.js';
import midi from 'midi';
const myArgs = process.argv.slice(2);
export let debug = false;
if (myArgs.includes("debug")) {
console.log("DEBUGGING");
debug = true;
}
const { colorFromHex, colors } = colorHelpers;
let midiTracks = [];
export let result = [];
// upon connect the launchpad will be put into "Session" mode
let settingToggles = {};
export const lp = autoDetect();
const midiTrack1 = new MidiLooper({
init: 81,
color: colors.teal,
name: "e352",
enabled: true
});
midiTracks.push(midiTrack1);
midiTrack1.setup();
export const miniOutput = new midi.Output();
const outputPort = findDevice(/Focusrite USB MIDI/, miniOutput);
onExit(() => { miniOutput.closePort() });
miniOutput.openPort(outputPort);
const midiKeyboard = new midi.Input();
const inputPort = findDevice(/MINIMAX/, midiKeyboard);
onExit(() => { midiKeyboard.closePort() });
midiKeyboard.openPort(inputPort);
midiKeyboard.ignoreTypes(false, false, false);
console.log(midiKeyboard);
let keyboardIndex = 0;
midiKeyboard.on('message', (delta, note) => {
miniOutput.sendMessage(note);
for (var i = 0; i < midiTracks.length; i++) {
if (midiTracks[i].armed && midiTracks[i].enabled) {
keyboardIndex = 0;
midiTracks[i].record(note);
}
if (midiTracks[i].recording) {
let obj = {};
obj.note = note;
obj.delta = delta * 1000;
midiTracks[i].loop[keyboardIndex] = obj;
keyboardIndex += 1;
}
}
/*if (lp.armedMidiLooperTracks) {
lp.activeMidiLooperTracks.forEach(track => {
if (lp.armedMidiLooperTracks[track]) {
// MidiLooper.record()
lp.armedMidiLooperTracks[track](note);
};
})
}*/
})
lp.input.on('message', (delta, note) => {
// console.log('Raw message', note, delta);
if (note[0] == 144 && note[2] == 0)
if (lp.map[note[1]])
lp.map[note[1]]();
if (note[0] == 176 && note[2] == 0)
lp.map[note[1]]()
});
lp.once('ready', (name) => {
console.log(`Connected to ${name}`);
});
lp.on('buttonDown', (note, value) => {
if (note.nr % 10 == 9) {
console.log(settingToggles);
if (settingToggles[note.nr]) {
console.log(settingToggles);
} else {
settingToggles[note.nr] = false;
console.log(settingToggles);
}
}
setTimeout(() => {
if (note.nr in settingToggles) {
settingToggles[note.nr] = true;
}
}, 200);
// generate a random color on each button press
const randHex = Math.floor(Math.random() * 16777215).toString(16);
// we have the parse the colors to a special RGB value as
// the launchpad does not go from 0-255 but from 0-63 for each color
const color = colorFromHex(randHex);
/* console.log(color);
console.log(`Button pressed: ${note.nr}`);
*/
//lp.setButtonColor(note, color);
});
lp.on('buttonUp', (note, value) => {
if (note.nr in settingToggles) {
delete settingToggles[note.nr];
}
//lp.setButtonColor(note, colors.off);
});