150 lines
4.6KB

  1. /* Show levels (RMS & Peak) for 8 I2S microphone inputs
  2. *
  3. * Connect 8 INMP411 I2S microphones to Teensy 4.0
  4. * Pin 8 SD on mics #1 and #2
  5. * Pin 6 SD on mics #3 and #4
  6. * Pin 9 SD on mics #5 and #6
  7. * Pin 32 SD on mics #7 and #8
  8. * Pin 20 WS on all mics
  9. * Pin 21 SCK on all mics
  10. *
  11. * Each mic needs GND to Teensy GND, VCC to Teensy 3.3V.
  12. * Connect L/R to GND on the odd numbered mics
  13. * and L/R to 3.3V on the even numbered mics.
  14. *
  15. * Optional - connect a Teensy Audio Shield or other I2S
  16. * output device, but do not connect it to pin 8, because
  17. * the INMP411 mics #1 & #2 send their signal to pin 8.
  18. *
  19. * This example code is in the public domain
  20. */
  21. #include <Audio.h>
  22. #include <Wire.h>
  23. #include <SPI.h>
  24. #include <SD.h>
  25. #include <SerialFlash.h>
  26. // If INMP411 mics are not available, the audio shield mic can
  27. // be used, but it will send a single signal to inputs #1 & #2
  28. // (if connected to pin 8).
  29. //const int myInput = AUDIO_INPUT_LINEIN;
  30. const int myInput = AUDIO_INPUT_MIC;
  31. AudioInputI2SOct audioInput; // audio shield: mic or line-in
  32. AudioAmplifier amp1;
  33. AudioAmplifier amp2;
  34. AudioAmplifier amp3;
  35. AudioAmplifier amp4;
  36. AudioAmplifier amp5;
  37. AudioAmplifier amp6;
  38. AudioAmplifier amp7;
  39. AudioAmplifier amp8;
  40. AudioAnalyzeRMS rms1;
  41. AudioAnalyzeRMS rms2;
  42. AudioAnalyzeRMS rms3;
  43. AudioAnalyzeRMS rms4;
  44. AudioAnalyzeRMS rms5;
  45. AudioAnalyzeRMS rms6;
  46. AudioAnalyzeRMS rms7;
  47. AudioAnalyzeRMS rms8;
  48. AudioAnalyzePeak peak1;
  49. AudioAnalyzePeak peak2;
  50. AudioAnalyzePeak peak3;
  51. AudioAnalyzePeak peak4;
  52. AudioAnalyzePeak peak5;
  53. AudioAnalyzePeak peak6;
  54. AudioAnalyzePeak peak7;
  55. AudioAnalyzePeak peak8;
  56. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  57. // Send all microphone signals to amps
  58. AudioConnection r1(audioInput, 0, amp1, 0);
  59. AudioConnection r2(audioInput, 1, amp2, 0);
  60. AudioConnection r3(audioInput, 2, amp3, 0);
  61. AudioConnection r4(audioInput, 3, amp4, 0);
  62. AudioConnection r5(audioInput, 4, amp5, 0);
  63. AudioConnection r6(audioInput, 5, amp6, 0);
  64. AudioConnection r7(audioInput, 6, amp7, 0);
  65. AudioConnection r8(audioInput, 7, amp8, 0);
  66. // Connect the amps to RMS and Peak analyzers
  67. AudioConnection a1(amp1, 0, rms1, 0);
  68. AudioConnection a2(amp2, 0, rms2, 0);
  69. AudioConnection a3(amp3, 0, rms3, 0);
  70. AudioConnection a4(amp4, 0, rms4, 0);
  71. AudioConnection a5(amp5, 0, rms5, 0);
  72. AudioConnection a6(amp6, 0, rms6, 0);
  73. AudioConnection a7(amp7, 0, rms7, 0);
  74. AudioConnection a8(amp8, 0, rms8, 0);
  75. AudioConnection p1(amp1, 0, peak1, 0);
  76. AudioConnection p2(amp2, 0, peak2, 0);
  77. AudioConnection p3(amp3, 0, peak3, 0);
  78. AudioConnection p4(amp4, 0, peak4, 0);
  79. AudioConnection p5(amp5, 0, peak5, 0);
  80. AudioConnection p6(amp6, 0, peak6, 0);
  81. AudioConnection p7(amp7, 0, peak7, 0);
  82. AudioConnection p8(amp8, 0, peak8, 0);
  83. // Also connect 2 of the amps to an I2S output (Pin 8)
  84. // to be able to listen to the sound.
  85. AudioConnection c10(amp1, 0, audioOutput, 0);
  86. AudioConnection c11(amp2, 0, audioOutput, 1);
  87. AudioControlSGTL5000 audioShield;
  88. void setup() {
  89. AudioMemory(26);
  90. audioShield.enable();
  91. audioShield.inputSelect(myInput);
  92. audioShield.micGain(12);
  93. audioShield.volume(0.5);
  94. const float microphoneAmplification = 72.0;
  95. amp1.gain(microphoneAmplification);
  96. amp2.gain(microphoneAmplification);
  97. amp3.gain(microphoneAmplification);
  98. amp4.gain(microphoneAmplification);
  99. amp5.gain(microphoneAmplification);
  100. amp6.gain(microphoneAmplification);
  101. amp7.gain(microphoneAmplification);
  102. amp8.gain(microphoneAmplification);
  103. Serial.begin(9600);
  104. }
  105. elapsedMillis fps;
  106. void loop() {
  107. if (fps > 24) {
  108. fps = 0;
  109. const int digits = 8;
  110. print_bar(1, rms1.read(), peak1.readPeakToPeak()/2, digits);
  111. print_bar(2, rms2.read(), peak2.readPeakToPeak()/2, digits);
  112. print_bar(3, rms3.read(), peak3.readPeakToPeak()/2, digits);
  113. print_bar(4, rms4.read(), peak4.readPeakToPeak()/2, digits);
  114. print_bar(5, rms5.read(), peak5.readPeakToPeak()/2, digits);
  115. print_bar(6, rms6.read(), peak6.readPeakToPeak()/2, digits);
  116. print_bar(7, rms7.read(), peak7.readPeakToPeak()/2, digits);
  117. print_bar(8, rms8.read(), peak8.readPeakToPeak()/2, digits);
  118. Serial.println();
  119. }
  120. }
  121. void print_bar(int in, float rms, float peak, int digits) {
  122. Serial.print(in);
  123. Serial.print(":");
  124. int num_rms = roundf(rms * digits);
  125. int num_peak = roundf(peak * digits);
  126. for (int i=0; i < digits; i++) {
  127. if (i < num_rms) {
  128. Serial.print("*");
  129. } else if (i < num_peak) {
  130. Serial.print(">");
  131. } else {
  132. Serial.print(" ");
  133. }
  134. }
  135. Serial.print(" ");
  136. }