Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: How does usbMIDI.read() works?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    Germany
    Posts
    18

    How does usbMIDI.read() works?

    Hi,

    I try to use some (lets say 3) rotary encoders with the Teensy3.2 as an midi device.

    The REs work fine on my Teensy and I can controll my DAW. Furthermore, I want to send the changed value, which I've alter in the software by mouse, back to the RE.
    I've solved for one RE by using
    Code:
    value = usbMIDI.getData2();
    myEnc.write(value);
    But how do I use it for more than one value/RE?

    In the documentation (https://www.pjrc.com/teensy/td_midi.html) is only the "usbMIDI.getChannel()" but I want to use the 1 with different controllers (e.g. 21,22,23).

    1. Basically, which kind of data is comming from getData1 and getData2?
    2. What is the differend between both getData?
    3. Does usbMIDI.getType() give me also the controller which has the "usbMIDI.ControlChange"? If yes, how do I acces the data? If no, how do I get the number of the controller which has change in the software?
    4. Will it be better to use another library like https://github.com/joshnishikawa/MIDIcontroller ?

    Many thanks!

  2. #2
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,088
    1) Standard MIDI is three bytes.
    The status byte, which tells the type and channel, as well as two data bytes. For CC messages D1 is the CC number and D2 is the value.
    2) in general D1 is the identifier and D2 the value. Note D1 / velocity D2 for example.
    3) getType returns the constant named usbMIDI.ControlChange when the message being processed by the most recent .read() is a control change.

    So you
    read()
    check that getType()==usbMIDI.ControlChange
    ... and then you use D1 to see which CC and D2 to get its value just as you have in your code fragment.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Location
    Germany
    Posts
    18
    Hi oddson,

    Thank you for your reply!

    If I've understood the code shoud be like the following, isn't it?

    Code:
    while (usbMIDI.read()) {
      if (usbMIDI.getType()==usbMIDI.ControlChange) {
        int cc = usbMIDI.getData1();
        int val = usbMIDI.getData2();
        switch (cc) {
          case 21:
            serial.print("knob 1 feedback: ");
            serial.print(val);
          break;
          case 22:
            serial.print("knob 2 feedback: ");
            serial.print(val);
          break;
          case 23:
            serial.print("knob 3 feedback: ");
            serial.print(val);
          break;
          default:
            serial.print("can't match controller number: ");
            serial.print(cc);
          break;  
        }
        serial.println();
      }
    }

  4. #4
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    67
    You also can have a callback in your setup() for every cc message as

    Code:
    usbMIDI.setHandleControlChange(handle_cc);
    Then declare the callback as

    Code:
    void handle_cc(byte channel, byte control, byte value) {
    switch (control) {
          case 21:
            serial.print("knob 1 feedback: ");
            serial.print(value);
          break;
          case 22:
            serial.print("knob 2 feedback: ");
            serial.print(value);
          break;
          case 23:
            serial.print("knob 3 feedback: ");
            serial.print(value);
          break;
          default:
            serial.print("can't match controller number: ");
            serial.print(control);
          break;  
        }
        serial.println();
    }
    And you'll get the channel, the control and its value

  5. #5
    Junior Member
    Join Date
    Mar 2019
    Location
    Germany
    Posts
    18
    Can I understand the "usbMIDI.setHandleControlChange(handle_cc);" as an Interrupt? Does everytime a controll change message is received, the "handle_cc" function will be executed?

  6. #6
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    67
    I presume that the interrupt mechanism is handled behind the scene by the midi library and that handle_cc is not an interrupt routine, but yes every time a CC is received handle_cc is executed

  7. #7
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    67
    But you have to put this statement in your loop() function

    Code:
    usbMIDI.read();
    If you don't, handle_cc will never be called

  8. #8
    Junior Member
    Join Date
    Mar 2019
    Location
    Germany
    Posts
    18
    Ok, I will give it a try and hopefully it will work like I expect.

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •