CMLSDelay.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. ==============================================================================
  3. CMLSDelay.cpp
  4. Created: 3 May 2025 5:39:35pm
  5. Author: Luigi
  6. ==============================================================================
  7. */
  8. #include "CMLSDelay.h"
  9. CMLSDelay::CMLSDelay() {
  10. delayLengthRange = new juce::NormalisableRange<float>(0, MAX_DELAY_LENGTH);
  11. feedbackRange = new juce::NormalisableRange<float>(0, MAX_FEEDBACK);
  12. this->delayLength = 0.0f;
  13. this->feedback = 0.75f;
  14. this->delayBufferLength = 1;
  15. this->delayReadPosition = 0;
  16. this->delayWritePosition = 0;
  17. }
  18. CMLSDelay::~CMLSDelay() {}
  19. void CMLSDelay::reset() {
  20. }
  21. void CMLSDelay::prepare(const juce::dsp::ProcessSpec& spec) {
  22. this->delayBufferLength = (int) 2.0 * spec.sampleRate;
  23. if (this->delayBufferLength < 1) {
  24. this->delayBufferLength = 1;
  25. }
  26. this->delayBuffer.setSize(spec.numChannels, this->delayBufferLength);
  27. this->delayBuffer.clear();
  28. this->delayReadPosition = (int)(this->delayWritePosition - (this->delayLength * spec.sampleRate) + this->delayBufferLength) % this->delayBufferLength;
  29. }
  30. void CMLSDelay::process(const juce::dsp::ProcessContextReplacing<float>& context) {
  31. auto audioBlock = context.getOutputBlock();
  32. auto numChannels = audioBlock.getNumChannels();
  33. const auto numSamples = audioBlock.getNumSamples();
  34. int dpr, dpw;
  35. for (int channel = 0; channel < numChannels; ++channel) {
  36. float* channelData = audioBlock.getChannelPointer(channel);
  37. float* delayData = this->delayBuffer.getWritePointer(juce::jmin(channel, this->delayBuffer.getNumChannels() - 1));
  38. dpr = this->delayReadPosition;
  39. dpw = this->delayWritePosition;
  40. for (int sample = 0; sample < numSamples; sample++) {
  41. const float in = channelData[sample];
  42. float out = 0.0f;
  43. out = ((1-this->dryWetProp) * in + this->dryWetProp * delayData[dpr]);
  44. delayData[dpw] = in + (this->feedback * delayData[dpr]);
  45. if (++dpr >= this->delayBufferLength) {
  46. dpr = 0;
  47. }
  48. if (++dpw >= this->delayBufferLength) {
  49. dpw = 0;
  50. }
  51. channelData[sample] = out;
  52. }
  53. }
  54. this->delayReadPosition = dpr;
  55. this->delayWritePosition = dpw;
  56. }
  57. void CMLSDelay::setDryWet(float value) {
  58. this->dryWetProp = value;
  59. }
  60. void CMLSDelay::setAmount(float value) {
  61. this->delayLength = this->delayLengthRange->convertFrom0to1(value);
  62. this->feedback = this->delayLengthRange->convertFrom0to1(value);
  63. }
  64. const float CMLSDelay::getDryWet() {
  65. return this->dryWetProp;
  66. }
  67. const float CMLSDelay::getAmount() {
  68. return this->amount;
  69. }