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.

228 lines
6.8KB

  1. /*
  2. Copyright (C) 2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  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. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackAudioAdapter.h"
  16. #include "JackError.h"
  17. #include "JackCompilerDeps.h"
  18. #include "JackTools.h"
  19. #include "JackTime.h"
  20. #include "jslist.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. using namespace std;
  25. namespace Jack
  26. {
  27. int JackAudioAdapter::Process(jack_nframes_t frames, void* arg)
  28. {
  29. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  30. return adapter->ProcessAux(frames);
  31. }
  32. int JackAudioAdapter::ProcessAux(jack_nframes_t frames)
  33. {
  34. // Always clear output
  35. for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
  36. fInputBufferList[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(fCapturePortList[i], frames);
  37. memset(fInputBufferList[i], 0, frames * sizeof(jack_default_audio_sample_t));
  38. }
  39. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
  40. fOutputBufferList[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(fPlaybackPortList[i], frames);
  41. }
  42. fAudioAdapter->PullAndPush(fInputBufferList, fOutputBufferList, frames);
  43. return 0;
  44. }
  45. int JackAudioAdapter::BufferSize(jack_nframes_t buffer_size, void* arg)
  46. {
  47. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  48. adapter->Reset();
  49. adapter->fAudioAdapter->SetHostBufferSize(buffer_size);
  50. return 0;
  51. }
  52. int JackAudioAdapter::SampleRate(jack_nframes_t sample_rate, void* arg)
  53. {
  54. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  55. adapter->Reset();
  56. adapter->fAudioAdapter->SetHostSampleRate(sample_rate);
  57. return 0;
  58. }
  59. void JackAudioAdapter::Latency(jack_latency_callback_mode_t mode, void* arg)
  60. {
  61. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  62. if (mode == JackCaptureLatency) {
  63. for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
  64. jack_latency_range_t range;
  65. range.min = range.max = adapter->fAudioAdapter->GetInputLatency(i);
  66. jack_port_set_latency_range(adapter->fCapturePortList[i], JackCaptureLatency, &range);
  67. }
  68. } else {
  69. for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
  70. jack_latency_range_t range;
  71. range.min = range.max = adapter->fAudioAdapter->GetOutputLatency(i);
  72. jack_port_set_latency_range(adapter->fPlaybackPortList[i], JackPlaybackLatency, &range);
  73. }
  74. }
  75. }
  76. JackAudioAdapter::JackAudioAdapter(jack_client_t* client, JackAudioAdapterInterface* audio_io, const JSList* params)
  77. :fClient(client), fAudioAdapter(audio_io)
  78. {
  79. const JSList* node;
  80. const jack_driver_param_t* param;
  81. fAutoConnect = false;
  82. for (node = params; node; node = jack_slist_next(node)) {
  83. param = (const jack_driver_param_t*)node->data;
  84. switch (param->character) {
  85. case 'c':
  86. fAutoConnect = true;
  87. break;
  88. }
  89. }
  90. }
  91. JackAudioAdapter::~JackAudioAdapter()
  92. {
  93. // When called, Close has already been used for the client, thus ports are already unregistered.
  94. delete fAudioAdapter;
  95. }
  96. void JackAudioAdapter::FreePorts()
  97. {
  98. for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
  99. if (fCapturePortList[i]) {
  100. jack_port_unregister(fClient, fCapturePortList[i]);
  101. }
  102. }
  103. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
  104. if (fPlaybackPortList[i]) {
  105. jack_port_unregister(fClient, fPlaybackPortList[i]);
  106. }
  107. }
  108. delete[] fCapturePortList;
  109. delete[] fPlaybackPortList;
  110. delete[] fInputBufferList;
  111. delete[] fOutputBufferList;
  112. }
  113. void JackAudioAdapter::ConnectPorts()
  114. {
  115. const char** ports;
  116. ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  117. if (ports != NULL) {
  118. for (int i = 0; i < fAudioAdapter->GetInputs() && ports[i]; i++) {
  119. jack_connect(fClient, jack_port_name(fCapturePortList[i]), ports[i]);
  120. }
  121. jack_free(ports);
  122. }
  123. ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  124. if (ports != NULL) {
  125. for (int i = 0; i < fAudioAdapter->GetOutputs() && ports[i]; i++) {
  126. jack_connect(fClient, ports[i], jack_port_name(fPlaybackPortList[i]));
  127. }
  128. jack_free(ports);
  129. }
  130. }
  131. void JackAudioAdapter::Reset()
  132. {
  133. fAudioAdapter->Reset();
  134. }
  135. int JackAudioAdapter::Open()
  136. {
  137. char name[32];
  138. jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
  139. fAudioAdapter->Create();
  140. //jack ports
  141. fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
  142. fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
  143. fInputBufferList = new jack_default_audio_sample_t*[fAudioAdapter->GetInputs()];
  144. fOutputBufferList = new jack_default_audio_sample_t*[fAudioAdapter->GetOutputs()];
  145. for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
  146. snprintf(name, sizeof(name), "capture_%d", i + 1);
  147. if ((fCapturePortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, 0)) == NULL) {
  148. goto fail;
  149. }
  150. }
  151. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
  152. snprintf(name, sizeof(name), "playback_%d", i + 1);
  153. if ((fPlaybackPortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, 0)) == NULL) {
  154. goto fail;
  155. }
  156. }
  157. //callbacks and activation
  158. if (jack_set_process_callback(fClient, Process, this) < 0) {
  159. goto fail;
  160. }
  161. if (jack_set_buffer_size_callback(fClient, BufferSize, this) < 0) {
  162. goto fail;
  163. }
  164. if (jack_set_sample_rate_callback(fClient, SampleRate, this) < 0) {
  165. goto fail;
  166. }
  167. if (jack_set_latency_callback(fClient, Latency, this) < 0) {
  168. goto fail;
  169. }
  170. if (jack_activate(fClient) < 0) {
  171. goto fail;
  172. }
  173. if (fAutoConnect) {
  174. ConnectPorts();
  175. }
  176. // Ring buffers are now allocated...
  177. return fAudioAdapter->Open();
  178. return 0;
  179. fail:
  180. FreePorts();
  181. fAudioAdapter->Destroy();
  182. return -1;
  183. }
  184. int JackAudioAdapter::Close()
  185. {
  186. fAudioAdapter->Close();
  187. fAudioAdapter->Destroy();
  188. return 0;
  189. }
  190. } //namespace