summaryrefslogtreecommitdiffstats
path: root/classes/USBMIDIIsm.sc
blob: 3a7518a82f290a74bf738e138dd8a150ed4d39bc (plain)
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
131
USBMIDIIsm{

  var <devices, verbose;

  *new{
    arg setVerbose=false;
    ^super.new.init(setVerbose);
  }

  init{
    arg setVerbose;
    devices = Array();
    verbose = setVerbose;
    this.discoverDevices();
  }

  discoverDevices{
    if(MIDIClient.initialized.not,{
      postln("Initializing MIDIClient...");
      MIDIClient.init(verbose: verbose);
    },{
      if(devices.size > 0, {
        postln("Resetting list of MIDI input devices.");
        devices.do({|device,i|
          if(device.inPortNum.notNil || device.outPortNum.notNil,{
            device.cleanup();
          });
        });
        devices = Array();
      });
      postln("Restarting MIDIClient...");
      MIDIClient.disposeClient;
      MIDIClient.init(verbose: verbose);
    });

    postln("Scanning for MIDI devices...");
    //TODO: find way to select in/out-only devices
    MIDIClient.sources.do({ |inEndPoint, inPortNum|
      if((inEndPoint.device.contains("SuperCollider") ||
          inEndPoint.device.contains("Midi Through") ||
          inEndPoint.device.contains("Timer") ||
          inEndPoint.device.contains("Announce")).not, {
        // correlate with output devices
        MIDIClient.destinations.do({|outEndPoint, outPortNum|
          if((inEndPoint.device == outEndPoint.device &&
              inEndPoint.uid == outEndPoint.uid), {
            // available devices
            case
            {inEndPoint.device.contains("Platform M+")} {
              devices = devices.add(
                PlatformMPlus.new(
                  inEndPoint.device,
                  uid: inEndPoint.uid.asSymbol,
                  inPort: inPortNum,
                  outPort: outPortNum,
                  verbose: verbose
                )
              );
            }
            {inEndPoint.device.contains("nanoKONTROL2")} {
              devices = devices.add(
                NanoKontrol2.new(
                  inEndPoint.device,
                  uid: inEndPoint.uid.asSymbol,
                  inPort: inPortNum,
                  outPort: outPortNum,
                  verbose: verbose
                )
              );
            };
          });
        });
      });
    });
  }

  listDevices{
    postln("Name, uid, in, out");
    devices.do({|device|
      postln(device.name++", "++device.uid++", "++device.inPortNum++", "++device.outPortNum);
    });
  }

  listen{
    arg listen = true;
    if(listen, {
      MIDIdef.cc(\listen_cc, {arg ...args; ("control : "++args).postln});
      MIDIdef.noteOn(\listen_noteOn, {arg ...args; ("noteOn : "++args).postln});
      MIDIdef.noteOff(\listen_noteOff, {arg ...args; ("noteOff : "++args).postln});
      MIDIdef.polytouch(\listen_polytouch, {arg ...args; ("polyTouch: "++args).postln});
      MIDIdef.touch(\listen_touch, {arg ...args; ("touch: "++args).postln});
      MIDIdef.bend(\listen_bend, {arg ...args; ("bend: "++args).postln});
      MIDIdef.program(\listen_program, {arg ...args; ("program: "++args).postln});
      // TODO: find something for sysex (too noisy)
      //  MIDIdef.sysex(\listen_sysex, {arg ...args; ("sysex: "++args).postln});
      MIDIdef.smpte(\listen_smpte, {arg ...args; ("smpte: "++args).postln});
      MIDIdef.songPosition(\listen_songPosition, {arg ...args; ("songPosition: "++args).postln});
      MIDIdef.songSelect(\listen_songSelect, {arg ...args; ("songSelect: "++args).postln});
      MIDIdef.tuneRequest(\listen_tuneRequest, {arg ...args; ("tuneRequest: "++args).postln});
      MIDIdef.midiClock(\listen_midiClock, {arg ...args; ("midiClock: "++args).postln});
      MIDIdef.sysrt(\listen_sysrt, {arg ...args; ("sysrt: "++args).postln});
      MIDIdef.tick(\listen_tick, {arg ...args; ("tick: "++args).postln});
      MIDIdef.start(\listen_start, {arg ...args; ("start: "++args).postln});
      MIDIdef.stop(\listen_stop, {arg ...args; ("stop: "++args).postln});
      MIDIdef.continue(\listen_continue, {arg ...args; ("continue: "++args).postln});
      MIDIdef.reset(\listen_reset, {arg ...args; ("reset: "++args).postln});
      MIDIdef.activeSense(\listen_activeSense, {arg ...args; ("activeSense: "++args).postln});
    }, {
      MIDIdef(\listen_cc).free;
      MIDIdef(\listen_noteOn).free;
      MIDIdef(\listen_noteOff).free;
      MIDIdef(\listen_polytouch).free;
      MIDIdef(\listen_touch).free;
      MIDIdef(\listen_bend).free;
      MIDIdef(\listen_program).free;
//      MIDIdef(\listen_sysex).free;
      MIDIdef(\listen_smpte).free;
      MIDIdef(\listen_songPosition).free;
      MIDIdef(\listen_songSelect).free;
      MIDIdef(\listen_tuneRequest).free;
      MIDIdef(\listen_midiClock).free;
      MIDIdef(\listen_sysrt).free;
      MIDIdef(\listen_tick).free;
      MIDIdef(\listen_start).free;
      MIDIdef(\listen_stop).free;
      MIDIdef(\listen_continue).free;
      MIDIdef(\listen_reset).free;
      MIDIdef(\listen_activeSense).free;
    });
  }
}