1

I'm working on an audio-related project that connects with Django backend via rest api. Part of the front-end requires to display waveforms of associated mp3 files and for this, it in turn requires optimized data of each mp3 file in form of an array, which the front-end (javascript) then processes and converts to a waveform. I can pick the associated mp3 file from backend storage, the problem is converting it into an array which I can serve to the front-end api. I have tried several methods but none seem to be working. I tried this How to read a MP3 audio file into a numpy array / save a numpy array to MP3? which leaves my computer hanging until I forced it to restart by holding the power button down. I have a working ffmpeg and so, I have also tried this Trying to convert an mp3 file to a Numpy Array, and ffmpeg just hangs which continues to raise TypeError on np.fromstring(data[data.find("data")+4:], np.int16). I can't actually say what the problem is and I really hope someone can help. Thank you in advance!

EDIT This is the django view for retrieving the waveform data:

NB: I've only included useful codes as I'm typing with my mobile phone.

def waveform(self, request, ptype, id):
    project = Project.objects.get(pk=id)
    audio = project.audio

    mp3_path = os.path.join(cdn_dir, audio) 
    cmd = ['ffmpeg', '-i', mp3_path, '-f', 'wav', '-']
    p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=0x8000000)
    data = p.communicate()[0]
    array = np.fromstring(data[data.find("data")+4:], np.int16)

    return Response(array)

The TypeError I get is this: TypeError: argument should be integer or bytes-like object, not "str"

  • 1
    Maybe showing a little more code would help, like how do you define all variables involved in the error. A TypeError is raised when you perform operations with invalid types. So, make sure each of these variables has the type you assume it has (as in `print(type(_variable_name_))`). – Marc Compte May 30 '21 at 12:17
  • See this answer https://stackoverflow.com/questions/9458480/read-mp3-in-python-3 did it help? – dev ved May 30 '21 at 12:35
  • @Marc Compte, I've updated my answer to include useful codes. Kindly check and give your suggestion. Thanks in advance. – Ajayi Olamide May 30 '21 at 16:32
  • @dev ved, I'll try those solutions and let you know if any of them works. Thanks for your help. – Ajayi Olamide May 30 '21 at 16:38

0 Answers0