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.

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