File size: 3,373 Bytes
b0bfea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Synthetizer } from "../synthetizer/synthetizer.js";
import { consoleColors } from "../utils/other.js";
import { SpessaSynthInfo, SpessaSynthWarn } from "../utils/loggin.js";

/**
 * midi_handler.js
 * purpose: handles the connection between MIDI devices and synthesizer/sequencer via Web MIDI API
 */

const NO_INPUT = null;

export class MIDIDeviceHandler
{
    constructor()
    {
    }
    
    /**
     * @returns {Promise<boolean>} if succeded
     */
    async createMIDIDeviceHandler()
    {
        /**
         * @type {MIDIInput}
         */
        this.selectedInput = NO_INPUT;
        /**
         * @type {MIDIOutput}
         */
        this.selectedOutput = NO_INPUT;
        if (navigator.requestMIDIAccess)
        {
            // prepare the midi access
            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;
        }
    }
    
    /**
     * Connects the sequencer to a given MIDI output port
     * @param output {MIDIOutput}
     * @param seq {Sequencer}
     */
    connectMIDIOutputToSeq(output, seq)
    {
        this.selectedOutput = output;
        seq.connectMidiOutput(output);
        SpessaSynthInfo(
            `%cPlaying MIDI to %c${output.name}`,
            consoleColors.info,
            consoleColors.recognized
        );
    }
    
    /**
     * Disconnects a midi output port from the sequencer
     * @param seq {Sequencer}
     */
    disconnectSeqFromMIDI(seq)
    {
        this.selectedOutput = NO_INPUT;
        seq.connectMidiOutput(undefined);
        SpessaSynthInfo(
            "%cDisconnected from MIDI out.",
            consoleColors.info
        );
    }
    
    /**
     * Connects a MIDI input to the synthesizer
     * @param input {MIDIInput}
     * @param synth {Synthetizer}
     */
    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
        );
    }
    
    /**
     * @param input {MIDIInput}
     */
    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;
        }
    }
}