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.

186 lines
4.5KB

  1. #pragma once
  2. #include <speex/speex_resampler.h>
  3. #include <dsp/common.hpp>
  4. #include <dsp/ringbuffer.hpp>
  5. #include <dsp/fir.hpp>
  6. #include <dsp/window.hpp>
  7. namespace rack {
  8. namespace dsp {
  9. /** Resamples by a fixed rational factor. */
  10. template <int MAX_CHANNELS>
  11. struct SampleRateConverter {
  12. SpeexResamplerState* st = NULL;
  13. int channels = MAX_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 MAX_CHANNELS. */
  26. void setChannels(int channels) {
  27. assert(channels <= MAX_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. (void) err;
  56. }
  57. }
  58. void process(const float* in, int inStride, int* inFrames, float* out, int outStride, int* outFrames) {
  59. assert(in);
  60. assert(inFrames);
  61. assert(out);
  62. assert(outFrames);
  63. if (st) {
  64. speex_resampler_set_input_stride(st, inStride);
  65. speex_resampler_set_output_stride(st, outStride);
  66. // Resample each channel at a time
  67. spx_uint32_t inLen = 0;
  68. spx_uint32_t outLen = 0;
  69. for (int c = 0; c < channels; c++) {
  70. inLen = *inFrames;
  71. outLen = *outFrames;
  72. int err = speex_resampler_process_float(st, c, &in[c], &inLen, &out[c], &outLen);
  73. (void) err;
  74. }
  75. *inFrames = inLen;
  76. *outFrames = outLen;
  77. }
  78. else {
  79. // Simply copy the buffer without conversion
  80. int frames = std::min(*inFrames, *outFrames);
  81. for (int i = 0; i < frames; i++) {
  82. for (int c = 0; c < channels; c++) {
  83. out[outStride * i + c] = in[inStride * i + c];
  84. }
  85. }
  86. *inFrames = frames;
  87. *outFrames = frames;
  88. }
  89. }
  90. void process(const Frame<MAX_CHANNELS>* in, int* inFrames, Frame<MAX_CHANNELS>* out, int* outFrames) {
  91. process((const float*) in, MAX_CHANNELS, inFrames, (float*) out, MAX_CHANNELS, outFrames);
  92. }
  93. };
  94. /** Downsamples by an integer factor. */
  95. template <int OVERSAMPLE, int QUALITY, typename T = float>
  96. struct Decimator {
  97. T inBuffer[OVERSAMPLE * QUALITY];
  98. float kernel[OVERSAMPLE * QUALITY];
  99. int inIndex;
  100. Decimator(float cutoff = 0.9f) {
  101. boxcarLowpassIR(kernel, OVERSAMPLE * QUALITY, cutoff * 0.5f / OVERSAMPLE);
  102. blackmanHarrisWindow(kernel, OVERSAMPLE * QUALITY);
  103. reset();
  104. }
  105. void reset() {
  106. inIndex = 0;
  107. std::memset(inBuffer, 0, sizeof(inBuffer));
  108. }
  109. /** `in` must be length OVERSAMPLE */
  110. T process(T* in) {
  111. // Copy input to buffer
  112. std::memcpy(&inBuffer[inIndex], in, OVERSAMPLE * sizeof(T));
  113. // Advance index
  114. inIndex += OVERSAMPLE;
  115. inIndex %= OVERSAMPLE * QUALITY;
  116. // Perform naive convolution
  117. T out = 0.f;
  118. for (int i = 0; i < OVERSAMPLE * QUALITY; i++) {
  119. int index = inIndex - 1 - i;
  120. index = (index + OVERSAMPLE * QUALITY) % (OVERSAMPLE * QUALITY);
  121. out += kernel[i] * inBuffer[index];
  122. }
  123. return out;
  124. }
  125. };
  126. /** Upsamples by an integer factor. */
  127. template <int OVERSAMPLE, int QUALITY>
  128. struct Upsampler {
  129. float inBuffer[QUALITY];
  130. float kernel[OVERSAMPLE * QUALITY];
  131. int inIndex;
  132. Upsampler(float cutoff = 0.9f) {
  133. boxcarLowpassIR(kernel, OVERSAMPLE * QUALITY, cutoff * 0.5f / OVERSAMPLE);
  134. blackmanHarrisWindow(kernel, OVERSAMPLE * QUALITY);
  135. reset();
  136. }
  137. void reset() {
  138. inIndex = 0;
  139. std::memset(inBuffer, 0, sizeof(inBuffer));
  140. }
  141. /** `out` must be length OVERSAMPLE */
  142. void process(float in, float* out) {
  143. // Zero-stuff input buffer
  144. inBuffer[inIndex] = OVERSAMPLE * in;
  145. // Advance index
  146. inIndex++;
  147. inIndex %= QUALITY;
  148. // Naively convolve each sample
  149. // TODO replace with polyphase filter hierarchy
  150. for (int i = 0; i < OVERSAMPLE; i++) {
  151. float y = 0.f;
  152. for (int j = 0; j < QUALITY; j++) {
  153. int index = inIndex - 1 - j;
  154. index = (index + QUALITY) % QUALITY;
  155. int kernelIndex = OVERSAMPLE * j + i;
  156. y += kernel[kernelIndex] * inBuffer[index];
  157. }
  158. out[i] = y;
  159. }
  160. }
  161. };
  162. } // namespace dsp
  163. } // namespace rack