Get an audio sample as float number from pyaudio-stream
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
Related videos on Youtube
Markus Burger
Updated on June 04, 2022Comments
-
Markus Burger almost 2 years
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, likeWhat 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:
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()
-
Markus Burger over 10 yearsBecause I thought there's some kind of mail-notification when someone posts an answer here, I read it a bit late. but indeed, numpy.fromstring did the trick! Thank you very much
-
Tyson Graham over 8 yearsI altered this code to simply print out the array. Any idea how to convert these values to frequencies?
-
Henry James about 3 yearsAny idea how to convert back to the byte array?