Skip to content
Advertisement

How to convert audio bytes generated by mediaRecorder and transfered using websocket to python to numpy array

I have a simple WebSocket project created by FastAPI like the following code:

JavaScript

After running the project, I want to convert data to a numpy array.

What I have tried: 1)

JavaScript

raises error:

JavaScript
JavaScript

This sample code does not raise any errors, but the output audio file does not contain any perceivable audio. Just noise is saved.

  1. Tried to use librosa & soundfile to read bytes io, but it does not recognize the format
JavaScript
JavaScript

Raised error:

JavaScript

import librosa @app.websocket(“/listen”) async def websocket_endpoint(websocket: WebSocket): print(“I’m here websocket_endpoint”) await websocket.accept()

JavaScript
JavaScript

Advertisement

Answer

Finally, by creating a bit similar issue on pytorch/audio github repository. I got the answer from moto in this comment. The final solution is as follows:

JavaScript

I could not convert the data to a numpy array, but the array.numpy() returns the numpy format if someone needs it.

PS: Versions of relevant libraries:

JavaScript

OS:

JavaScript
Advertisement