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.

313 lines
11KB

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