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.

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