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.

101 lines
2.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 <stdio.h>
  17. namespace Jack
  18. {
  19. JackResampler::JackResampler()
  20. :fRatio(1),fRingBufferSize(DEFAULT_RB_SIZE)
  21. {
  22. fRingBuffer = jack_ringbuffer_create(sizeof(float) * fRingBufferSize);
  23. jack_ringbuffer_read_advance(fRingBuffer, (sizeof(float) * fRingBufferSize) / 2);
  24. }
  25. JackResampler::JackResampler(unsigned int ringbuffer_size)
  26. :fRatio(1),fRingBufferSize(ringbuffer_size)
  27. {
  28. fRingBuffer = jack_ringbuffer_create(sizeof(float) * fRingBufferSize);
  29. jack_ringbuffer_read_advance(fRingBuffer, (sizeof(float) * fRingBufferSize) / 2);
  30. }
  31. JackResampler::~JackResampler()
  32. {
  33. if (fRingBuffer)
  34. jack_ringbuffer_free(fRingBuffer);
  35. }
  36. void JackResampler::Reset()
  37. {
  38. jack_ringbuffer_reset(fRingBuffer);
  39. jack_ringbuffer_read_advance(fRingBuffer, (sizeof(float) * fRingBufferSize) / 2);
  40. }
  41. unsigned int JackResampler::ReadSpace()
  42. {
  43. return (jack_ringbuffer_read_space(fRingBuffer) / sizeof(float));
  44. }
  45. unsigned int JackResampler::WriteSpace()
  46. {
  47. return (jack_ringbuffer_write_space(fRingBuffer) / sizeof(float));
  48. }
  49. unsigned int JackResampler::Read(float* buffer, unsigned int frames)
  50. {
  51. size_t len = jack_ringbuffer_read_space(fRingBuffer);
  52. jack_log("JackResampler::Read input available = %ld", len / sizeof(float));
  53. if (len < frames * sizeof(float)) {
  54. jack_error("JackResampler::Read : producer too slow, missing frames = %d", frames);
  55. return 0;
  56. } else {
  57. jack_ringbuffer_read(fRingBuffer, (char*)buffer, frames * sizeof(float));
  58. return frames;
  59. }
  60. }
  61. unsigned int JackResampler::Write(float* buffer, unsigned int frames)
  62. {
  63. size_t len = jack_ringbuffer_write_space(fRingBuffer);
  64. jack_log("JackResampler::Write output available = %ld", len / sizeof(float));
  65. if (len < frames * sizeof(float)) {
  66. jack_error("JackResampler::Write : consumer too slow, skip frames = %d", frames);
  67. return 0;
  68. } else {
  69. jack_ringbuffer_write(fRingBuffer, (char*)buffer, frames * sizeof(float));
  70. return frames;
  71. }
  72. }
  73. unsigned int JackResampler::ReadResample(float* buffer, unsigned int frames)
  74. {
  75. return Read(buffer, frames);
  76. }
  77. unsigned int JackResampler::WriteResample(float* buffer, unsigned int frames)
  78. {
  79. return Write(buffer, frames);
  80. }
  81. }