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.

163 lines
5.2KB

  1. /*
  2. Copyright (C) 2007 Dmitry Baikov
  3. Copyright (C) 2018 Filipe Coelho
  4. Original JACK MIDI implementation Copyright (C) 2004 Ian Esten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include "JackError.h"
  18. #include "JackPortType.h"
  19. #include "JackMidiPort.h"
  20. #include <assert.h>
  21. #include <string.h>
  22. namespace Jack
  23. {
  24. SERVER_EXPORT void JackMidiBuffer::Reset(jack_nframes_t nframes)
  25. {
  26. /* This line ate 1 hour of my life... dsbaikov */
  27. this->nframes = nframes;
  28. write_pos = 0;
  29. event_count = 0;
  30. lost_events = 0;
  31. }
  32. SERVER_EXPORT jack_shmsize_t JackMidiBuffer::MaxEventSize() const
  33. {
  34. assert (((jack_shmsize_t) - 1) < 0); // jack_shmsize_t should be signed
  35. jack_shmsize_t left = buffer_size - (sizeof(JackMidiBuffer) + sizeof(JackMidiEvent) * (event_count + 1) + write_pos);
  36. if (left < 0) {
  37. return 0;
  38. }
  39. if (left <= JackMidiEvent::INLINE_SIZE_MAX) {
  40. return JackMidiEvent::INLINE_SIZE_MAX;
  41. }
  42. return left;
  43. }
  44. SERVER_EXPORT jack_midi_data_t* JackMidiBuffer::ReserveEvent(jack_nframes_t time, jack_shmsize_t size)
  45. {
  46. jack_shmsize_t space = MaxEventSize();
  47. if (space == 0 || size > space) {
  48. jack_error("JackMidiBuffer::ReserveEvent - the buffer does not have "
  49. "enough room to enqueue a %lu byte event", size);
  50. lost_events++;
  51. return 0;
  52. }
  53. JackMidiEvent* event = &events[event_count++];
  54. event->time = time;
  55. event->size = size;
  56. if (size <= JackMidiEvent::INLINE_SIZE_MAX) {
  57. return event->data;
  58. }
  59. write_pos += size;
  60. event->offset = buffer_size - write_pos;
  61. return (jack_midi_data_t*)this + event->offset;
  62. }
  63. void MidiBufferInit(void* buffer, size_t buffer_size, jack_nframes_t nframes)
  64. {
  65. JackMidiBuffer* midi = (JackMidiBuffer*)buffer;
  66. midi->magic = JackMidiBuffer::MAGIC;
  67. /* Since port buffer has actually always BUFFER_SIZE_MAX frames, we can safely use all the size */
  68. midi->buffer_size = BUFFER_SIZE_MAX * sizeof(jack_default_audio_sample_t);
  69. midi->Reset(nframes);
  70. }
  71. /*
  72. * The mixdown function below, is a simplest (read slowest) implementation possible.
  73. * But, since it is unlikely that it will mix many buffers with many events,
  74. * it should perform quite good.
  75. * More efficient (and possibly, fastest possible) implementation (it exists),
  76. * using calendar queue algorithm is about 3 times bigger, and uses alloca().
  77. * So, let's listen to D.Knuth about premature optimisation, a leave the current
  78. * implementation as is, until it is proved to be a bottleneck.
  79. * Dmitry Baikov.
  80. */
  81. static void MidiBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
  82. {
  83. JackMidiBuffer* mix = static_cast<JackMidiBuffer*>(mixbuffer);
  84. if (!mix->IsValid()) {
  85. jack_error("Jack::MidiBufferMixdown - invalid mix buffer");
  86. return;
  87. }
  88. mix->Reset(nframes);
  89. uint32_t mix_index[src_count];
  90. int event_count = 0;
  91. for (int i = 0; i < src_count; ++i) {
  92. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  93. if (!buf->IsValid()) {
  94. jack_error("Jack::MidiBufferMixdown - invalid source buffer");
  95. return;
  96. }
  97. mix_index[i] = 0;
  98. event_count += buf->event_count;
  99. mix->lost_events += buf->lost_events;
  100. }
  101. int events_done;
  102. for (events_done = 0; events_done < event_count; ++events_done) {
  103. JackMidiBuffer* next_buf = 0;
  104. JackMidiEvent* next_event = 0;
  105. uint32_t next_buf_index = 0;
  106. // find the earliest event
  107. for (int i = 0; i < src_count; ++i) {
  108. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  109. if (mix_index[i] >= buf->event_count)
  110. continue;
  111. JackMidiEvent* e = &buf->events[mix_index[i]];
  112. if (!next_event || e->time < next_event->time) {
  113. next_event = e;
  114. next_buf = buf;
  115. next_buf_index = i;
  116. }
  117. }
  118. if (next_event == 0) {
  119. jack_error("Jack::MidiBufferMixdown - got invalid next event");
  120. break;
  121. }
  122. // write the event
  123. jack_midi_data_t* dest = mix->ReserveEvent(next_event->time, next_event->size);
  124. if (!dest) break;
  125. memcpy(dest, next_event->GetData(next_buf), next_event->size);
  126. mix_index[next_buf_index]++;
  127. }
  128. mix->lost_events += event_count - events_done;
  129. }
  130. static size_t MidiBufferSize()
  131. {
  132. return BUFFER_SIZE_MAX * sizeof(jack_default_audio_sample_t);
  133. }
  134. const JackPortType gMidiPortType =
  135. {
  136. JACK_DEFAULT_MIDI_TYPE,
  137. MidiBufferSize,
  138. MidiBufferInit,
  139. MidiBufferMixdown
  140. };
  141. } // namespace Jack