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

/**
 * web_midi_link.js
 * purpose: handles the web midi link connection to the synthesizer
 * https://www.g200kg.com/en/docs/webmidilink/
 */

export class WebMIDILinkHandler
{
    /**
     * @param synth {Synthetizer} the synth to play to
     */
    constructor(synth)
    {
        
        window.addEventListener("message", msg =>
        {
            if (typeof msg.data !== "string")
            {
                return;
            }
            /**
             * @type {string[]}
             */
            const data = msg.data.split(",");
            if (data[0] !== "midi")
            {
                return;
            }
            
            data.shift(); // remove MIDI
            
            const midiData = data.map(byte => parseInt(byte, 16));
            
            synth.sendMessage(midiData);
        });
        
        SpessaSynthInfo("%cWeb MIDI Link handler created!", consoleColors.recognized);
    }
}