Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an audio sample as float number from pyaudio-stream

Tags:

python

pyaudio

As I am currently about to build a device based on a Raspberry Pi for measuring some stuff from noise recorded with a sound card (e.g. variance), and trying to do this within python, I got stuck figuring out how to get a an audiosample as float-number for further calculations.

What did I do:
Took a Line-In-to-chinch-adapter and touching the plugs for generating some sort of test signal.
Recording to for example Audacity or Matlab shows plausible results, like

enter image description here

What I want to get:
In ideal, I want to get for example 5 frames á 1024 samples from the sound card, and convert them into a list, tuple or numpy array as a float number for further calculations.

When trying to achieve this with python/pyaudio with the code at the end of this post, I got something like this:

enter image description here

Due to the fact that the values I got with python seem to differ from them in Matlab (and others) by the factor of about two, I think I've overseen something or did something wrong. I think I made a mistake somewhere at the struct.unpack region, but can't figure out where exactly or why. I'd like to ask you for help, pointing out where the error is and what I did wrong.

Little testcode for getting some samples and plotting them:

import pyaudio
import struct
import matplotlib.pyplot as plt

FORMAT = pyaudio.paFloat32
SAMPLEFREQ = 44100
FRAMESIZE = 1024
NOFFRAMES = 220
p = pyaudio.PyAudio()
print('running')

stream = p.open(format=FORMAT,channels=1,rate=SAMPLEFREQ,input=True,frames_per_buffer=FRAMESIZE)
data = stream.read(NOFFRAMES*FRAMESIZE)
decoded = struct.unpack(str(NOFFRAMES*FRAMESIZE)+'f',data)

stream.stop_stream()
stream.close()
p.terminate()
print('done')
plt.plot(decoded)
plt.show()
like image 777
Markus Burger Avatar asked Oct 28 '13 07:10

Markus Burger


1 Answers

Try use "numpy.fromstring" function to replace "struct.unpack":

import numpy
stream = p.open(format=FORMAT,channels=1,rate=SAMPLEFREQ,input=True,frames_per_buffer=FRAMESIZE)
data = stream.read(NOFFRAMES*FRAMESIZE)
decoded = numpy.fromstring(data, 'Float32');

let me know if this works for you

like image 117
ederwander Avatar answered Oct 08 '22 23:10

ederwander