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.

50 lines
1.1KB

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