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.

214 lines
6.3KB

  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 "JackException.h"
  23. #include <assert.h>
  24. using namespace std;
  25. namespace Jack
  26. {
  27. JackMidiDriver::JackMidiDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  28. : JackDriver(name, alias, engine, table)
  29. {}
  30. JackMidiDriver::~JackMidiDriver()
  31. {}
  32. int JackMidiDriver::Open(bool capturing,
  33. bool playing,
  34. int inchannels,
  35. int outchannels,
  36. bool monitor,
  37. const char* capture_driver_name,
  38. const char* playback_driver_name,
  39. jack_nframes_t capture_latency,
  40. jack_nframes_t playback_latency)
  41. {
  42. fCaptureChannels = inchannels;
  43. fPlaybackChannels = outchannels;
  44. return JackDriver::Open(0, 0, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  45. }
  46. int JackMidiDriver::Attach()
  47. {
  48. JackPort* port;
  49. jack_port_id_t port_index;
  50. char name[REAL_JACK_PORT_NAME_SIZE+1];
  51. char alias[REAL_JACK_PORT_NAME_SIZE+1];
  52. int i;
  53. jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  54. for (i = 0; i < fCaptureChannels; i++) {
  55. snprintf(alias, sizeof(alias), "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  56. snprintf(name, sizeof(name), "%s:capture_%d", fClientControl.fName, i + 1);
  57. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  58. jack_error("driver: cannot register port for %s", name);
  59. return -1;
  60. }
  61. port = fGraphManager->GetPort(port_index);
  62. port->SetAlias(alias);
  63. fCapturePortList[i] = port_index;
  64. jack_log("JackMidiDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  65. }
  66. for (i = 0; i < fPlaybackChannels; i++) {
  67. snprintf(alias, sizeof(alias), "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  68. snprintf(name, sizeof(name), "%s:playback_%d", fClientControl.fName, i + 1);
  69. if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
  70. jack_error("driver: cannot register port for %s", name);
  71. return -1;
  72. }
  73. port = fGraphManager->GetPort(port_index);
  74. port->SetAlias(alias);
  75. fPlaybackPortList[i] = port_index;
  76. jack_log("JackMidiDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  77. }
  78. UpdateLatencies();
  79. return 0;
  80. }
  81. int JackMidiDriver::Detach()
  82. {
  83. int i;
  84. jack_log("JackMidiDriver::Detach");
  85. for (i = 0; i < fCaptureChannels; i++) {
  86. fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
  87. }
  88. for (i = 0; i < fPlaybackChannels; i++) {
  89. fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
  90. }
  91. return 0;
  92. }
  93. void JackMidiDriver::UpdateLatencies()
  94. {
  95. jack_latency_range_t range;
  96. for (int i = 0; i < fCaptureChannels; i++) {
  97. range.max = range.min = fEngineControl->fBufferSize;
  98. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &range);
  99. }
  100. for (int i = 0; i < fPlaybackChannels; i++) {
  101. if (! fEngineControl->fSyncMode) {
  102. range.max = range.min = fEngineControl->fBufferSize * 2;
  103. }
  104. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &range);
  105. }
  106. }
  107. int JackMidiDriver::SetBufferSize(jack_nframes_t buffer_size)
  108. {
  109. UpdateLatencies();
  110. return 0;
  111. }
  112. int JackMidiDriver::ProcessReadSync()
  113. {
  114. int res = 0;
  115. // Read input buffers for the current cycle
  116. if (Read() < 0) {
  117. jack_error("JackMidiDriver::ProcessReadSync: read error");
  118. res = -1;
  119. }
  120. if (ResumeRefNum() < 0) {
  121. jack_error("JackMidiDriver::ProcessReadSync: ResumeRefNum error");
  122. res = -1;
  123. }
  124. return res;
  125. }
  126. int JackMidiDriver::ProcessWriteSync()
  127. {
  128. int res = 0;
  129. if (SuspendRefNum() < 0) {
  130. jack_error("JackMidiDriver::ProcessWriteSync: SuspendRefNum error");
  131. res = -1;
  132. }
  133. // Write output buffers from the current cycle
  134. if (Write() < 0) {
  135. jack_error("JackMidiDriver::ProcessWriteSync: write error");
  136. res = -1;
  137. }
  138. return res;
  139. }
  140. int JackMidiDriver::ProcessReadAsync()
  141. {
  142. int res = 0;
  143. // Read input buffers for the current cycle
  144. if (Read() < 0) {
  145. jack_error("JackMidiDriver::ProcessReadAsync: read error");
  146. res = -1;
  147. }
  148. // Write output buffers from the previous cycle
  149. if (Write() < 0) {
  150. jack_error("JackMidiDriver::ProcessReadAsync: write error");
  151. res = -1;
  152. }
  153. if (ResumeRefNum() < 0) {
  154. jack_error("JackMidiDriver::ProcessReadAsync: ResumeRefNum error");
  155. res = -1;
  156. }
  157. return res;
  158. }
  159. int JackMidiDriver::ProcessWriteAsync()
  160. {
  161. return 0;
  162. }
  163. JackMidiBuffer* JackMidiDriver::GetInputBuffer(int port_index)
  164. {
  165. assert(fCapturePortList[port_index]);
  166. return (JackMidiBuffer*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  167. }
  168. JackMidiBuffer* JackMidiDriver::GetOutputBuffer(int port_index)
  169. {
  170. assert(fPlaybackPortList[port_index]);
  171. return (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  172. }
  173. } // end of namespace