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.

151 lines
4.9KB

  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. mix_index = 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. if (left <= JackMidiEvent::INLINE_SIZE_MAX)
  39. return JackMidiEvent::INLINE_SIZE_MAX;
  40. return left;
  41. }
  42. SERVER_EXPORT 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. /* Since port buffer has actually always BUFFER_SIZE_MAX frames, we can safely use all the size */
  63. midi->buffer_size = BUFFER_SIZE_MAX * sizeof(jack_default_audio_sample_t);
  64. midi->Reset(nframes);
  65. }
  66. /*
  67. * The mixdown function below, is a simplest (read slowest) implementation possible.
  68. * But, since it is unlikely that it will mix many buffers with many events,
  69. * it should perform quite good.
  70. * More efficient (and possibly, fastest possible) implementation (it exists),
  71. * using calendar queue algorithm is about 3 times bigger, and uses alloca().
  72. * So, let's listen to D.Knuth about premature optimisation, a leave the current
  73. * implementation as is, until it is proved to be a bottleneck.
  74. * Dmitry Baikov.
  75. */
  76. static void MidiBufferMixdown(void* mixbuffer, void** src_buffers, int src_count, jack_nframes_t nframes)
  77. {
  78. JackMidiBuffer* mix = static_cast<JackMidiBuffer*>(mixbuffer);
  79. if (!mix->IsValid()) {
  80. jack_error("Jack::MidiBufferMixdown - invalid mix buffer");
  81. return;
  82. }
  83. mix->Reset(nframes);
  84. int event_count = 0;
  85. for (int i = 0; i < src_count; ++i) {
  86. JackMidiBuffer* buf = static_cast<JackMidiBuffer*>(src_buffers[i]);
  87. if (!buf->IsValid()) {
  88. jack_error("Jack::MidiBufferMixdown - invalid source buffer");
  89. return;
  90. }
  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. static size_t MidiBufferSize()
  121. {
  122. return BUFFER_SIZE_MAX * sizeof(jack_default_audio_sample_t);
  123. }
  124. const JackPortType gMidiPortType =
  125. {
  126. JACK_DEFAULT_MIDI_TYPE,
  127. MidiBufferSize,
  128. MidiBufferInit,
  129. MidiBufferMixdown
  130. };
  131. } // namespace Jack