Guest User

read_audio for stretch lamp

a guest
Nov 17th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. // the following variables obviously shouldn't be declared here,
  2. // just putting them here for convinience
  3.  
  4. float half_factor = 0.00f;
  5. int   mono_volume;
  6.  
  7. int left[7];
  8. int right[7];
  9. int mono[7];
  10. int half_mapped[7];
  11.  
  12. int left_value;
  13. int right_value;
  14. int mono_value;
  15.  
  16. int filter_min = 100; // eliminate msgeq7 noise
  17. int filter_max = 1023;
  18. int value_floor = 0;
  19. int value_ceiling = 255;
  20.  
  21. // higher value for lowPass_audio means the animation is more responsive to audio,
  22. // BUT the brightness is less smooth, naturally. keep it lower to get that silky data goodness
  23. float lowPass_audio = 0.18f; // "f" is to just cut the floating integer off at 2 decimels
  24. // .18 is a good starting point for smoothing, I think. 0.14 starts to get less responsive
  25.  
  26. void ReadAudio() {
  27.  
  28.   half_factor = 0.00f;
  29.   mono_volume  = 0;
  30.  
  31.   digitalWrite(reset, HIGH); // reset dat thang
  32.   digitalWrite(reset, LOW);
  33.   delayMicroseconds(1);
  34.  
  35.   for (int band = 0; band < 7; band++)
  36.   {
  37.     digitalWrite(strobe, LOW);
  38.     delayMicroseconds(35);
  39.  
  40.     prev_value  = left[band]; // store what is now the PREVIOUS value for this band as prev_value
  41.     left_value  = analogRead(left_in); // store the CURRENT value for this band as a new variable
  42.    
  43.     //anddddd map/constrain that!
  44.     left_value  = constrain(left_value, filter_min, filter_max);
  45.     left_value  = map(left_value, filter_min, filter_max, value_floor, value_ceiling);
  46.  
  47.     //then smooth that thang out
  48.     left[band]  = prev_value + (left_value - prev_value) * lowPass_audio;
  49.  
  50.  
  51.     //and if you have enough processing power/two msgeq7 chips like the spectrum shield, read the other channel
  52.     //I've found that averaging both bands to mono inherently makes an animation smoother
  53.     prev_value  = right[band];
  54.     right_value = analogRead(right_in);
  55.     right_value = constrain(right_value, filter_min, filter_max);
  56.     right_value = map(right_value, filter_min, filter_max, value_floor, value_ceiling);
  57.     right[band] = prev_value + (right_value - prev_value) * lowPass_audio;
  58.  
  59.     // average to mono,
  60.     mono[band]  = (left[band] + right[band]) * 0.5;
  61.     // and add each  new mono band value to mono_volume, which is used to calculate the
  62.     // factor to multiply each band by below. this is how the stretching/contracting stuff works
  63.     // be sure to reset half_factor and mono_volume with each iteration
  64.     mono_volume += mono[band];
  65.  
  66.     digitalWrite(strobe, HIGH);
  67.     delayMicroseconds(1);
  68.  
  69.   }
  70.  
  71.   // now that we have the sum of each band, average dat
  72.   mono_volume  /= 7;
  73.   //NOW, half_factor is v important. i have 240 leds in total, so here i'm dividing
  74.   // half of that by mono_volume so that I can mirror the effect on both sides of the lamp
  75.   // say the average volume is 100 in this current cycle, and I have 120 leds on both sides.
  76.   // 120/100 is 1.2
  77.   // in the following loop, I basically multiply each megeq7 band value by 1.2 so that
  78.   // all leds are filled with the msgeq7 data, but the bands that are not as loud don't stretch as far.
  79.   // hope that makes sense
  80.   half_factor = HALF_NUM_LEDS / mono_volume;
  81.  
  82.  
  83.   // here is where the length of each band is calculated by multiplying the band
  84.   // by that "1.2" variable above (which onbiously changes with each iteration)
  85.   // if you were to print these values to the serial monitor, they would all
  86.   // add up to HALF_NUM_LEDS.
  87.   for (int band = 0; band < 7; band++)
  88.   {
  89.     half_MAPPED_AMPLITUDE = mono[band] * half_factor;
  90.     half_mapped[band] = half_MAPPED_AMPLITUDE;
  91.   }
  92.  
  93. // anddd print the audio if you want
  94. //#ifdef PRINT_AUDIO
  95. //  printAudio();
  96. //#endif
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment