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.

159 lines
5.1KB

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