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.

259 lines
8.0KB

  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. jack_latency_range_t latency_range;
  66. jack_nframes_t latency = fEngineControl->fBufferSize;
  67. int i;
  68. jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  69. latency_range.max = latency_range.min = latency;
  70. for (i = 0; i < fCaptureChannels; i++) {
  71. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  72. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  73. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
  74. jack_error("driver: cannot register port for %s", name);
  75. return -1;
  76. }
  77. port = fGraphManager->GetPort(port_index);
  78. port->SetAlias(alias);
  79. port->SetLatencyRange(JackCaptureLatency, &latency_range);
  80. fCapturePortList[i] = port_index;
  81. jack_log("JackMidiDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  82. }
  83. if (!fEngineControl->fSyncMode) {
  84. latency += fEngineControl->fBufferSize;;
  85. latency_range.max = latency_range.min = latency;
  86. }
  87. for (i = 0; i < fPlaybackChannels; i++) {
  88. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  89. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  90. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
  91. jack_error("driver: cannot register port for %s", name);
  92. return -1;
  93. }
  94. port = fGraphManager->GetPort(port_index);
  95. port->SetAlias(alias);
  96. port->SetLatencyRange(JackPlaybackLatency, &latency_range);
  97. fPlaybackPortList[i] = port_index;
  98. jack_log("JackMidiDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  99. }
  100. return 0;
  101. }
  102. int JackMidiDriver::Detach()
  103. {
  104. int i;
  105. jack_log("JackMidiDriver::Detach");
  106. for (i = 0; i < fCaptureChannels; i++) {
  107. fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
  108. }
  109. for (i = 0; i < fPlaybackChannels; i++) {
  110. fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
  111. }
  112. return 0;
  113. }
  114. int JackMidiDriver::Read()
  115. {
  116. return 0;
  117. }
  118. int JackMidiDriver::Write()
  119. {
  120. return 0;
  121. }
  122. int JackMidiDriver::ProcessNull()
  123. {
  124. return 0;
  125. }
  126. /*
  127. int JackMidiDriver::Process()
  128. {
  129. if (Read() < 0) {
  130. jack_error("JackMidiDriver::Process: read error, skip cycle");
  131. return 0; // Skip cycle, but continue processing...
  132. }
  133. if (fEngineControl->fSyncMode) {
  134. int res = 0;
  135. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0) {
  136. jack_error("JackMidiDriver::Process - ResumeRefNum error");
  137. res = -1;
  138. }
  139. if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable,
  140. DRIVER_TIMEOUT_FACTOR *
  141. fEngineControl->fTimeOutUsecs) < 0) {
  142. jack_error("JackMidiDriver::Process - SuspendRefNum error");
  143. res = -1;
  144. }
  145. if (Write() < 0) {
  146. jack_error("JackMidiDriver::Process - Write error");
  147. }
  148. return res;
  149. }
  150. // Not in sync mode
  151. if (Write() < 0) {
  152. jack_error("JackMidiDriver::Process - Write error");
  153. } else {
  154. fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
  155. }
  156. return 0;
  157. }
  158. */
  159. int JackMidiDriver::Process()
  160. {
  161. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  162. }
  163. int JackMidiDriver::ProcessSync()
  164. {
  165. int res = 0;
  166. // Read input buffers for the current cycle
  167. if (Read() < 0) {
  168. jack_error("JackMidiDriver::ProcessSync: read error, skip cycle");
  169. return 0; // Skip cycle, but continue processing...
  170. }
  171. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0) {
  172. jack_error("JackMidiDriver::ProcessSync - ResumeRefNum error");
  173. res = -1;
  174. }
  175. if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable,
  176. DRIVER_TIMEOUT_FACTOR *
  177. fEngineControl->fTimeOutUsecs) < 0) {
  178. jack_error("JackMidiDriver::ProcessSync - SuspendRefNum error");
  179. res = -1;
  180. }
  181. // Write output buffers from the current cycle
  182. if (Write() < 0) {
  183. jack_error("JackMidiDriver::ProcessSync - Write error");
  184. }
  185. return res;
  186. }
  187. int JackMidiDriver::ProcessAsync()
  188. {
  189. int res = 0;
  190. // Read input buffers for the current cycle
  191. if (Read() < 0) {
  192. jack_error("JackMidiDriver::ProcessAsync: read error, skip cycle");
  193. return 0; // Skip cycle, but continue processing...
  194. }
  195. // Write output buffers from the previous cycle
  196. if (Write() < 0) {
  197. jack_error("JackMidiDriver::ProcessAsync - Write error");
  198. }
  199. if (fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable) < 0) {
  200. jack_error("JackMidiDriver::ProcessAsync - ResumeRefNum error");
  201. res = -1;
  202. }
  203. return res;
  204. }
  205. JackMidiBuffer* JackMidiDriver::GetInputBuffer(int port_index)
  206. {
  207. assert(fCapturePortList[port_index]);
  208. return (JackMidiBuffer*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  209. }
  210. JackMidiBuffer* JackMidiDriver::GetOutputBuffer(int port_index)
  211. {
  212. assert(fPlaybackPortList[port_index]);
  213. return (JackMidiBuffer*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  214. }
  215. } // end of namespace