Advertisement
atuline

An FHT and FastLED routine.

Jan 8th, 2015
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1.  
  2. /* Fast Hartley Transform with FastLED
  3.  
  4. By: Andrew Tuline
  5.  
  6. Date: Oct, 2014
  7.  
  8.  
  9. This is an implementation of the FHT library with FastLED 3.0
  10.  
  11. FastLED is available at https://github.com/FastLED/FastLED
  12.  
  13. FHT is available at http://wiki.openmusiclabs.com/wiki/ArduinoFHT
  14.  
  15.  
  16. The example I started off with was:
  17.  
  18. https://github.com/TJC/arduino/blob/master/fhttest/fhttest.cpp
  19.  
  20.  
  21. Note: If you are using a microphone powered by the 3.3V signal, such as the Sparkfun MEMS microphone, then connect 3.3V to the AREF pin.
  22.  
  23. Note: If you receive compile errors (as I have in the Stino add-on for Sublime Text), set the compiler to 'Full Compilation'.
  24.  
  25. */
  26.  
  27.  
  28. #define qsubd(x, b) ((x>b)?wavebright:0)                     // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  29. #define qsuba(x, b) ((x>b)?x-b:0)                            // Unsigned subtraction macro. if result <0, then => 0.
  30.  
  31. #define wavebright 128                                        // qsubd result will be this value if subtraction is >0.
  32.  
  33. #include "FastLED.h"                                          // FastLED library. Preferably the latest copy of FastLED 2.1.
  34.  
  35. // Fixed definitions cannot change on the fly.
  36. #define LED_DT 12                                             // Data pin to connect to the strip.
  37. #define LED_CK 11                                             // Clock pin, if using 4 pin strips.
  38. #define COLOR_ORDER GRB                                       // Are they RGB, GRB or what??
  39. #define LED_TYPE APA102                                       // What kind of strip are you using?
  40. #define NUM_LEDS 15                                           // Number of LED's.
  41.  
  42. // Initialize changeable global variables.
  43. uint8_t max_bright = 128;                                     // Overall brightness definition. It can be changed on the fly.
  44.  
  45. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  46.  
  47.  
  48. #define LOG_OUT 1
  49. // #define OCT_NORM 1
  50. // #define OCTAVE 1
  51.  
  52.  
  53. #define FHT_N 256                                             // Set to 256 point fht.
  54. #define inputPin A5
  55.  
  56. #include <FHT.h>                                              // FHT library
  57.  
  58. uint8_t D[NUM_LEDS][12];                                      // Our initial samples used to normalize the output.
  59. uint8_t E[NUM_LEDS];                                          // Our normalized values.
  60.  
  61. uint8_t hueinc;                                               // A hue increment value to make it rotate a bit.
  62. uint8_t micmult = 10;
  63.  
  64.  
  65. void setup() {
  66. //  analogReference(EXTERNAL);                                  // Connect 3.3V to AREF pin for any microphones using 3.3V
  67.   Serial.begin(57600);                                        // use the serial port
  68. //  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2812B LED_TYPE
  69.   LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102 LED_TYPE
  70.  
  71.   FastLED.setBrightness(max_bright);
  72.  
  73.   for (int k = 0; k<12; k++) {                                // Let's get some initial FHT samples.
  74.     GetFHT();                                                 // Get 'em fast
  75.     for (int i=0; i<NUM_LEDS; i++){
  76.       D[i][k] = fht_log_out[2*i+2];                           // Get every 2nd array element starting with the 2nd one.
  77. //      D[i][k] = fht_oct_out[2*i+2];
  78. //      Serial.print(fht_log_out[2*i+2]);
  79. //      Serial.print(fht_oct_out[2*i+2]);
  80. //      Serial.print(", ");
  81.     }
  82. //    Serial.println();
  83.   }
  84.  
  85.   for (int m = 0; m <NUM_LEDS; m++) {                         // We need to average out those samples to mellow out the noise.
  86.       E[m] = (D[m][0] + D[m][1] + D[m][2] + D[m][3] + D[m][4] + D[m][5] + D[m][6] + D[m][7] + D[m][8] + D[m][9] + D[m][10] + D[m][11])/12*1.75;      // Let's average and 'fudge' the noise limiter.
  87. //      Serial.print(E[m]);
  88. //      Serial.print(", ");
  89.   }
  90. }
  91.  
  92. void loop() {
  93.   fhtsound();
  94.   FastLED.show();
  95. }  
  96.  
  97.  
  98. void fhtsound() {
  99.   hueinc++;                                                   // A cute little hue incrementer.
  100.   GetFHT();                                                   // Let's take FHT_N samples and crunch 'em.
  101. //  for (int i = 0 ; i < FHT_N/2 ; i++)                       // You can process up to FHT_N/2 of them. Hmm, Nyquist.
  102. //    B[i] = fht_log_out[i];
  103.  
  104.   for (int i= 0; i < NUM_LEDS; i++) {                         // Run through the LED array.
  105. //        Serial.print(fht_oct_out[2*i+2]);
  106. //        Serial.print(", ");
  107.  
  108. //  int tmp = qsuba(fht_oct_out[2*i+2], E[i]);                // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
  109.   int tmp = qsuba(fht_log_out[2*i+2], E[i]);                  // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
  110.  
  111.     if (tmp > (leds[i].r + leds[i].g + leds[i].b))            // Refresh an LED only when the intensity is low
  112.         leds[i] = CHSV(tmp*micmult+hueinc, 255, tmp*micmult);  // Note how we really cranked up the tmp value to get BRIGHT LED's. Also increment the hue for fun.
  113.     leds[i].nscale8(224);                                     // Let's fade the whole thing over time as well.
  114.   }
  115. //          Serial.println();
  116. } // fhtsound()
  117.  
  118.  
  119.  
  120. void GetFHT() {
  121. //  Serial.println("GetFHT");
  122.   cli();
  123.   for (int i = 0 ; i < FHT_N ; i++) {
  124.     fht_input[i] = analogRead(inputPin) - 512;
  125. //    Serial.print(fht_input[i]);
  126. //    Serial.print(", ");
  127.   }
  128.   sei();
  129. //  Serial.println();
  130.     // save 256 samples
  131.   fht_window();                                               // Window the data for better frequency response.
  132.   fht_reorder();                                              // Reorder the data before doing the fht.
  133.   fht_run();                                                  // Process the data in the fht.
  134. //  fht_mag_octave();                                         // Take the output of the fht and put in an array called fht_log_out[FHT_N/2]
  135.   fht_mag_log();
  136. } // GetFHT()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement