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.

177 lines
4.2KB

  1. #pragma once
  2. #ifndef RACK_SKIP_RESAMPLER
  3. #include <assert.h>
  4. #include <string.h>
  5. #include <speex/speex_resampler.h>
  6. #include "frame.hpp"
  7. #include "ringbuffer.hpp"
  8. #include "fir.hpp"
  9. namespace rack {
  10. template<int CHANNELS>
  11. struct SampleRateConverter {
  12. SpeexResamplerState *st = NULL;
  13. int channels = CHANNELS;
  14. int quality = SPEEX_RESAMPLER_QUALITY_DEFAULT;
  15. int inRate = 44100;
  16. int outRate = 44100;
  17. SampleRateConverter() {
  18. refreshState();
  19. }
  20. ~SampleRateConverter() {
  21. if (st) {
  22. speex_resampler_destroy(st);
  23. }
  24. }
  25. /** Sets the number of channels to actually process. This can be at most CHANNELS. */
  26. void setChannels(int channels) {
  27. assert(channels <= CHANNELS);
  28. if (channels == this->channels)
  29. return;
  30. this->channels = channels;
  31. refreshState();
  32. }
  33. /** From 0 (worst, fastest) to 10 (best, slowest) */
  34. void setQuality(int quality) {
  35. if (quality == this->quality)
  36. return;
  37. this->quality = quality;
  38. refreshState();
  39. }
  40. void setRates(int inRate, int outRate) {
  41. if (inRate == this->inRate && outRate == this->outRate)
  42. return;
  43. this->inRate = inRate;
  44. this->outRate = outRate;
  45. refreshState();
  46. }
  47. void refreshState() {
  48. if (st) {
  49. speex_resampler_destroy(st);
  50. st = NULL;
  51. }
  52. if (channels > 0 && inRate != outRate) {
  53. int err;
  54. st = speex_resampler_init(channels, inRate, outRate, quality, &err);
  55. assert(st);
  56. assert(err == RESAMPLER_ERR_SUCCESS);
  57. speex_resampler_set_input_stride(st, CHANNELS);
  58. speex_resampler_set_output_stride(st, CHANNELS);
  59. }
  60. }
  61. /** `in` and `out` are interlaced with the number of channels */
  62. void process(const Frame<CHANNELS> *in, int *inFrames, Frame<CHANNELS> *out, int *outFrames) {
  63. assert(in);
  64. assert(inFrames);
  65. assert(out);
  66. assert(outFrames);
  67. if (st) {
  68. // Resample each channel at a time
  69. spx_uint32_t inLen;
  70. spx_uint32_t outLen;
  71. for (int i = 0; i < channels; i++) {
  72. inLen = *inFrames;
  73. outLen = *outFrames;
  74. int err = speex_resampler_process_float(st, i, ((const float*) in) + i, &inLen, ((float*) out) + i, &outLen);
  75. assert(err == RESAMPLER_ERR_SUCCESS);
  76. }
  77. *inFrames = inLen;
  78. *outFrames = outLen;
  79. }
  80. else {
  81. // Simply copy the buffer without conversion
  82. int frames = min(*inFrames, *outFrames);
  83. memcpy(out, in, frames * sizeof(Frame<CHANNELS>));
  84. *inFrames = frames;
  85. *outFrames = frames;
  86. }
  87. }
  88. };
  89. template<int OVERSAMPLE, int QUALITY>
  90. struct Decimator {
  91. float inBuffer[OVERSAMPLE*QUALITY];
  92. float kernel[OVERSAMPLE*QUALITY];
  93. int inIndex;
  94. Decimator(float cutoff = 0.9f) {
  95. boxcarLowpassIR(kernel, OVERSAMPLE*QUALITY, cutoff * 0.5f / OVERSAMPLE);
  96. blackmanHarrisWindow(kernel, OVERSAMPLE*QUALITY);
  97. reset();
  98. }
  99. void reset() {
  100. inIndex = 0;
  101. memset(inBuffer, 0, sizeof(inBuffer));
  102. }
  103. /** `in` must be length OVERSAMPLE */
  104. float process(float *in) {
  105. // Copy input to buffer
  106. memcpy(&inBuffer[inIndex], in, OVERSAMPLE*sizeof(float));
  107. // Advance index
  108. inIndex += OVERSAMPLE;
  109. inIndex %= OVERSAMPLE*QUALITY;
  110. // Perform naive convolution
  111. float out = 0.f;
  112. for (int i = 0; i < OVERSAMPLE*QUALITY; i++) {
  113. int index = inIndex - 1 - i;
  114. index = (index + OVERSAMPLE*QUALITY) % (OVERSAMPLE*QUALITY);
  115. out += kernel[i] * inBuffer[index];
  116. }
  117. return out;
  118. }
  119. };
  120. template<int OVERSAMPLE, int QUALITY>
  121. struct Upsampler {
  122. float inBuffer[QUALITY];
  123. float kernel[OVERSAMPLE*QUALITY];
  124. int inIndex;
  125. Upsampler(float cutoff = 0.9f) {
  126. boxcarLowpassIR(kernel, OVERSAMPLE*QUALITY, cutoff * 0.5f / OVERSAMPLE);
  127. blackmanHarrisWindow(kernel, OVERSAMPLE*QUALITY);
  128. reset();
  129. }
  130. void reset() {
  131. inIndex = 0;
  132. memset(inBuffer, 0, sizeof(inBuffer));
  133. }
  134. /** `out` must be length OVERSAMPLE */
  135. void process(float in, float *out) {
  136. // Zero-stuff input buffer
  137. inBuffer[inIndex] = OVERSAMPLE * in;
  138. // Advance index
  139. inIndex++;
  140. inIndex %= QUALITY;
  141. // Naively convolve each sample
  142. // TODO replace with polyphase filter hierarchy
  143. for (int i = 0; i < OVERSAMPLE; i++) {
  144. float y = 0.f;
  145. for (int j = 0; j < QUALITY; j++) {
  146. int index = inIndex - 1 - j;
  147. index = (index + QUALITY) % QUALITY;
  148. int kernelIndex = OVERSAMPLE * j + i;
  149. y += kernel[kernelIndex] * inBuffer[index];
  150. }
  151. out[i] = y;
  152. }
  153. }
  154. };
  155. } // namespace rack
  156. #endif // RACK_SKIP_RESAMPLER