Configure and Test the Audio

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

  1. 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 or bcm2835 ALSA (not bcm2835 IEC958/HDMI).

      aplay -l

  2. 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>"
      }
    }
    
  3. Verify that recording and playback work:

    1. Adjust the playback volume.

      alsamixer

      Press the up arrow key to set the playback volume level to around 70.

    2. 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

    3. Record a short audio clip.

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

    4. Check the recording by replaying it. If you don't hear anything, you may need to check the recording volume in alsamixer.

      aplay --format=S16_LE --rate=16000 out.raw

    If recording and playback are working, then you are done configuring audio. If not, check that the microphone and speaker are properly connected. If this is not the issue, then try a different microphone or speaker.

    Note that 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.

Next step

Configure a Developer Project and Account Settings