Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor() {
super()
// Web MIDI is disabled
store.dispatch(enableMidi(false))
// Enable Web MIDI
WebMidi.enable(err => {
if (err) {
console.error('Web MIDI API could not be enabled:', err)
} else {
// MIDI input / output ports (from a single device) are connected to the computer
WebMidi.addListener('connected', e => {
const { manufacturer, name, id, type } = e.port
console.log('MIDIController added:', 'Manufacturer:', manufacturer, '| Name:', name, '| ID:', id, '| Type:', type)
})
WebMidi.addListener('disconnected', e => {
const { manufacturer, name, type, id } = e.port
console.log('MIDIController removed:', 'Manufacturer:', manufacturer, '| Name:', name, '| ID:', id, '| Type:', type)
})
// Web MIDI is enabled
constructor (opts = {}) {
super();
const options = Object.assign(opts, {logging: true});
log.enabled = options.logging;
this.activePads = [];
this.batchCommands = []; // batch commands in case something is called before web midi is enabled
this.midiEnabled = false;
this.pushConnected = false;
WebMidi.enable((err) => {
if (err) {
log('WebMidi could not be enabled.');
return;
}
this.midiEnabled = true;
log('WebMidi enabled!');
WebMidi.addListener('connected', (...args) => {
if (this.pushConnected) {
return;
}
this.pushConnected = true;
this.setupPush();
log('CONNECTED', args);
this.emit('push:connected');
requestMidiAccess = () => {
webmidi.enable(err => {
if (err) {
this.canAccessMidi = false;
alert("No midi support in your browser."); // eslint-disable-line
} else {
this.canAccessMidi = true;
this.initializeMidi();
}
});
}
private setupMidi() {
WebMidi.enable(err => {
if (err) {
console.warn(err)
return
}
const onMidimessage = ({data}: InputEventBase<'midimessage'>) => {
const [_, ch, velocity] = data
const pressed = velocity > 64
const address = `/midi/ch${ch}`
const toggleType = pressed ? 'press' : 'release'
const togglePayload = {address, value: pressed}
const changePayload = {address, value: velocity}
this.toggleTable.set(address, pressed)
this.emit(toggleType, togglePayload)
static init(){
if(!WebMidi.enabled){
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"
}
else {
Midi.all_notes_off()
WebMidi.disable() //undefines the listeners, maybe other stuff
setTimeout(Midi.init, 1000)
}
constructor(){
super()
this._isEnabled = false
WebMidi.enable((err) => {
if (!err){
this._isEnabled = true
if (WebMidi.inputs){
WebMidi.inputs.forEach((input) => this._bindInput(input))
}
WebMidi.addListener('connected', (device) => {
if (device.input){
this._bindInput(device.input)
}
})
}
})
}
this.ready = new Promise((done, error) => {
WebMidi.enable((e) => {
if (e){
error(e)
}
WebMidi.inputs.forEach(i => this._addListeners(i))
WebMidi.addListener('connected', (e) => {
if (e.port.type === 'input'){
this._addListeners(e.port)
}
})
WebMidi.addListener('disconnected', (e) => {
this._removeListeners(e.port)
})
done()
})
})
} else {
this.ready = new Promise((done, error) => {
WebMidi.enable((e) => {
if (e) {
error(e)
}
WebMidi.addListener('connected', (event) => {
if (event.port.type === 'input') {
this._addListeners(event.port)
}
})
WebMidi.addListener('disconnected', (event) => {
this._removeListeners(event.port)
})
done()
})
})
export function render(useChordsInstrument: boolean = false) {
let topControlsGridElem = document.getElementById('bottom-controls');
let midiOutSelectElem: HTMLElement = document.createElement('control-item');
midiOutSelectElem.id = 'select-midiout';
topControlsGridElem.appendChild(midiOutSelectElem);
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;
}