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.

51 lines
1.1KB

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