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.

220 lines
6.6KB

  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. jack_default_audio_sample_t* inputBuffer[adapter->fAudioAdapter->GetInputs()];
  31. jack_default_audio_sample_t* outputBuffer[adapter->fAudioAdapter->GetOutputs()];
  32. // Always clear output
  33. for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
  34. inputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fCapturePortList[i], frames);
  35. memset(inputBuffer[i], 0, frames * sizeof(jack_default_audio_sample_t));
  36. }
  37. for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
  38. outputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fPlaybackPortList[i], frames);
  39. }
  40. adapter->fAudioAdapter->PullAndPush(inputBuffer, outputBuffer, frames);
  41. return 0;
  42. }
  43. int JackAudioAdapter::BufferSize(jack_nframes_t buffer_size, void* arg)
  44. {
  45. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  46. adapter->Reset();
  47. adapter->fAudioAdapter->SetHostBufferSize(buffer_size);
  48. return 0;
  49. }
  50. int JackAudioAdapter::SampleRate(jack_nframes_t sample_rate, void* arg)
  51. {
  52. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  53. adapter->Reset();
  54. adapter->fAudioAdapter->SetHostSampleRate(sample_rate);
  55. return 0;
  56. }
  57. void JackAudioAdapter::Latency(jack_latency_callback_mode_t mode, void* arg)
  58. {
  59. JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
  60. if (mode == JackCaptureLatency) {
  61. for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
  62. jack_latency_range_t range;
  63. range.min = range.max = adapter->fAudioAdapter->GetInputLatency(i);
  64. jack_port_set_latency_range(adapter->fCapturePortList[i], JackCaptureLatency, &range);
  65. }
  66. } else {
  67. for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
  68. jack_latency_range_t range;
  69. range.min = range.max = adapter->fAudioAdapter->GetOutputLatency(i);
  70. jack_port_set_latency_range(adapter->fPlaybackPortList[i], JackPlaybackLatency, &range);
  71. }
  72. }
  73. }
  74. JackAudioAdapter::JackAudioAdapter(jack_client_t* client, JackAudioAdapterInterface* audio_io, const JSList* params)
  75. :fClient(client), fAudioAdapter(audio_io)
  76. {
  77. const JSList* node;
  78. const jack_driver_param_t* param;
  79. fAutoConnect = false;
  80. for (node = params; node; node = jack_slist_next(node)) {
  81. param = (const jack_driver_param_t*)node->data;
  82. switch (param->character) {
  83. case 'c':
  84. fAutoConnect = true;
  85. break;
  86. }
  87. }
  88. }
  89. JackAudioAdapter::~JackAudioAdapter()
  90. {
  91. // When called, Close has already been used for the client, thus ports are already unregistered.
  92. delete fAudioAdapter;
  93. }
  94. void JackAudioAdapter::FreePorts()
  95. {
  96. for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
  97. if (fCapturePortList[i]) {
  98. jack_port_unregister(fClient, fCapturePortList[i]);
  99. }
  100. }
  101. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
  102. if (fPlaybackPortList[i]) {
  103. jack_port_unregister(fClient, fPlaybackPortList[i]);
  104. }
  105. }
  106. delete[] fCapturePortList;
  107. delete[] fPlaybackPortList;
  108. }
  109. void JackAudioAdapter::ConnectPorts()
  110. {
  111. const char** ports;
  112. ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  113. if (ports != NULL) {
  114. for (int i = 0; i < fAudioAdapter->GetInputs() && ports[i]; i++) {
  115. jack_connect(fClient,jack_port_name(fCapturePortList[i]), ports[i]);
  116. }
  117. jack_free(ports);
  118. }
  119. ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  120. if (ports != NULL) {
  121. for (int i = 0; i < fAudioAdapter->GetOutputs() && ports[i]; i++) {
  122. jack_connect(fClient, ports[i], jack_port_name(fPlaybackPortList[i]));
  123. }
  124. jack_free(ports);
  125. }
  126. }
  127. void JackAudioAdapter::Reset()
  128. {
  129. fAudioAdapter->Reset();
  130. }
  131. int JackAudioAdapter::Open()
  132. {
  133. char name[32];
  134. jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
  135. fAudioAdapter->Create();
  136. //jack ports
  137. fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
  138. fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
  139. for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
  140. sprintf(name, "capture_%d", i + 1);
  141. if ((fCapturePortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, 0)) == NULL) {
  142. goto fail;
  143. }
  144. }
  145. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
  146. sprintf(name, "playback_%d", i + 1);
  147. if ((fPlaybackPortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, 0)) == NULL) {
  148. goto fail;
  149. }
  150. }
  151. //callbacks and activation
  152. if (jack_set_process_callback(fClient, Process, this) < 0) {
  153. goto fail;
  154. }
  155. if (jack_set_buffer_size_callback(fClient, BufferSize, this) < 0) {
  156. goto fail;
  157. }
  158. if (jack_set_sample_rate_callback(fClient, SampleRate, this) < 0) {
  159. goto fail;
  160. }
  161. if (jack_set_latency_callback(fClient, Latency, this) < 0) {
  162. goto fail;
  163. }
  164. if (jack_activate(fClient) < 0) {
  165. goto fail;
  166. }
  167. if (fAutoConnect) {
  168. ConnectPorts();
  169. }
  170. // Ring buffers are now allocated...
  171. return fAudioAdapter->Open();
  172. return 0;
  173. fail:
  174. FreePorts();
  175. fAudioAdapter->Destroy();
  176. return -1;
  177. }
  178. int JackAudioAdapter::Close()
  179. {
  180. fAudioAdapter->Close();
  181. fAudioAdapter->Destroy();
  182. return 0;
  183. }
  184. } //namespace