Assignment 4: Summary of Chapter 9
CIS 598 – Embedded System Programing
Multimedia
Using PyAudio to.Get Sound into.Your Computer
Here are all way of things you can do once you have sound in the PC, however they all begin with a similar procedure of getting sound in. he module PyAudio is the best one for rapidly and essentially getting sound into the PC. When you’ve caught the sound, you can do anything you desire with it, however on the off chance that you need to utilize it in different projects, you’ll need some configuration that other programming gets it. WAV is a basic lossless organization that is extraordinary for straightforward sound information (despite the fact that it can create immense iles in case you’re recording a great deal of information).
You’ll also need a USB microphone to get the sound. he jack port of the Pi is only for sound output.
As always, the irst thing you’ll need to do is install the modules with the following terminal command:
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev python3-setuptools python3-pip sudo pip-3.2 install pyaudio
Examples: –
import pyaudio
import wave
def record_sound(seconds, chunk_size, sample_rate, filename, channels, format_type):
p = pyaudio.PyAudio()
stream = p.open(format=format_type,
channels=channels,
rate=sample_rate,
input=True,
frames_per_buffer=chunk_size)
print(“Speak now”)
frames = []
for i in range(0, int(sample_rate / chunk_size * seconds)):
data = stream.read(chunk_size)
frames.append(data)
if i%int(sample_rate/chunk_size) == 0:
print(seconds – round(i/(sample_rate/chunk_size)), “seconds remaining”)
print(“Finished”)
stream.stop_stream()
stream.close() p.terminate()
wf = wave.open(“filename”, ‘wb’)
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(format_type))
wf.setframerate(sample_rate)
wf.writeframes(b”.join(frames))
wf.close()
chunk_size = 1024
sample_rate = 44100
seconds = 15
filename = “output.wav”
channels = 1
format_type = pyaudio.paInt16
record_sound(seconds, chunk_size, sample_rate, filename, channels, format_type)
Recording the Sound
The record_sound() work does the majority of the diligent work (it could be inline code as opposed to a capacity and it would work ine, yet we make it a capacity here so it is simpler to incorporate into different projects). The first and second lines make another PyAudio item and after that utilization it to open a stream. You get the sound information from this stream.
The main work of recording the sound is then done by the for loop:
for i in range(0, int(sample_rate / chunk_size * seconds)):
data = stream.read(chunk_size)
frames.append(data)
Speaking to Your Pi
There are all way of things you can do with the sound you’ve recorded, however one of the coolest that doesn’t require any melodic ability is voice control. To do this, you need some method for handling the sound to extricate its substance, and this is a shockingly hard errand.
Examples
def google_speech_recognition(filename):
global url
audio = open(filename,’rb’).read()
http_header={‘Content-Type’: ‘audio/x-flac; rate=16000’}
request = urllib.request.Request(url, data=audio, headers=http_header)
response = urllib.request.urlopen(request)
out = response.read() json_data = json.loads(out.decode(“utf-8”))
return json_data