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.

142 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 General Public License as published by
  6. the Free Software Foundation; either version 2 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. 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. mix_index = 0;
  31. }
  32. 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. if (left <= JackMidiEvent::INLINE_SIZE_MAX)
  39. return JackMidiEvent::INLINE_SIZE_MAX;
  40. return left;
  41. }
  42. jack_midi_data_t* JackMidiBuffer::ReserveEvent(jack_nframes_t time, jack_shmsize_t size)
  43. {
  44. jack_shmsize_t space = MaxEventSize();
  45. if (space == 0 || size > space) {
  46. lost_events++;
  47. return 0;
  48. }
  49. JackMidiEvent* event = &events[event_count++];
  50. event->time = time;
  51. event->size = size;
  52. if (size <= JackMidiEvent::INLINE_SIZE_MAX)
  53. return event->data;
  54. write_pos += size;
  55. event->offset = buffer_size - write_pos;
  56. return (jack_midi_data_t*)this + event->offset;
  57. }
  58. static void MidiBufferInit(void* buffer, size_t buffer_size, jack_nframes_t nframes)
  59. {
  60. JackMidiBuffer* midi = (JackMidiBuffer*)buffer;
  61. midi->magic = JackMidiBuffer::MAGIC;
  62. midi->buffer_size = buffer_size;
  63. midi->Reset(nframes);
  64. }
  65. /*
  66. * The mixdown function below, is a simplest (read slowest) implementation possible.
  67. * But, since it is unlikely that it will mix many buffers with many events,
  68. * it should perform quite good.
  69. * More efficient (and possibly, fastest possible) implementation (it exists),
  70. * using calendar queue algorithm is about 3 times bigger, and uses alloca().
  71. * So, let's listen to D.Knuth about premature optimisation, a leave the current
  72. * implementation as is, until it is proved to be a bottleneck.
  73. * Dmitry Baikov.
  74. */
  75. static void MidiBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
  76. {
  77. JackMidiBuffer* mix = static_cast<JackMidiBuffer*>(mixbuffer);
  78. if (!mix->IsValid()) {
  79. jack_error("MIDI: invalid mix buffer");
  80. return;
  81. }
  82. mix->Reset(nframes);
  83. int event_count = 0;
  84. for (int i = 0; i < src_count; ++i) {
  85. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  86. if (!buf->IsValid())
  87. return;
  88. buf->mix_index = 0;
  89. event_count += buf->event_count;
  90. mix->lost_events += buf->lost_events;
  91. }
  92. int events_done;
  93. for (events_done = 0; events_done < event_count; ++events_done) {
  94. JackMidiBuffer* next_buf = 0;
  95. JackMidiEvent* next_event = 0;
  96. // find the earliest event
  97. for (int i = 0; i < src_count; ++i) {
  98. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  99. if (buf->mix_index >= buf->event_count)
  100. continue;
  101. JackMidiEvent* e = &buf->events[buf->mix_index];
  102. if (!next_event || e->time < next_event->time) {
  103. next_event = e;
  104. next_buf = buf;
  105. }
  106. }
  107. assert(next_event != 0);
  108. // write the event
  109. jack_midi_data_t* dest = mix->ReserveEvent(next_event->time, next_event->size);
  110. if (!dest)
  111. break;
  112. memcpy(dest, next_event->GetData(next_buf), next_event->size);
  113. next_buf->mix_index++;
  114. }
  115. mix->lost_events += event_count - events_done;
  116. }
  117. const JackPortType gMidiPortType =
  118. {
  119. JACK_DEFAULT_MIDI_TYPE,
  120. MidiBufferInit,
  121. MidiBufferMixdown
  122. };
  123. } // namespace Jack