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.

278 lines
10KB

  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #ifdef WIN32
  20. #pragma warning (disable : 4786)
  21. #endif
  22. #include "JackAudioDriver.h"
  23. #include "JackTime.h"
  24. #include "JackError.h"
  25. #include "JackEngineControl.h"
  26. #include "JackClientControl.h"
  27. #include "JackPort.h"
  28. #include "JackGraphManager.h"
  29. #include "JackEngine.h"
  30. #include "JackException.h"
  31. #include <assert.h>
  32. namespace Jack
  33. {
  34. JackAudioDriver::JackAudioDriver(const char* name, const char* alias, JackEngineInterface* engine, JackSynchro* table)
  35. : JackDriver(name, alias, engine, table),
  36. fCaptureChannels(0),
  37. fPlaybackChannels(0),
  38. fWithMonitorPorts(false)
  39. {}
  40. JackAudioDriver::~JackAudioDriver()
  41. {}
  42. int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  43. {
  44. fEngineControl->fBufferSize = buffer_size;
  45. fGraphManager->SetBufferSize(buffer_size);
  46. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  47. if (!fEngineControl->fTimeOut)
  48. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  49. return 0;
  50. }
  51. int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
  52. {
  53. fEngineControl->fSampleRate = sample_rate;
  54. fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
  55. if (!fEngineControl->fTimeOut)
  56. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
  57. return 0;
  58. }
  59. int JackAudioDriver::Open(jack_nframes_t nframes,
  60. jack_nframes_t samplerate,
  61. int capturing,
  62. int playing,
  63. int inchannels,
  64. int outchannels,
  65. bool monitor,
  66. const char* capture_driver_name,
  67. const char* playback_driver_name,
  68. jack_nframes_t capture_latency,
  69. jack_nframes_t playback_latency)
  70. {
  71. fCaptureChannels = inchannels;
  72. fPlaybackChannels = outchannels;
  73. fWithMonitorPorts = monitor;
  74. return JackDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
  75. }
  76. int JackAudioDriver::Attach()
  77. {
  78. JackPort* port;
  79. jack_port_id_t port_index;
  80. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  81. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  82. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  83. int i;
  84. jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  85. for (i = 0; i < fCaptureChannels; i++) {
  86. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  87. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl->fName, i + 1);
  88. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  89. jack_error("driver: cannot register port for %s", name);
  90. return -1;
  91. }
  92. port = fGraphManager->GetPort(port_index);
  93. port->SetAlias(alias);
  94. port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
  95. fCapturePortList[i] = port_index;
  96. jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  97. }
  98. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  99. for (i = 0; i < fPlaybackChannels; i++) {
  100. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
  101. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl->fName, i + 1);
  102. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  103. jack_error("driver: cannot register port for %s", name);
  104. return -1;
  105. }
  106. port = fGraphManager->GetPort(port_index);
  107. port->SetAlias(alias);
  108. // Add more latency if "async" mode is used...
  109. port->SetLatency(fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + fPlaybackLatency);
  110. fPlaybackPortList[i] = port_index;
  111. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  112. // Monitor ports
  113. if (fWithMonitorPorts) {
  114. jack_log("Create monitor port ");
  115. snprintf(name, sizeof(name) - 1, "%s:%s:monitor_%u", fAliasName, fPlaybackDriverName, i + 1);
  116. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  117. jack_error("Cannot register monitor port for %s", name);
  118. return -1;
  119. } else {
  120. port = fGraphManager->GetPort(port_index);
  121. port->SetLatency(fEngineControl->fBufferSize);
  122. fMonitorPortList[i] = port_index;
  123. }
  124. }
  125. }
  126. return 0;
  127. }
  128. int JackAudioDriver::Detach()
  129. {
  130. int i;
  131. jack_log("JackAudioDriver::Detach");
  132. for (i = 0; i < fCaptureChannels; i++) {
  133. fGraphManager->ReleasePort(fClientControl->fRefNum, fCapturePortList[i]);
  134. }
  135. for (i = 0; i < fPlaybackChannels; i++) {
  136. fGraphManager->ReleasePort(fClientControl->fRefNum, fPlaybackPortList[i]);
  137. if (fWithMonitorPorts)
  138. fGraphManager->ReleasePort(fClientControl->fRefNum, fMonitorPortList[i]);
  139. }
  140. return 0;
  141. }
  142. int JackAudioDriver::Write()
  143. {
  144. for (int i = 0; i < fPlaybackChannels; i++) {
  145. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  146. float* buffer = GetOutputBuffer(i);
  147. int size = sizeof(float) * fEngineControl->fBufferSize;
  148. // Monitor ports
  149. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  150. memcpy(GetMonitorBuffer(i), buffer, size);
  151. }
  152. }
  153. return 0;
  154. }
  155. int JackAudioDriver::Process()
  156. {
  157. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  158. }
  159. /*
  160. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  161. synchronize to the end of client graph execution.
  162. */
  163. int JackAudioDriver::ProcessAsync()
  164. {
  165. // Read input buffers for the current cycle
  166. if (Read() < 0) {
  167. jack_error("ProcessAsync: read error, skip cycle");
  168. return 0; // Skip cycle, but continue processing...
  169. }
  170. // Write output buffers from the previous cycle
  171. if (Write() < 0) {
  172. jack_error("ProcessAsync: write error, skip cycle");
  173. return 0; // Skip cycle, but continue processing...
  174. }
  175. if (fIsMaster) {
  176. if (!fEngine->Process(fLastWaitUst)) // fLastWaitUst is set in the "low level" layer
  177. jack_error("JackAudioDriver::ProcessAsync Process error");
  178. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  179. if (ProcessSlaves() < 0)
  180. jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
  181. } else {
  182. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  183. }
  184. return 0;
  185. }
  186. /*
  187. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  188. output buffers computed at the *current cycle* are used.
  189. */
  190. int JackAudioDriver::ProcessSync()
  191. {
  192. // Read input buffers for the current cycle
  193. if (Read() < 0) {
  194. jack_error("ProcessSync: read error, skip cycle");
  195. return 0; // Skip cycle, but continue processing...
  196. }
  197. if (fIsMaster) {
  198. if (fEngine->Process(fLastWaitUst)) { // fLastWaitUst is set in the "low level" layer
  199. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  200. if (ProcessSlaves() < 0)
  201. jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
  202. if (fGraphManager->SuspendRefNum(fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0)
  203. jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
  204. } else { // Graph not finished: do not activate it
  205. jack_error("ProcessSync: error");
  206. }
  207. // Write output buffers for the current cycle
  208. if (Write() < 0) {
  209. jack_error("ProcessSync: write error, skip cycle");
  210. return 0; // Skip cycle, but continue processing...
  211. }
  212. } else {
  213. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  214. }
  215. return 0;
  216. }
  217. void JackAudioDriver::NotifyXRun(jack_time_t callback_usecs, float delayed_usecs)
  218. {
  219. fEngine->NotifyXRun(callback_usecs, delayed_usecs);
  220. }
  221. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  222. {
  223. assert(fCapturePortList[port_index]);
  224. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  225. }
  226. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  227. {
  228. assert(fPlaybackPortList[port_index]);
  229. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  230. }
  231. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  232. {
  233. assert(fPlaybackPortList[port_index]);
  234. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
  235. }
  236. } // end of namespace