Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. void ChorusFlangerAudioProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages)
  2. {
  3. ScopedNoDenormals noDenormals;
  4. auto totalNumInputChannels = getTotalNumInputChannels();
  5. auto totalNumOutputChannels = getTotalNumOutputChannels();
  6.  
  7. // Clear any garbage data
  8. for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
  9. buffer.clear (i, 0, buffer.getNumSamples());
  10.  
  11. // Obtain left and right audio data pointers
  12. float* leftChannel = buffer.getWritePointer(0);
  13. float* rightChannel = buffer.getWritePointer(1);
  14.  
  15. // Iterate through all samples in buffer
  16. for (int i = 0; i < buffer.getNumSamples(); i++)
  17. {
  18. // Write data into circular buffer
  19. mCircularBufferLeft[mCircularBufferWriteHead] = leftChannel[i] + mFeedbackLeft;
  20. mCircularBufferRight[mCircularBufferWriteHead] = rightChannel[i] + mFeedbackRight;
  21.  
  22. // Generate left channel LFO output
  23. float lfoOutLeft = sin(2 * double_Pi * mLFOPhase);
  24.  
  25. // Calculate right channel LFO phase
  26. float lfoPhaseRight = mLFOPhase + *mPhaseOffsetParameter;
  27.  
  28. // Perform bounds checking
  29. if (lfoPhaseRight >= 1)
  30. {
  31. lfoPhaseRight -= 1;
  32. }
  33.  
  34. // Generate right channel LFO output
  35. float lfoOutRight = sin(2 * double_Pi * mLFOPhase);
  36.  
  37. // Increase LFO phase
  38. mLFOPhase += *mRateParameter / getSampleRate();
  39.  
  40. if (mLFOPhase >= 1)
  41. {
  42. mLFOPhase -= 1;
  43. }
  44.  
  45. // Control LFO depth through parameter
  46. lfoOutLeft *= *mDepthParameter;
  47. lfoOutRight *= *mDepthParameter;
  48.  
  49. // Map LFO outputs
  50. // Initialize mapped outputs
  51. float lfoOutMappedLeft = 0;
  52. float lfoOutMappedRight = 0;
  53.  
  54. // chorus
  55. if (*mTypeParameter == 0)
  56. {
  57. lfoOutMappedLeft = jmap(lfoOutLeft, -1.f, 1.f, 0.005f, 0.03f);
  58. lfoOutMappedRight = jmap(lfoOutRight, -1.f, 1.f, 0.005f, 0.03f);
  59. }
  60.  
  61. // flanger
  62. else
  63. {
  64. lfoOutMappedLeft = jmap(lfoOutLeft, -1.f, 1.f, 0.001f, 0.005f);
  65. lfoOutMappedRight = jmap(lfoOutRight, -1.f, 1.f, 0.001f, 0.005f);
  66. }
  67.  
  68. // Calculate delay lengths in samples
  69. float delayTimeSamplesLeft = getSampleRate() * lfoOutMappedLeft;
  70. float delayTimeSamplesRight = getSampleRate() * lfoOutMappedRight;
  71.  
  72. // Calculate read head positions
  73. float delayReadHeadLeft = mCircularBufferWriteHead - delayTimeSamplesLeft;
  74. float delayReadHeadRight = mCircularBufferWriteHead - delayTimeSamplesRight;
  75.  
  76. // Perform bounds checking for left channel read head
  77. if (delayReadHeadLeft < 0)
  78. {
  79. delayReadHeadLeft += mCircularBufferLength;
  80. }
  81.  
  82. // Perform bounds checking for right channel read head
  83. if (delayReadHeadRight < 0)
  84. {
  85. delayReadHeadRight += mCircularBufferLength;
  86. }
  87.  
  88. // Calculate linear interpolation point for left channel
  89. int readHeadLeft_x = (int)delayReadHeadLeft;
  90. int readHeadLeft_x1 = readHeadLeft_x + 1;
  91. float readHeadFloatLeft = delayReadHeadLeft - readHeadLeft_x;
  92.  
  93. // Perform bounds checking
  94. if (readHeadLeft_x1 >= mCircularBufferLength)
  95. {
  96. readHeadLeft_x1 -= mCircularBufferLength;
  97. }
  98.  
  99. // Calculate linear interpolation point for right channel
  100. int readHeadRight_x = (int)delayReadHeadRight;
  101. int readHeadRight_x1 = readHeadRight_x + 1;
  102. float readHeadFloatRight = delayReadHeadRight - readHeadRight_x;
  103.  
  104. // Perform bounds checking
  105. if (readHeadRight_x1 >= mCircularBufferLength)
  106. {
  107. readHeadRight_x1 -= mCircularBufferLength;
  108. }
  109.  
  110. // Calculate interpolated samples
  111. float delay_sample_left = lin_interp(mCircularBufferLeft[readHeadLeft_x], mCircularBufferLeft[readHeadLeft_x1], readHeadFloatLeft);
  112. float delay_sample_right = lin_interp(mCircularBufferRight[readHeadRight_x], mCircularBufferRight[readHeadRight_x1], readHeadFloatRight);
  113.  
  114. // Store delayed samples as feedback
  115. mFeedbackLeft = delay_sample_left * *mFeedbackParameter;
  116. mFeedbackRight = delay_sample_right * *mFeedbackParameter;
  117.  
  118. // Increment write head
  119. mCircularBufferWriteHead++;
  120.  
  121. // Perform bounds checking
  122. if (mCircularBufferWriteHead >= mCircularBufferLength)
  123. {
  124. mCircularBufferWriteHead = 0;
  125. }
  126.  
  127. // Send Dry/Wet signal to audio buffer output
  128. buffer.setSample(0, i, buffer.getSample(0, i) * (1 - *mDryWetParameter) + delay_sample_left * (*mDryWetParameter));
  129. buffer.setSample(1, i, buffer.getSample(1, i) * (1 - *mDryWetParameter) + delay_sample_right * (*mDryWetParameter));
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement