2

I am currently working on a django project, in which i need to record the users audio and sent it to the server for processing. i have successfully recorded the audio using webRTC and passed it to views using an ajax request. But when i try to load the audio file using librosa for prosessing it gives an error for unknown file format.

in my console i can clearly see that the wav file is received but it cannot load the file for processing

to check what if i wrong with the file formats while recording tried different file formats which are supported by librosa and webRTC both

  1. audio/wav;codec=MS_PCM
  2. audio/wav;codec=MS_GSM610

to check if i had a problem with the file in receiving it on server i did upload the file on the server and i found that the file gets uploaded on the server and can be played on player directly.

code that i used to upload the file and retrive it (note: i directly gave the path to the file to any recorded file just to check)

def voice2(request):
    if request.method=='GET':
        return render(request,'record3.html')
    else:
        if request.method == 'POST' :
            print(request.FILES)
            audio=request.FILES.get("audioData")
            print(audio)

            file = request.FILES['audioData'] 
            path = default_storage.save('media/somename.wav', ContentFile(file.read()))
            (os.path.join(settings.MEDIA_ROOT, path))

            sig, samplerate = librosa.load(r'C:\Users\KRISHNA DAVE\project\tempproject\media\media\somename.wav')

            return render(request, 'record3.html')

now to check librosa couldn't read the file or the file had problem, this time i downloaded a sample audio file from the internet, then i created a new audio.py file in which i made a attempt to load both the files(the one i uploaded and the one i downloaded)

import librosa
#downloaded from internet
a,b=librosa.load(r"C:\Users\KRISHNA DAVE\Music\samplewav.wav")
print(a,b)
#recorded file
y, sr = librosa.load(r"C:\Users\KRISHNA DAVE\project\tempproject\media\media\somename.wav")
print(y,sr)

here i found that the downloaded file was loaded successfully but the file i recorded was still not loaded(i still get the error that the file is in unknown format)

I am still unable to understand how should i solve this problem. Should i replace using webRTC with something else or should i change the audio codec or there is a need to use different libraries(note: i already used librosa which also indirectly use soundfile and also tried soundfile directly, and i cannot use pyAudioAnalysis because i am using python 3.6 and pyAudioAnalysis is incompatible with this version)?

  • are you recording using the MediaRecorder API? If so, what are you passing as the mimeType in the constructor? See also https://stackoverflow.com/questions/41739837/all-mime-types-supported-by-mediarecorder-in-firefox-and-chrome – Philipp Hancke Jan 26 '20 at 13:32
  • yes i am using MediaRecorder API and i have tried audio/webm;codecs=opus, audio/wav;codec=MS_GSM610, audio/wav;codec=MS_PCM. @PhilippHancke [please have a look on my code for recording in my old post](https://stackoverflow.com/questions/59170410/what-is-the-problem-with-my-ajax-post-or-with-code-in-my-views-py-file-that-the) in all the three mimetypes the blob is created because its seen in the console – Krishna Dave Jan 27 '20 at 01:02
  • Hello! Did you manage to solve this issue? I am facing the same problem in my Django project using video-js record on the frontend to record audio from the users. I previously tried MediaRecorder and the behaviour is the same. I get the blob on the backend, but it is not recognized by librosa. – Catalin Mares Jan 19 '22 at 16:44
  • no the problem with librosa persist.... so I just got the blob with the help of request.FILES and saved it as a weba file, then I used pyaudioconverter api to convert that in wav format. Also I would suggest try using pydub package for with audios maybe that helps. – Krishna Dave Jan 20 '22 at 17:34
  • @CatalinMares also check that while passing the audio in your ajax its attributes contentType and processData should be False otherwise it creates a problem while fetching the audio in views. – Krishna Dave Jan 20 '22 at 17:40

0 Answers0