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.

192 lines
6.4KB

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