Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const input = midi.inputs.findIndex(x => x.manufacturer === "Behringer" && x.name.startsWith("X-TOUCH MINI"));
if (input === -1) {
throw new Error("Could not find Behringer X-TOUCH MINI input");
}
const output = midi.outputs.findIndex(x => x.manufacturer === "Behringer" && x.name.startsWith("X-TOUCH MINI"));
if (output === -1) {
throw new Error("Could not find Behringer X-TOUCH MINI output");
}
this.set('_controller_input', input);
this.set('_controller_output', output);
// Make sure we are in MCU protocol mode
midi.outputs[output].sendChannelMode(
127,
1 /* MCU mode */,
1 /* global channel */
);
this.setup().then((controls: any) => {
this.set(controls);
this.save_changes();
});
}
start(seconds_per_beat=1){
if (!WebMidi.enabled) {
Midi.init()
let the_note = this
setTimeout(function() { the_note.start(seconds_per_beat) }, 1000)
}
else if (WebMidi.outputs.length == 0) {
dde_error("There are no WebMidi outputs to play a Note on. <a href="#">Help</a>")
}
else if (this.is_rest()) { return this } //skip playing the "Rest" as it would error
else {
var extra_args = {duration: Math.round(this.dur * seconds_per_beat * 1000),
time: "+" + Math.round(this.time * seconds_per_beat * 1000),
velocity: this.velocity}
const pitch = this.pitch
const chan = this.channel
WebMidi.outputs[0].playNote(pitch, chan, extra_args)
return this
}
}
WebMidi.enable(function (err: any) {
if (err) {
alert("WebMidi could not be enabled.");
}
const output = WebMidi.outputs[0];
const input = WebMidi.inputs[0];
input.addListener('noteon', "all",
function (e: any) {
console.log("Received 'noteon' message (" + e.note.name + e.note.octave + ").");
output.playNote(`${e.note.name}${e.note.octave}`, 'all', { velocity: 1 });
synth.triggerAttack(`${e.note.name}${e.note.octave}`);
}
);
input.addListener('noteoff', "all",
function (e: any) {
console.log("Received 'noteoff' message (" + e.note.name + e.note.octave + ").");
output.playNote(`${e.note.name}${e.note.octave}`, 'all', { velocity: 0 });
synth.triggerRelease();
}
);
if (!WebMidi.enabled) {
Midi.init()
let the_note = this
setTimeout(function() { the_note.start(seconds_per_beat) }, 1000)
}
else if (WebMidi.outputs.length == 0) {
dde_error("There are no WebMidi outputs to play a Note on. <a href="#">Help</a>")
}
else if (this.is_rest()) { return this } //skip playing the "Rest" as it would error
else {
var extra_args = {duration: Math.round(this.dur * seconds_per_beat * 1000),
time: "+" + Math.round(this.time * seconds_per_beat * 1000),
velocity: this.velocity}
const pitch = this.pitch
const chan = this.channel
WebMidi.outputs[0].playNote(pitch, chan, extra_args)
return this
}
}
static all_notes_off(){
for(let an_out of WebMidi.outputs){
an_out.sendChannelMode('allnotesoff', 0)
}
}
}
initialize(attributes: any, options: any) {
super.initialize(attributes, options);
if (!midi.enabled) {
throw new Error('WebMidi library not enabled');
}
const values = {...this.defaults(), ...attributes};
const input = midi.inputs[values._controller_input];
const output = midi.outputs[values._controller_output];
this._button = new Button({input, output}, values._control, {
mode: values.mode,
light: values._light
});
this._button.stateChanged.connect((sender, args) => {
switch (args.name) {
case 'toggled':
this.set('value', args.newValue);
break;
case 'mode':
this.set('mode', args.newValue);
break;
}
this.save_changes();
});
this.listenTo(this, 'change', () => {
WebMidi.enable(function (err) {
if (err) log.error(err);
let midiOutSelect = new Nexus.Select('#select-midiout', {
'size': [150, 50],
'options': ['No Output'].concat(WebMidi.outputs.map((output) => output.name)),
});
function midiOutOnChange(ev) {
if (this.value !== 'No Output') {
Instruments.mute(true, useChordsInstrument);
midiOut = WebMidi.getOutputByName(this.value);
}
else {
Instruments.mute(false, useChordsInstrument);
midiOut = dummyMidiOut;
}
log.info('Selected MIDI out: ' + this.value);
};
midiOutSelect.on('change', midiOutOnChange.bind(midiOutSelect));
midiOutSelect.value = 'No Output';
WebMidi.enable(function(err) {
if (err) { warning("WebMidi couldn't be enabled: " + err) }
else {
out("WedMidi enabled")
//out(WebMidi.inputs)
//out(WebMidi.outputs)
inspect({"WebMidi.inputs": WebMidi.inputs,
"WebMidi.outputs": WebMidi.outputs})
}
})
//eval_and_play_button_id.style.display = "inline-block"