Recording audio on your Raspberry Pi

by May 30, 2020Raspberry Pi

Raspberry Pi can record and playback fairly good quality audio through its USB 2.0 ports. For recording audio and playback we need two peripheral devices, a USB microphone, and a speaker. You can choose to use a USB speaker, or a speaker with a 3.5mm sound jack. Alternatively, if you are connected to a device with inbuilt speakers through HDMI(like your laptop, that will work too. The audio recorded can then be used for several applications using the Python audio analysis.

Hardware requirements:

  • Raspberry Pi with OS installed (available on the official website)
  • Ethernet cable/WiFi
  • Power cable
  • Monitor
  • HDMI connector
  • USB or Bluetooth mouse
  • USB or Bluetooth keyboard
  • Speaker (USB or with 3.5mm sound jack)
  • USB microphone

Setup for recording audio:

Hardware:

  1. Attach the Speaker and microphone to the Pi
  2. Power it on
Connections for hardware setup .

Software:

1. Open configuration settings

Open the terminal, and open the configuration settings by

sudo raspi-config

Alternatively, you can open it from:  start menu –> Preferences —> Raspberry Pi Configuration

2. Select the output

Use your arrow keys to scroll down to  Advanced Options and click enter to open the options

Scroll down and select Audio

Depending on the Speaker you are using, select the option you need.

Then click Ok and then Finish

3. Identify input device and card number

In the terminal type the command:

arecord -l

You will then see the list of capture hardware devices. From this, note down the card number and device number of the microphone.

In this case, it is card number: 1 and device number: 0

4. Identify output device and card number

In the terminal type the command:

aplay -l

You will then see the list of playback hardware devices. From this, note down the card number and device number of the speaker. If you are using an audio jack, it usually shows up as Analog or bcm2835 ALSA.

In this case, it is card number: 0 and device number: 0

5. Create a file in home directory

 In the terminal, type:

nano .asoundrc

In the GNU opened on clicking enter after previous command type

pcm.!default{
    type asym
    capture.pcm “mic”
    playback.pc “speaker”
}
pcm.mic {
    type plug
    slave {
    pcm “hw:< insert card number>,<device number>”
    }
}
pcm.speaker {
    type plug
    slave {
    pcm “hw:< insert card number>,<device number>”
    }
}

Replace the numbers to look like this,

Save and then exit. Then, to check if it has been stored in the home directory, type:

ls -a

This will enlist the whole list of the current directory including the hidden files, in which your file should also be visible. If it isnt, go back and check if you saved it in the right directory.

6. Adjust the playback volume:

This will control how loud the playback will be. Playback means playing ‘back’ the pre-recorded material. To do this first type:

alsamixer

You can then control the volume using the up and down arrow keys. Optimally, bring it to around 70.

7. Test the speaker output

Speaker output can be tested by playing a test audio. To do so, in the terminal type:

speaker-test -t wav

Once you can hear the voice, click ctrl+C. If you are unable to hear anything. Recheck your speaker connection.

8. Recording audio clip

To start a recording, in the terminal type

arecord --format=S16_LE --duration=5 --rate=16000 --filetype=raw sample.wav

This will record a short audio clip that is 5 seconds long. You can however, change the value in the arecord command to increase the duration of the clip.

9. Play the recording

To check if the recording has been saved successfully, we can now play it. To do this, in the terminal type:

aplay --format=S16_LE --rate=16000 sample.wav

This should play back the audio for you successfully!

In case of error:

If error occurred on testing the speaker with the test audio, and you werent able to hear: check the hardware connections. In addition to this, also ensure that you used the right device and card number. Else, replace the speaker.

If the speaker worked with the test audio, but not for the playback – check the microphone to ensure all is working. Otherwise, retry with another one.

You can now go ahead and use this in many applications. Whether its building a home assistant, or a speech-to-text system you can use this simple set of commands as a foundation!

\

Creating a multiplication Skill in Alexa using python

Written By Jyotsna Rajaraman

Hi! I'm Jyotsna, an electronics and communication undergrad who likes reading, writing, talking and learning. So when it comes to teaching, all my favorite things come together! I hope my articles have helped and taught you something. 🙂

RELATED POSTS

How to Setup an NGINX Server on Raspberry Pi ?

How to Setup an NGINX Server on Raspberry Pi ?

What is NGINX ? NGINX is a popular lightweight web server application you can install on the Raspberry Pi to allow it to serve web pages. In its initial release, it functioned for HTTP web serving. It is a web server that can also be used as...

FM Radio Transmitter with Raspberry Pi

FM Radio Transmitter with Raspberry Pi

We’ve all listened to the radio and sang along to the songs, atleast in the car. But have you ever found yourself switching through several channels because you couldn’t find even one song that YOU like? In this article we will learn how to build a FM radio...

Raspberry Pi Camera and its Variety

Raspberry Pi Camera and its Variety

Choosing a suitable raspberry pi camera for your projects can get really difficult. RPI camera v1, RPI camera v2, RPI NOIR camera, and the list continues. Each one has different properties and should be used according to them. They have been used in various fields and...

WiFi extender using Raspberry Pi

WiFi extender using Raspberry Pi

It is always useful to know how to use your Raspberry Pi in pet projects that are actually useful around the house. Especially when you aren’t building something new – using your dormant Raspberry Pi to build useful devices around the house is a fun idea. If you are...

Using Raspberry Pi as various Servers

Using Raspberry Pi as various Servers

Raspberry Pi is a widely popular SoC, that is versatile and easy to use for even beginners. If you are new and are unfamiliar with Pi, take a look at these articles. Regardless of the model of Raspberry Pi you have, I am sure you have discovered a plethora of projects...

SQLite Database on Raspberry Pi

Welcome to another tutorial on Raspberry Pi. In this tutorial, we will see how to use the SQLite Database on Raspberry Pi. Apart from the installation and a few basic commands, in the end, we shall look at a simple project to log sensor data into an SQLite database....

Mosquitto MQTT Broker on Raspberry Pi

This tutorial will show you what is Mosquitto MQTT Broker and how to install it on Raspberry Pi. Contents What is MQTT?Installing Mosquitto MQTT on Raspberry PiCreating an MQTT Broker on Raspberry PiSubscribe to a TopicPublish a message to a TopicSample...

Evolution of the Raspberry Pi – A Comparison

Evolution of the Raspberry Pi – A Comparison

The Raspberry Pi is an inexpensive credit card-sized micro-computer. The Raspberry Pi was originally designed as a way to teach how computers work and the rest of computer science in general. It was originally developed in the UK by a team that included Eben Upton,...

Tutorial: Dropbox with Raspberry Pi

Tutorial: Dropbox with Raspberry Pi

Many Raspberry Pi projects require synchronization of files over more than just one device. Dropbox, which is a popular file-hosting service, can be used for this with ease. Synchronizing data between different devices may seem a little tricky, especially since the...

Creating a Raspberry Pi Network Scanner

Creating a Raspberry Pi Network Scanner

The Raspberry Pi network scanner that we will be creating in the following tutorial will scan through your local network and get the local IP address and MAC address of all devices connected to your network. To achieve this we are going to use the scapy module in a...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....