1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <esp_heap_caps.h> 22 23 #include "sound_lab.h" 24 25 namespace { 26 27 28 29 struct Scope { 30 int spanMs = 20 ; 31 bool trigger = true ; 32 bool frozen = false ; 33 int16_t shown [ 2048 ] ; 34 int count = 0 ; 35 float scale = 256 ; 36 } scope ; 37 38 39 constexpr int kSlots = 150 ; 40 struct Strip { 41 uint8_t loud [ kSlots ] = { } ; 42 bool high [ kSlots ] = { } ; 43 int head = 0 ; 44 float slotPeak = 0 ; 45 uint32_t risesSeen = 0 ; 46 uint32_t risesPerSec = 0 , risesAt = 0 , risesThen = 0 ; 47 } strip ; 48 49 50 struct SoftComparator { 51 float threshDb = 15 ; 52 bool high = false ; 53 uint32_t rises = 0 ; 54 } soft ; 55 56 57 snd :: Mic other ; 58 snd :: Level otherLevel ; 59 bool otherOn = false ; 60 uint32_t otherMs = 0 ; 61 struct Compare { 62 int16_t a [ 320 ] , b [ 320 ] ; 63 float scaleA = 256 , scaleB = 256 ; 64 float holdA = 0 , holdB = 0 ; 65 } cmp ; 66 67 bool comparatorHigh ( SoundLab & lab ) { return lab . mic ( ) . digital ( ) ? soft . high : lab . mic ( ) . doHigh ( ) ; } 68 uint32_t comparatorRises ( SoundLab & lab ) { return lab . mic ( ) . digital ( ) ? soft . rises : lab . mic ( ) . doRises ( ) ; } 69 70 71 72 void takeScope ( SoundLab & lab ) { 73 if ( scope . frozen ) return ; 74 const uint32_t rate = lab . mic ( ) . rate ( ) ; 75 const int span = std :: min < int > ( int ( rate * scope . spanMs / 1000 ) , 2048 ) ; 76 static int16_t look [ 4096 ] ; 77 const int lookN = span * 2 ; 78 if ( ! lab . mic ( ) . latest ( look , lookN ) ) return ; 79 int start = lookN - span ; 80 if ( scope . trigger ) { 81 82 const int16_t hyst = int16_t ( std :: max ( 32.0f , scope . scale * 0.08f ) ) ; 83 for ( int i = lookN - span ; i > 1 ; i -- ) 84 if ( look [ i - 1 ] < 0 && look [ i ] >= 0 && look [ std :: max ( 0 , i - 8 ) ] < - hyst ) { 85 start = i ; 86 break ; 87 } 88 } 89 memcpy ( scope . shown , look + start , span * sizeof ( int16_t ) ) ; 90 scope . count = span ; 91 92 const float want = sv :: autoScale ( scope . shown , span , 128 ) ; 93 scope . scale = want > scope . scale ? want : scope . scale * 0.95f + want * 0.05f ; 94 } 95 96 void drawScope ( SoundLab & lab , FrameBuffer & fb , float ) { 97 takeScope ( lab ) ; 98 const int x = 30 , y = sv :: kTop + 6 , w = 282 , h = 108 ; 99 fb . drawRect ( x - 1 , y - 1 , w + 2 , h + 2 , pal :: dim ) ; 100 for ( int i = 1 ; i < 10 ; i ++ ) 101 for ( int j = y ; j < y + h ; j += 4 ) fb . drawPixel ( x + i * w / 10 , j , pal :: dim ) ; 102 sv :: wave ( fb , x , y , w , h , scope . shown , scope . count , scope . scale , sv :: kSound ) ; 103 char text [ 48 ] ; 104 if ( lab . mic ( ) . digital ( ) ) { 105 106 107 const float pct = scope . scale / 32768.0f * 25.0f ; 108 snprintf ( text , sizeof ( text ) , "+%.2g" , pct ) ; 109 sv :: small ( fb , 2 , y , pal :: muted , text ) ; 110 snprintf ( text , sizeof ( text ) , "-%.2g" , pct ) ; 111 sv :: small ( fb , 2 , y + h - 8 , pal :: muted , text ) ; 112 sv :: small ( fb , 2 , y + h - 18 , pal :: muted , "%FS" ) ; 113 } else { 114 115 const int mv = lab . mic ( ) . toMv ( int ( lab . mic ( ) . biasRaw ( ) + scope . scale / 16 ) ) - lab . mic ( ) . biasMv ( ) ; 116 snprintf ( text , sizeof ( text ) , "+%d" , mv ) ; 117 sv :: small ( fb , 2 , y , pal :: muted , text ) ; 118 snprintf ( text , sizeof ( text ) , "-%d" , mv ) ; 119 sv :: small ( fb , 2 , y + h - 8 , pal :: muted , text ) ; 120 sv :: small ( fb , 2 , y + h - 18 , pal :: muted , "mV" ) ; 121 } 122 sv :: small ( fb , 2 , y + h / 2 - 3 , pal :: muted , "0" ) ; 123 snprintf ( text , sizeof ( text ) , "%d ms across - %s%s" , scope . spanMs , scope . trigger ? "trigger on" : "free-running" , scope . frozen ? " - FROZEN" : "" ) ; 124 sv :: small ( fb , x , y + h + 6 , scope . frozen ? pal :: amber : pal :: muted , text ) ; 125 snprintf ( text , sizeof ( text ) , "%s %.0f dB" , snd :: sourceShort ( lab . mic ( ) . source ( ) ) , lab . level ( ) . above ( ) ) ; 126 sv :: small ( fb , x + w - sv :: textWidth ( fb , text ) , y + h + 6 , sv :: levelColour ( lab . level ( ) . above ( ) ) , text ) ; 127 } 128 129 130 void lane ( FrameBuffer & fb , int y , const char * name , const snd :: LevelReading & r , float hold , const int16_t * s , float & scale ) { 131 const int x = 92 , w = 222 , h = 44 ; 132 fb . drawRect ( x - 1 , y - 1 , w + 2 , h + 2 , pal :: dim ) ; 133 sv :: bold ( fb , 4 , y + 14 , sv :: kSound , name ) ; 134 const float want = sv :: autoScale ( s , 320 , 128 ) ; 135 scale = want > scale ? want : scale * 0.9f + want * 0.1f ; 136 sv :: wave ( fb , x , y , w , h , s , 320 , scale , sv :: kSound ) ; 137 char text [ 32 ] ; 138 snprintf ( text , sizeof ( text ) , "quiet %.0f dBFS" , r . floorDb ) ; 139 sv :: small ( fb , 4 , y + 22 , pal :: muted , text ) ; 140 snprintf ( text , sizeof ( text ) , "now +%.0f dB" , hold ) ; 141 sv :: small ( fb , 4 , y + 33 , sv :: levelColour ( hold ) , text ) ; 142 } 143 144 void drawMics ( SoundLab & lab , FrameBuffer & fb , float ) { 145 const bool primaryDigital = lab . mic ( ) . digital ( ) ; 146 if ( ! otherOn ) { 147 sv :: bold ( fb , 8 , sv :: kTop + 20 , pal :: ink , "Only one microphone found." ) ; 148 if ( primaryDigital ) { 149 sv :: small ( fb , 8 , sv :: kTop + 34 , pal :: muted , "Wire a KY-037 too, to compare:" ) ; 150 sv :: small ( fb , 8 , sv :: kTop + 48 , pal :: ink , "+ -> 3V3 G -> GND AO -> GPIO 6 DO -> 21" ) ; 151 } else { 152 sv :: small ( fb , 8 , sv :: kTop + 34 , pal :: muted , "Wire an INMP441 too, to compare:" ) ; 153 sv :: small ( fb , 8 , sv :: kTop + 48 , pal :: ink , "VDD -> 3V3 GND -> GND L/R -> GND" ) ; 154 sv :: small ( fb , 8 , sv :: kTop + 60 , pal :: ink , "SCK -> 40 WS -> 41 SD -> 42" ) ; 155 } 156 sv :: small ( fb , 8 , sv :: kTop + 80 , pal :: muted , "then press RST: S1 looks for it at start-up." ) ; 157 return ; 158 } 159 160 lab . mic ( ) . latest ( cmp . a , 320 ) ; 161 other . latest ( cmp . b , 320 ) ; 162 const snd :: LevelReading & main = lab . level ( ) , & second = otherLevel . reading ( ) ; 163 const float holdDigital = primaryDigital ? cmp . holdA : cmp . holdB , holdAnalog = primaryDigital ? cmp . holdB : cmp . holdA ; 164 if ( primaryDigital ) { 165 lane ( fb , sv :: kTop + 4 , "INMP441" , main , cmp . holdA , cmp . a , cmp . scaleA ) ; 166 lane ( fb , sv :: kTop + 54 , "KY-037" , second , cmp . holdB , cmp . b , cmp . scaleB ) ; 167 } else { 168 lane ( fb , sv :: kTop + 4 , "INMP441" , second , cmp . holdB , cmp . b , cmp . scaleB ) ; 169 lane ( fb , sv :: kTop + 54 , "KY-037" , main , cmp . holdA , cmp . a , cmp . scaleA ) ; 170 } 171 172 char text [ 56 ] ; 173 const bool heard = holdDigital >= 6 || holdAnalog >= 6 ; 174 if ( ! heard ) snprintf ( text , sizeof ( text ) , "clap or talk: how far does each rise above its hiss?" ) ; 175 else if ( holdDigital >= holdAnalog ) snprintf ( text , sizeof ( text ) , "INMP441 hears it %.0f dB more clearly" , holdDigital - holdAnalog ) ; 176 else snprintf ( text , sizeof ( text ) , "KY-037 hears it %.0f dB more clearly (closer?)" , holdAnalog - holdDigital ) ; 177 sv :: small ( fb , 4 , sv :: kTop + 108 , heard ? pal :: ink : pal :: muted , text ) ; 178 sv :: small ( fb , 4 , sv :: kTop + 120 , pal :: muted , "each wave scaled to fit: compare the hiss" ) ; 179 } 180 181 182 void drawBiasAnalog ( SoundLab & lab , FrameBuffer & fb ) { 183 snd :: Mic & m = lab . mic ( ) ; 184 const int x = 22 , y = sv :: kTop + 8 , h = 112 , w = 46 ; 185 auto yOf = [ & ] ( int mv ) { return y + h - mv * h / 3300 ; } ; 186 fb . drawRect ( x , y , w , h , pal :: dim ) ; 187 const int top = snd :: attenTopMv ( m . atten ( ) ) ; 188 fb . fillRect ( x + 1 , yOf ( top ) , w - 2 , y + h - yOf ( top ) - 1 , sv :: kPanel ) ; 189 const int lo = m . toMv ( m . rawLow ( ) ) , hi = m . toMv ( m . rawHigh ( ) ) , bias = m . biasMv ( ) ; 190 fb . fillRect ( x + 6 , yOf ( hi ) , w - 12 , std :: max ( 2 , yOf ( lo ) - yOf ( hi ) ) , mix ( sv :: kSound , pal :: night , 120 ) ) ; 191 fb . drawFastHLine ( x - 4 , yOf ( bias ) , w + 8 , sv :: kSound ) ; 192 for ( int v = 0 ; v <= 3300 ; v += 500 ) { 193 fb . drawFastHLine ( x - 3 , yOf ( v ) , 3 , pal :: muted ) ; 194 char t [ 8 ] ; 195 snprintf ( t , sizeof ( t ) , "%.1f" , v / 1000.0f ) ; 196 if ( v % 1000 == 0 ) sv :: small ( fb , 0 , yOf ( v ) - 3 , pal :: muted , t ) ; 197 } 198 char text [ 48 ] ; 199 snprintf ( text , sizeof ( text ) , "%d mV" , bias ) ; 200 sv :: stat ( fb , 92 , sv :: kTop + 10 , "bias (the steady part)" , text , sv :: kSound ) ; 201 snprintf ( text , sizeof ( text ) , "%d mV" , std :: max ( 0 , hi - lo ) ) ; 202 sv :: stat ( fb , 92 , sv :: kTop + 48 , "swing, last 0.1 s" , text ) ; 203 const float mvPerStep = top / 4095.0f ; 204 snprintf ( text , sizeof ( text ) , "%.2f mV" , mvPerStep ) ; 205 sv :: stat ( fb , 212 , sv :: kTop + 48 , "one ADC step" , text ) ; 206 snprintf ( text , sizeof ( text ) , "atten %s" , snd :: attenName ( m . atten ( ) ) ) ; 207 sv :: small ( fb , 212 , sv :: kTop + 10 , pal :: muted , text ) ; 208 209 const char * hint ; 210 uint16_t c = pal :: muted ; 211 if ( m . clipping ( ) ) hint = "CLIPPING: the wave hits the range's edge" , c = pal :: coral ; 212 else if ( bias > top - 150 ) hint = "bias near the top: use a bigger atten" , c = pal :: amber ; 213 else if ( m . atten ( ) == ADC_ATTEN_DB_12 && hi < 1600 ) hint = "fits in 0-1.75 V: try atten 6 (2x finer)" , c = pal :: teal ; 214 else if ( m . atten ( ) == ADC_ATTEN_DB_6 && hi < 1150 ) hint = "fits in 0-1.25 V: try atten 2.5" , c = pal :: teal ; 215 else hint = "the wave fits the range: good" ; 216 sv :: small ( fb , 92 , sv :: kTop + 92 , c , hint ) ; 217 sv :: small ( fb , 92 , sv :: kTop + 106 , pal :: muted , "clap: the band grows around the line" ) ; 218 } 219 220 221 void drawBiasDigital ( SoundLab & lab , FrameBuffer & fb ) { 222 snd :: Mic & m = lab . mic ( ) ; 223 const int lo = m . rawLow ( ) , hi = m . rawHigh ( ) ; 224 const int64_t swing = int64_t ( hi ) - lo ; 225 const float bits = swing > 0 ? log2f ( float ( swing ) ) : 0 ; 226 const int biggest = std :: max ( std :: abs ( lo ) , std :: abs ( hi ) ) ; 227 const float headroom = biggest > 0 ? 20.0f * log10f ( float ( m . rawMax ( ) ) / float ( biggest ) ) : 138 ; 228 229 const int x = 14 , y = sv :: kTop + 6 , cell = 5 ; 230 for ( int b = 0 ; b < 24 ; b ++ ) { 231 const int cy = y + ( 23 - b ) * cell ; 232 const bool used = b < int ( bits + 0.5f ) ; 233 const uint16_t c = b >= 22 ? pal :: coral : b >= 18 ? pal :: amber : sv :: kSound ; 234 fb . fillRect ( x , cy , 34 , cell - 1 , used ? c : sv :: kPanel ) ; 235 } 236 sv :: small ( fb , x + 38 , y - 2 , pal :: muted , "bit 23" ) ; 237 sv :: small ( fb , x + 38 , y + 23 * cell - 2 , pal :: muted , "bit 0" ) ; 238 char text [ 48 ] ; 239 snprintf ( text , sizeof ( text ) , "%.1f of 24" , bits ) ; 240 sv :: stat ( fb , 104 , sv :: kTop + 8 , "bits the sound uses" , text , sv :: kSound ) ; 241 snprintf ( text , sizeof ( text ) , "%.0f dB" , headroom ) ; 242 sv :: stat ( fb , 216 , sv :: kTop + 8 , "headroom" , text , headroom < 6 ? pal :: coral : pal :: ink ) ; 243 snprintf ( text , sizeof ( text ) , "%d" , int ( m . biasRaw ( ) ) ) ; 244 sv :: stat ( fb , 104 , sv :: kTop + 46 , "offset (24-bit)" , text ) ; 245 snprintf ( text , sizeof ( text ) , "%d" , int ( swing ) ) ; 246 sv :: stat ( fb , 216 , sv :: kTop + 46 , "swing, last 0.1 s" , text ) ; 247 const char * hint = "digital: no bias, the converter is inside" ; 248 uint16_t c = pal :: muted ; 249 if ( m . clipping ( ) ) hint = "CLIPPING: louder than the mic can count" , c = pal :: coral ; 250 else if ( bits > 6 ) hint = "each bit = 6 dB: a clap lights a dozen more" , c = pal :: teal ; 251 sv :: small ( fb , 104 , sv :: kTop + 92 , c , hint ) ; 252 sv :: small ( fb , 104 , sv :: kTop + 106 , pal :: muted , "nothing to tune: no attenuation, no pot" ) ; 253 } 254 255 void drawBias ( SoundLab & lab , FrameBuffer & fb , float ) { 256 if ( lab . mic ( ) . digital ( ) ) drawBiasDigital ( lab , fb ) ; 257 else drawBiasAnalog ( lab , fb ) ; 258 } 259 260 void drawComparator ( SoundLab & lab , FrameBuffer & fb , float ) { 261 const bool digital = lab . mic ( ) . digital ( ) ; 262 263 const bool high = comparatorHigh ( lab ) ; 264 fb . fillCircle ( 34 , sv :: kTop + 30 , 18 , high ? pal :: amber : sv :: kPanel ) ; 265 fb . drawCircle ( 34 , sv :: kTop + 30 , 18 , pal :: muted ) ; 266 sv :: small ( fb , 14 , sv :: kTop + 54 , pal :: muted , digital ? ( high ? "soft high" : "soft low" ) : ( high ? "DO high" : "DO low" ) ) ; 267 char text [ 48 ] ; 268 snprintf ( text , sizeof ( text ) , "%lu/s" , static_cast < unsigned long > ( strip . risesPerSec ) ) ; 269 sv :: stat ( fb , 68 , sv :: kTop + 8 , "rising edges" , text , strip . risesPerSec ? pal :: amber : pal :: ink ) ; 270 if ( digital ) snprintf ( text , sizeof ( text ) , "%.0f dB" , soft . threshDb ) ; 271 else snprintf ( text , sizeof ( text ) , "%lu" , static_cast < unsigned long > ( comparatorRises ( lab ) ) ) ; 272 sv :: stat ( fb , 168 , sv :: kTop + 8 , digital ? "threshold" : "total" , text ) ; 273 274 const int x = 10 , y = sv :: kTop + 66 , w = 300 ; 275 for ( int i = 0 ; i < kSlots ; i ++ ) { 276 const int at = ( strip . head + i ) % kSlots ; 277 const int px = x + i * w / kSlots ; 278 const int h = std :: min ( 30 , int ( strip . loud [ at ] ) * 30 / 50 ) ; 279 if ( h ) fb . fillRect ( px , y + 30 - h , 2 , h , sv :: levelColour ( strip . loud [ at ] ) ) ; 280 if ( strip . high [ at ] ) fb . fillRect ( px , y + 36 , 2 , 10 , pal :: amber ) ; 281 } 282 if ( digital ) { 283 const int ty = y + 30 - std :: min ( 30 , int ( soft . threshDb ) * 30 / 50 ) ; 284 for ( int i = x ; i < x + w ; i += 3 ) fb . drawPixel ( i , ty , pal :: ink ) ; 285 } 286 fb . drawFastHLine ( x , y + 31 , w , pal :: dim ) ; 287 fb . drawFastHLine ( x , y + 47 , w , pal :: dim ) ; 288 sv :: small ( fb , x , y + 50 , pal :: muted , digital ? "loudness vs the software comparator, 6 s" : "loudness (top) vs DO (bottom), last 6 s" ) ; 289 290 int highSlots = 0 , loudSlots = 0 ; 291 for ( int i = kSlots - 50 ; i < kSlots ; i ++ ) { 292 const int at = ( strip . head + i ) % kSlots ; 293 highSlots += strip . high [ at ] ; 294 loudSlots += strip . loud [ at ] >= 15 ; 295 } 296 const char * hint = digital ? "INMP441: no DO pin, so this one is code" : "clap: DO should light with each clap" ; 297 uint16_t c = pal :: muted ; 298 if ( highSlots > 45 ) hint = digital ? "always on: raise it (thresh 25)" : "DO always on: turn the pot the other way" , c = pal :: amber ; 299 else if ( loudSlots > 3 && highSlots == 0 ) hint = digital ? "loud but off: lower it (thresh 10)" : "loud but DO stays off: turn the pot back" , c = pal :: amber ; 300 else if ( highSlots > 0 && highSlots <= loudSlots + 2 ) hint = digital ? "follows the loud moments: tuned" : "DO follows the loud moments: tuned" , c = pal :: teal ; 301 sv :: small ( fb , 68 , sv :: kTop + 42 , c , hint ) ; 302 } 303 304 void drawNumbers ( SoundLab & lab , FrameBuffer & fb , float ) { 305 snd :: Mic & m = lab . mic ( ) ; 306 const snd :: LevelReading & r = lab . level ( ) ; 307 char a [ 24 ] , b [ 48 ] , c [ 24 ] , d [ 24 ] ; 308 snprintf ( a , sizeof ( a ) , "%lu Hz" , static_cast < unsigned long > ( m . measuredRate ( ) ) ) ; 309 sv :: stat ( fb , 10 , sv :: kTop + 6 , "microphone" , snd :: sourceShort ( m . source ( ) ) , sv :: kSound ) ; 310 sv :: stat ( fb , 170 , sv :: kTop + 6 , "samples / second" , a ) ; 311 snprintf ( c , sizeof ( c ) , "%.1f dBFS" , r . fastDb ) ; 312 snprintf ( d , sizeof ( d ) , "%.1f dBFS" , r . floorDb ) ; 313 sv :: stat ( fb , 10 , sv :: kTop + 46 , "level (fast)" , c ) ; 314 sv :: stat ( fb , 170 , sv :: kTop + 46 , "quiet (floor)" , d ) ; 315 snprintf ( a , sizeof ( a ) , "%.1f dB" , r . above ( ) ) ; 316 sv :: stat ( fb , 10 , sv :: kTop + 86 , "above the quiet" , a , sv :: levelColour ( r . above ( ) ) ) ; 317 if ( m . digital ( ) ) { 318 snprintf ( b , sizeof ( b ) , "%.0f dB SPL" , lab . spl ( ) ) ; 319 sv :: stat ( fb , 170 , sv :: kTop + 86 , "sound level (unweighted)" , b ) ; 320 } else { 321 snprintf ( b , sizeof ( b ) , "%u KB" , unsigned ( heap_caps_get_free_size ( MALLOC_CAP_SPIRAM ) / 1024 ) ) ; 322 sv :: stat ( fb , 170 , sv :: kTop + 86 , "free PSRAM" , b ) ; 323 } 324 snprintf ( b , sizeof ( b ) , "lost: DMA %lu, frames %lu" , static_cast < unsigned long > ( m . overruns ( ) ) , static_cast < unsigned long > ( m . framesDropped ( ) ) ) ; 325 sv :: small ( fb , 10 , sv :: kTop + 120 , m . overruns ( ) || m . framesDropped ( ) ? pal :: amber : pal :: muted , b ) ; 326 } 327 328 const SoundView kViews [ ] = { 329 { "scope" , drawScope , "freeze" } , 330 { "mics" , drawMics , "status" } , 331 { "bias" , drawBias , "atten next" } , 332 { "comparator" , drawComparator , "pot" } , 333 { "numbers" , drawNumbers , "status" } , 334 } ; 335 336 337 338 void cmdFreeze ( SoundLab & lab , const String & , const String & lower ) { 339 scope . frozen = lower . isEmpty ( ) ? ! scope . frozen : lower != "off" ; 340 lab . note ( sv :: Note :: Info , scope . frozen ? "scope frozen (freeze again to go on)" : "scope running" ) ; 341 } 342 343 void cmdSpan ( SoundLab & lab , const String & arg , const String & ) { 344 long ms = 0 ; 345 if ( ! SoundLab :: number ( arg , 2 , 120 , ms ) ) return lab . warn ( "span <ms>, 2-120 (now %d). 20 ms = one cycle of 50 Hz" , scope . spanMs ) ; 346 scope . spanMs = int ( ms ) ; 347 scope . frozen = false ; 348 } 349 350 void cmdTrigger ( SoundLab & lab , const String & , const String & lower ) { 351 scope . trigger = lower . isEmpty ( ) ? ! scope . trigger : lower == "on" ; 352 lab . note ( sv :: Note :: Info , scope . trigger ? "trigger on: a steady tone stands still" : "free-running" ) ; 353 } 354 355 void cmdPot ( SoundLab & lab , const String & , const String & ) { 356 if ( lab . mic ( ) . digital ( ) ) { 357 Serial . println ( "The INMP441 has no comparator and no pot: S1 compares in software instead." ) ; 358 Serial . printf ( " thresh <dB> sets the line (now %.0f dB above the quiet). Same idea: quiet = off, clap = on.\n" , soft . threshDb ) ; 359 Serial . println ( " To tune a KY-037's pot, make it the main microphone: mic ky037 (restarts)." ) ; 360 } else { 361 Serial . println ( "Tuning the comparator (the blue trim pot, a small screwdriver):" ) ; 362 Serial . println ( " 1. Stay quiet. If the module's second LED (and DO) is on, turn the screw until it just goes off." ) ; 363 Serial . println ( " 2. Clap. DO should flash with each clap: the comparator view shows DO under the loudness." ) ; 364 Serial . println ( " 3. Lit all the time: turn it back a little. Never lights: turn it further toward the edge." ) ; 365 Serial . println ( " Which way is 'more sensitive' differs between module makers: watch the lamp, not the direction." ) ; 366 } 367 lab . showView ( "comparator" ) ; 368 } 369 370 void cmdThresh ( SoundLab & lab , const String & arg , const String & ) { 371 float db = 0 ; 372 if ( ! SoundLab :: decimal ( arg , 3 , 50 , db ) ) 373 return lab . warn ( "thresh <dB>, 3-50: the software comparator's line above the quiet (now %.0f)" , soft . threshDb ) ; 374 soft . threshDb = db ; 375 lab . note ( sv :: Note :: Info , "software comparator at %.0f dB" , db ) ; 376 if ( ! lab . mic ( ) . digital ( ) ) Serial . println ( " (only used with the INMP441: the KY-037 has its hardware comparator on DO)" ) ; 377 } 378 379 void cmdDump ( SoundLab & lab , const String & arg , const String & ) { 380 long n = 0 ; 381 if ( arg . isEmpty ( ) ) n = 320 ; 382 else if ( ! SoundLab :: number ( arg , 16 , 4096 , n ) ) return lab . warn ( "dump [n], 16-4096 samples (default 320 = 20 ms)" ) ; 383 static int16_t s [ 4096 ] ; 384 if ( ! lab . mic ( ) . latest ( s , n ) ) return lab . warn ( "not enough sound yet" ) ; 385 Serial . printf ( "# %ld samples at %lu Hz, int16 after removing the %s (%s). Paste into a spreadsheet.\n" , n , 386 static_cast < unsigned long > ( lab . mic ( ) . rate ( ) ) , lab . mic ( ) . digital ( ) ? "offset" : "bias" , 387 lab . mic ( ) . digital ( ) ? "one unit = 64 of the INMP441's 24-bit steps" : "one ADC step = 16" ) ; 388 for ( long i = 0 ; i < n ; i ++ ) Serial . printf ( "%d%c" , s [ i ] , ( i % 16 == 15 || i == n - 1 ) ? '\n' : ',' ) ; 389 } 390 391 const SoundCommand kCommands [ ] = { 392 { "freeze" , "[off]" , "hold the scope picture (double-click on scope)" , cmdFreeze } , 393 { "span" , "<ms>" , "time across the scope, 2-120 ms (default 20)" , cmdSpan } , 394 { "trigger" , "[on|off]" , "line the wave up on a rising zero crossing" , cmdTrigger } , 395 { "pot" , "" , "how to tune the comparator (trim pot, or thresh)" , cmdPot } , 396 { "thresh" , "<dB>" , "the INMP441's software comparator line (default 15)" , cmdThresh } , 397 { "dump" , "[n]" , "print the newest n samples as numbers" , cmdDump } , 398 } ; 399 400 401 402 403 404 405 void setup ( SoundLab & lab ) { 406 if ( lab . mic ( ) . digital ( ) ) otherOn = other . begin ( snd :: Source :: Ky037 ) ; 407 else if ( snd :: Mic :: probeInmp441 ( ) ) otherOn = other . begin ( snd :: Source :: Inmp441 ) ; 408 Serial . printf ( "Second microphone for the mics view: %s\n" , otherOn ? snd :: sourceName ( other . source ( ) ) : "none found" ) ; 409 } 410 411 void frame ( SoundLab & lab , const snd :: Frame & , uint32_t ) { 412 const float above = lab . level ( ) . instAbove ( ) ; 413 strip . slotPeak = std :: max ( strip . slotPeak , above ) ; 414 415 if ( ! soft . high && above >= soft . threshDb ) soft . high = true , soft . rises ++ ; 416 else if ( soft . high && above < soft . threshDb - 3 ) soft . high = false ; 417 cmp . holdA = std :: max ( cmp . holdA - 10.0f * 0.004f , above ) ; 418 } 419 420 void tick ( SoundLab & lab ) { 421 const uint32_t rises = comparatorRises ( lab ) ; 422 strip . loud [ strip . head ] = uint8_t ( std :: min ( 255.0f , strip . slotPeak ) ) ; 423 strip . high [ strip . head ] = comparatorHigh ( lab ) || rises != strip . risesSeen ; 424 strip . head = ( strip . head + 1 ) % kSlots ; 425 strip . risesSeen = rises ; 426 strip . slotPeak = 0 ; 427 if ( millis ( ) - strip . risesAt >= 1000 ) strip . risesPerSec = rises - strip . risesThen , strip . risesThen = rises , strip . risesAt = millis ( ) ; 428 429 if ( otherOn ) { 430 snd :: Frame f ; 431 while ( other . frame ( f ) ) { 432 otherMs += snd :: kChunk * 1000 / other . rate ( ) ; 433 otherLevel . push ( f . rms , otherMs ) ; 434 cmp . holdB = std :: max ( cmp . holdB - 10.0f * 0.004f , otherLevel . reading ( ) . instAbove ( ) ) ; 435 } 436 } 437 } 438 439 440 bool gesture ( SoundLab & lab , Gesture g , bool ) { 441 if ( g != Gesture :: DoubleClick || strcmp ( lab . spec ( ) . views [ lab . view ( ) ] . name , "bias" ) != 0 ) return false ; 442 if ( lab . mic ( ) . digital ( ) ) { 443 lab . note ( sv :: Note :: Info , "INMP441: fixed 24-bit range, nothing to switch" ) ; 444 return true ; 445 } 446 const adc_atten_t a = lab . mic ( ) . atten ( ) ; 447 const char * next = a == ADC_ATTEN_DB_12 ? "6" : a == ADC_ATTEN_DB_6 ? "2.5" : a == ADC_ATTEN_DB_2_5 ? "0" : "12" ; 448 Serial . printf ( "> atten %s (double-click)\n" , next ) ; 449 lab . runLine ( String ( "atten " ) + next ) ; 450 return true ; 451 } 452 453 const SoundLabSpec kSpec { 454 "sound-lab-s1" , "S1" , "Hear" , kViews , sizeof ( kViews ) / sizeof ( kViews [ 0 ] ) , 0 , 455 kCommands , sizeof ( kCommands ) / sizeof ( kCommands [ 0 ] ) , setup , frame , tick , gesture , "freeze" , 456 } ; 457 458 SoundLab lab ( kSpec ) ; 459 460 } 461 462 void setup ( ) { lab . begin ( ) ; } 463 464 void loop ( ) { lab . loop ( ) ; }