├── doc ├── Audivis_Processing-Overview.png └── Audivis_Processing-Overview.graphml ├── .gitignore ├── include └── fix_fft.h ├── .vscode └── extensions.json ├── README.md ├── platformio.ini ├── src ├── fix_fft.cpp └── M5StickC_AudiVisLedApp.cpp └── LICENSE /doc/Audivis_Processing-Overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/esikora/M5StickC_AudioVisLed/HEAD/doc/Audivis_Processing-Overview.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | 7 | include/README 8 | lib/README 9 | test/README 10 | 11 | *.log -------------------------------------------------------------------------------- /include/fix_fft.h: -------------------------------------------------------------------------------- 1 | #ifndef FIX_FFT_H 2 | #define FIX_FFT_H 3 | 4 | #include 5 | 6 | int32_t fix_fft(int16_t[], int16_t[], uint8_t, uint8_t); 7 | 8 | int32_t fix_fftr(int16_t[], uint8_t, uint8_t); 9 | 10 | #endif -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # M5StickC_AudioVisLed 2 | Audio visualization based on an M5StickC (ESP32): 3 | - Sampling of audio data from built-in microphone using i2s (44100 Hz sample rate) 4 | - Transformation of sampled data into the frequency domain using arduinoFFT (2048 samples FFT, 21.5 Hz frequency resolution) 5 | - Visualization of 20 frequency bands and beat detection using an RGB LED strip with 72 LEDs (configurable) 6 | 7 | ## Getting Started 8 | #### Development environment 9 | - Visual Studio Code (version 1.59.0) 10 | - PlatformIO IDE for VSCode 11 | 12 | #### System 13 | - Device: [M5StickC](https://docs.m5stack.com/#/en/core/m5stickc) 14 | - Platform: espressif32 15 | - Board: m5stick-c 16 | - Framework: arduino 17 | 18 | #### Peripherals 19 | - [RGB LED strip](https://docs.m5stack.com/en/unit/neopixel) 20 | 21 | #### Libraries used 22 | - M5StickC 23 | - arduinoFFT (develop branch) 24 | - FastLED 25 | 26 | ## Project Description 27 | 28 | A comprehensive description of this project is available at [hackster.io](https://www.hackster.io/esikora/audio-visualization-with-esp32-i2s-mic-and-rgb-led-strip-4a251c). 29 | 30 | ## License 31 | 32 | [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) 33 | 34 | See the [LICENSE](LICENSE) file for details. 35 | 36 | Copyright 2021 © Ernst Sikora 37 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [platformio] 12 | ;default_envs = M5StickC_AudioVisLed_Release 13 | default_envs = M5StickC_AudioVisLed_Debug 14 | 15 | [env:M5StickC_AudioVisLed_Debug] 16 | platform = espressif32 17 | board = m5stick-c 18 | framework = arduino 19 | 20 | lib_deps = 21 | M5StickC 22 | FastLED 23 | https://github.com/kosme/arduinoFFT.git#develop 24 | 25 | upload_speed = 1500000 26 | monitor_speed = 115200 27 | 28 | build_type = debug 29 | 30 | ;build_flags = -D CORE_DEBUG_LEVEL=5 ; 'Verbose' 31 | build_flags = -D CORE_DEBUG_LEVEL=4 ; 'Debug' 32 | 33 | monitor_filters = log2file, esp32_exception_decoder, default 34 | 35 | [env:M5StickC_AudioVisLed_LogRawAudio] 36 | platform = espressif32 37 | board = m5stick-c 38 | framework = arduino 39 | 40 | lib_deps = 41 | M5StickC 42 | FastLED 43 | https://github.com/kosme/arduinoFFT.git#develop 44 | 45 | upload_speed = 1500000 46 | monitor_speed = 115200 47 | 48 | build_type = debug 49 | 50 | ;build_flags = -D CORE_DEBUG_LEVEL=5 ; 'Verbose' 51 | build_flags = -D CORE_DEBUG_LEVEL=0 ; 'None' 52 | 53 | monitor_filters = log2file, direct 54 | 55 | [env:M5StickC_AudioVisLed_Release] 56 | platform = espressif32 57 | board = m5stick-c 58 | framework = arduino 59 | 60 | lib_deps = 61 | M5StickC 62 | FastLED 63 | https://github.com/kosme/arduinoFFT.git#develop 64 | 65 | upload_speed = 1500000 66 | monitor_speed = 115200 67 | 68 | build_type = release 69 | 70 | build_flags = -D CORE_DEBUG_LEVEL=1 71 | 72 | monitor_filters = time, default 73 | 74 | 75 | ; ***** Available Log Levels ***** 76 | 77 | ;#define ARDUHAL_LOG_LEVEL_NONE (0) 78 | ;#define ARDUHAL_LOG_LEVEL_ERROR (1) 79 | ;#define ARDUHAL_LOG_LEVEL_WARN (2) 80 | ;#define ARDUHAL_LOG_LEVEL_INFO (3) 81 | ;#define ARDUHAL_LOG_LEVEL_DEBUG (4) 82 | ;#define ARDUHAL_LOG_LEVEL_VERBOSE (5) -------------------------------------------------------------------------------- /src/fix_fft.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* fix_fft.c - Fixed-point in-place Fast Fourier Transform */ 4 | /* 5 | All data are fixed-point short integers, in which -32768 6 | to +32768 represent -1.0 to +1.0 respectively. Integer 7 | arithmetic is used for speed, instead of the more natural 8 | floating-point. 9 | For the forward FFT (time -> freq), fixed scaling is 10 | performed to prevent arithmetic overflow, and to map a 0dB 11 | sine/cosine wave (i.e. amplitude = 32767) to two -6dB freq 12 | coefficients. The return value is always 0. 13 | For the inverse FFT (freq -> time), fixed scaling cannot be 14 | done, as two 0dB coefficients would sum to a peak amplitude 15 | of 64K, overflowing the 32k range of the fixed-point integers. 16 | Thus, the fix_fft() routine performs variable scaling, and 17 | returns a value which is the number of bits LEFT by which 18 | the output must be shifted to get the actual amplitude 19 | (i.e. if fix_fft() returns 3, each value of fr[] and fi[] 20 | must be multiplied by 8 (2**3) for proper scaling. 21 | Clearly, this cannot be done within fixed-point short 22 | integers. In practice, if the result is to be used as a 23 | filter, the scale_shift can usually be ignored, as the 24 | result will be approximately correctly normalized as is. 25 | Written by: Tom Roberts 11/8/89 26 | Made portable: Malcolm Slaney 12/15/94 malcolm@interval.com 27 | Enhanced: Dimitrios P. Bouras 14 Jun 2006 dbouras@ieee.org 28 | */ 29 | 30 | /* 31 | 12/30/2020: Modifications for use with VSCode, PlatformIO and ESP32. 32 | - Changed type short to int16_t 33 | - Changed type int to int32_t 34 | */ 35 | 36 | #define N_WAVE 1024 /* full length of Sinewave[] */ 37 | #define LOG2_N_WAVE 10 /* log2(N_WAVE) */ 38 | 39 | /* 40 | Henceforth "short" implies 16-bit word. If this is not 41 | the case in your architecture, please replace "short" 42 | with a type definition which *is* a 16-bit word. 43 | */ 44 | 45 | /* 46 | Since we only use 3/4 of N_WAVE, we define only 47 | this many samples, in order to conserve data space. 48 | */ 49 | const int16_t Sinewave[N_WAVE-N_WAVE/4] = { 50 | 0, 201, 402, 603, 804, 1005, 1206, 1406, 51 | 1607, 1808, 2009, 2209, 2410, 2610, 2811, 3011, 52 | 3211, 3411, 3611, 3811, 4011, 4210, 4409, 4608, 53 | 4807, 5006, 5205, 5403, 5601, 5799, 5997, 6195, 54 | 6392, 6589, 6786, 6982, 7179, 7375, 7571, 7766, 55 | 7961, 8156, 8351, 8545, 8739, 8932, 9126, 9319, 56 | 9511, 9703, 9895, 10087, 10278, 10469, 10659, 10849, 57 | 11038, 11227, 11416, 11604, 11792, 11980, 12166, 12353, 58 | 12539, 12724, 12909, 13094, 13278, 13462, 13645, 13827, 59 | 14009, 14191, 14372, 14552, 14732, 14911, 15090, 15268, 60 | 15446, 15623, 15799, 15975, 16150, 16325, 16499, 16672, 61 | 16845, 17017, 17189, 17360, 17530, 17699, 17868, 18036, 62 | 18204, 18371, 18537, 18702, 18867, 19031, 19194, 19357, 63 | 19519, 19680, 19840, 20000, 20159, 20317, 20474, 20631, 64 | 20787, 20942, 21096, 21249, 21402, 21554, 21705, 21855, 65 | 22004, 22153, 22301, 22448, 22594, 22739, 22883, 23027, 66 | 23169, 23311, 23452, 23592, 23731, 23869, 24006, 24143, 67 | 24278, 24413, 24546, 24679, 24811, 24942, 25072, 25201, 68 | 25329, 25456, 25582, 25707, 25831, 25954, 26077, 26198, 69 | 26318, 26437, 26556, 26673, 26789, 26905, 27019, 27132, 70 | 27244, 27355, 27466, 27575, 27683, 27790, 27896, 28001, 71 | 28105, 28208, 28309, 28410, 28510, 28608, 28706, 28802, 72 | 28897, 28992, 29085, 29177, 29268, 29358, 29446, 29534, 73 | 29621, 29706, 29790, 29873, 29955, 30036, 30116, 30195, 74 | 30272, 30349, 30424, 30498, 30571, 30643, 30713, 30783, 75 | 30851, 30918, 30984, 31049, 31113, 31175, 31236, 31297, 76 | 31356, 31413, 31470, 31525, 31580, 31633, 31684, 31735, 77 | 31785, 31833, 31880, 31926, 31970, 32014, 32056, 32097, 78 | 32137, 32176, 32213, 32249, 32284, 32318, 32350, 32382, 79 | 32412, 32441, 32468, 32495, 32520, 32544, 32567, 32588, 80 | 32609, 32628, 32646, 32662, 32678, 32692, 32705, 32717, 81 | 32727, 32736, 32744, 32751, 32757, 32761, 32764, 32766, 82 | 32767, 32766, 32764, 32761, 32757, 32751, 32744, 32736, 83 | 32727, 32717, 32705, 32692, 32678, 32662, 32646, 32628, 84 | 32609, 32588, 32567, 32544, 32520, 32495, 32468, 32441, 85 | 32412, 32382, 32350, 32318, 32284, 32249, 32213, 32176, 86 | 32137, 32097, 32056, 32014, 31970, 31926, 31880, 31833, 87 | 31785, 31735, 31684, 31633, 31580, 31525, 31470, 31413, 88 | 31356, 31297, 31236, 31175, 31113, 31049, 30984, 30918, 89 | 30851, 30783, 30713, 30643, 30571, 30498, 30424, 30349, 90 | 30272, 30195, 30116, 30036, 29955, 29873, 29790, 29706, 91 | 29621, 29534, 29446, 29358, 29268, 29177, 29085, 28992, 92 | 28897, 28802, 28706, 28608, 28510, 28410, 28309, 28208, 93 | 28105, 28001, 27896, 27790, 27683, 27575, 27466, 27355, 94 | 27244, 27132, 27019, 26905, 26789, 26673, 26556, 26437, 95 | 26318, 26198, 26077, 25954, 25831, 25707, 25582, 25456, 96 | 25329, 25201, 25072, 24942, 24811, 24679, 24546, 24413, 97 | 24278, 24143, 24006, 23869, 23731, 23592, 23452, 23311, 98 | 23169, 23027, 22883, 22739, 22594, 22448, 22301, 22153, 99 | 22004, 21855, 21705, 21554, 21402, 21249, 21096, 20942, 100 | 20787, 20631, 20474, 20317, 20159, 20000, 19840, 19680, 101 | 19519, 19357, 19194, 19031, 18867, 18702, 18537, 18371, 102 | 18204, 18036, 17868, 17699, 17530, 17360, 17189, 17017, 103 | 16845, 16672, 16499, 16325, 16150, 15975, 15799, 15623, 104 | 15446, 15268, 15090, 14911, 14732, 14552, 14372, 14191, 105 | 14009, 13827, 13645, 13462, 13278, 13094, 12909, 12724, 106 | 12539, 12353, 12166, 11980, 11792, 11604, 11416, 11227, 107 | 11038, 10849, 10659, 10469, 10278, 10087, 9895, 9703, 108 | 9511, 9319, 9126, 8932, 8739, 8545, 8351, 8156, 109 | 7961, 7766, 7571, 7375, 7179, 6982, 6786, 6589, 110 | 6392, 6195, 5997, 5799, 5601, 5403, 5205, 5006, 111 | 4807, 4608, 4409, 4210, 4011, 3811, 3611, 3411, 112 | 3211, 3011, 2811, 2610, 2410, 2209, 2009, 1808, 113 | 1607, 1406, 1206, 1005, 804, 603, 402, 201, 114 | 0, -201, -402, -603, -804, -1005, -1206, -1406, 115 | -1607, -1808, -2009, -2209, -2410, -2610, -2811, -3011, 116 | -3211, -3411, -3611, -3811, -4011, -4210, -4409, -4608, 117 | -4807, -5006, -5205, -5403, -5601, -5799, -5997, -6195, 118 | -6392, -6589, -6786, -6982, -7179, -7375, -7571, -7766, 119 | -7961, -8156, -8351, -8545, -8739, -8932, -9126, -9319, 120 | -9511, -9703, -9895, -10087, -10278, -10469, -10659, -10849, 121 | -11038, -11227, -11416, -11604, -11792, -11980, -12166, -12353, 122 | -12539, -12724, -12909, -13094, -13278, -13462, -13645, -13827, 123 | -14009, -14191, -14372, -14552, -14732, -14911, -15090, -15268, 124 | -15446, -15623, -15799, -15975, -16150, -16325, -16499, -16672, 125 | -16845, -17017, -17189, -17360, -17530, -17699, -17868, -18036, 126 | -18204, -18371, -18537, -18702, -18867, -19031, -19194, -19357, 127 | -19519, -19680, -19840, -20000, -20159, -20317, -20474, -20631, 128 | -20787, -20942, -21096, -21249, -21402, -21554, -21705, -21855, 129 | -22004, -22153, -22301, -22448, -22594, -22739, -22883, -23027, 130 | -23169, -23311, -23452, -23592, -23731, -23869, -24006, -24143, 131 | -24278, -24413, -24546, -24679, -24811, -24942, -25072, -25201, 132 | -25329, -25456, -25582, -25707, -25831, -25954, -26077, -26198, 133 | -26318, -26437, -26556, -26673, -26789, -26905, -27019, -27132, 134 | -27244, -27355, -27466, -27575, -27683, -27790, -27896, -28001, 135 | -28105, -28208, -28309, -28410, -28510, -28608, -28706, -28802, 136 | -28897, -28992, -29085, -29177, -29268, -29358, -29446, -29534, 137 | -29621, -29706, -29790, -29873, -29955, -30036, -30116, -30195, 138 | -30272, -30349, -30424, -30498, -30571, -30643, -30713, -30783, 139 | -30851, -30918, -30984, -31049, -31113, -31175, -31236, -31297, 140 | -31356, -31413, -31470, -31525, -31580, -31633, -31684, -31735, 141 | -31785, -31833, -31880, -31926, -31970, -32014, -32056, -32097, 142 | -32137, -32176, -32213, -32249, -32284, -32318, -32350, -32382, 143 | -32412, -32441, -32468, -32495, -32520, -32544, -32567, -32588, 144 | -32609, -32628, -32646, -32662, -32678, -32692, -32705, -32717, 145 | -32727, -32736, -32744, -32751, -32757, -32761, -32764, -32766, 146 | }; 147 | 148 | /* 149 | FIX_MPY() - fixed-point multiplication & scaling. 150 | Substitute inline assembly for hardware-specific 151 | optimization suited to a particluar DSP processor. 152 | Scaling ensures that result remains 16-bit. 153 | */ 154 | inline int16_t FIX_MPY(int16_t a, int16_t b) 155 | { 156 | /* shift right one less bit (i.e. 15-1) */ 157 | int32_t c = ((int32_t)a * (int32_t)b) >> 14; 158 | /* last bit shifted out = rounding-bit */ 159 | b = c & 0x01; 160 | /* last shift + rounding bit */ 161 | a = (c >> 1) + b; 162 | return a; 163 | } 164 | 165 | /* 166 | fix_fft() - perform forward/inverse fast Fourier transform. 167 | fr[n],fi[n] are real and imaginary arrays, both INPUT AND 168 | RESULT (in-place FFT), with 0 <= n < 2**m; set inverse to 169 | 0 for forward transform (FFT), or 1 for iFFT. 170 | */ 171 | int32_t fix_fft(int16_t fr[], int16_t fi[], uint8_t m, uint8_t inverse) 172 | { 173 | int32_t mr, nn, i, j, l, k, istep, n, scale, shift; 174 | int16_t qr, qi, tr, ti, wr, wi; 175 | 176 | n = 1 << m; 177 | 178 | /* max FFT size = N_WAVE */ 179 | if (n > N_WAVE) 180 | return -1; 181 | 182 | mr = 0; 183 | nn = n - 1; 184 | scale = 0; 185 | 186 | /* decimation in time - re-order data */ 187 | for (m=1; m<=nn; ++m) { 188 | l = n; 189 | do { 190 | l >>= 1; 191 | } while (mr+l > nn); 192 | mr = (mr & (l-1)) + l; 193 | 194 | if (mr <= m) 195 | continue; 196 | tr = fr[m]; 197 | fr[m] = fr[mr]; 198 | fr[mr] = tr; 199 | ti = fi[m]; 200 | fi[m] = fi[mr]; 201 | fi[mr] = ti; 202 | } 203 | 204 | l = 1; 205 | k = LOG2_N_WAVE-1; 206 | while (l < n) { 207 | if (inverse) { 208 | /* variable scaling, depending upon data */ 209 | shift = 0; 210 | for (i=0; i 16383 || m > 16383) { 218 | shift = 1; 219 | break; 220 | } 221 | } 222 | if (shift) 223 | ++scale; 224 | } else { 225 | /* 226 | fixed scaling, for proper normalization -- 227 | there will be log2(n) passes, so this results 228 | in an overall factor of 1/n, distributed to 229 | maximize arithmetic accuracy. 230 | */ 231 | shift = 1; 232 | } 233 | /* 234 | it may not be obvious, but the shift will be 235 | performed on each data point exactly once, 236 | during this pass. 237 | */ 238 | istep = l << 1; 239 | for (m=0; m>= 1; 248 | wi >>= 1; 249 | } 250 | for (i=m; i>= 1; 258 | qi >>= 1; 259 | } 260 | fr[j] = qr - tr; 261 | fi[j] = qi - ti; 262 | fr[i] = qr + tr; 263 | fi[i] = qi + ti; 264 | } 265 | } 266 | --k; 267 | l = istep; 268 | } 269 | return scale; 270 | } 271 | 272 | /* 273 | fix_fftr() - forward/inverse FFT on array of real numbers. 274 | Real FFT/iFFT using half-size complex FFT by distributing 275 | even/odd samples into real/imaginary arrays respectively. 276 | In order to save data space (i.e. to avoid two arrays, one 277 | for real, one for imaginary samples), we proceed in the 278 | following two steps: a) samples are rearranged in the real 279 | array so that all even samples are in places 0-(N/2-1) and 280 | all imaginary samples in places (N/2)-(N-1), and b) fix_fft 281 | is called with fr and fi pointing to index 0 and index N/2 282 | respectively in the original array. The above guarantees 283 | that fix_fft "sees" consecutive real samples as alternating 284 | real and imaginary samples in the complex array. 285 | */ 286 | int32_t fix_fftr(int16_t f[], uint8_t m, uint8_t inverse) 287 | { 288 | int32_t i, N = 1<<(m-1), scale = 0; 289 | int16_t tt, *fr=f, *fi=&f[N]; 290 | 291 | if (inverse) 292 | scale = fix_fft(fi, fr, m-1, inverse); 293 | for (i=1; i 2 | 3 | 4 | 5 | 6 | true 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | {"version":"2.0.0","theme":{"name":"dark","version":"1.0.0"}} 60 | 61 | 62 | 5 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | Application 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 0 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 1 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 2 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 3 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 4 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 5 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 6 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 7 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 6 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | -------------------------------------------------------------------------------- /src/M5StickC_AudiVisLedApp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | M5StickC_AudioVisLedApp: 3 | This application has been developed to use an M5StickC device (ESP32) 4 | as an audio sampling and visualization device. It samples audio data 5 | from the built-in microphone using i2s. The sampled data is transformed 6 | into the frequency domain using the arduinoFFT library. 7 | Copyright (C) 2021 by Ernst Sikora 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | 30 | #include 31 | 32 | #include 33 | 34 | /* ----- General constants ----- */ 35 | 36 | const uint16_t kSampleRate = 44100; // Unit: Hz 37 | 38 | /* ----- FFT constants ----- */ 39 | 40 | typedef float fftData_t; 41 | 42 | const uint8_t kFFT_SampleCountLog2 = 11; 43 | 44 | const uint16_t kFFT_SampleCount = 1 << kFFT_SampleCountLog2; 45 | 46 | const fftData_t kFFT_SampleCountInv = 1.0f / kFFT_SampleCount; 47 | 48 | const fftData_t kFFT_SamplingFreq = (fftData_t) kSampleRate; 49 | 50 | const uint16_t kFFT_FreqBinCount = kFFT_SampleCount / 2; 51 | 52 | const float kFFT_FreqStep = kFFT_SamplingFreq / kFFT_SampleCount; 53 | 54 | /* ----- FFT variables ----- */ 55 | 56 | fftData_t fftDataReal_[kFFT_SampleCount] = {0.0}; 57 | 58 | fftData_t fftDataImag_[kFFT_SampleCount] = {0.0}; 59 | 60 | fftData_t magnitudeSpectrumAvg_[kFFT_FreqBinCount] = {0}; 61 | 62 | ArduinoFFT fft_ = ArduinoFFT(fftDataReal_, fftDataImag_, kFFT_SampleCount, kFFT_SamplingFreq); // Create FFT object 63 | 64 | /* ----- i2s hardware constants ----- */ 65 | 66 | const i2s_port_t kI2S_Port = I2S_NUM_0; 67 | 68 | const int kI2S_PinClk = 0; 69 | 70 | const int kI2S_PinData = 34; 71 | 72 | /* ----- i2s constants ----- */ 73 | 74 | const i2s_bits_per_sample_t kI2S_BitsPerSample = I2S_BITS_PER_SAMPLE_16BIT; 75 | 76 | const uint8_t kI2S_BytesPerSample = kI2S_BitsPerSample / 8; 77 | 78 | const uint16_t kI2S_ReadSizeBytes = kFFT_SampleCount * kI2S_BytesPerSample; 79 | 80 | const uint16_t kI2S_BufferSizeSamples = 1024; 81 | 82 | const uint16_t kI2S_BufferSizeBytes = kI2S_BufferSizeSamples * kI2S_BytesPerSample; 83 | 84 | const uint16_t kI2S_BufferCount = (3 * kFFT_SampleCount) / (2 * kI2S_BufferSizeSamples); 85 | 86 | const uint8_t kI2S_BufferCountPerFFT = kFFT_SampleCount / kI2S_BufferSizeSamples; 87 | 88 | const int kI2S_QueueLength = 16; 89 | 90 | /* ----- i2s variables ----- */ 91 | 92 | int16_t micReadBuffer_[kFFT_SampleCount] = {0}; 93 | 94 | QueueHandle_t pI2S_Queue_ = nullptr; 95 | 96 | /* ----- Fastled constants ----- */ 97 | 98 | const uint8_t kPinLedStrip = 32; // M5StickC grove port, yellow cable 99 | 100 | const uint8_t kNumLeds = 72; 101 | 102 | const uint8_t kLedStripBrightness = 50; 103 | 104 | /* ----- Fastled variables ----- */ 105 | 106 | // LED strip controller 107 | CRGB ledStrip_[kNumLeds]; 108 | 109 | // Frequency bands 110 | // Source: https://www.teachmeaudio.com/mixing/techniques/audio-spectrum 111 | // 112 | // Sub-bass: 20-60 Hz 113 | // Bass: 60-250 Hz 114 | // Low midrange: 250-500 Hz 115 | // Midrange: 500-2000 Hz 116 | // Upper midrange: 2000-4000 Hz 117 | // Presence: 4000-6000 Hz 118 | // Brilliance: 6000-20000 Hz 119 | 120 | // 20Hz, 25Hz, 31.5Hz, 40Hz, 50Hz, 63Hz, 80Hz, 100Hz, 125Hz 160Hz, 200Hz, 250Hz, 315Hz, 400Hz, 500Hz, 630Hz, 800Hz, 1kHz, 1.25kHz, 1.6kHz, 2kHz, 2.5kHz, 3.15kHz, 4kHz, 5kHz, 6.3kHz, 8kHz, 10kHz, 12.5kHz, 16kHz, 20kHz 121 | 122 | //const uint8_t kFreqBandCount = 7; 123 | //const float kFreqBandStartHz = 20; 124 | //const float kFreqBandEndHz[kFreqBandCount] = {60, 250, 500, 2000, 4000, 6000, 20000}; 125 | 126 | const uint8_t kFreqBandCount = 20; 127 | 128 | const float kFreqBandStartHz = 20; 129 | 130 | // Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 131 | const float kFreqBandEndHz[kFreqBandCount] = { 30, 50, 75, 100, 140, 180, 225, 270, 350, 440, 550, 700, 900, 1100, 1400, 1800, 2200, 2800, 3550, 18000}; 132 | const float kFreqBandAmp[kFreqBandCount] = { 0.15f, 0.3f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f, 0.3f, 0.4f, 0.4f, 0.4f, 0.5f, 0.8f, 1, 1, 1, 1, 1, 0.3f}; 133 | 134 | fftData_t sensitivityFactor_ = 1; 135 | const float kSensitivityFactorMax = 1000.0f; 136 | 137 | float magnitudeBandMax_[kFreqBandCount] = { 0.0f }; 138 | 139 | /* Start indices, end indices and normalization factors for each frequency band 140 | The indices relate to the frequency bins resulting from the FFT */ 141 | uint16_t freqBandBinIdxStart_[kFreqBandCount] = { 0 }; 142 | uint16_t freqBandBinIdxEnd_[kFreqBandCount] = { 0 }; 143 | uint16_t freqBandBinCount_[kFreqBandCount] = { 0 }; 144 | 145 | /* ----- Beat detection constants and variables ----- */ 146 | 147 | const uint8_t kBeatDetectBand = 3; 148 | 149 | const float kBeatThreshold = 4.0f; 150 | 151 | float beatHist_[3] = {0.0f}; 152 | 153 | uint8_t beatVisIntensity_ = 0; 154 | 155 | 156 | bool setupI2Smic() 157 | { 158 | esp_err_t i2sErr; 159 | 160 | // i2s configuration for sampling 16 bit mono audio data 161 | // 162 | // Notes related to i2s.c: 163 | // - 'dma_buf_len', i.e. the number of samples in each DMA buffer, is limited to 1024 164 | // - 'dma_buf_len' * 'bytes_per_sample' is limted to 4092 165 | // - 'I2S_CHANNEL_FMT_ONLY_RIGHT' means "mono", i.e. only one channel to be received via i2s 166 | // In the M5StickC microphone example 'I2S_CHANNEL_FMT_ALL_RIGHT' is used which means two channels. 167 | // Afterwards, i2s_set_clk is called to change the DMA configuration to just one channel. 168 | // 169 | i2s_config_t i2sConfig = { 170 | .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM), 171 | .sample_rate = kSampleRate, 172 | .bits_per_sample = kI2S_BitsPerSample, 173 | .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT, 174 | .communication_format = I2S_COMM_FORMAT_I2S, 175 | .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, 176 | .dma_buf_count = kI2S_BufferCount, 177 | .dma_buf_len = kI2S_BufferSizeSamples 178 | }; 179 | 180 | i2sErr = i2s_driver_install(kI2S_Port, &i2sConfig, kI2S_QueueLength, &pI2S_Queue_); 181 | 182 | if (i2sErr) 183 | { 184 | log_e("Failed to start i2s driver. ESP error: %s (%x)", esp_err_to_name(i2sErr), i2sErr); 185 | return false; 186 | } 187 | 188 | if (pI2S_Queue_ == nullptr) 189 | { 190 | log_e("Failed to setup i2s event queue."); 191 | return false; 192 | } 193 | 194 | // Configure i2s pins for sampling audio data from the built-in microphone of the M5StickC 195 | i2s_pin_config_t i2sPinConfig = { 196 | .bck_io_num = I2S_PIN_NO_CHANGE, 197 | .ws_io_num = kI2S_PinClk, 198 | .data_out_num = I2S_PIN_NO_CHANGE, 199 | .data_in_num = kI2S_PinData 200 | }; 201 | 202 | i2sErr = i2s_set_pin(kI2S_Port, &i2sPinConfig); 203 | 204 | if (i2sErr) 205 | { 206 | log_e("Failed to set i2s pins. ESP error: %s (%x)", esp_err_to_name(i2sErr), i2sErr); 207 | return false; 208 | } 209 | 210 | return true; 211 | } 212 | 213 | bool setupSpectrumAnalysis() 214 | { 215 | bool success = true; 216 | 217 | // Variables for bin indices and bin count belonging to the current frequency band 218 | uint16_t binIdxStart; // Index of the first frequency bin of the current frequency band 219 | uint16_t binIdxEnd; // Index of the last frequency bin of the current frequency band 220 | 221 | // Set bin index for the start of the first frequency band 222 | binIdxStart = ceilf( kFreqBandStartHz / kFFT_FreqStep); 223 | 224 | // Compute values for all frequency bands 225 | for (uint8_t bandIdx = 0; bandIdx < kFreqBandCount; bandIdx++) 226 | { 227 | // Store index of first frequency bin of current band 228 | if ( binIdxStart < kFFT_FreqBinCount ) 229 | { 230 | freqBandBinIdxStart_[bandIdx] = binIdxStart; 231 | } 232 | else 233 | { 234 | freqBandBinIdxStart_[bandIdx] = 0; 235 | 236 | success = false; 237 | 238 | log_e("Failed to set start bin index for frequency band no. %d", bandIdx); 239 | } 240 | 241 | // Compute index of last frequency bin of current band 242 | binIdxEnd = ceilf( kFreqBandEndHz[bandIdx] / kFFT_FreqStep ) - 1; 243 | 244 | if ( binIdxEnd < kFFT_FreqBinCount) 245 | { 246 | freqBandBinIdxEnd_[bandIdx] = binIdxEnd; 247 | } 248 | else 249 | { 250 | freqBandBinIdxEnd_[bandIdx] = 0; 251 | binIdxEnd = kFFT_FreqBinCount - 1; 252 | 253 | success = false; 254 | 255 | log_e("Failed to set end bin index for frequency band no. %d", bandIdx); 256 | } 257 | 258 | // Compute bin count for current band 259 | freqBandBinCount_[bandIdx] = binIdxEnd - binIdxStart + 1; 260 | 261 | // Set binIdxStart for next band 262 | binIdxStart = binIdxEnd + 1; 263 | 264 | log_d("Bins in band %d: %d to %d. Number of bins: %d.", 265 | bandIdx, 266 | freqBandBinIdxStart_[bandIdx], freqBandBinIdxEnd_[bandIdx], 267 | freqBandBinCount_[bandIdx]); 268 | } 269 | 270 | return success; 271 | } 272 | 273 | void setupLedStrip() 274 | { 275 | FastLED.addLeds(ledStrip_, kNumLeds); 276 | FastLED.clear(); 277 | FastLED.setBrightness(kLedStripBrightness); 278 | FastLED.setMaxPowerInVoltsAndMilliamps(5, 450); // Set maximum power consumption to 5 V and 450 mA 279 | ledStrip_[0].setHSV(60, 255, 255); 280 | FastLED.show(); 281 | } 282 | 283 | void setup() { 284 | M5.begin(); 285 | M5.Lcd.setRotation(1); 286 | M5.Lcd.fillScreen(BLACK); 287 | 288 | M5.Lcd.setTextSize(2); 289 | M5.Lcd.setTextColor(WHITE, BLUE); 290 | M5.Lcd.println("Audio Vis"); 291 | 292 | setupI2Smic(); 293 | 294 | setupSpectrumAnalysis(); 295 | 296 | setupLedStrip(); 297 | 298 | log_d("Setup successfully completed."); 299 | 300 | log_d("portTICK_PERIOD_MS: %d", portTICK_PERIOD_MS); 301 | 302 | delay(1000); 303 | } 304 | 305 | unsigned long timeReadLastMicros_ = 0; 306 | 307 | uint8_t userTrigger_ = 0; 308 | 309 | uint8_t cycleNr_ = 0; 310 | 311 | float maxCurrent_ = 0.0f; 312 | 313 | /* 314 | uint16_t testSignalFreqFactor_ = 0; 315 | */ 316 | 317 | void loop() { 318 | 319 | esp_err_t i2sErr = ESP_OK; 320 | 321 | size_t i2sBytesRead = 0; 322 | 323 | // Store time stamp for debug output 324 | unsigned long timeBeforeReadMicros = micros(); 325 | 326 | // Note: If the I2S DMA buffer is empty, 'i2s_read' blocks the current thread until data becomes available 327 | i2sErr = i2s_read(kI2S_Port, micReadBuffer_, kI2S_ReadSizeBytes, &i2sBytesRead, 100 / portTICK_PERIOD_MS); 328 | 329 | // Get timestamp after reading 330 | unsigned long timeAferReadMicros = micros(); 331 | 332 | // Compute read duration for debug output 333 | unsigned long timeInRead = timeAferReadMicros - timeBeforeReadMicros; 334 | 335 | // Compute duration since last read 336 | unsigned long timeBetweenRead = timeAferReadMicros - timeReadLastMicros_; 337 | 338 | // Store timestamp for next computation 339 | timeReadLastMicros_ = timeAferReadMicros; 340 | 341 | // Check i2s error state after reading 342 | if (i2sErr) 343 | { 344 | log_e("i2s_read failure. ESP error: %s (%x)", esp_err_to_name(i2sErr), i2sErr); 345 | } 346 | 347 | // Check whether right number of bytes has been read 348 | if (i2sBytesRead != kI2S_ReadSizeBytes) 349 | { 350 | log_w("i2s_read unexpected number of bytes: %d", i2sBytesRead); 351 | } 352 | 353 | // Analyse event queue to check whether i2s is working correctly 354 | i2s_event_t i2sEvent = {}; 355 | uint8_t i2sEventRxDoneCount = 0; 356 | 357 | uint8_t i2sMsgCount = uxQueueMessagesWaiting(pI2S_Queue_); 358 | 359 | log_v("Number of I2S events waiting in queue: %d", i2sMsgCount); 360 | 361 | // Iterate over all events in the i2s event queue 362 | for (uint8_t i = 0; i < i2sMsgCount; i++) 363 | { 364 | // Take next event from queue 365 | if ( xQueueReceive(pI2S_Queue_, (void*) &i2sEvent, 0) == pdTRUE ) 366 | { 367 | switch (i2sEvent.type) 368 | { 369 | case I2S_EVENT_DMA_ERROR: 370 | log_e("I2S_EVENT_DMA_ERROR"); 371 | break; 372 | 373 | case I2S_EVENT_TX_DONE: 374 | log_v("I2S_EVENT_TX_DONE"); 375 | break; 376 | 377 | // Count the number of "RX done" events 378 | case I2S_EVENT_RX_DONE: 379 | log_v("I2S_EVENT_RX_DONE"); 380 | i2sEventRxDoneCount += 1; 381 | break; 382 | 383 | case I2S_EVENT_MAX: 384 | log_w("I2S_EVENT_MAX"); 385 | break; 386 | } 387 | } 388 | } 389 | 390 | // If there are more RX done events in the queue than expected, probably data processing takes too long 391 | if (i2sEventRxDoneCount > kI2S_BufferCountPerFFT) 392 | { 393 | log_w("Frame loss. Number of I2S_EVENT_RX_DONE events is: %d", i2sEventRxDoneCount); 394 | } 395 | else 396 | { 397 | if (i2sEventRxDoneCount < kI2S_BufferCountPerFFT) 398 | { 399 | log_e("Configuration error? Number of I2S_EVENT_RX_DONE events is: %d", i2sEventRxDoneCount); 400 | } 401 | } 402 | 403 | log_v("Read duration [µs]: %d. Duration since last read [µs]: %d", timeInRead, timeBetweenRead); 404 | 405 | // Store start time of processing to compute duration later on 406 | unsigned long timeStartMicros = micros(); 407 | 408 | // Compute sum of the current sample block 409 | int32_t blockSum = micReadBuffer_[0]; 410 | 411 | for (uint16_t i = 1; i < kFFT_SampleCount; i++) 412 | { 413 | blockSum += micReadBuffer_[i]; 414 | } 415 | 416 | // Compute average value for the current sample block 417 | int16_t blockAvg = blockSum / kFFT_SampleCount; 418 | 419 | /* 420 | // Increment factor for test signal frequency 421 | if ( slotNr_ == 0 ) 422 | { 423 | testSignalFreqFactor_ += 1; 424 | 425 | if ( testSignalFreqFactor_ > kFFT_FreqBinCount ) 426 | { 427 | testSignalFreqFactor_ = 1; 428 | } 429 | } 430 | */ 431 | 432 | // Initialize fft input data 433 | for (uint16_t i = 0; i < kFFT_SampleCount; i++) 434 | { 435 | // Corrected input value: Subtract the block average from each sample in order remove the DC component 436 | int16_t v = micReadBuffer_[i] - blockAvg; 437 | 438 | // Constant for normalizing int16 input values to floating point range -1.0 to 1.0 439 | const fftData_t kInt16MaxInv = 1.0f / __INT16_MAX__; 440 | 441 | // Input value in floating point representation 442 | fftData_t r; 443 | 444 | // Compute input value for FFT 445 | r = kInt16MaxInv * v; 446 | 447 | /* 448 | // Generate test signal 449 | const float k2Pi = 6.2831853f; 450 | const float k2PiSampleCountInv = k2Pi * kFFT_SampleCountInv; 451 | 452 | r = sinf( k2PiSampleCountInv * (testSignalFreqFactor_ * i) ); 453 | */ 454 | 455 | // Store value in FFT input array 456 | fftDataReal_[i] = r; 457 | fftDataImag_[i] = 0.0f; 458 | } 459 | 460 | //fft_.windowing(FFTWindow::Hamming, FFTDirection::Forward); 461 | fft_.compute(FFTDirection::Forward); 462 | 463 | fftData_t magnitudeSum = 0; 464 | 465 | // Compute magnitude value for each frequency bin, i.e. only first half of the FFT results 466 | for (uint16_t i = 0; i < kFFT_FreqBinCount; i++) 467 | { 468 | float magValNew = sqrtf( fftDataReal_[i] * fftDataReal_[i] + fftDataImag_[i] * fftDataImag_[i] ); 469 | 470 | // Update the averaged spectrum using the current values 471 | const float w1 = 16.0f / 128.0f; 472 | const float w2 = 1 - w1; 473 | 474 | // Compute low pass filtered magnitude for each frequency bin 475 | magnitudeSpectrumAvg_[i] = magValNew * w1 + magnitudeSpectrumAvg_[i] * w2; 476 | 477 | // Compute overall sum of all (low pass filtered) frequency bins 478 | magnitudeSum += magnitudeSpectrumAvg_[i]; 479 | } 480 | 481 | // Compute magnitude for each frequency band as maximum over all contained frequency bins 482 | float magnitudeBand[kFreqBandCount] = { 0.0f }; 483 | 484 | float magnitudeBandWeightedMax = 0.0f; 485 | 486 | for (uint8_t bandIdx = 0; bandIdx < kFreqBandCount; bandIdx++) 487 | { 488 | // Interate over all frequency bins assigned to the frequency band 489 | for (uint16_t binIdx = freqBandBinIdxStart_[bandIdx]; binIdx <= freqBandBinIdxEnd_[bandIdx]; binIdx++) 490 | { 491 | // Apply maximum norm to the frequency bins of each frequency band 492 | if ( magnitudeSpectrumAvg_[binIdx] > magnitudeBand[bandIdx] ) 493 | magnitudeBand[bandIdx] = magnitudeSpectrumAvg_[binIdx]; 494 | } 495 | 496 | float magnitudeBandWeighted = magnitudeBand[bandIdx] * kFreqBandAmp[bandIdx]; 497 | 498 | // Compute maximum magnitude value for each frequency band 499 | if ( magnitudeBand[bandIdx] > magnitudeBandMax_[bandIdx] ) 500 | { 501 | magnitudeBandMax_[bandIdx] = magnitudeBand[bandIdx]; 502 | } 503 | 504 | // Compute maximum magnitude value across all frequency bands 505 | if ( magnitudeBandWeighted > magnitudeBandWeightedMax ) 506 | { 507 | magnitudeBandWeightedMax = magnitudeBandWeighted; 508 | } 509 | } 510 | 511 | // Update the sensitivity factor 512 | const float s1 = 8.0f / 1024.0f; 513 | const float s2 = 1.0f - s1; 514 | sensitivityFactor_ = min( (250.0f / magnitudeBandWeightedMax) * s1 + sensitivityFactor_ * s2, kSensitivityFactorMax ); 515 | 516 | // ----- Beat detection ----- 517 | 518 | // Maintain history of last three magnitude values of the bass band 519 | beatHist_[0] = beatHist_[1]; 520 | beatHist_[1] = beatHist_[2]; 521 | beatHist_[2] = magnitudeBand[kBeatDetectBand] * kFreqBandAmp[kBeatDetectBand] * sensitivityFactor_; 522 | 523 | float diff1 = beatHist_[1] - beatHist_[0]; 524 | float diff2 = beatHist_[2] - beatHist_[1]; 525 | 526 | // Detect magnitude peak 527 | if ( ((diff1 >= kBeatThreshold) && (diff2 < 0)) || ((diff1 > 0) && (diff2 <= -kBeatThreshold)) ) 528 | { 529 | beatVisIntensity_ = 250; 530 | } 531 | else { 532 | if ( beatVisIntensity_ >= 25 ) 533 | beatVisIntensity_ -= 25; 534 | } 535 | 536 | // ----- Update the Led strip ----- 537 | 538 | if (kNumLeds <= 2*kFreqBandCount + 4) 539 | { 540 | // Show beat detection at the beginning of the strip 541 | const uint8_t numBassLeds = (kNumLeds - kFreqBandCount) / 2; 542 | 543 | for (int i = 0; i < numBassLeds; i++) 544 | { 545 | ledStrip_[i].setHSV( 250, 255, beatVisIntensity_ ); 546 | } 547 | 548 | // Show frequency intensities on the remaining Leds 549 | const uint8_t colorStart = 30; 550 | const uint8_t colorEnd = 210; 551 | const uint8_t colorStep = (colorEnd - colorStart) / kFreqBandCount; 552 | 553 | for (int k = 0; k < kFreqBandCount; k++) 554 | { 555 | uint8_t color = colorStart + k * colorStep; 556 | uint8_t lightness = min( int(magnitudeBand[k] * kFreqBandAmp[k] * sensitivityFactor_), 255); 557 | 558 | ledStrip_[k+numBassLeds].setHSV(color, 255, lightness); 559 | } 560 | 561 | // Show beat detection at the beginning of the strip 562 | for (int i = numBassLeds + kFreqBandCount; i < kNumLeds; i++) 563 | { 564 | ledStrip_[i].setHSV( 250, 255, beatVisIntensity_ ); 565 | } 566 | } 567 | else 568 | { 569 | // Show beat detection at the beginning of the strip 570 | const uint8_t numBassLeds = (kNumLeds - 2 * kFreqBandCount) / 2; 571 | 572 | for (int i = 0; i < numBassLeds; i++) 573 | { 574 | ledStrip_[i].setHSV( 250, 255, beatVisIntensity_ ); 575 | } 576 | 577 | // Show frequency intensities on the remaining Leds 578 | const uint8_t colorStart = 30; 579 | const uint8_t colorEnd = 210; 580 | const uint8_t colorStep = (colorEnd - colorStart) / kFreqBandCount; 581 | 582 | for (int k = 0; k < kFreqBandCount; k++) 583 | { 584 | uint8_t color = colorStart + k * colorStep; 585 | uint8_t lightness = min( int(magnitudeBand[k] * kFreqBandAmp[k] * sensitivityFactor_), 255); 586 | 587 | ledStrip_[numBassLeds + k].setHSV(color, 255, lightness); 588 | 589 | ledStrip_[numBassLeds + 2*kFreqBandCount - k - 1].setHSV(color, 255, lightness); 590 | } 591 | 592 | // Show beat detection at the beginning of the strip 593 | for (int i = numBassLeds + 2*kFreqBandCount; i < kNumLeds; i++) 594 | { 595 | ledStrip_[i].setHSV( 250, 255, beatVisIntensity_ ); 596 | } 597 | } 598 | 599 | FastLED.show(); 600 | 601 | // Determine current consumption from USB 602 | float vBusCurrent = M5.Axp.GetVBusCurrent(); 603 | 604 | if (vBusCurrent > maxCurrent_) 605 | { 606 | maxCurrent_ = vBusCurrent; 607 | } 608 | 609 | // Determine current consumption from battery 610 | float batCurrent = 0.5f * M5.Axp.GetIdischargeData(); 611 | 612 | if (batCurrent > maxCurrent_) 613 | { 614 | maxCurrent_ = batCurrent; 615 | } 616 | 617 | // Show current consumption on display 618 | if (cycleNr_ == 1) 619 | { 620 | M5.Lcd.setTextSize(2); 621 | M5.Lcd.setTextColor(WHITE, BLACK); 622 | int16_t cursorX = M5.Lcd.getCursorX(); 623 | int16_t cursorY = M5.Lcd.getCursorY(); 624 | M5.Lcd.printf("%03.0f mA", maxCurrent_); 625 | M5.Lcd.setCursor(cursorX, cursorY); 626 | 627 | maxCurrent_ = 0; 628 | } 629 | 630 | // If user presses ButtonA, print the current frequency spectrum to serial 631 | M5.BtnA.read(); 632 | 633 | // Compute duration of processing 634 | unsigned long timeEndMicros = micros(); 635 | unsigned long timeDeltaMicros = timeEndMicros - timeStartMicros; 636 | 637 | if (userTrigger_ == 0) 638 | { 639 | if ( M5.BtnA.isPressed() ) 640 | { 641 | userTrigger_ = 5; 642 | } 643 | } 644 | else { 645 | if (userTrigger_ == 1) 646 | { 647 | Serial.printf("AXP192: VBus current: %.3f mA\n", M5.Axp.GetVBusCurrent()); 648 | Serial.printf("FastLed Brightness: %d %%\n", FastLED.getBrightness()); 649 | 650 | Serial.printf("Processing time: %d\n", timeDeltaMicros); 651 | Serial.printf("Sensitivity: %.1f\n", sensitivityFactor_); 652 | 653 | for (uint8_t i = 0; i < kFreqBandCount; i++) 654 | { 655 | Serial.printf("to %.0f Hz: %.2f (Max: %.2f)\n", kFreqBandEndHz[i], magnitudeBand[i], magnitudeBandMax_[i]); 656 | } 657 | } 658 | userTrigger_ -= 1; 659 | } 660 | 661 | float nf; 662 | 663 | if (fabs(magnitudeBand[1]) < 0.001f) 664 | { 665 | nf = 1.0f; 666 | } 667 | else 668 | { 669 | nf = 1.0f / magnitudeBand[1]; 670 | } 671 | 672 | log_v("0:%04.2f 1:%04.2f 2:%04.2f 3:%04.2f 4:%04.2f 5:%04.2f 6:%04.2f 7:%04.2f 8:%04.2f 9:%04.2f 10:%04.2f 11:%04.2f 12:%04.2f 13:%04.2f 14:%04.2f 15:%04.2f 16:%04.2f 17:%04.2f 18:%04.2f 19:%04.2f Sum:%05.1f Sens: %04.1f t: %d", 673 | magnitudeBand[0] * nf, 674 | magnitudeBand[1] * nf, 675 | magnitudeBand[2] * nf, 676 | magnitudeBand[3] * nf, 677 | magnitudeBand[4] * nf, 678 | magnitudeBand[5] * nf, 679 | magnitudeBand[6] * nf, 680 | magnitudeBand[7] * nf, 681 | magnitudeBand[8] * nf, 682 | magnitudeBand[9] * nf, 683 | magnitudeBand[10] * nf, 684 | magnitudeBand[11] * nf, 685 | magnitudeBand[12] * nf, 686 | magnitudeBand[13] * nf, 687 | magnitudeBand[14] * nf, 688 | magnitudeBand[15] * nf, 689 | magnitudeBand[16] * nf, 690 | magnitudeBand[17] * nf, 691 | magnitudeBand[18] * nf, 692 | magnitudeBand[19] * nf, 693 | magnitudeSum, 694 | sensitivityFactor_, 695 | timeDeltaMicros); 696 | 697 | cycleNr_ = (cycleNr_ + 1) % 20; 698 | } 699 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------