Configure and Test the Audio

Before running the library sample, you must configure the audio system on the Raspberry Pi.

  1. Verify that recording and playback work:

    1. Play a test sound (this will be a person speaking). Press Ctrl+C when done. If you don't hear anything when you run this, check your speaker connection.

      speaker-test -t wav

    2. Record a short audio clip. If you get an error, go to step 2.

      arecord --format=S16_LE --duration=5 --rate=16k --file-type=raw out.raw

    3. Check the recording by replaying it.

      aplay --format=S16_LE --rate=16k out.raw

    4. Adjust the playback and recording volume.

      alsamixer

    If recording and playback are working, then you are done configuring audio. If not (or if you receive an error), continue to the next step below.

  2. Find your recording and playback devices.

    1. Note the card number and device number for recording.

      arecord -l

    2. Note the card number and device number for playback.

      aplay -l

  3. Create a new file named .asoundrc in the home directory (/home/pi). Make sure it has the right slave definitions for microphone and speaker:

    pcm.!default {
      type asym
      capture.pcm "mic"
      playback.pcm "speaker"
    }
    pcm.mic {
      type plug
      slave {
        pcm "hw:<card number>,<device number>"
      }
    }
    pcm.speaker {
      type plug
      slave {
        pcm "hw:<card number>,<device number>"
      }
    }
    
  4. If you have both an HDMI monitor and a 3.5mm jack speaker connected, you can play audio out of either one. Run the following command:

    sudo raspi-config

    Go to Advanced options > Audio and select the desired output device.

  5. Repeat Step 1 to verify that recording and playback work. If it's still not working, check that the microphone and speaker are properly connected. If this is not the issue, then try a different microphone or speaker.