Spresense Header CodeSpresense Header Code

Spresense - エッジコンピューティングを低消費電力で

Spresense で乾電池でも動く本格的なエッジコンピューティングを体験してみませんか?

!
誠に申し訳ございませんがこちらのページは日本語でのご案内はございません。英語のページをご参照下さい。

How to use the multiple microphone inputs of Spresense with the Arduino IDE

Spresense MultimicsSpresense Multimics

To connect microphones to the Spresense board, the Spresense extension board is needed to make the connection pins available. More information about how to connect microphones can be found here.

According to the schematic of microphone channel placement in the link above there are 4 different analog ports named A to D. To record in stereo mode it is enough to simply connect two microphones to input A and B, and then select stereo mode (AS_CHANNEL_STEREO) when initializing the recorder. The captured data will have all input from Input A in the left audio channel and that from Input B in the right one. Similarly, if all four inputs A to D are connected, use mode AS_CHANNEL_4CH when initializing the recorder and the resulting audio data will contain four channels.

For this quick demonstration of how to record from two microphones simultaneously we will need the following equipment:

  • 1 Spresense main board
  • 1 Spresense extension board
  • 2 Analog electret microphones
  • 2 resistors at 2.2 kΩ

1Step 1

Attach the Spresense main board to the extension board if you haven’t already. Detailed information on how to do this can be found here.

2Step 2

Connect the microphones and resistors to the extension board according to the drawing below. In this example we will use electret condenser-based microphones which require a bias voltage on the input line, and to supply this some resistors are needed:

Stereo Recording BbStereo Recording Bb

For more information about connecting analog microphones and how to add resistors to the board itself instead (which would void the warranty) please see https://developer.sony.com/develop/spresense/docs/hw_docs_en.html#_how_to_connect_analog_microphones

3Step 3

To test the microphones, copy the small program below into the Arduino IDE and run it on the board. Try to keep the microphones pointing in different directions so it is easy to separate them in the resulting audio.

#include <Audio.h>

AudioClass *theAudio;

void setup() {
  theAudio = AudioClass::getInstance();
  theAudio->begin();
  /* Setup a baseband audio path from mic input to line out */
  theAudio->setThroughMode(AudioClass::MicIn, AudioClass::None,
                           true, 160, AS_SP_DRV_MODE_LINEOUT);
  theAudio->setVolume(-160);
}

void loop() {
  /* Do nothing */
}

4Step 4

Connect a stereo headset to the Spresense extension board 3.5 mm connector. If the microphones are properly connected you should hear audio from the microphones in the headset and be able to distinguish between the left and the right one.

5Step 5

Disconnect the headset and run the below program in the Arduino IDE instead:

(If you prefer, you can download the sketch from here: SPRESENSE_MULTIMIC_DETECTOR.INO )

#include <Audio.h>

#define READ_PACKET_SIZE 6144
#define BUFFER_SIZE READ_PACKET_SIZE * 4

AudioClass *audio;

/* Buffer to keep captured audio data */
char buffer[BUFFER_SIZE];

void setup() {
  audio = AudioClass::getInstance();
  audio->begin();
  
  /* Setup recorder to read from mic input*/
  audio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC_A, 150);
  audio->initRecorder(AS_CODECTYPE_PCM, AS_SAMPLINGRATE_48000, AS_CHANNEL_STEREO);
}

void loop() {
  uint32_t temp;
  uint32_t left = 0;
  uint32_t right = 0;
  uint32_t read_size = 0;

  audio->readFrames(buffer, BUFFER_SIZE, &read_size);
  audio->startRecorder();
  audio->stopRecorder();

  /* Audio channels are interleaved and each frame is two bytes (16 bits)
     like this: "LLRRLLRRLLRR...". Therefore we read 4 bytes at the time
     and handle them in pairs. */
  for (uint32_t i = 0; i < read_size; i += 4)
  {
    temp  = (buffer[i + 1] << 8) | buffer[i];
    if (temp > left)
      left = temp;

    temp = (buffer[i + 3] << 8) | buffer[i + 2];
    if (temp > right)
      right = temp;
  }

  /* Our simple analysis only keeps track of the highest peak value
     in each channel and uses this as an indicator of loudness. */
  if (right < left)
  {
    ledOn(PIN_LED0);
    ledOff(PIN_LED3);
  }
  else if (left < right)
  {
    ledOn(PIN_LED3);
    ledOff(PIN_LED0);
  }
  else
  {
    ledOff(PIN_LED0);
    ledOff(PIN_LED3);
  }
}

6Step 6

The application captures audio data for both channels and does a very simple analysis to see which one is the loudest. When making some noise in one of the microphones you should now see the onboard LED indicator on the corresponding side of the board (LED number 0 or 3) light up.

Due to the simplicity of this application there is no filtering etc, so the LEDs might seem to blink a bit at random as the microphones pick up background noise.