Difference between revisions of "Audio Input and Output from USB Microphone + Raspberry Pi"

From ESE205 Wiki
Jump to: navigation, search
Line 1: Line 1:
'''Introduction''':
+
'''Overview''' <br/>
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.
+
This tutorial will describe how to input audio to a Raspberry Pi using a USB microphone and furthermore how to output that audio simultaneously if needed.  
 +
<br/>
 +
'''Materials and Prerequisites'''<br/>
 +
*USB Microphone
 +
*Raspberry Pi
 +
'''Process'''
 +
<br/>
 +
Install the following libraries:
 +
<br/>
 +
PyAudio: Used to play and record audio on a variety of platforms. Install it by typing
 +
<font face="Courier"><code>pip install pyaudio</code></font> into the terminal
 +
<br/>
 +
Numpy: fundamental package for scientific computing in Python. Install it by typing
 +
<font face="Courier"><code>pip install numpy</code></font> into the terminal
 +
<br/>
  
''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
+
Copy the following Python script to read audio input to wave files:
 
+
<br/>
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:
+
Copy the following Python script for simultaneously streaming audio output with input:
 
 
<font face="Courier"><code>pip install pyaudio</code></font>
 
 
 
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:
 
 
 
<font face="Courier"><code>pip install numpy</code></font>
 
 
 
 
 
'''Step 2''': The Code
 
 
 
Copy the code into your python script.
 
  
 
<source lang="python">
 
<source lang="python">
 
import pyaudio
 
import pyaudio
import struct
 
 
import numpy as np
 
import numpy as np
  
+
chunk=4096
CHUNK = 1024 * 4        # samples per frame
+
RATE=44100
FORMAT = pyaudio.paInt16 # audio format (bytes per sample)
+
 
CHANNELS = 1              # single channel for microphone
+
p=pyaudio.PyAudio()
RATE = 44100                # samples per second
 
  
+
#input stream setup
+
stream=p.open(format = pyaudio.paInt16,rate=RATE,channels=1, input_device_index = 2, input=True, frames_per_buffer=chunk)
p = pyaudio.PyAudio()
 
 
stream = p.open(
 
    format=FORMAT,
 
    channels=CHANNELS,
 
    rate=RATE,
 
    input=True,
 
    output=True,
 
    frames_per_buffer=CHUNK
 
)
 
  
while True:
+
#output stream setup
    data = stream.read(CHUNK)                 #reading input
+
player=p.open(format = pyaudio.paInt16,rate=RATE,channels=1, output=True, frames_per_buffer=chunk)
    data_int = struct.unpack(str(2 * CHUNK) + 'B', data) #converts bytes to integers
 
    data_int                               #prints the integer values
 
  
 +
while True:            #Used to continuously stream audio
 +
    data=np.fromstring(stream.read(chunk,exception_on_overflow = False),dtype=np.int16)
 +
    player.write(data,chunk)
 +
   
 +
#closes streams
 +
stream.stop_stream()
 +
stream.close()
 +
p.terminate
 
</source>
 
</source>
'''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.
+
'''Authors''' <br/>
[[File:Running Values.mp4|thumb|center|Running Integer Values]]
+
Pavan Narahari - Fall 2018 <br/>
 +
Sam Manyak - Fall 2018
 +
<br/>
 +
'''Group Link'''<br/>
 +
'''References'''<br/>
 +
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
 +
<br/>
 +
Here is the documentation for the PyAudio Library: [https://people.csail.mit.edu/hubert/pyaudio/docs/ PyAudio]
 +
 
  
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.
 
 
[[Category:HowTos]]
 
[[Category:HowTos]]
 
[[Category:Programming]]
 
[[Category:Programming]]
 
[[Category:Raspberry_Pi]]
 
[[Category:Raspberry_Pi]]

Revision as of 12:33, 7 December 2018

Overview
This tutorial will describe how to input audio to a Raspberry Pi using a USB microphone and furthermore how to output that audio simultaneously if needed.
Materials and Prerequisites

  • USB Microphone
  • Raspberry Pi

Process
Install the following libraries:
PyAudio: Used to play and record audio on a variety of platforms. Install it by typing pip install pyaudio into the terminal
Numpy: fundamental package for scientific computing in Python. Install it by typing pip install numpy into the terminal


Copy the following Python script to read audio input to wave files:
Copy the following Python script for simultaneously streaming audio output with input:

import pyaudio
import numpy as np

chunk=4096
RATE=44100

p=pyaudio.PyAudio()

#input stream setup
stream=p.open(format = pyaudio.paInt16,rate=RATE,channels=1, input_device_index = 2, input=True, frames_per_buffer=chunk)

#output stream setup
player=p.open(format = pyaudio.paInt16,rate=RATE,channels=1, output=True, frames_per_buffer=chunk)

while True:            #Used to continuously stream audio
     data=np.fromstring(stream.read(chunk,exception_on_overflow = False),dtype=np.int16)
     player.write(data,chunk)
    
#closes streams
stream.stop_stream()
stream.close()
p.terminate

Authors
Pavan Narahari - Fall 2018
Sam Manyak - Fall 2018
Group Link
References
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
Here is the documentation for the PyAudio Library: PyAudio