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.

264 lines
9.5KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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 "JackTools.h"
  28. #include <assert.h>
  29. namespace Jack
  30. {
  31. JackAudioDriver::JackAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
  32. : JackDriver(name, 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. float new_val = 1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize; // in microsec
  44. if (JackTools::EqualFloat(2.f * fEngineControl->fPeriodUsecs, fEngineControl->fTimeOutUsecs)) // -t (timeout) was not used, see JackDriver::Open...
  45. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * new_val);
  46. fEngineControl->fPeriodUsecs = jack_time_t(new_val);
  47. return 0;
  48. }
  49. int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
  50. {
  51. fEngineControl->fSampleRate = sample_rate;
  52. float new_val = 1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize; // in microsec
  53. if (JackTools::EqualFloat(2.f * fEngineControl->fPeriodUsecs, fEngineControl->fTimeOutUsecs)) // -t (timeout) was not used, see JackDriver::Open...
  54. fEngineControl->fTimeOutUsecs = jack_time_t(2.f * new_val);
  55. fEngineControl->fPeriodUsecs = jack_time_t(new_val);
  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 buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  80. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  81. int i;
  82. JackLog("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  83. for (i = 0; i < fCaptureChannels; i++) {
  84. snprintf(buf, sizeof(buf) - 1, "%s:%s:out%d", fClientControl->fName, fCaptureDriverName, i + 1);
  85. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  86. jack_error("driver: cannot register port for %s", buf);
  87. return -1;
  88. }
  89. port = fGraphManager->GetPort(port_index);
  90. port->Rename("system:capture_%d", i + 1);
  91. port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
  92. fCapturePortList[i] = port_index;
  93. JackLog("JackAudioDriver::Attach fCapturePortList[i] %ld = \n", port_index);
  94. }
  95. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  96. for (i = 0; i < fPlaybackChannels; i++) {
  97. snprintf(buf, sizeof(buf) - 1, "%s:%s:in%d", fClientControl->fName, fPlaybackDriverName, i + 1);
  98. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  99. jack_error("driver: cannot register port for %s", buf);
  100. return -1;
  101. }
  102. port = fGraphManager->GetPort(port_index);
  103. port->Rename("system:playback_%d", i + 1);
  104. port->SetLatency(fEngineControl->fBufferSize + fPlaybackLatency);
  105. fPlaybackPortList[i] = port_index;
  106. JackLog("JackAudioDriver::Attach fPlaybackPortList[i] %ld = \n", port_index);
  107. // Monitor ports
  108. if (fWithMonitorPorts) {
  109. JackLog("Create monitor port \n");
  110. snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
  111. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput)) == NO_PORT) {
  112. jack_error("Cannot register monitor port for %s", buf);
  113. return -1;
  114. } else {
  115. port = fGraphManager->GetPort(port_index);
  116. port->SetLatency(fEngineControl->fBufferSize);
  117. fMonitorPortList[i] = port_index;
  118. }
  119. }
  120. }
  121. return 0;
  122. }
  123. int JackAudioDriver::Detach()
  124. {
  125. int i;
  126. JackLog("JackAudioDriver::Detach\n");
  127. for (i = 0; i < fCaptureChannels; i++) {
  128. fGraphManager->ReleasePort(fClientControl->fRefNum, fCapturePortList[i]);
  129. }
  130. for (i = 0; i < fPlaybackChannels; i++) {
  131. fGraphManager->ReleasePort(fClientControl->fRefNum, fPlaybackPortList[i]);
  132. if (fWithMonitorPorts)
  133. fGraphManager->ReleasePort(fClientControl->fRefNum, fMonitorPortList[i]);
  134. }
  135. return 0;
  136. }
  137. int JackAudioDriver::Write()
  138. {
  139. for (int i = 0; i < fPlaybackChannels; i++) {
  140. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  141. float* buffer = GetOutputBuffer(i);
  142. int size = sizeof(float) * fEngineControl->fBufferSize;
  143. // Monitor ports
  144. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
  145. memcpy(GetMonitorBuffer(i), buffer, size);
  146. }
  147. }
  148. return 0;
  149. }
  150. int JackAudioDriver::Process()
  151. {
  152. return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
  153. }
  154. /*
  155. The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
  156. synchronize to the end of client graph execution.
  157. */
  158. int JackAudioDriver::ProcessAsync()
  159. {
  160. if (Read() < 0) { // Read input buffers for the current cycle
  161. jack_error("ProcessAsync: read error");
  162. return 0;
  163. }
  164. if (Write() < 0) { // Write output buffers from the previous cycle
  165. jack_error("ProcessAsync: write error");
  166. return 0;
  167. }
  168. if (fIsMaster) {
  169. fEngine->Process(fLastWaitUst); // fLastWaitUst is set in the "low level" layer
  170. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  171. ProcessSlaves();
  172. } else {
  173. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  174. }
  175. return 0;
  176. }
  177. /*
  178. The driver SYNC mode: the server does synchronize to the end of client graph execution,
  179. output buffers computed at the *current cycle* are used.
  180. */
  181. int JackAudioDriver::ProcessSync()
  182. {
  183. if (Read() < 0) { // Read input buffers for the current cycle
  184. jack_error("ProcessSync: read error");
  185. return 0;
  186. }
  187. if (fIsMaster) {
  188. if (fEngine->Process(fLastWaitUst)) { // fLastWaitUst is set in the "low level" layer
  189. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  190. if (ProcessSlaves() < 0)
  191. jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
  192. if (fGraphManager->SuspendRefNum(fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0)
  193. jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
  194. } else { // Graph not finished: do not activate it
  195. jack_error("ProcessSync: error");
  196. }
  197. if (Write() < 0) // Write output buffers for the current cycle
  198. jack_error("ProcessSync: write error");
  199. } else {
  200. fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
  201. }
  202. return 0;
  203. }
  204. void JackAudioDriver::NotifyXRun(jack_time_t callback_usecs)
  205. {
  206. fEngine->NotifyXRun(callback_usecs);
  207. }
  208. jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
  209. {
  210. assert(fCapturePortList[port_index]);
  211. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
  212. }
  213. jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
  214. {
  215. assert(fPlaybackPortList[port_index]);
  216. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
  217. }
  218. jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
  219. {
  220. assert(fPlaybackPortList[port_index]);
  221. return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
  222. }
  223. } // end of namespace