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.

267 lines
9.9KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. (at your option) any later version.
  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. int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  39. {
  40. fEngineControl->fBufferSize = buffer_size;
  41. fGraphManager->SetBufferSize(buffer_size);
  42. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  43. if (!fEngineControl->fTimeOut)
  44. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  45. return 0;
  46. }
  47. int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
  48. {
  49. fEngineControl->fSampleRate = sample_rate;
  50. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  51. if (!fEngineControl->fTimeOut)
  52. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  53. return 0;
  54. }
  55. int JackAudioDriver::Open(jack_nframes_t nframes,
  56. jack_nframes_t samplerate,
  57. int capturing,
  58. int playing,
  59. int inchannels,
  60. int outchannels,
  61. bool monitor,
  62. const char* capture_driver_name,
  63. const char* playback_driver_name,
  64. jack_nframes_t capture_latency,
  65. jack_nframes_t playback_latency)
  66. {
  67. fCaptureChannels = inchannels;
  68. fPlaybackChannels = outchannels;
  69. fWithMonitorPorts = monitor;
  70. return JackDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  71. }
  72. int JackAudioDriver::Attach()
  73. {
  74. JackPort* port;
  75. jack_port_id_t port_index;
  76. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  77. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  78. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  79. int i;
  80. jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  81. for (i = 0; i < fCaptureChannels; i++) {
  82. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fClientControl->fName, fCaptureDriverName, i + 1);
  83. snprintf(name, sizeof(name) - 1, "system:capture_%d", i + 1);
  84. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  85. jack_error("driver: cannot register port for %s", name);
  86. return -1;
  87. }
  88. port = fGraphManager->GetPort(port_index);
  89. port->SetAlias(alias);
  90. port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
  91. fCapturePortList[i] = port_index;
  92. jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  93. }
  94. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  95. for (i = 0; i < fPlaybackChannels; i++) {
  96. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fClientControl->fName, fPlaybackDriverName, i + 1);
  97. snprintf(name, sizeof(name) - 1, "system:playback_%d", i + 1);
  98. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  99. jack_error("driver: cannot register port for %s", name);
  100. return -1;
  101. }
  102. port = fGraphManager->GetPort(port_index);
  103. port->SetAlias(alias);
  104. // Add more latency if "async" mode is used...
  105. port->SetLatency(fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + fPlaybackLatency);
  106. fPlaybackPortList[i] = port_index;
  107. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  108. // Monitor ports
  109. if (fWithMonitorPorts) {
  110. jack_log("Create monitor port ");
  111. snprintf(name, sizeof(name) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  112. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  113. jack_error("Cannot register monitor port for %s", name);
  114. return -1;
  115. } else {
  116. port = fGraphManager->GetPort(port_index);
  117. port->SetLatency(fEngineControl->fBufferSize);
  118. fMonitorPortList[i] = port_index;
  119. }
  120. }
  121. }
  122. return 0;
  123. }
  124. int JackAudioDriver::Detach()
  125. {
  126. int i;
  127. jack_log("JackAudioDriver::Detach");
  128. for (i = 0; i < fCaptureChannels; i++) {
  129. fGraphManager->ReleasePort(fClientControl->fRefNum, fCapturePortList[i]);
  130. }
  131. for (i = 0; i < fPlaybackChannels; i++) {
  132. fGraphManager->ReleasePort(fClientControl->fRefNum, fPlaybackPortList[i]);
  133. if (fWithMonitorPorts)
  134. fGraphManager->ReleasePort(fClientControl->fRefNum, fMonitorPortList[i]);
  135. }
  136. return 0;
  137. }
  138. int JackAudioDriver::Write()
  139. {
  140. for (int i = 0; i < fPlaybackChannels; i++) {
  141. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  142. float* buffer = GetOutputBuffer(i);
  143. int size = sizeof(float) * fEngineControl->fBufferSize;
  144. // Monitor ports
  145. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  146. memcpy(GetMonitorBuffer(i), buffer, size);
  147. }
  148. }
  149. return 0;
  150. }
  151. int JackAudioDriver::Process()
  152. {
  153. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  154. }
  155. /*
  156. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  157. synchronize to the end of client graph execution.
  158. */
  159. int JackAudioDriver::ProcessAsync()
  160. {
  161. if (Read() < 0) { // Read input buffers for the current cycle
  162. jack_error("ProcessAsync: read error");
  163. return 0;
  164. }
  165. if (Write() < 0) { // Write output buffers from the previous cycle
  166. jack_error("ProcessAsync: write error");
  167. return 0;
  168. }
  169. if (fIsMaster) {
  170. if (!fEngine->Process(fLastWaitUst)) // fLastWaitUst is set in the "low level" layer
  171. jack_error("JackAudioDriver::ProcessAsync Process error");
  172. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  173. if (ProcessSlaves() < 0)
  174. jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
  175. } else {
  176. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  177. }
  178. return 0;
  179. }
  180. /*
  181. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  182. output buffers computed at the *current cycle* are used.
  183. */
  184. int JackAudioDriver::ProcessSync()
  185. {
  186. if (Read() < 0) { // Read input buffers for the current cycle
  187. jack_error("ProcessSync: read error");
  188. return 0;
  189. }
  190. if (fIsMaster) {
  191. if (fEngine->Process(fLastWaitUst)) { // fLastWaitUst is set in the "low level" layer
  192. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  193. if (ProcessSlaves() < 0)
  194. jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
  195. if (fGraphManager->SuspendRefNum(fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0)
  196. jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
  197. } else { // Graph not finished: do not activate it
  198. jack_error("ProcessSync: error");
  199. }
  200. if (Write() < 0) // Write output buffers for the current cycle
  201. jack_error("ProcessSync: write error");
  202. } else {
  203. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  204. }
  205. return 0;
  206. }
  207. void JackAudioDriver::NotifyXRun(jack_time_t callback_usecs)
  208. {
  209. fEngine->NotifyXRun(callback_usecs);
  210. }
  211. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  212. {
  213. assert(fCapturePortList[port_index]);
  214. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  215. }
  216. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  217. {
  218. assert(fPlaybackPortList[port_index]);
  219. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  220. }
  221. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  222. {
  223. assert(fPlaybackPortList[port_index]);
  224. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
  225. }
  226. } // end of namespace