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.

145 lines
4.1KB

  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 "JackCallbackNetIOAdapter.h"
  16. #include "JackLibSampleRateResampler.h"
  17. #include "JackError.h"
  18. #include "JackExports.h"
  19. #include <stdio.h>
  20. #include <assert.h>
  21. using namespace std;
  22. namespace Jack
  23. {
  24. int JackCallbackNetIOAdapter::Process(jack_nframes_t frames, void* arg)
  25. {
  26. JackCallbackNetIOAdapter* adapter = static_cast<JackCallbackNetIOAdapter*>(arg);
  27. float* buffer;
  28. bool failure = false;
  29. int i;
  30. if (!adapter->fIOAdapter->IsRunning())
  31. return 0;
  32. // DLL
  33. adapter->fIOAdapter->SetCallbackTime(jack_get_time());
  34. // Push/pull from ringbuffer
  35. for (i = 0; i < adapter->fCaptureChannels; i++) {
  36. buffer = static_cast<float*>(jack_port_get_buffer(adapter->fCapturePortList[i], frames));
  37. if (adapter->fCaptureRingBuffer[i]->Read(buffer, frames) == 0)
  38. failure = true;
  39. }
  40. for (i = 0; i < adapter->fPlaybackChannels; i++) {
  41. buffer = static_cast<float*>(jack_port_get_buffer(adapter->fPlaybackPortList[i], frames));
  42. if (adapter->fPlaybackRingBuffer[i]->Write(buffer, frames) == 0)
  43. failure = true;
  44. }
  45. // Reset all ringbuffers in case of failure
  46. if (failure) {
  47. jack_error("JackCallbackNetIOAdapter::Process ringbuffer failure... reset");
  48. adapter->Reset();
  49. }
  50. return 0;
  51. }
  52. int JackCallbackNetIOAdapter::BufferSize(jack_nframes_t nframes, void* arg)
  53. {
  54. JackCallbackNetIOAdapter* adapter = static_cast<JackCallbackNetIOAdapter*>(arg);
  55. adapter->Reset();
  56. adapter->fIOAdapter->SetBufferSize(nframes);
  57. return 0;
  58. }
  59. void JackCallbackNetIOAdapter::Reset()
  60. {
  61. int i;
  62. for (i = 0; i < fCaptureChannels; i++) {
  63. fCaptureRingBuffer[i]->Reset();
  64. }
  65. for (i = 0; i < fPlaybackChannels; i++) {
  66. fPlaybackRingBuffer[i]->Reset();
  67. }
  68. fIOAdapter->Reset();
  69. }
  70. JackCallbackNetIOAdapter::JackCallbackNetIOAdapter(jack_client_t* jack_client,
  71. JackIOAdapterInterface* audio_io,
  72. int input,
  73. int output)
  74. : JackNetIOAdapter(jack_client, audio_io, input, output)
  75. {
  76. int i;
  77. fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
  78. fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
  79. for (i = 0; i < fCaptureChannels; i++) {
  80. fCaptureRingBuffer[i] = new JackLibSampleRateResampler();
  81. }
  82. for (i = 0; i < fPlaybackChannels; i++) {
  83. fPlaybackRingBuffer[i] = new JackLibSampleRateResampler();
  84. }
  85. fIOAdapter->SetRingBuffers(fCaptureRingBuffer, fPlaybackRingBuffer);
  86. jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
  87. jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
  88. if (jack_set_process_callback(fJackClient, Process, this) < 0)
  89. goto fail;
  90. if (jack_set_buffer_size_callback(fJackClient, BufferSize, this) < 0)
  91. goto fail;
  92. if (jack_activate(fJackClient) < 0)
  93. goto fail;
  94. return;
  95. fail:
  96. FreePorts();
  97. }
  98. JackCallbackNetIOAdapter::~JackCallbackNetIOAdapter()
  99. {
  100. int i;
  101. for (i = 0; i < fCaptureChannels; i++) {
  102. delete(fCaptureRingBuffer[i]);
  103. }
  104. for (i = 0; i < fPlaybackChannels; i++) {
  105. delete(fPlaybackRingBuffer[i]);
  106. }
  107. delete[] fCaptureRingBuffer;
  108. delete[] fPlaybackRingBuffer;
  109. }
  110. } //namespace