|
|
import { Synthetizer } from "../synthetizer/synthetizer.js"; |
|
|
import { consoleColors } from "../utils/other.js"; |
|
|
import { SpessaSynthInfo, SpessaSynthWarn } from "../utils/loggin.js"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const NO_INPUT = null; |
|
|
|
|
|
export class MIDIDeviceHandler |
|
|
{ |
|
|
constructor() |
|
|
{ |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async createMIDIDeviceHandler() |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
this.selectedInput = NO_INPUT; |
|
|
|
|
|
|
|
|
|
|
|
this.selectedOutput = NO_INPUT; |
|
|
if (navigator.requestMIDIAccess) |
|
|
{ |
|
|
|
|
|
try |
|
|
{ |
|
|
const response = await navigator.requestMIDIAccess({ sysex: true, software: true }); |
|
|
this.inputs = response.inputs; |
|
|
this.outputs = response.outputs; |
|
|
SpessaSynthInfo("%cMIDI handler created!", consoleColors.recognized); |
|
|
return true; |
|
|
} |
|
|
catch (e) |
|
|
{ |
|
|
SpessaSynthWarn(`Could not get MIDI Devices:`, e); |
|
|
this.inputs = []; |
|
|
this.outputs = []; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
SpessaSynthWarn("Web MIDI Api not supported!", consoleColors.unrecognized); |
|
|
this.inputs = []; |
|
|
this.outputs = []; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connectMIDIOutputToSeq(output, seq) |
|
|
{ |
|
|
this.selectedOutput = output; |
|
|
seq.connectMidiOutput(output); |
|
|
SpessaSynthInfo( |
|
|
`%cPlaying MIDI to %c${output.name}`, |
|
|
consoleColors.info, |
|
|
consoleColors.recognized |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disconnectSeqFromMIDI(seq) |
|
|
{ |
|
|
this.selectedOutput = NO_INPUT; |
|
|
seq.connectMidiOutput(undefined); |
|
|
SpessaSynthInfo( |
|
|
"%cDisconnected from MIDI out.", |
|
|
consoleColors.info |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connectDeviceToSynth(input, synth) |
|
|
{ |
|
|
this.selectedInput = input; |
|
|
input.onmidimessage = event => |
|
|
{ |
|
|
synth.sendMessage(event.data); |
|
|
}; |
|
|
SpessaSynthInfo( |
|
|
`%cListening for messages on %c${input.name}`, |
|
|
consoleColors.info, |
|
|
consoleColors.recognized |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disconnectDeviceFromSynth(input) |
|
|
{ |
|
|
this.selectedInput = NO_INPUT; |
|
|
input.onmidimessage = undefined; |
|
|
SpessaSynthInfo( |
|
|
`%cDisconnected from %c${input.name}`, |
|
|
consoleColors.info, |
|
|
consoleColors.recognized |
|
|
); |
|
|
} |
|
|
|
|
|
disconnectAllDevicesFromSynth() |
|
|
{ |
|
|
this.selectedInput = NO_INPUT; |
|
|
for (const i of this.inputs) |
|
|
{ |
|
|
i[1].onmidimessage = undefined; |
|
|
} |
|
|
} |
|
|
} |