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.

147 lines
5.6KB

  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 "JackLibSampleRateResampler.h"
  16. namespace Jack
  17. {
  18. JackLibSampleRateResampler::JackLibSampleRateResampler()
  19. :JackResampler(),fRatio(1)
  20. {
  21. int error;
  22. fResampler = src_new(SRC_LINEAR, 1, &error);
  23. //fResampler = src_new(SRC_SINC_BEST_QUALITY, 1, &error);
  24. if (error != 0)
  25. jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
  26. }
  27. JackLibSampleRateResampler::~JackLibSampleRateResampler()
  28. {
  29. src_delete(fResampler);
  30. }
  31. void JackLibSampleRateResampler::Reset()
  32. {
  33. JackResampler::Reset();
  34. src_reset(fResampler);
  35. }
  36. unsigned int JackLibSampleRateResampler::ReadResample(float* buffer, unsigned int frames)
  37. {
  38. jack_ringbuffer_data_t ring_buffer_data[2];
  39. SRC_DATA src_data;
  40. unsigned int frames_to_write = frames;
  41. unsigned int written_frames = 0;
  42. int res;
  43. jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data);
  44. unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
  45. jack_log("Output available = %ld", available_frames);
  46. for (int j = 0; j < 2; j++) {
  47. if (ring_buffer_data[j].len > 0) {
  48. src_data.data_in = (float*)ring_buffer_data[j].buf;
  49. src_data.data_out = &buffer[written_frames];
  50. src_data.input_frames = ring_buffer_data[j].len / sizeof(float);
  51. src_data.output_frames = frames_to_write;
  52. src_data.end_of_input = 0;
  53. src_data.src_ratio = fRatio;
  54. res = src_process(fResampler, &src_data);
  55. if (res != 0) {
  56. jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
  57. return 0;
  58. }
  59. frames_to_write -= src_data.output_frames_gen;
  60. written_frames += src_data.output_frames_gen;
  61. if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
  62. jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
  63. , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
  64. }
  65. jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  66. jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(float));
  67. }
  68. }
  69. if (written_frames < frames) {
  70. jack_error("Output available = %ld", available_frames);
  71. jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames);
  72. }
  73. return written_frames;
  74. }
  75. unsigned int JackLibSampleRateResampler::WriteResample(float* buffer, unsigned int frames)
  76. {
  77. jack_ringbuffer_data_t ring_buffer_data[2];
  78. SRC_DATA src_data;
  79. unsigned int frames_to_read = frames;
  80. unsigned int read_frames = 0;
  81. int res;
  82. jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data);
  83. unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
  84. jack_log("Input available = %ld", available_frames);
  85. for (int j = 0; j < 2; j++) {
  86. if (ring_buffer_data[j].len > 0) {
  87. src_data.data_in = &buffer[read_frames];
  88. src_data.data_out = (float*)ring_buffer_data[j].buf;
  89. src_data.input_frames = frames_to_read;
  90. src_data.output_frames = (ring_buffer_data[j].len / sizeof(float));
  91. src_data.end_of_input = 0;
  92. src_data.src_ratio = fRatio;
  93. res = src_process(fResampler, &src_data);
  94. if (res != 0) {
  95. jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
  96. return 0;
  97. }
  98. frames_to_read -= src_data.input_frames_used;
  99. read_frames += src_data.input_frames_used;
  100. if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
  101. jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
  102. , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
  103. }
  104. jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  105. jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(float));
  106. }
  107. }
  108. if (read_frames < frames) {
  109. jack_error("Input available = %ld", available_frames);
  110. jack_error("JackLibSampleRateResampler::ReadResample error read_frames = %ld", read_frames);
  111. }
  112. return read_frames;
  113. }
  114. }