Difference between revisions of "Audio Input and Output from USB Microphone + Raspberry Pi"
Jump to navigation
Jump to search
Bvandersman (talk | contribs) |
|||
(17 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | + | == 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. | |
+ | <br/> | ||
+ | == Materials and Prerequisites == | ||
+ | *USB Microphone | ||
+ | *Raspberry Pi | ||
+ | *Speaker for audio output | ||
− | '' | + | == Process == |
+ | '''Libraries to Install''' | ||
+ | <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/> | ||
− | ''' | + | '''Code''' <br/> |
+ | Copy the following Python script to read audio input to wave files: | ||
+ | <source lang="python"> | ||
+ | import pyaudio | ||
+ | import wave | ||
+ | import os | ||
+ | |||
+ | #The following code comes from markjay4k as referenced below | ||
+ | form_1 = pyaudio.paInt16 | ||
+ | chans=1 | ||
+ | samp_rate = 44100 | ||
+ | chunk = 4096 | ||
+ | record_secs = 1 #record time | ||
+ | dev_index = 2 | ||
+ | wav_output_filename = 'test1.wav' | ||
+ | |||
+ | |||
+ | audio = pyaudio.PyAudio() | ||
− | + | #setup audio input stream | |
+ | stream=audio.open(format = form_1,rate=samp_rate,channels=chans, input_device_index = dev_index, input=True, frames_per_buffer=chunk) | ||
+ | print("recording") | ||
+ | frames=[] | ||
− | + | for ii in range(0,int((samp_rate/chunk)*record_secs)): | |
+ | data=stream.read(chunk,exception_on_overflow = False) | ||
+ | frames.append(data) | ||
− | + | print("finished recording") | |
− | + | stream.stop_stream() | |
+ | stream.close() | ||
+ | audio.terminate() | ||
+ | #creates wave file with audio read in | ||
+ | #Code is from the wave file audio tutorial as referenced below | ||
+ | wavefile=wave.open(wav_output_filename,'wb') | ||
+ | wavefile.setnchannels(chans) | ||
+ | wavefile.setsampwidth(audio.get_sample_size(form_1)) | ||
+ | wavefile.setframerate(samp_rate) | ||
+ | wavefile.writeframes(b''.join(frames)) | ||
+ | wavefile.close() | ||
− | + | #plays the audio file | |
+ | os.system("aplay test1.wav") | ||
− | Copy the | + | </source> |
+ | Copy the following Python script for simultaneously streaming audio output with input: | ||
<source lang="python"> | <source lang="python"> | ||
import pyaudio | import pyaudio | ||
− | |||
import numpy as np | import numpy as np | ||
− | + | #The following code comes from markjay4k as referenced below | |
− | |||
− | |||
− | |||
− | |||
− | + | 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) | ||
+ | |||
+ | #the code below is from the pyAudio library documentation referenced below | ||
+ | #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 | ||
</source> | </source> | ||
− | |||
− | + | == Authors == | |
− | + | Pavan Narahari - Fall 2018 <br/> | |
+ | Sam Manyak - Fall 2018 | ||
+ | <br/> | ||
+ | == Group Link == | ||
+ | [https://classes.engineering.wustl.edu/ese205/core/index.php?title=Crutooth Crutooth] | ||
+ | <br/> | ||
+ | == 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: [https://people.csail.mit.edu/hubert/pyaudio/docs/ PyAudio] | ||
+ | *Tutorial for audio input to wave file: https://docs.python.org/3/library/wave.html | ||
+ | |||
+ | |||
[[Category:HowTos]] | [[Category:HowTos]] | ||
+ | [[Category:Programming]] | ||
+ | [[Category:Raspberry_Pi]] |
Latest revision as of 22:23, 11 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
- Speaker for audio output
Process
Libraries to Install
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
Code
Copy the following Python script to read audio input to wave files:
import pyaudio
import wave
import os
#The following code comes from markjay4k as referenced below
form_1 = pyaudio.paInt16
chans=1
samp_rate = 44100
chunk = 4096
record_secs = 1 #record time
dev_index = 2
wav_output_filename = 'test1.wav'
audio = pyaudio.PyAudio()
#setup audio input stream
stream=audio.open(format = form_1,rate=samp_rate,channels=chans, input_device_index = dev_index, input=True, frames_per_buffer=chunk)
print("recording")
frames=[]
for ii in range(0,int((samp_rate/chunk)*record_secs)):
data=stream.read(chunk,exception_on_overflow = False)
frames.append(data)
print("finished recording")
stream.stop_stream()
stream.close()
audio.terminate()
#creates wave file with audio read in
#Code is from the wave file audio tutorial as referenced below
wavefile=wave.open(wav_output_filename,'wb')
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b''.join(frames))
wavefile.close()
#plays the audio file
os.system("aplay test1.wav")
Copy the following Python script for simultaneously streaming audio output with input:
import pyaudio
import numpy as np
#The following code comes from markjay4k as referenced below
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)
#the code below is from the pyAudio library documentation referenced below
#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
- Tutorial for audio input to wave file: https://docs.python.org/3/library/wave.html