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.

61 lines
1.3KB

  1. #pragma once
  2. #include <assert.h>
  3. #include <samplerate.h>
  4. #include <string.h>
  5. #include "frame.hpp"
  6. namespace rack {
  7. template<int CHANNELS>
  8. struct SampleRateConverter {
  9. SRC_STATE *state;
  10. SRC_DATA data;
  11. SampleRateConverter() {
  12. int error;
  13. state = src_new(SRC_SINC_FASTEST, CHANNELS, &error);
  14. assert(!error);
  15. data.src_ratio = 1.0;
  16. data.end_of_input = false;
  17. }
  18. ~SampleRateConverter() {
  19. src_delete(state);
  20. }
  21. /** output_sample_rate / input_sample_rate */
  22. void setRatio(float r) {
  23. src_set_ratio(state, r);
  24. data.src_ratio = r;
  25. }
  26. void setRatioSmooth(float r) {
  27. data.src_ratio = r;
  28. }
  29. /** `in` and `out` are interlaced with the number of channels */
  30. void process(const Frame<CHANNELS> *in, int *inFrames, Frame<CHANNELS> *out, int *outFrames) {
  31. /*
  32. if (nearf(data.src_ratio, 1.0)) {
  33. int len = mini(*inFrames, *outFrames);
  34. memcpy(out, in, len * sizeof(Frame<CHANNELS>));
  35. *inFrames = len;
  36. *outFrames = len;
  37. return;
  38. }
  39. */
  40. // Old versions of libsamplerate use float* here instead of const float*
  41. data.data_in = (float*) in;
  42. data.input_frames = *inFrames;
  43. data.data_out = (float*) out;
  44. data.output_frames = *outFrames;
  45. src_process(state, &data);
  46. *inFrames = data.input_frames_used;
  47. *outFrames = data.output_frames_gen;
  48. }
  49. void reset() {
  50. src_reset(state);
  51. }
  52. };
  53. } // namespace rack