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.

306 lines
11KB

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