How to convert a 24-bit wav file to 16 or 32 bit files in python3

2024/10/1 23:45:07

I am trying to make spectrogram's of a bunch of .wav files so I can further analyze them(in python 3.6), however, I keep getting this nasty error

 ValueError: Unsupported bit depth: the wav file has 24-bit data.

I have looked into other stack overflow posts such as How do I write a 24-bit WAV file in Python? but theses didn't solve the issue!

I found a audio library called Pysoundfile

http://pysoundfile.readthedocs.io/en/0.9.0/

I installed it with

pip3 install pysoundfile

I have looked over the documentation and it is still not clear to me how to convert a 24-bit .wav file to a 32-bit wav file or a 16-bit wav file so that I can create a spectrogram from it.

Any help would be appreciated!

Answer

I would suggest using SoX for this task. Changing the bit depth is very simple:

sox old.wav -b 16 new.wav

If you must use Python, then you could use PySoundFile as you found. Here's a little code snippet:

import soundfiledata, samplerate = soundfile.read('old.wav')
soundfile.write('new.wav', data, samplerate, subtype='PCM_16')

You should also use soundfile.available_subtypes to see which subtypes you can convert a file to. Here's its sample usage, taken from their documentation:

>>> import soundfile as sf
>>> sf.available_subtypes('FLAC')
{'PCM_24': 'Signed 24 bit PCM','PCM_16': 'Signed 16 bit PCM','PCM_S8': 'Signed 8 bit PCM'}
https://en.xdnf.cn/q/70912.html

Related Q&A

Get consistent Key error: \n [duplicate]

This question already has answers here:How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?(23 answers)Closed 8 years ago.When trying to run a script containin…

Cannot take the length of Shape with unknown rank

I have a neural network, from a tf.data data generator and a tf.keras model, as follows (a simplified version-because it would be too long):dataset = ...A tf.data.Dataset object that with the next_x me…

Pre-fill new functions in Eclipse and Pydev with docstring and Not Implemented exception

I am editing my Python source code with Eclipse and Pydev.I want to document all of my functions and raise a "Not Implemented" exception whenever a function have not yet been implemented. For…

How to serialize hierarchical relationship in Django REST

I have a Django model that is hierarchical using django-mptt, which looks like:class UOMCategory(MPTTModel, BaseModel):"""This represents categories of different unit of measurements.&qu…

Django: Loading another template on click of a button

Ive been working on a django project for a few weeks now, just playing around so that I can get the hang of it. I am a little bit confused. I have a template now called "home.html". I was wo…

Given two python lists of same length. How to return the best matches of similar values?

Given are two python lists with strings in them (names of persons):list_1 = [J. Payne, George Bush, Billy Idol, M Stuart, Luc van den Bergen] list_2 = [John Payne, George W. Bush, Billy Idol, M. Stuart…

Extracting Javascript gettext messages using Babel CLI extractor

It is stated here that Babel can extract gettext messages for Python and Javascript files.Babel comes with a few builtin extractors: python (which extractsmessages from Python source files), javascript…

Getting TTFB (time till first byte) for an HTTP Request

Here is a python script that loads a url and captures response time:import urllib2 import timeopener = urllib2.build_opener() request = urllib2.Request(http://example.com)start = time.time() resp = ope…

accessing kubernetes python api through a pod

so I need to connect to the python kubernetes client through a pod. Ive been trying to use config.load_incluster_config(), basically following the example from here. However its throwing these errors. …

Understanding DictVectorizer in scikit-learn?

Im exploring the different feature extraction classes that scikit-learn provides. Reading the documentation I did not understand very well what DictVectorizer can be used for? Other questions come to …