Difference between revisions of "Audio Input and Output from USB Microphone + Raspberry Pi"
m (Ethanshry moved page How to Read Audio Input from a USB Microphone on the Raspberry Pi to Audio Input from USB Microphone + Raspberry Pi: Fall 2018 Wiki Reworks) |
m (Naraharip moved page Audio Input from USB Microphone + Raspberry Pi to Audio Input and Output from USB Microphone + Raspberry Pi) |
(No difference)
|
Revision as of 18:04, 7 December 2018
Introduction: One of the major components of my project, Vybz, was being able to read raw audio input from a USB microphone. Information from a microphone is originally converted from an analog signal into a group of bytes (binary digits) using the microphone’s built in A/D converter. However, bytes were a relatively difficult unit to work with. For the project, I needed access to nice integer values that were an accurate representation of the volume level in the room. Thus, this tutorial will show you how to read audio input from a USB microphone on the Raspberry Pi.
Note: The basis of the code was derived from the code developed by markjay4k, an engineer working with audio spectrum analyzers. You can find a link to his github at: https://github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python
Step 1: Install Libraries
In order to read raw audio data, you will first need to install some useful libraries that will be implemented in your code. First, install pyaudio. With PyAudio, you can easily use Python to play and record audio on a variety of platforms. To install, type the following into the terminal:
pip install pyaudio
Then you’ll want to install NumPy which is a fundamental package for scientific computing in Python. You can install it my typing this into the terminal:
pip install numpy
Step 2: The Code
Copy the code into your python script.
import pyaudio
import struct
import numpy as np
CHUNK = 1024 * 4 # samples per frame
FORMAT = pyaudio.paInt16 # audio format (bytes per sample)
CHANNELS = 1 # single channel for microphone
RATE = 44100 # samples per second
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK
)
while True:
data = stream.read(CHUNK) #reading input
data_int = struct.unpack(str(2 * CHUNK) + 'B', data) #converts bytes to integers
data_int #prints the integer values
Step 3: Run the code
Once you run the code, you should be able to see the running integer values for the bytes taken in by your microphone.
The video above demonstrates the running integer values that represents the relative volume input from the microphone. On the left side of the monitor is the console displaying the integers and on the rights side of the console is the code.