Takehisa Oneta
/
SoundWS2812B-FFT
LED flash by FFT
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 //-------------------------------------------------------------- 00002 /** 00003 * SoundWS2812B-FFT for LPC1114FN28 00004 * 00005 * programed by Takehisa Oneta(ohneta) 00006 * Aug. 2015 00007 */ 00008 //-------------------------------------------------------------- 00009 00010 #include "mbed.h" 00011 #include <math.h> 00012 #include "neopixel.h" 00013 #include "fftReal.hpp" 00014 00015 #include "SoundWS2812B-FFT.h" 00016 00017 using namespace Mikami; 00018 00019 //-------------------------------------------------------------- 00020 #ifdef _DEBUG_UART_ 00021 Serial pc(USBTX, USBRX); 00022 #endif 00023 AnalogIn ainR(dp9); 00024 AnalogIn ainL(dp10); 00025 Ticker soundInterrupt; 00026 DigitalOut led(LED2); 00027 00028 //---------------------------------------------------- 00029 neopixel::PixelArray pixelArray(dp2, neopixel::BYTE_ORDER_GRB, neopixel::PROTOCOL_800KHZ); 00030 neopixel::Pixel gPixelRGB[] = { 00031 {0x00, 0x00, 0x00}, 00032 {0x00, 0x00, 0x00}, 00033 {0x00, 0x00, 0x00}, 00034 {0x00, 0x00, 0x00}, 00035 }; 00036 00037 #ifdef _DEBUG_UART_ 00038 const char *gBarGraf[] = { 00039 " ", 00040 "* ", 00041 "** ", 00042 "*** ", 00043 "**** ", 00044 "***** ", 00045 "****** ", 00046 "******* ", 00047 "******** ", 00048 "********* ", 00049 "********** ", 00050 "*********** ", 00051 "************ ", 00052 "************* ", 00053 "************** ", 00054 "*************** ", 00055 "**************** ", 00056 "***************** ", 00057 "****************** ", 00058 "******************* ", 00059 "******************** ", 00060 "********************* ", 00061 "********************** ", 00062 "*********************** ", 00063 "************************ ", 00064 "************************* ", 00065 "************************** ", 00066 "*************************** ", 00067 "**************************** ", 00068 "***************************** ", 00069 "****************************** ", 00070 "******************************* ", 00071 "********************************", 00072 }; 00073 #endif 00074 00075 //-------------------------------------------------------------- 00076 00077 uint32_t gSoundIntrFlag = 0; 00078 uint32_t gSoundIntrCount = 0; 00079 uint32_t gLedCounter = 0; 00080 00081 float gBufferR[SAMPLING_NUM]; 00082 float gBufferL[SAMPLING_NUM]; 00083 FftReal gFFT(SAMPLING_NUM); 00084 00085 //---------------------------------------------------- 00086 /** 00087 * Ticker handler 00088 */ 00089 void soundInterruptHandle() 00090 { 00091 if (gSoundIntrFlag == 0) { 00092 gSoundIntrFlag = 1; 00093 } 00094 gLedCounter++; 00095 } 00096 00097 //---------------------------------------------------- 00098 #ifdef _DEBUG_UART_ 00099 void uartFftView(Complex *yRight, Complex *yLeft) 00100 { 00101 00102 //pc.printf("%c[2J%c[;H", 27, 27); 00103 pc.printf("%c[%c;%cH", 27, 0, 0); 00104 //pc.printf("%c[;H", 27); 00105 00106 pc.printf("Right\r\n"); 00107 for (int n = 0; n < (SAMPLING_NUM / 2); n++) { 00108 uint32_t dt = (uint32_t)abs(yRight[n]); 00109 if (dt >= (SAMPLING_NUM / 2)) { 00110 dt = SAMPLING_NUM / 2; 00111 } 00112 pc.printf("%2d[%02d]: %s\r\n", n, dt, gBarGraf[dt]); 00113 } 00114 00115 pc.printf("\r\nLeft\r\n"); 00116 for (int n = 0; n < (SAMPLING_NUM / 2); n++) { 00117 uint32_t dt = (uint32_t)abs(yLeft[n]); 00118 if (dt >= (SAMPLING_NUM / 2)) { 00119 dt = SAMPLING_NUM / 2; 00120 } 00121 pc.printf("%2d[%02d]: %s\r\n", n, dt, gBarGraf[dt]); 00122 } 00123 } 00124 #endif 00125 00126 //---------------------------------------------------- 00127 void outputLeds(Complex *yRight, Complex *yLeft) 00128 { 00129 int r, g, b; 00130 #if 1 00131 r = ( (uint32_t)abs(yRight[1]) + 00132 (uint32_t)abs(yRight[2]) ); 00133 g = ( (uint32_t)abs(yLeft[1]) + 00134 (uint32_t)abs(yLeft[2]) ); 00135 #else 00136 r = ( (uint32_t)abs(yRight[0]) + 00137 (uint32_t)abs(yRight[1]) + 00138 (uint32_t)abs(yRight[2]) ); 00139 g = ( (uint32_t)abs(yLeft[0]) + 00140 (uint32_t)abs(yLeft[1]) + 00141 (uint32_t)abs(yLeft[2]) ); 00142 #endif 00143 b = (r + g) / 2; 00144 gPixelRGB[0].red = (r > 255) ? 255 : r; 00145 gPixelRGB[0].green = (g > 255) ? 255 : g; 00146 gPixelRGB[0].blue = (b > 255) ? 255 : b; 00147 00148 r = ( (uint32_t)abs(yRight[3]) + 00149 (uint32_t)abs(yRight[4]) + 00150 (uint32_t)abs(yRight[5]) + 00151 (uint32_t)abs(yRight[6]) ); 00152 g = ( (uint32_t)abs(yLeft[3]) + 00153 (uint32_t)abs(yLeft[4]) + 00154 (uint32_t)abs(yLeft[5]) + 00155 (uint32_t)abs(yLeft[6]) ); 00156 b = (r + g) / 2; 00157 gPixelRGB[1].red = (r > 255) ? 255 : r; 00158 gPixelRGB[1].green = (g > 255) ? 255 : g; 00159 gPixelRGB[1].blue = (b > 255) ? 255 : b; 00160 00161 r = ( (uint32_t)abs(yRight[7]) + 00162 (uint32_t)abs(yRight[8]) + 00163 (uint32_t)abs(yRight[9]) + 00164 (uint32_t)abs(yRight[10]) + 00165 (uint32_t)abs(yRight[11]) + 00166 (uint32_t)abs(yRight[12]) + 00167 (uint32_t)abs(yRight[14]) + 00168 (uint32_t)abs(yRight[15]) ); 00169 g = ( (uint32_t)abs(yLeft[7]) + 00170 (uint32_t)abs(yLeft[8]) + 00171 (uint32_t)abs(yLeft[9]) + 00172 (uint32_t)abs(yLeft[10]) + 00173 (uint32_t)abs(yLeft[11]) + 00174 (uint32_t)abs(yLeft[12]) + 00175 (uint32_t)abs(yLeft[14]) + 00176 (uint32_t)abs(yLeft[15]) ); 00177 b = (r + g) / 2; 00178 gPixelRGB[2].red = (r > 255) ? 255 : r; 00179 gPixelRGB[2].green = (g > 255) ? 255 : g; 00180 gPixelRGB[2].blue = (b > 255) ? 255 : b; 00181 00182 r = ( (uint32_t)abs(yRight[16]) + 00183 (uint32_t)abs(yRight[17]) + 00184 (uint32_t)abs(yRight[18]) + 00185 (uint32_t)abs(yRight[19]) + 00186 (uint32_t)abs(yRight[20]) + 00187 (uint32_t)abs(yRight[21]) + 00188 (uint32_t)abs(yRight[22]) + 00189 (uint32_t)abs(yRight[23]) + 00190 (uint32_t)abs(yRight[24]) + 00191 (uint32_t)abs(yRight[25]) + 00192 (uint32_t)abs(yRight[26]) + 00193 (uint32_t)abs(yRight[27]) + 00194 (uint32_t)abs(yRight[28]) + 00195 (uint32_t)abs(yRight[29]) + 00196 (uint32_t)abs(yRight[30]) + 00197 (uint32_t)abs(yRight[31]) + 00198 (uint32_t)abs(yRight[32]) ); 00199 g = ( (uint32_t)abs(yLeft[16]) + 00200 (uint32_t)abs(yLeft[17]) + 00201 (uint32_t)abs(yLeft[18]) + 00202 (uint32_t)abs(yLeft[19]) + 00203 (uint32_t)abs(yLeft[20]) + 00204 (uint32_t)abs(yLeft[21]) + 00205 (uint32_t)abs(yLeft[22]) + 00206 (uint32_t)abs(yLeft[23]) + 00207 (uint32_t)abs(yLeft[24]) + 00208 (uint32_t)abs(yLeft[25]) + 00209 (uint32_t)abs(yLeft[26]) + 00210 (uint32_t)abs(yLeft[27]) + 00211 (uint32_t)abs(yLeft[28]) + 00212 (uint32_t)abs(yLeft[29]) + 00213 (uint32_t)abs(yLeft[30]) + 00214 (uint32_t)abs(yLeft[31]) + 00215 (uint32_t)abs(yLeft[32]) ); 00216 b = (r + g) / 2; 00217 gPixelRGB[3].red = (r > 255) ? 255 : r; 00218 gPixelRGB[3].green = (g > 255) ? 255 : g; 00219 gPixelRGB[3].blue = (b > 255) ? 255 : b; 00220 00221 00222 pixelArray.update(gPixelRGB, 4); 00223 } 00224 00225 //---------------------------------------------------- 00226 //---------------------------------------------------- 00227 void outputLeds2(Complex *yRight, Complex *yLeft, float rVolume, float lVolume) 00228 { 00229 int rr, rg, rb; 00230 int lr, lg, lb; 00231 00232 if (rVolume > 1.0) rVolume = 1.0; 00233 if (lVolume > 1.0) lVolume = 1.0; 00234 00235 rr = ( 00236 (uint32_t)abs(yRight[1]) + 00237 (uint32_t)abs(yRight[2]) + 00238 (uint32_t)abs(yRight[3]) 00239 ); 00240 rg = (int)(rVolume * 255.0); 00241 rb = ( 00242 (uint32_t)abs(yRight[4]) + 00243 (uint32_t)abs(yRight[5]) + 00244 (uint32_t)abs(yRight[6]) + 00245 (uint32_t)abs(yRight[7]) + 00246 (uint32_t)abs(yRight[8]) + 00247 (uint32_t)abs(yRight[9]) + 00248 (uint32_t)abs(yRight[10]) + 00249 (uint32_t)abs(yRight[11]) + 00250 (uint32_t)abs(yRight[12]) + 00251 (uint32_t)abs(yRight[14]) + 00252 (uint32_t)abs(yRight[15]) + 00253 (uint32_t)abs(yRight[16]) + 00254 (uint32_t)abs(yRight[17]) + 00255 (uint32_t)abs(yRight[18]) + 00256 (uint32_t)abs(yRight[19]) + 00257 (uint32_t)abs(yRight[20]) + 00258 (uint32_t)abs(yRight[21]) + 00259 (uint32_t)abs(yRight[22]) + 00260 (uint32_t)abs(yRight[23]) + 00261 (uint32_t)abs(yRight[24]) + 00262 (uint32_t)abs(yRight[25]) + 00263 (uint32_t)abs(yRight[26]) + 00264 (uint32_t)abs(yRight[27]) + 00265 (uint32_t)abs(yRight[28]) + 00266 (uint32_t)abs(yRight[29]) + 00267 (uint32_t)abs(yRight[30]) + 00268 (uint32_t)abs(yRight[31]) + 00269 (uint32_t)abs(yRight[32]) 00270 ); 00271 rr = (rr > 255) ? 255 : rr; 00272 rg = (rg > 255) ? 255 : rg; 00273 rb = (rb > 255) ? 255 : rb; 00274 00275 00276 lr = ( 00277 (uint32_t)abs(yLeft[1]) + 00278 (uint32_t)abs(yLeft[2]) + 00279 (uint32_t)abs(yLeft[3]) 00280 ); 00281 lg = (int)(lVolume * 255.0); 00282 lb = ( 00283 (uint32_t)abs(yLeft[4]) + 00284 (uint32_t)abs(yLeft[5]) + 00285 (uint32_t)abs(yLeft[6]) + 00286 (uint32_t)abs(yLeft[7]) + 00287 (uint32_t)abs(yLeft[8]) + 00288 (uint32_t)abs(yLeft[9]) + 00289 (uint32_t)abs(yLeft[10]) + 00290 (uint32_t)abs(yLeft[11]) + 00291 (uint32_t)abs(yLeft[12]) + 00292 (uint32_t)abs(yLeft[14]) + 00293 (uint32_t)abs(yLeft[15]) + 00294 (uint32_t)abs(yLeft[16]) + 00295 (uint32_t)abs(yLeft[17]) + 00296 (uint32_t)abs(yLeft[18]) + 00297 (uint32_t)abs(yLeft[19]) + 00298 (uint32_t)abs(yLeft[20]) + 00299 (uint32_t)abs(yLeft[21]) + 00300 (uint32_t)abs(yLeft[22]) + 00301 (uint32_t)abs(yLeft[23]) + 00302 (uint32_t)abs(yLeft[24]) + 00303 (uint32_t)abs(yLeft[25]) + 00304 (uint32_t)abs(yLeft[26]) + 00305 (uint32_t)abs(yLeft[27]) + 00306 (uint32_t)abs(yLeft[28]) + 00307 (uint32_t)abs(yLeft[29]) + 00308 (uint32_t)abs(yLeft[30]) + 00309 (uint32_t)abs(yLeft[31]) + 00310 (uint32_t)abs(yLeft[32]) 00311 ); 00312 lr = (lr > 255) ? 255 : lr; 00313 lg = (lg > 255) ? 255 : lg; 00314 lb = (lb > 255) ? 255 : lb; 00315 00316 #if 0 00317 gPixelRGB[1].red = lr; 00318 gPixelRGB[1].green = lg; 00319 gPixelRGB[1].blue = lb; 00320 { 00321 int x; 00322 x = (int)((float)lr * 0.8 + (float)rr * 0.2); 00323 gPixelRGB[0].red = (x > 255) ? 255 : x; 00324 x = (int)((float)lg * 0.8 + (float)rg * 0.2); 00325 gPixelRGB[0].green = (x > 255) ? 255 : x; 00326 x = (int)((float)lb * 0.8 + (float)rb * 0.2); 00327 gPixelRGB[0].blue = (x > 255) ? 255 : x; 00328 00329 x = (int)((float)lr * 0.2 + (float)rr * 0.8); 00330 gPixelRGB[3].red = (x > 255) ? 255 : x; 00331 x = (int)((float)lg * 0.2 + (float)rg * 0.8); 00332 gPixelRGB[3].green = (x > 255) ? 255 : x; 00333 x = (int)((float)lb * 0.2 + (float)rb * 0.8); 00334 gPixelRGB[3].blue = (x > 255) ? 255 : x; 00335 } 00336 gPixelRGB[2].red = rr; 00337 gPixelRGB[2].green = rg; 00338 gPixelRGB[2].blue = rb; 00339 #else 00340 gPixelRGB[0].red = (lr >> 2); 00341 gPixelRGB[0].green = (lg >> 2); 00342 gPixelRGB[0].blue = (lb >> 1); 00343 00344 gPixelRGB[1].red = lr; 00345 gPixelRGB[1].green = lg; 00346 gPixelRGB[1].blue = lb; 00347 00348 gPixelRGB[2].red = rr; 00349 gPixelRGB[2].green = rg; 00350 gPixelRGB[2].blue = rb; 00351 00352 gPixelRGB[3].red = (rr >> 2); 00353 gPixelRGB[3].green = (rg >> 2); 00354 gPixelRGB[3].blue = (rb >> 1); 00355 00356 #endif 00357 00358 pixelArray.update(gPixelRGB, 4); 00359 } 00360 00361 //---------------------------------------------------- 00362 //---------------------------------------------------- 00363 00364 int main() 00365 { 00366 led = 0; 00367 #ifdef _DEBUG_UART_ 00368 pc.baud(115200); 00369 pc.printf("### START \n"); 00370 #endif 00371 00372 Complex yRight[SAMPLING_NUM / 2 + 1], yLeft[SAMPLING_NUM / 2 + 1]; 00373 00374 gSoundIntrFlag = 0; 00375 gSoundIntrCount = 0; 00376 soundInterrupt.attach_us(&soundInterruptHandle, 25); 00377 00378 float dd = 20.0f; 00379 while (1) { 00380 00381 if (gSoundIntrFlag == 1) { 00382 float r = ainR; 00383 float l = ainL; 00384 00385 gBufferR[gSoundIntrCount] = r * dd - 1.0f; 00386 if (gBufferR[gSoundIntrCount] < -1.0f) { 00387 gBufferR[gSoundIntrCount] = -1.0f; 00388 } else if (gBufferR[gSoundIntrCount] > 1.0f) { 00389 gBufferR[gSoundIntrCount] = 1.0f; 00390 } 00391 gBufferL[gSoundIntrCount] = l * dd - 1.0f; 00392 if (gBufferL[gSoundIntrCount] < -1.0f) { 00393 gBufferL[gSoundIntrCount] = -1.0f; 00394 } else if (gBufferL[gSoundIntrCount] > 1.0f) { 00395 gBufferL[gSoundIntrCount] = 1.0f; 00396 } 00397 00398 gSoundIntrCount++; 00399 if (gSoundIntrCount >= SAMPLING_NUM) { 00400 gSoundIntrCount = 0; 00401 00402 gFFT.Execute(gBufferR, yRight); 00403 gFFT.Execute(gBufferL, yLeft); 00404 #ifdef _DEBUG_UART_ 00405 uartFftView(yRight, yLeft); 00406 #endif 00407 //outputLeds(yRight, yLeft); 00408 outputLeds2(yRight, yLeft, r, l); 00409 00410 gSoundIntrFlag = 0; 00411 } 00412 } 00413 00414 if (gLedCounter > 1000) { 00415 led = !led; 00416 gLedCounter = 0; 00417 } 00418 00419 } 00420 00421 }
Generated on Sat Jul 23 2022 12:40:38 by 1.7.2