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.

125 lines
3.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 "JackResampler.h"
  16. #include "JackError.h"
  17. #include <stdio.h>
  18. namespace Jack
  19. {
  20. JackRingBuffer::JackRingBuffer(int size):fRingBufferSize(size)
  21. {
  22. fRingBuffer = jack_ringbuffer_create(sizeof(jack_default_audio_sample_t) * fRingBufferSize);
  23. Reset(fRingBufferSize);
  24. }
  25. JackRingBuffer::~JackRingBuffer()
  26. {
  27. if (fRingBuffer) {
  28. jack_ringbuffer_free(fRingBuffer);
  29. }
  30. }
  31. void JackRingBuffer::Reset(unsigned int new_size)
  32. {
  33. fRingBufferSize = new_size;
  34. jack_ringbuffer_reset(fRingBuffer);
  35. jack_ringbuffer_reset_size(fRingBuffer, sizeof(jack_default_audio_sample_t) * fRingBufferSize);
  36. jack_ringbuffer_read_advance(fRingBuffer, (sizeof(jack_default_audio_sample_t) * new_size/2));
  37. }
  38. unsigned int JackRingBuffer::ReadSpace()
  39. {
  40. return (jack_ringbuffer_read_space(fRingBuffer) / sizeof(jack_default_audio_sample_t));
  41. }
  42. unsigned int JackRingBuffer::WriteSpace()
  43. {
  44. return (jack_ringbuffer_write_space(fRingBuffer) / sizeof(jack_default_audio_sample_t));
  45. }
  46. unsigned int JackRingBuffer::Read(jack_default_audio_sample_t* buffer, unsigned int frames)
  47. {
  48. size_t len = jack_ringbuffer_read_space(fRingBuffer);
  49. jack_log("JackRingBuffer::Read input available = %ld", len / sizeof(jack_default_audio_sample_t));
  50. if (len < frames * sizeof(jack_default_audio_sample_t)) {
  51. jack_error("JackRingBuffer::Read : producer too slow, missing frames = %d", frames);
  52. return 0;
  53. } else {
  54. jack_ringbuffer_read(fRingBuffer, (char*)buffer, frames * sizeof(jack_default_audio_sample_t));
  55. return frames;
  56. }
  57. }
  58. unsigned int JackRingBuffer::Write(jack_default_audio_sample_t* buffer, unsigned int frames)
  59. {
  60. size_t len = jack_ringbuffer_write_space(fRingBuffer);
  61. jack_log("JackRingBuffer::Write output available = %ld", len / sizeof(jack_default_audio_sample_t));
  62. if (len < frames * sizeof(jack_default_audio_sample_t)) {
  63. jack_error("JackRingBuffer::Write : consumer too slow, skip frames = %d", frames);
  64. return 0;
  65. } else {
  66. jack_ringbuffer_write(fRingBuffer, (char*)buffer, frames * sizeof(jack_default_audio_sample_t));
  67. return frames;
  68. }
  69. }
  70. unsigned int JackRingBuffer::Read(void* buffer, unsigned int bytes)
  71. {
  72. size_t len = jack_ringbuffer_read_space(fRingBuffer);
  73. jack_log("JackRingBuffer::Read input available = %ld", len);
  74. if (len < bytes) {
  75. jack_error("JackRingBuffer::Read : producer too slow, missing bytes = %d", bytes);
  76. return 0;
  77. } else {
  78. jack_ringbuffer_read(fRingBuffer, (char*)buffer, bytes);
  79. return bytes;
  80. }
  81. }
  82. unsigned int JackRingBuffer::Write(void* buffer, unsigned int bytes)
  83. {
  84. size_t len = jack_ringbuffer_write_space(fRingBuffer);
  85. jack_log("JackRingBuffer::Write output available = %ld", len);
  86. if (len < bytes) {
  87. jack_error("JackRingBuffer::Write : consumer too slow, skip bytes = %d", bytes);
  88. return 0;
  89. } else {
  90. jack_ringbuffer_write(fRingBuffer, (char*)buffer, bytes);
  91. return bytes;
  92. }
  93. }
  94. unsigned int JackResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames)
  95. {
  96. return Read(buffer, frames);
  97. }
  98. unsigned int JackResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames)
  99. {
  100. return Write(buffer, frames);
  101. }
  102. }