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.

265 lines
7.0KB

  1. /*
  2. PROC/MEM 9/4
  3. Modify filter_test_f to try out a chorus effect.
  4. TODO:
  5. 140203
  6. o
  7. I have mixed up the names "chorus" and flange". The sketches named
  8. chorus have, up to version 'm', actually implemented a flanger.
  9. From version 'o' onwards the sketches named chorus will implement
  10. a real chorus effect and flange will do a flanging effect.
  11. n only changed the effect parameters
  12. m
  13. 140201
  14. l - found the problem at last using my_flange_cd_usd_c
  15. YES! Way back when I found I hadn't been using d_depth. Now I
  16. have just discovered that I haven't been using delay_offset!!!!!
  17. 140201
  18. k
  19. interpolation doesn't remove the ticking
  20. 140131
  21. j
  22. >>> The lower the frequency, the less ticking.
  23. - try interpolation
  24. 140201
  25. - got this restored to the way it was last night
  26. and then reinstated the changes. I had a couple of
  27. changes to the right channel that were incorrect or
  28. weren't carried over from the left channel changes
  29. i
  30. - don't know why but this version seems to have more "presence"
  31. than previous versions.
  32. The presence occurred when "sign = 1" was put in front of the left.
  33. It essentially makes it a passthrough.
  34. If both have "sign=1" then it is identical to passthrough
  35. Ticking is still not fixed
  36. h
  37. - add sign reversal. It seems to make audio much clearer
  38. BUT it hasn't got rid of the ticking noise
  39. g
  40. - I wasn't even using delay_depth!!!!
  41. 140131
  42. f
  43. - added code to print the processor and memory usage every 5 seconds
  44. NOW the problem is to try to remove the ticking
  45. e
  46. FOUND the problem with the right channel. It was also in the left channel
  47. but the placement of the delay line arrays made it more noticeable in the
  48. right channel. I was not calculating idx properly. In particular, the
  49. resuling index could be negative.
  50. I have shortened the delay line to only 2*AUDIO_BLOCK_SAMPLES
  51. - removed redundancies in the update code. rewrite the block
  52. instead of getting a new one
  53. Haven't solved the noise in the right channel yet.
  54. Tried duplicating right channel code to left but noise stays on the right
  55. 140130
  56. d
  57. The noise stays in the right channel even if it is calculated first
  58. Switching the L/R inputs doesn't switch the noise to the left channel
  59. c
  60. >> Now add a sinusoidal modulation to the offset
  61. There's an awful noise in both channels but it is much louder in
  62. the right channel. NOPE - it is ONLY in the right channel
  63. but the audio does sound like it is working (sort of) except that it
  64. is rather tinny. Maybe it needs to have the interpolation added.
  65. b
  66. - this works with clip16_6s.wav.
  67. The original of this audio file was from http://www.donreiman.com/Chorus/Chorus.htm
  68. I reworked it with Goldwave to make it a stereo WAV file
  69. But with Rick Wakewan's Jane Seymour it seems to act more like
  70. a high-pass filter.
  71. a
  72. - removed FIR stuff and changed the name to AudioEffectChorus
  73. it's basically a blank template and compiles.
  74. From: http://www.cs.cf.ac.uk/Dave/CM0268/PDF/10_CM0268_Audio_FX.pdf
  75. about Comb filter effects
  76. Effect Delay range (ms) Modulation
  77. Resonator 0 - 20 None
  78. Flanger 0 - 15 Sinusoidal (approx 1Hz)
  79. Chorus 25 - 50 None
  80. Echo >50 None
  81. FMI:
  82. The audio board uses the following pins.
  83. 6 - MEMCS
  84. 7 - MOSI
  85. 9 - BCLK
  86. 10 - SDCS
  87. 11 - MCLK
  88. 12 - MISO
  89. 13 - RX
  90. 14 - SCLK
  91. 15 - VOL
  92. 18 - SDA
  93. 19 - SCL
  94. 22 - TX
  95. 23 - LRCLK
  96. AudioProcessorUsage()
  97. AudioProcessorUsageMax()
  98. AudioProcessorUsageMaxReset()
  99. AudioMemoryUsage()
  100. AudioMemoryUsageMax()
  101. AudioMemoryUsageMaxReset()
  102. The CPU usage is an integer from 0 to 100, and the memory is from 0 to however
  103. many blocks you provided with AudioMemory().
  104. */
  105. #include <arm_math.h>
  106. #include <Audio.h>
  107. #include <Wire.h>
  108. //#include <WM8731.h>
  109. #include <SD.h>
  110. #include <SPI.h>
  111. #include <Bounce.h>
  112. // Number of samples in ONE channel
  113. #define CHORUS_DELAY_LENGTH (16*AUDIO_BLOCK_SAMPLES)
  114. // Allocate the delay line for left and right channels
  115. // The delayline will hold left and right samples so it
  116. // should be declared to be twice as long as the desired
  117. // number of samples in one channel
  118. #define CHORUS_DELAYLINE (CHORUS_DELAY_LENGTH*2)
  119. // The delay line for left and right channels
  120. short delayline[CHORUS_DELAYLINE];
  121. // If this pin is grounded the FIR filter is turned which
  122. // makes just pass through the audio
  123. // Don't use any of the pins listed above
  124. #define PASSTHRU_PIN 1
  125. Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
  126. //const int myInput = AUDIO_INPUT_MIC;
  127. const int myInput = AUDIO_INPUT_LINEIN;
  128. AudioInputI2S audioInput; // audio shield: mic or line-in
  129. AudioEffectChorus myEffect;
  130. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  131. // Create Audio connections between the components
  132. // Both channels of the audio input go to the FIR filter
  133. AudioConnection c1(audioInput, 0, myEffect, 0);
  134. AudioConnection c2(audioInput, 1, myEffect, 1);
  135. // both channels from the FIR filter go to the audio output
  136. AudioConnection c3(myEffect, 0, audioOutput, 0);
  137. AudioConnection c4(myEffect, 1, audioOutput, 1);
  138. AudioControlSGTL5000 audioShield;
  139. // number of "voices" in the chorus which INCLUDES the original voice
  140. int n_chorus = 3;
  141. // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
  142. void setup() {
  143. Serial.begin(9600);
  144. while (!Serial) ;
  145. delay(3000);
  146. pinMode(PASSTHRU_PIN,INPUT_PULLUP);
  147. // Maximum memory usage was reported as 4
  148. // Proc = 9 (9), Mem = 4 (4)
  149. AudioMemory(4);
  150. audioShield.enable();
  151. audioShield.inputSelect(myInput);
  152. audioShield.volume(50);
  153. // Warn that the passthru pin is grounded
  154. if(!digitalRead(PASSTHRU_PIN)) {
  155. Serial.print("PASSTHRU_PIN (");
  156. Serial.print(PASSTHRU_PIN);
  157. Serial.println(") is grounded");
  158. }
  159. // Initialize the effect
  160. // address of delayline
  161. // total number of samples (left AND right) in the delay line
  162. // number of voices in the chorus INCLUDING the original voice
  163. if(!myEffect.begin(delayline,CHORUS_DELAYLINE,n_chorus)) {
  164. Serial.println("AudioEffectChorus - begin failed");
  165. while(1);
  166. }
  167. // I want output on the line out too
  168. audioShield.unmuteLineout();
  169. // audioShield.muteHeadphone();
  170. Serial.println("setup done");
  171. AudioProcessorUsageMaxReset();
  172. AudioMemoryUsageMaxReset();
  173. }
  174. // audio volume
  175. int volume = 0;
  176. unsigned long last_time = millis();
  177. void loop()
  178. {
  179. // Volume control
  180. int n = analogRead(15);
  181. if (n != volume) {
  182. volume = n;
  183. audioShield.volume((float)n / 10.23);
  184. }
  185. if(1) {
  186. if(millis() - last_time >= 5000) {
  187. Serial.print("Proc = ");
  188. Serial.print(AudioProcessorUsage());
  189. Serial.print(" (");
  190. Serial.print(AudioProcessorUsageMax());
  191. Serial.print("), Mem = ");
  192. Serial.print(AudioMemoryUsage());
  193. Serial.print(" (");
  194. Serial.print(AudioMemoryUsageMax());
  195. Serial.println(")");
  196. last_time = millis();
  197. }
  198. }
  199. // update the button
  200. b_passthru.update();
  201. // If the passthru button is pushed, switch the effect to passthru
  202. if(b_passthru.fallingEdge()) {
  203. myEffect.modify(0);
  204. }
  205. // If passthru button is released, restore the previous chorus
  206. if(b_passthru.risingEdge()) {
  207. myEffect.modify(n_chorus);
  208. }
  209. }