Raw numpy array from real-time network audio stream in Python -
i raw data (optimally numpy array) pcm network audio stream have url. goal analyze signal network stream in real-time.
there seem lot of python audio modules or wrappers around e.g. ffmpeg, after quite extensive search yet find single example of complete pipeline.
for familiar opencv, looking audio counterpart of opencv videocampture class.
any suggestions of modules @ or code code snippets welcome!
ok, figured out. apparently, can done without using external libraries, relying on urllib , wave. here code snippet streams data, converts numpy array (for instance processing) , in order save file. tested python 3.
import urllib import base64 import wave import numpy np # open network wave stream request = urllib.request.request("http://url") request.add_header('authorization', b'basic ' + base64.b64encode(b'user:password')) in_file = urllib.request.urlopen(request) in_wave = wave.open(in_file, 'rb') # parameters such number of channels, framerate etc. params = in_wave.getparams() # open , initialize output file out_wave = wave.open('/home/czikus/out.wav', 'wb') out_wave.setparams(params) while true: # n frames byte array frame = in_wave.readframes(10000) # convert bytes numpy array arr = np.fromstring(frame, 'int16') # write numpy array wave file out_wave.writeframes(arr.tostring())
Comments
Post a Comment