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.

110 lines
3.5KB

  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 "JackError.h"
  17. #include "JackExports.h"
  18. #include <stdio.h>
  19. #include <assert.h>
  20. using namespace std;
  21. namespace Jack
  22. {
  23. #define DEFAULT_RB_SIZE 16384
  24. int JackCallbackNetIOAdapter::Process(jack_nframes_t frames, void* arg)
  25. {
  26. JackCallbackNetIOAdapter* adapter = static_cast<JackCallbackNetIOAdapter*>(arg);
  27. char* buffer;
  28. int i;
  29. if (!adapter->fIOAdapter->IsRunning())
  30. return 0;
  31. for (i = 0; i < adapter->fCaptureChannels; i++) {
  32. buffer = static_cast<char*>(jack_port_get_buffer(adapter->fCapturePortList[i], frames));
  33. size_t len = jack_ringbuffer_read_space(adapter->fCaptureRingBuffer);
  34. if (len < frames * sizeof(float)) {
  35. jack_error("JackCallbackNetIOAdapter::Process : consumer too slow, skip frames = %d", (frames * sizeof(float)) - len);
  36. jack_ringbuffer_read(adapter->fCaptureRingBuffer, buffer, len);
  37. } else {
  38. jack_ringbuffer_read(adapter->fCaptureRingBuffer, buffer, frames * sizeof(float));
  39. }
  40. }
  41. for (i = 0; i < adapter->fPlaybackChannels; i++) {
  42. buffer = static_cast<char*>(jack_port_get_buffer(adapter->fPlaybackPortList[i], frames));
  43. size_t len = jack_ringbuffer_write_space(adapter->fPlaybackRingBuffer);
  44. if (len < frames * sizeof(float)) {
  45. jack_error("JackCallbackNetIOAdapter::Process : producer too slow, missing frames = %d", (frames * sizeof(float)) - len);
  46. jack_ringbuffer_write(adapter->fPlaybackRingBuffer, buffer, len);
  47. } else {
  48. jack_ringbuffer_write(adapter->fPlaybackRingBuffer, buffer, frames * sizeof(float));
  49. }
  50. }
  51. return 0;
  52. }
  53. JackCallbackNetIOAdapter::JackCallbackNetIOAdapter(jack_client_t* jack_client,
  54. JackIOAdapterInterface* audio_io,
  55. int input,
  56. int output)
  57. : JackNetIOAdapter(jack_client, audio_io, input, output)
  58. {
  59. fCaptureRingBuffer = jack_ringbuffer_create(fCaptureChannels * sizeof(float) * DEFAULT_RB_SIZE);
  60. if (fCaptureRingBuffer == NULL)
  61. goto fail;
  62. fPlaybackRingBuffer = jack_ringbuffer_create(fPlaybackChannels * sizeof(float) * DEFAULT_RB_SIZE);
  63. if (fPlaybackRingBuffer == NULL)
  64. goto fail;
  65. fIOAdapter->SetRingBuffers(fCaptureRingBuffer, fPlaybackRingBuffer);
  66. if (jack_set_process_callback(fJackClient, Process, this) < 0)
  67. goto fail;
  68. if (jack_activate(fJackClient) < 0)
  69. goto fail;
  70. return;
  71. fail:
  72. FreePorts();
  73. }
  74. JackCallbackNetIOAdapter::~JackCallbackNetIOAdapter()
  75. {
  76. if (fCaptureRingBuffer)
  77. jack_ringbuffer_free(fCaptureRingBuffer);
  78. if (fPlaybackRingBuffer)
  79. jack_ringbuffer_free(fPlaybackRingBuffer);
  80. }
  81. } //namespace