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.

272 lines
9.4KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #ifdef WIN32
  17. #pragma warning (disable : 4786)
  18. #endif
  19. #include "JackAudioDriver.h"
  20. #include "JackTime.h"
  21. #include "JackError.h"
  22. #include "JackEngineControl.h"
  23. #include "JackClientControl.h"
  24. #include "JackPort.h"
  25. #include "JackGraphManager.h"
  26. #include "JackEngine.h"
  27. #include <assert.h>
  28. namespace Jack
  29. {
  30. JackAudioDriver::JackAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
  31. : JackDriver(name, engine, table),
  32. fCaptureChannels(0),
  33. fPlaybackChannels(0),
  34. fWithMonitorPorts(false)
  35. {}
  36. JackAudioDriver::~JackAudioDriver()
  37. {}
  38. // DB: This is here because audio driver is the only one, who can change buffer size.
  39. // It can be moved into JackDriver, but then it would be called twice
  40. // because of JackServer::SetBufferSize() implementation.
  41. // Initial values are set in JackDriver::Open(...). Yes it is a duplicate code (bad).
  42. int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  43. {
  44. fEngineControl->fBufferSize = buffer_size;
  45. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  46. return 0;
  47. }
  48. int JackAudioDriver::Open(jack_nframes_t nframes,
  49. jack_nframes_t samplerate,
  50. int capturing,
  51. int playing,
  52. int inchannels,
  53. int outchannels,
  54. bool monitor,
  55. const char* capture_driver_name,
  56. const char* playback_driver_name,
  57. jack_nframes_t capture_latency,
  58. jack_nframes_t playback_latency)
  59. {
  60. fCaptureChannels = inchannels;
  61. fPlaybackChannels = outchannels;
  62. fWithMonitorPorts = monitor;
  63. return JackDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  64. }
  65. int JackAudioDriver::Attach()
  66. {
  67. JackPort* port;
  68. jack_port_id_t port_index;
  69. char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  70. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  71. int i;
  72. JackLog("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  73. for (i = 0; i < fCaptureChannels; i++) {
  74. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%d", fClientControl->fName, fCaptureDriverName, i + 1);
  75. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
  76. jack_error("driver: cannot register port for %s", buf);
  77. return -1;
  78. }
  79. port = fGraphManager->GetPort(port_index);
  80. port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
  81. fCapturePortList[i] = port_index;
  82. JackLog("JackAudioDriver::Attach fCapturePortList[i] %ld = \n", port_index);
  83. }
  84. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  85. for (i = 0; i < fPlaybackChannels; i++) {
  86. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%d", fClientControl->fName, fPlaybackDriverName, i + 1);
  87. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
  88. jack_error("driver: cannot register port for %s", buf);
  89. return -1;
  90. }
  91. port = fGraphManager->GetPort(port_index);
  92. port->SetLatency(fEngineControl->fBufferSize + fPlaybackLatency);
  93. fPlaybackPortList[i] = port_index;
  94. JackLog("JackAudioDriver::Attach fPlaybackPortList[i] %ld = \n", port_index);
  95. // Monitor ports
  96. if (fWithMonitorPorts) {
  97. JackLog("Create monitor port \n");
  98. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  99. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JackPortIsOutput)) == NO_PORT) {
  100. jack_error("Cannot register monitor port for %s", buf);
  101. return -1;
  102. } else {
  103. port = fGraphManager->GetPort(port_index);
  104. port->SetLatency(fEngineControl->fBufferSize);
  105. fMonitorPortList[i] = port_index;
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. int JackAudioDriver::Detach()
  112. {
  113. int i;
  114. JackLog("JackAudioDriver::Detach\n");
  115. for (i = 0; i < fCaptureChannels; i++) {
  116. fGraphManager->ReleasePort(fClientControl->fRefNum, fCapturePortList[i]);
  117. }
  118. for (i = 0; i < fPlaybackChannels; i++) {
  119. fGraphManager->ReleasePort(fClientControl->fRefNum, fPlaybackPortList[i]);
  120. if (fWithMonitorPorts)
  121. fGraphManager->ReleasePort(fClientControl->fRefNum, fMonitorPortList[i]);
  122. }
  123. return 0;
  124. }
  125. int JackAudioDriver::Write()
  126. {
  127. for (int i = 0; i < fPlaybackChannels; i++) {
  128. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  129. float* buffer = GetOutputBuffer(i);
  130. int size = sizeof(float) * fEngineControl->fBufferSize;
  131. // Monitor ports
  132. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  133. memcpy(GetMonitorBuffer(i), buffer, size);
  134. }
  135. }
  136. return 0;
  137. }
  138. int JackAudioDriver::Process()
  139. {
  140. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  141. }
  142. /*
  143. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  144. synchronize to the end of client graph execution.
  145. */
  146. int JackAudioDriver::ProcessAsync()
  147. {
  148. if (Read() < 0) { // Read input buffers for the current cycle
  149. jack_error("ProcessAsync: read error");
  150. return 0;
  151. }
  152. if (Write() < 0) { // Write output buffers from the previous cycle
  153. jack_error("ProcessAsync: write error");
  154. return 0;
  155. }
  156. if (fIsMaster) {
  157. fEngine->Process(fLastWaitUst); // fLastWaitUst is set in the "low level" layer
  158. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  159. ProcessSlaves();
  160. } else {
  161. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  162. }
  163. return 0;
  164. }
  165. /*
  166. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  167. output buffers computed at the *current cycle* are used.
  168. */
  169. int JackAudioDriver::ProcessSync()
  170. {
  171. if (Read() < 0) { // Read input buffers for the current cycle
  172. jack_error("ProcessSync: read error");
  173. return 0;
  174. }
  175. if (fIsMaster) {
  176. if (fEngine->Process(fLastWaitUst)) { // fLastWaitUst is set in the "low level" layer
  177. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  178. if (ProcessSlaves() < 0)
  179. jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
  180. if (fGraphManager->SuspendRefNum(fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0)
  181. jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
  182. } else { // Graph not finished: do not activate it
  183. jack_error("JackAudioDriver::ProcessSync: error");
  184. }
  185. if (Write() < 0) // Write output buffers for the current cycle
  186. jack_error("Process: write error");
  187. } else {
  188. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  189. }
  190. return 0;
  191. }
  192. void JackAudioDriver::RenamePorts()
  193. {
  194. JackPort* port;
  195. char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  196. int i;
  197. for (i = 0; i < fCaptureChannels; i++) {
  198. port = fGraphManager->GetPort(fCapturePortList[i]);
  199. port->SetAlias(port->GetName());
  200. snprintf(buf, sizeof(buf) - 1, "system:capture_%d", i + 1);
  201. port->SetFullName(buf);
  202. }
  203. for (i = 0; i < fPlaybackChannels; i++) {
  204. port = fGraphManager->GetPort(fPlaybackPortList[i]);
  205. port->SetAlias(port->GetName());
  206. snprintf(buf, sizeof(buf) - 1, "system:playback_%d", i + 1);
  207. port->SetFullName(buf);
  208. }
  209. }
  210. void JackAudioDriver::NotifyXRun(jack_time_t callback_usecs)
  211. {
  212. fEngine->NotifyXRun(callback_usecs);
  213. }
  214. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  215. {
  216. assert(fCapturePortList[port_index]);
  217. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  218. }
  219. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  220. {
  221. assert(fPlaybackPortList[port_index]);
  222. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  223. }
  224. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  225. {
  226. assert(fPlaybackPortList[port_index]);
  227. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
  228. }
  229. } // end of namespace