Python wav module obtains the sampling rate, sampling point, channel quantization bits (example code)

Time:2020-3-19

Installation:

pip install wave

In the wav module, we mainly introduce a method: getparams(), which returns the following results:


_wave_params(nchannels=1, sampwidth=2, framerate=48000, nframes=171698592, comptype='NONE', compname='not compressed')

Parameter interpretation:

  • Nchannels: number of channels
  • Sampwidth: quantized bits (bytes)
  • Framerate: sampling frequency
  • Nframes: number of sampling points

The code is as follows:

import wave

wavFile = r"C:\Users\Lenovo\Desktop\G0001.wav"
f = wave.open(wavFile)
#Audio head parameters
params = f.getparams()
Channels = f.getnchannels()
SampleRate = f.getframerate()
bit_type = f.getsampwidth() * 8
frames = f.getnframes()
Duration = frames / float (SampleRate) ා unit: S

Print ("audio header parameter:", params)
Print ("channels:", channels)
Print ("SampleRate:", SampleRate)
Print ("precision:", bit "type)
Print ("sampling points (frames):", frames)
Print ("duration:", duration)

Output results:

Audio head parameters: “wave” params (nchannels = 1, sampwidth = 2, framerate = 48000, nframes = 171698592, comptype =’none ‘, compname =’not compressed’)
Channels: 1
Sample rate: 48000
Precision: 16
Sampling points (frames): 171698592
Duration: 3577.054

Verification:

This is the result of using Sox command to check under win:
Sox command under win (SOX needs to be installed, but Baidu is allowed):sox --i G0001.wav

It can be seen that the output results of the two methods are consistent.

summary

The above-mentioned is the python wav module introduced by Xiaobian to get the quantized bits of the channel at the sampling rate sampling point, hoping to help you!