MIDIIn.connectAll;

~pulsar = 1056899563;
~mellotron = 8388611;

(


~notes = Array.newClear(128);  // Initialize the array to store synths

// Handle noteOn for MIDI device 8454149 (first device)
MIDIdef.noteOn(\noteOnTest_device1, {
    arg vel, nn, chan, src;
    [vel, nn, src].postln;  // Post the velocity, note number, and source to thethe console

    // Only respond to MIDI input from source 8454149 (device 1)
	if (src == ~pulsar, {
        // Create a new synth for device 1
        ~notes[nn] = Synth.new(
			\convolv,            [
                \freq, nn.midicps,
                \amp, vel.linexp(1,127,0.01,0.3),
            ]
        );
    });
});

// Handle noteOff for MIDI device 8454149 (first device)
MIDIdef.noteOff(\noteOffTest_device1, {
    arg vel, nn, chan, src;
    [vel, nn, src].postln;

    // Only respond to MIDI input from source 8454149 (device 1)
    if (src == ~pulsar, {
        // Turn off the synth and free the memory for device 1
		~notes[nn].set(\gate, 0);
		~notes[nn] = nil;
    });
});

// Handle noteOn for MIDI device 8454145 (second device)
MIDIdef.noteOn(\noteOnTest_device2, {
    arg vel, nn, chan, src;
    [vel, nn, src].postln;  // Post the velocity, note number, and source to the console

    // Only respond to MIDI input from source 8454145 (device 2)
    if (src == ~mellotron, {
        // Create a new synth for device 2
        ~notes[nn] = Synth.new(
            \mellotron,
            [
                \freq, nn.midicps,
                \amp, vel.linexp(1,127,0.01,0.3),
            ]
        );
    });
});

// Handle noteOff for MIDI device 8454145 (second device)
MIDIdef.noteOff(\noteOffTest_device2, {
    arg vel, nn, chan, src;
    [vel, nn, src].postln;

    // Only respond to MIDI input from source 8454145 (dev
    if (src == ~mellotron, {
        // Turn off the synth and free the memory for device 2
        ~notes[nn].set(\gate, 0);
        ~notes[nn] = nil;
    });
});

)

MIDIIn.free;