I am trying to record audio via webbrowser for 6 seconds and send it to flask. The audio file then gets converted into a Spectogramm. Afterwards it should be transformed into a Numpy Array to be processed for a model.
I do not know if the problem is my upload function or something with librosa
I tried several different versions and options but cannot manage.
This is JS code:
function lDUp(){
var formData = new FormData();
const startButton = document.getElementById("startLungBtn");
startButton.addEventListener("click", function() {
if (!navigator.mediaDevices) {
console.error("getUserMedia not supported.")
return;
}
const constraints = { audio: true };
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
var chunks = [];
const recorder = new MediaRecorder(stream);
recorder.ondataavailable = event => {
// Collect all the chunks of the recording in an array.
chunks.push(event.data);
};
const audioContext = new AudioContext();
const audioStreamSource = audioContext.createMediaStreamSource(stream);
recorder.onstop = event => {
console.log("Recording stopped.")
// Create a blob with all the chunks of the recording.
let blob = new Blob(chunks, { type: "audio/wav" });
// let blob = new Blob(chunks, { type: recorder.mimeType });
chunks = [];
startButton.disabled = false;
// Create form data that contain the recording.
formData.append("audio_file", blob, "audio_file.wav");
$.ajax({
type: "POST",
url: "http://127.0.0.1:5000/LungUploader",
processData: false,
contentType: false,
cache: false,
data: formData,
success: function(response) {
console.log(response);
console.log("This is class: ", response)
},
error: function(err) {
console.log(err);
}
});
};
recorder.onstart = event => {
console.log("Recording started.");
startButton.disabled = true;
// Stop recording when the time is up.
setTimeout(function() { recorder.stop(); }, 6000);
};
recorder.start();
})
.catch(function(err) {
console.error(err);
});
});
}
This is code for flask:
@app.route('/LUploader', methods=["GET", 'POST'])
def lUploader():
if request.method == "POST":
f = request.files["audio_file.wav"]
with open('audio.wav', 'wb') as audio:
f.save(audio)
print("Checkpoint1")
y, sr = lb.load(f)
print("Checkpoint")
#Plot signal in
plt.figure(figsize=(10,3))
src_ft = lb.stft(y)
src_db = lb.amplitude_to_db(abs(src_ft))
specshow(src_db, sr=sr, x_axis='time', y_axis='hz')
plt.ylim(0, 5000)
plt.close()
}
Thanks for help in advance :)