Digital Microphones and Arduino



  • I would like to use the record example from Arduino IDE to check the sound on my digital microphones. What changes do I need to make to the sketch to have it work for 2 digital microphones in D01?


  • DeveloperWorld

    Hi @atw72

    Have you seen the https://developer.sony.com/develop/spresense/developer-tools/hardware-documentation#_how_to_connect_digital_microphones ?

    Looking in the audio driver of the I found the following:

    
    err_t setRecorderMode(
          uint8_t input_device, /**< Select input device. AS_SETRECDR_STS_INPUTDEVICE_MIC or
                                     AS_SETRECDR_STS_INPUTDEVICE_I2S. */
          int32_t input_gain,   /**< Input gain : value range
                                     Analog Mic  -7850:-78.50dB, ... , -5:-0.05dB, 0:0dB, 5:+0.5dB, ... , 210:+21.0dB
                                     Digital Mic -7850:-78.50dB, ... , -5:-0.05dB, 0:0dB (Max is 0dB.)
                                     set #AS_MICGAIN_HOLD is keep setting. */
          uint32_t bufsize,     /**< Buffer size of recorder. */
          bool is_digital       /**< Select mic type. true:Digital, false:Analog. */
      );
    

    If you could try to change the call to theAudio->setRecorderMode in your example to specify that you want to use a Digial microphone by setting the is_digital parameter.

    Please have a look and see if it helps you.



  • Hi there!

    I did see the link above. I made those changes to the Spresense board and now have digital microphones connected to the board. Under the Spresense Arduino IDE development, I was trying to use the recorder sketch that is under the Audio Library at this link: https://developer.sony.com/develop/spresense/developer-tools/get-started-using-arduino-ide/developer-guide#_example_audio_sketches. There is a reference to a link that says setting of "MIC channel select map" for SDK configuration information on how to use digital microphones, however, the link just goes back to the top of the page. Do you know what changes I have to make to the SDK to use the recorder example but with digital microphones?

    Thank you!





  • @TE-KarlKomierowski I gave this line a try:

    theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC, AS_MICGAIN_HOLD, 68, true);

    I received this error on the serial monitor.

    initialization Audio Library
    Init Recorder!
    Recording Start!
    Attention: module[4][0] attention id[1]/code[6] (objects/media_recorder/audio_recorder_sink.cpp L84)

    Attention: module[4][0] attention id[1]/code[6] (objects/media_recorder/audio_recorder_sink.cpp L84)


  • DeveloperWorld

    Hi @atw72

    I checked the code in media_recorder/audio_recorder_sink.cpp
    and the warning on line 84 is in this function AudioRecorderSink::write this:
    AS_ATTENTION_SUB_CODE_SIMPLE_FIFO_OVERFLOW

    Attention: module[4][0] attention id[1]/code[6] (objects/media_recorder/audio_recorder_sink.cpp L84)

    Could you share your sketch with us and it will be easier to see all the settings that have been used when this warning occurred.

    And also, do you use an SD card? What type/brand is it?

    Thank you.
    BR
    Karl



  • Hi @TE-KarlKomierowski, below is the recorder sketch I was using. I am using an SD Card. It's a SanDisk 64GB Ultra microSDXC

    #include <SDHCI.h>
    #include <Audio.h>
    
    SDClass theSD;
    AudioClass *theAudio;
    
    File myFile;
    
    bool ErrEnd = false;
    
    /**
     * @brief Audio attention callback
     *
     * When audio internal error occurc, this function will be called back.
     */
    
    static void audio_attention_cb(const ErrorAttentionParam *atprm)
    {
      puts("Attention!");
      
      if (atprm->error_code >= AS_ATTENTION_CODE_WARNING)
        {
          ErrEnd = true;
       }
    }
    
    /**
     * @brief Setup recording of mp3 stream to file
     *
     * Select input device as microphone <br>
     * Initialize filetype to stereo mp3 with 48 Kb/s sampling rate <br>
     * Open "Sound.mp3" file in write mode
     */
    
    static const int32_t recoding_frames = 400;
    static const int32_t recoding_size = recoding_frames*288; /* 96kbps, 1152sample */
    
    void setup()
    {
      theAudio = AudioClass::getInstance();
    
      theAudio->begin(audio_attention_cb);
    
      puts("initialization Audio Library");
    
      /* Select input device as microphone */
      theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC, AS_MICGAIN_HOLD, 68, true);
    
      /*
       * Initialize filetype to stereo mp3 with 48 Kb/s sampling rate
       * Search for MP3 codec in "/mnt/sd0/BIN" directory
       */
      theAudio->initRecorder(AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_48000, AS_CHANNEL_STEREO);
      puts("Init Recorder!");
    
      /* Open file for data write on SD card */
      myFile = theSD.open("Sound.mp3", FILE_WRITE);
      /* Verify file open */
      if (!myFile)
        {
          printf("File open error\n");
          exit(1);
        }
    
      theAudio->startRecorder();
      puts("Recording Start!");
    }
    
    /**
     * @brief Record given frame number
     */
    void loop() 
    {
      err_t err;
      /* recording end condition */
      if (theAudio->getRecordingSize() > recoding_size)
        {
          theAudio->stopRecorder();
          sleep(1);
          err = theAudio->readFrames(myFile);
    
          goto exitRecording;
        }
    
      /* Read frames to record in file */
      err = theAudio->readFrames(myFile);
    
      if (err != AUDIOLIB_ECODE_OK)
        {
          printf("File End! =%d\n",err);
          theAudio->stopRecorder();
          goto exitRecording;
        }
    
      if (ErrEnd)
        {
          printf("Error End\n");
          theAudio->stopRecorder();
          goto exitRecording;
        }
    
      /* This sleep is adjusted by the time to write the audio stream file.
         Please adjust in according with the processing contents
         being processed at the same time by Application.
      */
    //  usleep(10000);
    
      return;
    
    exitRecording:
    
      theAudio->closeOutputFile(myFile);
      myFile.close();
      
      theAudio->setReadyMode();
      theAudio->end();
      
      puts("End Recording");
      exit(1);
    }
    

  • DeveloperWorld

    Hi @atw72

    Could you try the following and see what the outcome is?

    #include <SDHCI.h>
    #include <Audio.h>
    
    SDClass theSD;
    AudioClass *theAudio;
    
    File myFile;
    
    bool ErrEnd = false;
    
    
    /**
       @brief Audio attention callback
    
       When audio internal error occurc, this function will be called back.
    */
    
    static void audio_attention_cb(const ErrorAttentionParam *atprm)
    {
      puts("Attention!");
    
      switch (atprm->error_code) {
        case AS_ATTENTION_CODE_INFORMATION:
        case AS_ATTENTION_CODE_WARNING    :
          ErrEnd = false;
          break;
        case AS_ATTENTION_CODE_ERROR      :
        case AS_ATTENTION_CODE_FATAL      :
        default:
          ErrEnd = true;
    
      }
    }
    
    /**
       @brief Setup recording of mp3 stream to file
    
       Select input device as microphone <br>
       Initialize filetype to stereo mp3 with 48 Kb/s sampling rate <br>
       Open "Sound.mp3" file in write mode
    */
    
    static const int32_t recoding_frames = 400;
    static const int32_t recoding_size = recoding_frames * 288; /* 96kbps, 1152sample */
    
    void setup()
    {
      theAudio = AudioClass::getInstance();
    
      theAudio->begin(audio_attention_cb);
    
      puts("initialization Audio Library");
    
      /* Select input device as microphone */
      theAudio->setRecorderMode(AS_SETRECDR_STS_INPUTDEVICE_MIC, AS_MICGAIN_HOLD, 68, true);
      
    
      /*
         Initialize filetype to stereo mp3 with 48 Kb/s sampling rate
         Search for MP3 codec in "/mnt/sd0/BIN" directory
      */
      theAudio->initRecorder(AS_CODECTYPE_MP3, "/mnt/sd0/BIN", AS_SAMPLINGRATE_48000, AS_CHANNEL_STEREO);
      puts("Init Recorder!");
    
      /* Open file for data write on SD card */
      myFile = theSD.open("sound.mp3", FILE_WRITE);
    
        /* Verify file open */
      if (!myFile)
      {
        printf("File open error\n");
        exit(1);
      }
    
      theAudio->startRecorder();
      puts("Recording Start!");
    }
    
    /**
       @brief Record given frame number
    */
    void loop()
    {
      err_t err;
      /* recording end condition */
      if (theAudio->getRecordingSize() > recoding_size)
      {
        theAudio->stopRecorder();
        sleep(1);
        err = theAudio->readFrames(myFile);
    
        goto exitRecording;
      }
    
      /* Read frames to record in file */
      err = theAudio->readFrames(myFile);
    
      if (err != AUDIOLIB_ECODE_OK)
      {
        printf("File End! =%d\n", err);
        theAudio->stopRecorder();
        goto exitRecording;
      }
    
      if (ErrEnd)
      {
        printf("Error End\n");
        theAudio->stopRecorder();
        goto exitRecording;
      }
    
      /* This sleep is adjusted by the time to write the audio stream file.
         Please adjust in according with the processing contents
         being processed at the same time by Application.
      */
      //  usleep(10000);
    
      return;
    
    exitRecording:
    
      theAudio->closeOutputFile(myFile);
      myFile.close();
    
      theAudio->setReadyMode();
      theAudio->end();
    
      puts("End Recording");
      exit(1);
    }