pythonwavbit-depth

Extract bit depth from wav file using wavfile.read


I was attempting to read the bit-depth from a wav file using wavfile.read, however, I have not been successful.

Here is the code that I have so far:

import numpy as np
from numpy.fft import fft, rfft
import matplotlib.pyplot as plt
from scipy.io import wavfile
from scipy import signal
import librosa
import librosa.display

input_file = (r'G:/pt5GAL_TCL_mono_Mono.wav')
sample_rate1, samples = wavfile.read(input_file)

# File Info
signal, sample_rate2 = librosa.load(input_file)
print('Total number of samples: ', signal.shape[0])
print('Sample rate: ', sample_rate1)
print('Length of file in seconds: ', librosa.get_duration(signal))
# print('Bit Depth: ', bits_per_sample)

How I can return the bit depth of the wav file?


Solution

  • You cannot directly access the number of bits per sample from the return value of scipy.io.wavfile.read, but you can determine it indirectly (and possibly ambiguously) from the dtype of the returned NumPy array according to the table shown in https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html.

    Of course, you could just use the wave module from the Python standard library which has a method getsampwidth.