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.

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