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.

190 lines
6.2KB

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