Search code examples
pythonaudionumpyffmpegpyaudio

Raw numpy array from real-time network audio stream in Python


I would like to get raw data (optimally as a numpy array) from a PCM network audio stream for which I have an URL. The goal is to analyze the signal from the network stream in real-time.

There seem to be a lot of Python audio modules or wrappers around e.g. FFMPEG, but after quite an extensive search I am yet to find a single example of a complete pipeline.

For those familiar with OpenCV, I am looking for an audio counterpart of the OpenCV VideoCampture class.

Any suggestions of modules to look at or code code snippets welcome!


Solution

  • Ok, figured it out. Apparently, this can be done without using any external libraries, just relying on urllib and wave. Here is a code snippet that streams data, converts it to a numpy array (for instance for processing) and then back in order to save it to a file. Tested for Python 3.

    import urllib
    import base64
    import wave
    import numpy as np
    
    # Open the 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')
    
    # Get parameters such as number of channels, framerate etc.
    params = in_wave.getparams()
    
    # Open and initialize an output file
    out_wave = wave.open('/home/user/out.wav', 'wb')
    out_wave.setparams(params)
    
    while True:
        # Get N frames as byte array
        frame = in_wave.readframes(10000)
        # Convert the bytes to numpy array
        arr = np.fromstring(frame, 'Int16')
        # Write a numpy array into a wave file
        out_wave.writeframes(arr.tostring())