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.

177 lines
6.4KB

  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. namespace Jack
  17. {
  18. JackResampler::JackResampler():fRatio(0)
  19. {
  20. int error;
  21. fResampler = src_new(SRC_LINEAR, 1, &error);
  22. if (error != 0)
  23. jack_error("JackResampler::JackResampler err = %s", src_strerror(error));
  24. fRingBuffer = jack_ringbuffer_create(sizeof(float) * DEFAULT_RB_SIZE);
  25. jack_ringbuffer_read_advance(fRingBuffer, (sizeof(float) * DEFAULT_RB_SIZE) / 2);
  26. }
  27. JackResampler::~JackResampler()
  28. {
  29. src_delete(fResampler);
  30. if (fRingBuffer)
  31. jack_ringbuffer_free(fRingBuffer);
  32. }
  33. unsigned int JackResampler::ReadSpace()
  34. {
  35. return jack_ringbuffer_read_space(fRingBuffer);
  36. }
  37. unsigned int JackResampler::WriteSpace()
  38. {
  39. return jack_ringbuffer_write_space(fRingBuffer);
  40. }
  41. int JackResampler::Read(float* buffer, unsigned int frames)
  42. {
  43. size_t len = jack_ringbuffer_read_space(fRingBuffer);
  44. jack_log("JackResampler::Read INPUT available = %ld", len / sizeof(float));
  45. if (len < frames * sizeof(float)) {
  46. jack_error("JackResampler::Read : producer too slow, missing frames = %d", frames);
  47. //jack_ringbuffer_read(fRingBuffer, buffer, len);
  48. return 0;
  49. } else {
  50. jack_ringbuffer_read(fRingBuffer, (char*)buffer, frames * sizeof(float));
  51. return frames;
  52. }
  53. }
  54. int JackResampler::Write(float* buffer, unsigned int frames)
  55. {
  56. size_t len = jack_ringbuffer_write_space(fRingBuffer);
  57. jack_log("JackResampler::Write OUTPUT available = %ld", len / sizeof(float));
  58. if (len < frames * sizeof(float)) {
  59. jack_error("JackResampler::Write : consumer too slow, skip frames = %d", frames);
  60. //jack_ringbuffer_write(fRingBuffer, buffer, len);
  61. return 0;
  62. } else {
  63. jack_ringbuffer_write(fRingBuffer, (char*)buffer, frames * sizeof(float));
  64. return frames;
  65. }
  66. }
  67. int JackResampler::ReadResample(float* buffer, unsigned int frames)
  68. {
  69. jack_ringbuffer_data_t ring_buffer_data[2];
  70. SRC_DATA src_data;
  71. unsigned int frames_to_write = frames;
  72. unsigned int written_frames = 0;
  73. int res;
  74. jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data);
  75. unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
  76. jack_log("OUPUT available = %ld", available_frames);
  77. for (int j = 0; j < 2; j++) {
  78. if (ring_buffer_data[j].len > 0) {
  79. src_data.data_in = (float*)ring_buffer_data[j].buf;
  80. src_data.data_out = &buffer[written_frames];
  81. src_data.input_frames = ring_buffer_data[j].len / sizeof(float);
  82. src_data.output_frames = frames_to_write;
  83. src_data.end_of_input = 0;
  84. src_data.src_ratio = fRatio;
  85. res = src_process(fResampler, &src_data);
  86. if (res != 0)
  87. jack_error("JackResampler::ReadResample err = %s", src_strerror(res));
  88. frames_to_write -= src_data.output_frames_gen;
  89. written_frames += src_data.output_frames_gen;
  90. if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
  91. jack_error("OUTPUT : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  92. }
  93. jack_log("OUTPUT : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  94. jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(float));
  95. }
  96. }
  97. if (written_frames < frames) {
  98. jack_error("OUPUT available = %ld", available_frames);
  99. jack_error("JackResampler::ReadResample error written_frames = %ld", written_frames);
  100. }
  101. return written_frames;
  102. }
  103. int JackResampler::WriteResample(float* buffer, unsigned int frames)
  104. {
  105. jack_ringbuffer_data_t ring_buffer_data[2];
  106. SRC_DATA src_data;
  107. unsigned int frames_to_read = frames;
  108. unsigned int read_frames = 0;
  109. int res;
  110. jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data);
  111. unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
  112. jack_log("INPUT available = %ld", available_frames);
  113. for (int j = 0; j < 2; j++) {
  114. if (ring_buffer_data[j].len > 0) {
  115. src_data.data_in = &buffer[read_frames];
  116. src_data.data_out = (float*)ring_buffer_data[j].buf;
  117. src_data.input_frames = frames_to_read;
  118. src_data.output_frames = (ring_buffer_data[j].len / sizeof(float));
  119. src_data.end_of_input = 0;
  120. src_data.src_ratio = fRatio;
  121. res = src_process(fResampler, &src_data);
  122. if (res != 0)
  123. jack_error("JackResampler::ReadResample err = %s", src_strerror(res));
  124. frames_to_read -= src_data.input_frames_used;
  125. read_frames += src_data.input_frames_used;
  126. if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
  127. jack_error("INPUT : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  128. }
  129. jack_log("INPUT : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  130. jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(float));
  131. }
  132. }
  133. if (read_frames < frames) {
  134. jack_error("INPUT available = %ld", available_frames);
  135. jack_error("JackResampler::ReadResample error read_frames = %ld", read_frames);
  136. }
  137. return read_frames;
  138. }
  139. }