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.

179 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 "JackLibSampleRateResampler.h"
  16. #include "JackError.h"
  17. namespace Jack
  18. {
  19. JackLibSampleRateResampler::JackLibSampleRateResampler()
  20. :JackResampler()
  21. {
  22. int error;
  23. fResampler = src_new(SRC_LINEAR, 1, &error);
  24. if (error != 0)
  25. jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
  26. }
  27. JackLibSampleRateResampler::JackLibSampleRateResampler(unsigned int quality)
  28. :JackResampler()
  29. {
  30. switch (quality) {
  31. case 0:
  32. quality = SRC_LINEAR;
  33. break;
  34. case 1:
  35. quality = SRC_ZERO_ORDER_HOLD;
  36. break;
  37. case 2:
  38. quality = SRC_SINC_FASTEST;
  39. break;
  40. case 3:
  41. quality = SRC_SINC_MEDIUM_QUALITY;
  42. break;
  43. case 4:
  44. quality = SRC_SINC_BEST_QUALITY;
  45. break;
  46. default:
  47. quality = SRC_LINEAR;
  48. jack_error("Out of range resample quality");
  49. break;
  50. }
  51. int error;
  52. fResampler = src_new(quality, 1, &error);
  53. if (error != 0) {
  54. jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
  55. }
  56. }
  57. JackLibSampleRateResampler::~JackLibSampleRateResampler()
  58. {
  59. src_delete(fResampler);
  60. }
  61. void JackLibSampleRateResampler::Reset(unsigned int new_size)
  62. {
  63. JackResampler::Reset(new_size);
  64. src_reset(fResampler);
  65. }
  66. unsigned int JackLibSampleRateResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames)
  67. {
  68. jack_ringbuffer_data_t ring_buffer_data[2];
  69. SRC_DATA src_data;
  70. unsigned int frames_to_write = frames;
  71. unsigned int written_frames = 0;
  72. int res;
  73. jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data);
  74. unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(jack_default_audio_sample_t);
  75. jack_log("Output available = %ld", available_frames);
  76. for (int j = 0; j < 2; j++) {
  77. if (ring_buffer_data[j].len > 0) {
  78. src_data.data_in = (jack_default_audio_sample_t*)ring_buffer_data[j].buf;
  79. src_data.data_out = &buffer[written_frames];
  80. src_data.input_frames = ring_buffer_data[j].len / sizeof(jack_default_audio_sample_t);
  81. src_data.output_frames = frames_to_write;
  82. src_data.end_of_input = 0;
  83. src_data.src_ratio = fRatio;
  84. res = src_process(fResampler, &src_data);
  85. if (res != 0) {
  86. jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
  87. return 0;
  88. }
  89. frames_to_write -= src_data.output_frames_gen;
  90. written_frames += src_data.output_frames_gen;
  91. if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
  92. jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
  93. , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
  94. }
  95. jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  96. jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(jack_default_audio_sample_t));
  97. }
  98. }
  99. if (written_frames < frames) {
  100. jack_error("Output available = %ld", available_frames);
  101. jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames);
  102. }
  103. return written_frames;
  104. }
  105. unsigned int JackLibSampleRateResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames)
  106. {
  107. jack_ringbuffer_data_t ring_buffer_data[2];
  108. SRC_DATA src_data;
  109. unsigned int frames_to_read = frames;
  110. unsigned int read_frames = 0;
  111. int res;
  112. jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data);
  113. unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(jack_default_audio_sample_t);
  114. jack_log("Input available = %ld", available_frames);
  115. for (int j = 0; j < 2; j++) {
  116. if (ring_buffer_data[j].len > 0) {
  117. src_data.data_in = &buffer[read_frames];
  118. src_data.data_out = (jack_default_audio_sample_t*)ring_buffer_data[j].buf;
  119. src_data.input_frames = frames_to_read;
  120. src_data.output_frames = (ring_buffer_data[j].len / sizeof(jack_default_audio_sample_t));
  121. src_data.end_of_input = 0;
  122. src_data.src_ratio = fRatio;
  123. res = src_process(fResampler, &src_data);
  124. if (res != 0) {
  125. jack_error("JackLibSampleRateResampler::WriteResample ratio = %f err = %s", fRatio, src_strerror(res));
  126. return 0;
  127. }
  128. frames_to_read -= src_data.input_frames_used;
  129. read_frames += src_data.input_frames_used;
  130. if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
  131. jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
  132. , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
  133. }
  134. jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
  135. jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(jack_default_audio_sample_t));
  136. }
  137. }
  138. if (read_frames < frames) {
  139. jack_error("Input available = %ld", available_frames);
  140. jack_error("JackLibSampleRateResampler::WriteResample error read_frames = %ld", read_frames);
  141. }
  142. return read_frames;
  143. }
  144. }