djipco / webmidi

Tame the Web MIDI API. Send and receive MIDI messages with ease. Control instruments with user-friendly functions (playNote, sendPitchBend, etc.). React to MIDI input with simple event listeners (noteon, pitchbend, controlchange, etc.).
Apache License 2.0
1.54k stars 116 forks source link

channel is missing from control-change #266

Closed elmcapp closed 2 years ago

elmcapp commented 2 years ago

In older version we could get the the channel (e.channel) In current version I can not get the channel

{
  port: Input {
    eventMap: {},
    eventsSuspended: false,
    _midiInput: MIDIInput {
      type: 'input',
      id: 'IAC Driver Bus 1',
      name: 'IAC Driver Bus 1',
      manufacturer: 'Apple Inc.',
      version: '0.0',
      state: [Getter],
      connection: [Getter],
      onmidimessage: [Getter/Setter],
      onstatechange: [Getter/Setter],
      open: [Function (anonymous)],
      close: [Function (anonymous)]
    },
    _octaveOffset: 0,
    channels: [
      <1 empty item>, [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel], [InputChannel],
      [InputChannel]
    ],
    _forwarders: []
  },
  target: InputChannel {
    eventMap: {
      noteon: [Array],
      noteoff: [Array],
      controlchange: [Array],
      programchange: [Array]
    },
    eventsSuspended: false,
    _input: Input {
      eventMap: {},
      eventsSuspended: false,
      _midiInput: [MIDIInput],
      _octaveOffset: 0,
      channels: [Array],
      _forwarders: []
    },
    _number: 1,
    _octaveOffset: 0,
    _nrpnBuffer: [],
    _rpnBuffer: [],
    parameterNumberEventsEnabled: true,
    notesState: [
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false, false, false, false, false,
      false, false, false, false,
      ... 28 more items
    ]
  },
  message: Message {
    rawData: Uint8Array(3) [ 176, 90, 64 ],
    data: [ 176, 90, 64 ],
    statusByte: 176,
    rawDataBytes: Uint8Array(2) [ 90, 64 ],
    dataBytes: [ 90, 64 ],
    isChannelMessage: true,
    isSystemMessage: false,
    command: 11,
    channel: 1,
    manufacturerId: undefined,
    type: 'controlchange'
  },
  timestamp: 26005.28849899769,
  type: 'controlchange',
  data: [ 176, 90, 64 ],
  rawData: [ 176, 90, 64 ],
  statusByte: 176,
  dataBytes: [ 90, 64 ],
  controller: { number: 90, name: 'controller90' },
  subtype: 'controller90',
  value: 0.5039370078740157,
  rawValue: 64
}
djipco commented 2 years ago

This is one of the main differences between v2 and v3. In v3, channels now have their own object (InputChannel or OutputChannel). Therefore, to retrieve the channel, you must first retrieve the channel object using the target property of the event and from there you can get the channel number:

WebMidi.inputs[0].addListener("controlchange", e => {
    console.log(e.target.number);
});

By the way, the channel number is also available in the new message property of the event by using: e.message.channel.