PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sysex-codec.md 2.6KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # SysEx Encoding & Decoding
  2. There are various ways of encoding & decoding arbitrary 8-bit wide data into
  3. SysEx, which is 7-bit wide.
  4. The [official documentation](http://www.somascape.org/midi/tech/spec.html#nusx_fd)
  5. for FileDump data exchanges states the following:
  6. > The 8-bit file data needs to be converted to 7-bit form,
  7. > with the result that every 7 bytes of file data translates
  8. > to 8 bytes in the MIDI stream.
  9. >
  10. > For each group of 7 bytes (of file data) the top bit from each
  11. > is used to construct an eigth byte, which is sent first.
  12. > So:
  13. > ```
  14. > AAAAaaaa BBBBbbbb CCCCcccc DDDDdddd EEEEeeee FFFFffff GGGGgggg
  15. > ```
  16. > becomes:
  17. > ```
  18. > 0ABCDEFG 0AAAaaaa 0BBBbbbb 0CCCcccc 0DDDdddd 0EEEeeee 0FFFffff 0GGGgggg
  19. > ```
  20. >
  21. > The final group may have less than 7 bytes, and is coded as follows
  22. > (e.g. with 3 bytes in the final group):
  23. > ```
  24. > 0ABC0000 0AAAaaaa 0BBBbbbb 0CCCcccc
  25. > ```
  26. ## SysEx encoding / decoding functions
  27. The MIDI library supplies two functions to do this, `encodeSysEx` and `decodeSysEx`.
  28. Example usage:
  29. ```c++
  30. #include <MIDI.h>
  31. static const byte myData[12] = {
  32. // Hex dump: CAFEBABE BAADF00D FACADE42
  33. 0xca, 0xfe, 0xba, 0xbe, 0xba, 0xad, 0xf0, 0x0d,
  34. 0xfa, 0xca, 0xde, 0x42
  35. };
  36. byte encoded[16];
  37. const unsigned encodedSize = midi::encodeSysEx(myData, encoded, 12);
  38. // Encoded hex dump: 07 4a 7e 3a 3e 3a 2d 70 07 0d 7a 4a 5e 42
  39. byte decoded[12];
  40. const unsigned decoded = midi::decodeSysEx(encoded, decoded, encodedSize);
  41. ```
  42. ## Special case for Korg devices
  43. Korg apparently uses another convention for their SysEx encoding / decoding,
  44. where:
  45. ```
  46. AAAAaaaa BBBBbbbb CCCCcccc DDDDdddd EEEEeeee FFFFffff GGGGgggg
  47. ```
  48. becomes:
  49. ```
  50. 0GFEDCBA 0AAAaaaa 0BBBbbbb 0CCCcccc 0DDDdddd 0EEEeeee 0FFFffff 0GGGgggg
  51. ```
  52. The order of the bits in the "header" byte is reversed.
  53. To follow this beheaviour, set the inFlipHeaderBits argument to true.
  54. Example:
  55. ```c++
  56. void handleSysEx(byte* inData, unsigned inSize)
  57. {
  58. // SysEx body data starts at 3rd byte: F0 42 aa bb cc dd F7
  59. // 42 being the hex value of the Korg SysEx ID.
  60. const unsigned dataStartOffset = 2;
  61. const unsigned encodedDataLength = inSize - 3; // Remove F0 42 & F7
  62. // Create a large enough buffer where to decode the message
  63. byte decodedData[64];
  64. const unsigned decodedSize = decodeSysEx(inData + dataStartOffset,
  65. decodedData,
  66. encodedDataLength,
  67. true); // flip header bits
  68. // Do stuff with your message
  69. }
  70. ```
  71. See original discussion in issue [#92](FortySevenEffects/arduino_midi_library#92).