Difference between revisions of "Audio Input and Output from USB Microphone + Raspberry Pi"
Bvandersman (talk | contribs) (Created page with "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 conver...") |
Bvandersman (talk | contribs) |
||
Line 16: | Line 16: | ||
Copy the code into your python script. | Copy the code into your python script. | ||
+ | <source lang="python"> | ||
import pyaudio | import pyaudio | ||
import struct | import struct | ||
Line 44: | Line 45: | ||
data_int #prints the integer values | data_int #prints the integer values | ||
+ | </source> | ||
Step 3: | 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. | 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. |
Revision as of 03:10, 1 May 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.