jack2 codebase
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.

147 lines
4.5KB

  1. /*
  2. Copyright (C) 2009 Devin Anderson
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackPhysicalMidiInput__
  16. #define __JackPhysicalMidiInput__
  17. #include "JackMidiPort.h"
  18. #include "ringbuffer.h"
  19. namespace Jack {
  20. class JackPhysicalMidiInput {
  21. private:
  22. size_t buffered_bytes;
  23. size_t expected_data_bytes;
  24. jack_ringbuffer_t *input_ring;
  25. JackMidiBuffer *port_buffer;
  26. jack_midi_data_t status_byte;
  27. size_t unbuffered_bytes;
  28. void
  29. Clear();
  30. void
  31. WriteBufferedEvent(jack_nframes_t);
  32. void
  33. WriteBufferedSysexEvent(jack_nframes_t);
  34. void
  35. WriteByteEvent(jack_nframes_t, jack_midi_data_t);
  36. protected:
  37. /**
  38. * Override to specify how to react when 1 or more bytes of a MIDI
  39. * message are lost because there wasn't enough room in the input
  40. * buffer. The first argument is the amount of bytes that couldn't be
  41. * buffered, and the second argument is the total amount of bytes in
  42. * the MIDI message. The default implementation calls 'jack_error'
  43. * with a basic error message.
  44. */
  45. virtual void
  46. HandleBufferFailure(size_t, size_t);
  47. /**
  48. * Override to specify how to react when a new status byte is received
  49. * before all of the data bytes in a message are received. The
  50. * argument is the number of bytes being discarded. The default
  51. * implementation calls 'jack_error' with a basic error message.
  52. */
  53. virtual void
  54. HandleIncompleteMessage(size_t);
  55. /**
  56. * Override to specify how to react when an invalid status byte (0xf4,
  57. * 0xf5, 0xfd) is received. The argument contains the invalid status
  58. * byte. The default implementation calls 'jack_error' with a basic
  59. * error message.
  60. */
  61. virtual void
  62. HandleInvalidStatusByte(jack_midi_data_t);
  63. /**
  64. * Override to specify how to react when a sysex end byte (0xf7) is
  65. * received without first receiving a sysex start byte (0xf0). The
  66. * argument contains the amount of bytes that will be discarded. The
  67. * default implementation calls 'jack_error' with a basic error
  68. * message.
  69. */
  70. virtual void
  71. HandleUnexpectedSysexEnd(size_t);
  72. /**
  73. * Override to specify how to react when a MIDI message can not be
  74. * written to the port buffer. The argument specifies the length of
  75. * the MIDI message. The default implementation calls 'jack_error'
  76. * with a basic error message.
  77. */
  78. virtual void
  79. HandleWriteFailure(size_t);
  80. /**
  81. * This method *must* be overridden to handle receiving MIDI bytes.
  82. * The first argument is a pointer to the memory location at which the
  83. * MIDI byte should be stored. The second argument is the last frame
  84. * at which a MIDI byte was received, except at the beginning of the
  85. * period when the value is 0. The third argument is the total number
  86. * of frames in the period. The return value is the frame at which the
  87. * MIDI byte is received at, or the value of the third argument is no
  88. * more MIDI bytes can be received in this period.
  89. */
  90. virtual jack_nframes_t
  91. Receive(jack_midi_data_t *, jack_nframes_t, jack_nframes_t) = 0;
  92. public:
  93. JackPhysicalMidiInput(size_t buffer_size=1024);
  94. ~JackPhysicalMidiInput();
  95. /**
  96. * Called to process MIDI data during a period.
  97. */
  98. void
  99. Process(jack_nframes_t);
  100. /**
  101. * Set the MIDI buffer that will receive incoming messages.
  102. */
  103. inline void
  104. SetPortBuffer(JackMidiBuffer *port_buffer)
  105. {
  106. this->port_buffer = port_buffer;
  107. }
  108. };
  109. }
  110. #endif