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.

164 lines
4.9KB

  1. /*
  2. * dRowAudio_TappedDelayLine.h
  3. *
  4. * Created by David Rowland on 13/04/2009.
  5. * Copyright 2009 dRowAudio. All rights reserved.
  6. *
  7. */
  8. #ifndef _DROWAUDIO_TAPPEDDELAYLINE_H_
  9. #define _DROWAUDIO_TAPPEDDELAYLINE_H_
  10. #include "includes.h"
  11. // #include "../../utility/dRowAudio_Utility.h"
  12. // #include "../dRowAudio_AudioUtility.h"
  13. struct Tap
  14. {
  15. int delaySamples;
  16. int originalDelaySamples;
  17. int sampleRateWhenCreated;
  18. float tapGain;
  19. float tapFeedback;
  20. float originalTapFeedback;
  21. /* float tapPan;
  22. float tapHPCutoff;
  23. float tapLPCutoff;
  24. */
  25. };
  26. class TappedDelayLine
  27. {
  28. public:
  29. /** Creates a TappedDelayline with a given size in samples.
  30. If no size is specified a default of 9600 is used.
  31. */
  32. TappedDelayLine(int initialBufferSize =96000);
  33. TappedDelayLine(float bufferLengthMs, double sampleRate);
  34. /// Destructor
  35. ~TappedDelayLine();
  36. /** Adds a tap to the delay line at a given number of samples.
  37. This will not make a note of the current sample rate being used
  38. unless you explecity specify it. Use
  39. addTap(int newTapPosMs, double sampleRate) if you need the delay
  40. to be dependant on time.
  41. */
  42. void addTap(int noDelaySamples, int sampleRate =0);
  43. /** Adds a tap to the delay line at a given time.
  44. If the sample rate changes make sure you call updateDelayTimes()
  45. to recalculate the number of samples for the taps to delay by.
  46. */
  47. void addTapAtTime(int newTapPosMs, double sampleRate);
  48. /** Moves a specific tap to a new delay position.
  49. This will return true if a tap was actually changed.
  50. */
  51. bool setTapDelayTime(int tapIndex, int newTapPosMs, double sampleRate);
  52. /** Changes the number of samples a specific tap will delay.
  53. This will return true if a tap was actually changed.
  54. */
  55. bool setTapDelaySamples(int tapIndex, int newDelaySamples);
  56. /** Scales the spacing between the taps.
  57. This value must be greater than 0. Values < 1 will squash the taps,
  58. creating a denser delay, values greater than 1 will expand the taps
  59. spacing creating a more sparse delay.
  60. The value is used as a proportion of the explicitly set delay time.
  61. This is simpler than manually setting all of the tap positions.
  62. */
  63. void setTapSpacing(float newSpacingCoefficient);
  64. /** This has the same effect as setTapSpacing() but does not check to
  65. see if the coeficient has changed so will always update the spacing.
  66. */
  67. void setTapSpacingExplicitly(float newSpacingCoefficient);
  68. /** Scales all of the taps feedback coeficients in one go.
  69. This should be between 0 and 1 to avoid blowing up the line.
  70. The value is a proportion of the explicitly set feedback coefficient
  71. for each tap so setting this to 1 will return them all to their default.
  72. */
  73. void scaleFeedbacks(float newFeedbackCoefficient);
  74. /** Returns an array of sample positions where there are taps.
  75. This can then be used to remove a specific tap.
  76. */
  77. Array<int> getTapSamplePositions();
  78. /** Removes a tap at a specific index.
  79. Returns true if a tap was removed.
  80. */
  81. bool removeTapAtIndex(int tapIndex);
  82. /** Removes a tap with a specific delay samples.
  83. Returns true if a tap was revoved, false otherwise.
  84. */
  85. bool removeTapAtSample(int sampleForRemovedTap);
  86. /** Attempts to remove a tap at a specific time.
  87. Returns true if a tap was revoved, false otherwise.
  88. */
  89. bool removeTapAtMs(int timeMsForRemovedTap, int sampleRate);
  90. /** Removes all the taps.
  91. */
  92. void removeAllTaps();
  93. /** Updates the delay samples of all the taps based on their time.
  94. Call this if you change sample rates to make sure the taps are
  95. still positioned at the right time.
  96. */
  97. void updateDelayTimes(double newSampleRate);
  98. /** Resizes the buffer to a given number of samples.
  99. This will return the number of taps that have been removed if
  100. they overrun the new buffer size.
  101. */
  102. int setBufferSize(int noSamples);
  103. /** Resizes the buffer to a size given the time and sample rate.
  104. This will return the number of taps that have been removed if
  105. they overrun the new buffer size.
  106. */
  107. int setBufferSize(int timeMs, double sampleRate);
  108. /// Returns the current size in samples of the buffer.
  109. int getBufferSize() { return bufferSize; }
  110. /// Returns the current length in milliseconds of the buffer for a given sample rate.
  111. int getBufferLengthMs(double sampleRate) { return bufferSize * sampleRate; }
  112. /// Returns the number of taps currently being used.
  113. int getNumberOfTaps() { return readTaps.size(); }
  114. /// Processes a single sample returning a new sample with summed delays.
  115. float processSingleSample(float newSample) throw();
  116. /// Processes a number of samples in one go.
  117. void processSamples(float* const samples,
  118. const int numSamples) throw();
  119. private:
  120. CriticalSection processLock;
  121. float *pfDelayBuffer;
  122. int bufferSize, bufferWritePos;
  123. float inputGain, feedbackGain;
  124. int noTaps;
  125. Array<Tap> readTaps;
  126. float spacingCoefficient, feedbackCoefficient;
  127. void initialiseBuffer(int bufferSize);
  128. JUCE_LEAK_DETECTOR (TappedDelayLine);
  129. };
  130. #endif //_DROWAUDIO_TAPPEDDELAYLINE_H_