sysex_tools.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // midi for embedded chips,
  2. // Copyright 2010 Alex Norman
  3. //
  4. // This file is part of avr-midi.
  5. //
  6. // avr-midi is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. //(at your option) any later version.
  10. //
  11. // avr-midi is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
  18. #pragma once
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include <inttypes.h>
  23. /**
  24. * @file
  25. * @brief Sysex utility functions
  26. *
  27. * These functions are for converting data to and from a "midi-safe" format,
  28. * which can be use to send data with sysex messages. Sysex messages may only
  29. * contain data where the to bit is not set.
  30. *
  31. * An "encoded" midi message is one that contains all of the data from its
  32. * original state, but does not have any of the top bits set.
  33. *
  34. * Every 7 bytes of decoded data is converted into 8 bytes of encoded data and
  35. * visa-versa. If you'd like to operate on small segments, make sure that you
  36. * encode in 7 byte increments and decode in 8 byte increments.
  37. *
  38. */
  39. /** @defgroup sysex_tools Sysex utility functions
  40. * @{
  41. */
  42. /**
  43. * @brief Compute the length of a message after it is encoded.
  44. *
  45. * @param decoded_length The length, in bytes, of the message to encode.
  46. *
  47. * @return The length, in bytes, of the message after encodeing.
  48. */
  49. uint16_t sysex_encoded_length(uint16_t decoded_length);
  50. /**
  51. * @brief Compute the length of a message after it is decoded.
  52. *
  53. * @param encoded_length The length, in bytes, of the encoded message.
  54. *
  55. * @return The length, in bytes, of the message after it is decoded.
  56. */
  57. uint16_t sysex_decoded_length(uint16_t encoded_length);
  58. /**
  59. * @brief Encode data so that it can be transmitted safely in a sysex message.
  60. *
  61. * @param encoded The output data buffer, must be at least sysex_encoded_length(length) bytes long.
  62. * @param source The input buffer of data to be encoded.
  63. * @param length The number of bytes from the input buffer to encode.
  64. *
  65. * @return number of bytes encoded.
  66. */
  67. uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, uint16_t length);
  68. /**
  69. * @brief Decode encoded data.
  70. *
  71. * @param decoded The output data buffer, must be at least sysex_decoded_length(length) bytes long.
  72. * @param source The input buffer of data to be decoded.
  73. * @param length The number of bytes from the input buffer to decode.
  74. *
  75. * @return number of bytes decoded.
  76. */
  77. uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, uint16_t length);
  78. /**@}*/
  79. #ifdef __cplusplus
  80. }
  81. #endif