'Stream binary audio data from http request for librosa analysis
I have a large audio file streaming from a web service.
I would like to load the audio data into librosa for batched stream analysis.
I took a look at librosa.core.stream where the description mentiones:
Any codec supported by
soundfileis permitted here.
But I can't seem to figure out how I can feed the binary batch data from requests:
import requests
import numpy as np
audio_url = "http://localhost/media/audioplayback.m4a"
response = requests.get(
audio_url,
stream=True,
)
for chunk in response.iter_content(chunk_size=4096):
npChunk = np.frombuffer(chunk, dtype=np.float64)
# Load chunk data into librosa
I know I need to convert the audio format but I'm not sure what is the recommended way to do this. I know it is possible to load the data directly into numpy array instead of calling librosa.stream. But I can't figure out the combination of soundfile, audioread, or GStreamer to do the format conversion.
I am using python==3.6.5 inside conda environtment inside Windows Subsystem for Linux
Any help would be greatly appreciated! Thank you!
Solution 1:[1]
My current solution is this:
You need to install pydub
from pydub import AudioSegment
audio_bytes = []
for b in request.files['audio_data'].stream.read():
audio_bytes += [b]
audio_bytes = bytes(audio_bytes)
s = io.BytesIO(audio_bytes)
audioObj = AudioSegment.from_file(s)
audioObj = audioObj.set_sample_width(2).set_frame_rate(16000).set_channels(1)
audioObj.export("input_audio.wav")
wav, sr = librosa.load("input_audio.wav")
wav = librosa.core.resample(wav, sr, 16000)
return wav
my frontend code is this:
recorder.onComplete = function(recorder, blob) {
console.log("Encoding complete");
createDownloadLink(blob,recorder.encoding);
//START AJAX HERE
var fd = new FormData();
fd.append('audio_data', blob);
console.log('Transcribing...')
document.getElementById('res_stat').innerHTML = "Waiting For Server's Response"
$.ajax({
type: 'POST',
url: "/audio",
data: fd,
processData: false,
contentType: false,
dataType: "json",
success: function(text){
console.log("Output Received")
document.getElementById("predOut").value = text.text;
}
});
console.log("Waiting For Server's Response")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | thethiny |
