Configure and Test the Audio

Before running the 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=16000 --file-type=raw out.raw

    3. Check the recording by replaying it.

      aplay --format=S16_LE --rate=16000 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. Locate your USB microphone in the list of capture hardware devices. Write down the card number and device number.

      arecord -l

    2. Locate your speaker in the list of playback hardware devices. Write down the card number and device number. Note that the 3.5mm-jack is typically labeled Analog.

      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; use the configuration below but replace <card number> and <device number> with the numbers you wrote down in the previous step. Do this for both pcm.mic and pcm.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.