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.

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