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) 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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackError.h"
  20. #include "JackPortType.h"
  21. #include "JackMidiPort.h"
  22. #include <assert.h>
  23. #include <string.h>
  24. namespace Jack
  25. {
  26. void JackMidiBuffer::Reset(jack_nframes_t nframes)
  27. {
  28. /* This line ate 1 hour of my life... dsbaikov */
  29. this->nframes = nframes;
  30. write_pos = 0;
  31. event_count = 0;
  32. lost_events = 0;
  33. mix_index = 0;
  34. }
  35. jack_shmsize_t JackMidiBuffer::MaxEventSize() const
  36. {
  37. assert (((jack_shmsize_t) - 1) < 0); // jack_shmsize_t should be signed
  38. jack_shmsize_t left = buffer_size - (sizeof(JackMidiBuffer) + sizeof(JackMidiEvent) * (event_count + 1) + write_pos);
  39. if (left < 0)
  40. return 0;
  41. if (left <= JackMidiEvent::INLINE_SIZE_MAX)
  42. return JackMidiEvent::INLINE_SIZE_MAX;
  43. return left;
  44. }
  45. jack_midi_data_t* JackMidiBuffer::ReserveEvent(jack_nframes_t time, jack_shmsize_t size)
  46. {
  47. jack_shmsize_t space = MaxEventSize();
  48. if (space == 0 || size > space) {
  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. write_pos += size;
  58. event->offset = buffer_size - write_pos;
  59. return (jack_midi_data_t*)this + event->offset;
  60. }
  61. static void MidiBufferInit(void* buffer, size_t buffer_size, jack_nframes_t nframes)
  62. {
  63. JackMidiBuffer* midi = (JackMidiBuffer*)buffer;
  64. midi->magic = JackMidiBuffer::MAGIC;
  65. midi->buffer_size = buffer_size;
  66. midi->Reset(nframes);
  67. }
  68. /*
  69. * The mixdown function below, is a simplest (read slowest) implementation possible.
  70. * But, since it is unlikely that it will mix many buffers with many events,
  71. * it should perform quite good.
  72. * More efficient (and possibly, fastest possible) implementation (it exists),
  73. * using calendar queue algorithm is about 3 times bigger, and uses alloca().
  74. * So, let's listen to D.Knuth about premature optimisation, a leave the current
  75. * implementation as is, until it is proved to be a bottleneck.
  76. * Dmitry Baikov.
  77. */
  78. static void MidiBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
  79. {
  80. JackMidiBuffer* mix = static_cast<JackMidiBuffer*>(mixbuffer);
  81. if (!mix->IsValid()) {
  82. jack_error("MIDI: invalid mix buffer");
  83. return;
  84. }
  85. mix->Reset(nframes);
  86. int event_count = 0;
  87. for (int i = 0; i < src_count; ++i) {
  88. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  89. if (!buf->IsValid())
  90. return;
  91. buf->mix_index = 0;
  92. event_count += buf->event_count;
  93. mix->lost_events += buf->lost_events;
  94. }
  95. int events_done;
  96. for (events_done = 0; events_done < event_count; ++events_done) {
  97. JackMidiBuffer* next_buf = 0;
  98. JackMidiEvent* next_event = 0;
  99. // find the earliest event
  100. for (int i = 0; i < src_count; ++i) {
  101. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  102. if (buf->mix_index >= buf->event_count)
  103. continue;
  104. JackMidiEvent* e = &buf->events[buf->mix_index];
  105. if (!next_event || e->time < next_event->time) {
  106. next_event = e;
  107. next_buf = buf;
  108. }
  109. }
  110. assert(next_event != 0);
  111. // write the event
  112. jack_midi_data_t* dest = mix->ReserveEvent(next_event->time, next_event->size);
  113. if (!dest)
  114. break;
  115. memcpy(dest, next_event->GetData(next_buf), next_event->size);
  116. next_buf->mix_index++;
  117. }
  118. mix->lost_events += event_count - events_done;
  119. }
  120. const JackPortType gMidiPortType =
  121. {
  122. JACK_DEFAULT_MIDI_TYPE,
  123. MidiBufferInit,
  124. MidiBufferMixdown
  125. };
  126. } // namespace Jack