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.

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