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.

147 lines
4.9KB

  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. if (!adapter->fAudioAdapter->IsRunning())
  32. return 0;
  33. float* inputBuffer[adapter->fAudioAdapter->GetInputs()];
  34. float* outputBuffer[adapter->fAudioAdapter->GetOutputs()];
  35. for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
  36. inputBuffer[i] = (float*)jack_port_get_buffer(adapter->fCapturePortList[i], frames);
  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()
  60. {
  61. // When called, Close has already been used for the client, thus ports are already unregistered.
  62. delete fAudioAdapter;
  63. }
  64. void JackAudioAdapter::FreePorts()
  65. {
  66. for (int i = 0; i < fAudioAdapter->GetInputs(); i++ )
  67. if ( fCapturePortList[i] )
  68. jack_port_unregister ( fJackClient, fCapturePortList[i] );
  69. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++ )
  70. if ( fPlaybackPortList[i] )
  71. jack_port_unregister ( fJackClient, fPlaybackPortList[i] );
  72. delete[] fCapturePortList;
  73. delete[] fPlaybackPortList;
  74. }
  75. void JackAudioAdapter::Reset()
  76. {
  77. fAudioAdapter->Reset();
  78. }
  79. int JackAudioAdapter::Open()
  80. {
  81. char name[32];
  82. jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
  83. fAudioAdapter->Create();
  84. //jack ports
  85. fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
  86. fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
  87. for (int i = 0; i < fAudioAdapter->GetInputs(); i++)
  88. {
  89. sprintf(name, "capture_%d", i + 1);
  90. if ((fCapturePortList[i] = jack_port_register(fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == NULL)
  91. goto fail;
  92. }
  93. for (int i = 0; i < fAudioAdapter->GetOutputs(); i++)
  94. {
  95. sprintf(name, "playback_%d", i + 1);
  96. if ((fPlaybackPortList[i] = jack_port_register(fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 )) == NULL)
  97. goto fail;
  98. }
  99. //callbacks and activation
  100. if ( jack_set_process_callback ( fJackClient, Process, this ) < 0 )
  101. goto fail;
  102. if ( jack_set_buffer_size_callback ( fJackClient, BufferSize, this ) < 0 )
  103. goto fail;
  104. if ( jack_set_sample_rate_callback ( fJackClient, SampleRate, this ) < 0 )
  105. goto fail;
  106. if ( jack_activate ( fJackClient ) < 0 )
  107. goto fail;
  108. // Ring buffer are now allocated..
  109. return fAudioAdapter->Open();
  110. fail:
  111. FreePorts();
  112. fAudioAdapter->Destroy();
  113. return -1;
  114. }
  115. int JackAudioAdapter::Close()
  116. {
  117. fAudioAdapter->Close();
  118. fAudioAdapter->Destroy();
  119. return 0;
  120. }
  121. } //namespace