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.

255 lines
7.5KB

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