Python0019 audio processing (2) .wav file

Python0019 audio processing (2) .wav file


        In the previous article, I wrote that using the wave module provided by python to read and write .wav files, I feel that borrowing code written by others to read .wav files is not enough. Naturally there is a strong interest in .wav files. After yesterday's efforts, I have a little understanding of the file in this format, and realized the reading of .wav with java. This article is intended to be replaced by the process of getting to know this article. Instead of introducing a bunch of concepts at the beginning, and then putting code. It is easier to understand when written and read according to the cognitive process.


1. What is a .wav file

This is an audio file, take a .wav file, then read the file binary data, and output. Then use python's wave module to read the audio data and output it. Then compare the properties of the files and find some rules.

(1) Find a .wav file and process it with the following code:

import wave
f="./sound11.wav"
w=wave.open(f, "r")
dt=w.readframes(20)
print(dt)
w.close()

b=open(f,'rb')
for _ in range(10):
    print(b.read(40))
b.close()

Here is the output:

b"\x9b\xfd\x0e\xfcu\xffe\xfc+\xff\n\xfd\xec\xffG\xfe\xe7\x00\xb6\xfe\xe7\x00\xfd\xfd\x14\x01A\xfe\x8c\xff\xe3\xfe\xc4\xfe\x8d\xffP\xff\xea\xff\xcd\xfe\x0e\x00F\xff\xcb\x00$\xff)\xff\xcf\xfdk\xfd\xa2\xfe\r\xfd\xff\xfe\xe0\xfc{\xfe'\xfd\x18\xff\xdb\xfd:\xff\x1f\xfeq\xfe\xe0\xfc"
b'RIFFF\xe2\x04\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00@\x1f\x00\x00\x00}\x00\x00\x04\x00\x10\x00LIST'
b'\x1a\x00\x00\x00INFOISFT\x0e\x00\x00\x00Lavf58.10.100\x00data\x00\xe2\x04\x00\x9b\xfd'
b'\x0e\xfcu\xffe\xfc+\xff\n\xfd\xec\xffG\xfe\xe7\x00\xb6\xfe\xe7\x00\xfd\xfd\x14\x01A\xfe\x8c\xff\xe3\xfe\xc4\xfe\x8d\xffP\xff\xea\xff\xcd\xfe'
b"\x0e\x00F\xff\xcb\x00$\xff)\xff\xcf\xfdk\xfd\xa2\xfe\r\xfd\xff\xfe\xe0\xfc{\xfe'\xfd\x18\xff\xdb\xfd:\xff\x1f\xfeq\xfe\xe0\xfc\xc1\xfe"
b '\ xe0 \ xfb \ x85 \ xfe \ xf3 \ xfc \ x80 \ xfeg \ xfd \ xb8 \ xfd \ xd2 \ xfb \ x04 \ xfd \ xd3 \ xfb \ xc6 \ xfd_ \ xfc \ x07 \ xfe \ xeb \ xfb \ xc2 \ xfd \ x11 \ xfc \ x94 \ xfe \ xaf \ xfc \ xde \ xfe \ x14 \ xfd6 \ xff '
b'N \ xfdu \ x00 \ x9f \ xfd \ xbb \ x00 \ xef \ xfdY \ x01 \ xa5 \ xfeH \ x00 \ x02 \ xfea \ xfe \ xe9 \ xfd \ xcb \ xfd \ x9a \ xfd \ xfa \ xfd \ x12 \ xfd \ xe0 \ xfe \ xc6 \ xfdr \ xff \ xe0 \ xfe \ xef \ xfe '
b '\ xab \ xff6 \ xfe \ xfc \ xfe \ x15 \ xff \ xb2 \ xfe ^ \ xff \ xd8 \ xfea \ x00 \ x03 \ x00 \ xbe \ x00 \ xb5 \ x00 \ x94 \ x00 \ xe0 \ x01 \ x14 \ x01 \ xbc \ x01 \ xc1 \ x01 \ x03 \ x01q \ x01 \ xd1 \ x01; \ x02 '
b'\x97\x03$\x02\xb1\x03\xa1\x01\x16\x02\xf6\x01S\x02\xa2\x00\xee\x01j\x00a\x01\x8c\x00\x06\x01\xbe\x00\xbe\x00\xf2\x01p\x01C\x02\x00\x01\t\x02'
b'\xd1\x00\xdd\x01\xf6\x00e\x01m\x01-\x00\xb9\x00\xfd\xff\x10\x01\xbf\xff\xc1\x01\xa7\xfeJ\x00\\\xfe\x06\x00\t\xff\xd7\xff\xfe\xfe\xe0\xfe\r\x00'
b'\xe0\xfeQ\x01\x14\x00\x8c\x00\n\x00\xf8\xff\x08\xffV\x00\xcb\xff\xd9\xff\xa6\x00|\xff\x10\x02\xce\xff\xb9\x02A\xff\xd9\x01\x8a\xffz\x00\xf1\xff'

The first line is to read the binary data of the first 20 frames of the audio file, and the following lines are the binary data of the file. After seeing this data, my first reaction was the overlap. After careful comparison, I finally found that the last two bytes at the end of the third line of the output are the same as the audio frame data of the first line. Line 4 is also repeated.

Preliminary conclusion: .wave is an uncompressed audio file. The first 78 bytes of the .wave file record audio-related information.


(2) The next step is to study the first 78B of the file header.

Header data:

RIFFF\xe2\x04\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00@\x1f\x00\x00\x00}\x00\x00\x04\x00\x10\x00LIST

\x1a\x00\x00\x00INFOISFT\x0e\x00\x00\x00Lavf58.10.100\x00data\x00\xe2\x04\x00\

It can be seen that there are some understandable data of suspected fixed letters, which should be the fixed information in the header of the file. For example RIFF,WACE,fmt_,[LIST],[INFOISFT],data. Then I went to the Internet to find some information about the .wava header, as follows:

[4B] Fixed string 'RIFF' [4B] Integer, representing the remaining file length [4B] Fixed string ' WAVE' [4B] Fixed string 'fmt' (with a space after it)


[4B] Integer, representing the length of the next few audio file parameters [2B] Integer, audio_format_tag [2B] Integer, number of channels [4B] Integer, sampling rate

[4B] Integer, audio data transfer rate AvgBytesPerSec [2B] Integer, BlockAlign data block adjustment number [2B] Integer, sample size bitsPerSample


#The following part may or may not be, record some useless information.

[4B] Fixed string 'LIST' [4B] Integer, representing the total length of the remaining useless information s [sB] There is no useful information, there may be author, company and other information, just skip it.


[4B] Fixed string 'data' [4B] Integer, representing the total length of the next audio data n [nB] Audio data


(3) Analyze the header just now according to the header information above 

Note: Some binary data are displayed in character form, you can check here http://ascii.911cha.com/ to find the corresponding hexadecimal

Note: The integers inside are unsigned integers, and the data direction is from back to front. For example, F\xe2\x04\x00 represents the integer 0x0004e266

RIFF  F\xe2\x04\x00 The remaining file length is: 0x4e266 320102B    WAVE


The length of the data following fmt_   \x10\x00\x00\x00 is: 0x10 16B     \x01\x00 audio_famat_tag:0x1     \x02\x00The number of channels 0x2 2 channels

@\x1f\x00\x00 Sample rate 0x1f40 8000fps \x00}\x00\x00 Audio data transfer rate 0x7d00 32000Bps \x04\x00 Block adjustment 0x4 4Bpf         

\x10\x00Sampling size 0x10 16b=2B


LIST   \x1a\x00\x00\x00 The next data length is: 0x1a 26B INFOISFT \x0e\x00\x00\x00 The next data length is: 0xe 14B

Lavf 58.10.100  \x00 Deadline


data   \x00\xe2\x04\x00\ The following data length is: 0x4e200 320000B



2. With the above knowledge, it is not difficult to write a read and write class of a wave file.

slightly































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721155&siteId=291194637