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.

185 lines
6.1KB

  1. /*
  2. Copyright (C) 2009 Grame.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. (at your option) any later version.
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackSystemDeps.h"
  16. #include "JackMidiDriver.h"
  17. #include "JackTime.h"
  18. #include "JackError.h"
  19. #include "JackEngineControl.h"
  20. #include "JackPort.h"
  21. #include "JackGraphManager.h"
  22. #include "JackLockedEngine.h"
  23. #include "JackException.h"
  24. #include <assert.h>
  25. namespace Jack
  26. {
  27. JackMidiDriver::JackMidiDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  28. : JackDriver(name, alias, engine, table),
  29. fCaptureChannels(0),
  30. fPlaybackChannels(0)
  31. {
  32. for (int i = 0; i < DRIVER_PORT_NUM; i++) {
  33. fRingBuffer[i] = NULL;
  34. }
  35. }
  36. JackMidiDriver::~JackMidiDriver()
  37. {
  38. for (int i = 0; i < fCaptureChannels; i++) {
  39. if (fRingBuffer[i])
  40. jack_ringbuffer_free(fRingBuffer[i]);
  41. }
  42. }
  43. int JackMidiDriver::Open(jack_nframes_t buffer_size,
  44. jack_nframes_t samplerate,
  45. bool capturing,
  46. bool playing,
  47. int inchannels,
  48. int outchannels,
  49. bool monitor,
  50. const char* capture_driver_name,
  51. const char* playback_driver_name,
  52. jack_nframes_t capture_latency,
  53. jack_nframes_t playback_latency)
  54. {
  55. fCaptureChannels = inchannels;
  56. fPlaybackChannels = outchannels;
  57. for (int i = 0; i < fCaptureChannels; i++) {
  58. fRingBuffer[i] = jack_ringbuffer_create(sizeof(float) * BUFFER_SIZE_MAX);
  59. }
  60. return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  61. }
  62. int JackMidiDriver::Attach()
  63. {
  64. JackPort* port;
  65. jack_port_id_t port_index;
  66. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  67. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  68. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  69. int i;
  70. jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  71. for (i = 0; i < fCaptureChannels; i++) {
  72. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  73. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  74. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  75. jack_error("driver: cannot register port for %s", name);
  76. return -1;
  77. }
  78. port = fGraphManager->GetPort(port_index);
  79. port->SetAlias(alias);
  80. fCapturePortList[i] = port_index;
  81. jack_log("JackMidiDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  82. }
  83. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  84. for (i = 0; i < fPlaybackChannels; i++) {
  85. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  86. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  87. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  88. jack_error("driver: cannot register port for %s", name);
  89. return -1;
  90. }
  91. port = fGraphManager->GetPort(port_index);
  92. port->SetAlias(alias);
  93. fPlaybackPortList[i] = port_index;
  94. jack_log("JackMidiDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  95. }
  96. return 0;
  97. }
  98. int JackMidiDriver::Detach()
  99. {
  100. int i;
  101. jack_log("JackMidiDriver::Detach");
  102. for (i = 0; i < fCaptureChannels; i++) {
  103. fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
  104. }
  105. for (i = 0; i < fPlaybackChannels; i++) {
  106. fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
  107. }
  108. return 0;
  109. }
  110. int JackMidiDriver::Read()
  111. {
  112. return 0;
  113. }
  114. int JackMidiDriver::Write()
  115. {
  116. return 0;
  117. }
  118. int JackMidiDriver::ProcessNull()
  119. {
  120. return 0;
  121. }
  122. int JackMidiDriver::Process()
  123. {
  124. // Read input buffers for the current cycle
  125. if (Read() < 0) {
  126. jack_error("JackMidiDriver::Process: read error, skip cycle");
  127. return 0; // Skip cycle, but continue processing...
  128. }
  129. fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  130. if (fEngineControl->fSyncMode) {
  131. if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0) {
  132. jack_error("JackFreewheelDriver::ProcessSync SuspendRefNum error");
  133. return -1;
  134. }
  135. }
  136. // Write output buffers for the current cycle
  137. if (Write() < 0) {
  138. jack_error("JackMidiDriver::Process: write error, skip cycle");
  139. return 0; // Skip cycle, but continue processing...
  140. }
  141. return 0;
  142. }
  143. JackMidiBuffer* JackMidiDriver::GetInputBuffer(int port_index)
  144. {
  145. assert(fCapturePortList[port_index]);
  146. return (JackMidiBuffer*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  147. }
  148. JackMidiBuffer* JackMidiDriver::GetOutputBuffer(int port_index)
  149. {
  150. assert(fPlaybackPortList[port_index]);
  151. return (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  152. }
  153. } // end of namespace