Wav file parsing

Source: Internet
Author: User

Wave file format description:

 

Offset address

Bytes

Data Type

Internal capacity

 

File Header

00 h

4

Char

"Riff" flag

04 H

4

Long int

File Length

08 h

4

Char

"Wave" flag

0ch

4

Char

"FMT" flag

10 h

4

 

Transition bytes (not fixed)

14 h

2

Int

Format category (10 h for PCM audio data)

16 h

2

Int

Number of channels, 1 for single channel, 2 for dual channels

18 h

4

Int

Sampling rate (number of samples per second), indicating the playback speed of each channel,

1ch

4

Long int

The transmission rate of waveform audio data. The value is the number of channels × the number of data digits per second × the number of data digits per sample/8. The playback software can use this value to estimate the buffer size.

20 h

2

Int

The number of adjustment data blocks (calculated in bytes). The value is the number of channels × the data bit value per sample/8. The playback software needs to process multiple bytes of data of this value at a time to adjust the value for the buffer zone.

22 h

2

 

The number of data digits of each sample, indicating the number of data digits of each sample in each channel. If multiple channels exist, the sample size is the same for each channel.

24 h

4

Char

DATA tag "data"

28 h

4

Long int

Length of Speech Data

Storage Method of PCM Data:

 

Sample 1

Sample 2

8-Bit Single Channel

0-Channel

0-Channel

8-bit stereo

0 channels (left)

1-channel (right)

0 channels (left)

1-channel (right)

16-Bit Single Channel

0-channel low byte

0-channel high byte

0-channel low byte

0-channel high byte

16-bit stereo

0-channel (left) Low byte

0-channel (left) high byte

1-channel (right) Low byte

1-channel (right) high byte


Read all the fields in the wav file and write a new WAV file: wav_write.cpp.

# Include <iostream> # include <fstream> # include <string. h> # include <math. h> # include <cmath> # include <stdlib. h >#include <bitset ># include <iomanip> using namespace STD; struct my_wave {unsigned long chunckid; unsigned long chunksize; unsigned long format; unsigned long interval; unsigned long subchunk1size; unsigned short audioformat; unsigned short duration; unsigned long byterate; unsigned short blockalign; unsigned short duration; unsigned long duration; unsigned char * z_data ;}; int main (INT argc, char ** argv) {ifstream FS; ofstream OFS; my_wave MW; // read the original file FS. open ("f :\\ wave.wav", IOS: Binary | IOs: In); // you need to change this sentence to your file path ofs. open ("F: \ waveout.wav", IOS: Binary | IOs: Out); // write the file to be read to a new file // read all the fields of the wav file FS. seekg (0); FS. read (char *) & mW. chunckid, sizeof (MW. chunckid); FS. seekg (0x04); FS. read (char *) & mW. chunksize, sizeof (MW. chunksize); FS. seekg (0x08); FS. read (char *) & mW. format, sizeof (MW. format); FS. seekg (0x0c); FS. read (char *) & mW. subchunk1id, sizeof (MW. subchunk1id); FS. seekg (0x10); FS. read (char *) & mW. subchunk1size, sizeof (MW. subchunk1size); FS. seekg (0x14); FS. read (char *) & mW. audioformat, sizeof (MW. audioformat); FS. seekg (0x16); FS. read (char *) & mW. numchannels, sizeof (MW. numchannels); FS. seekg (0x18); FS. read (char *) & mW. samplerate, sizeof (MW. samplerate); FS. seekg (0x1c); FS. read (char *) & mW. byterate, sizeof (MW. byterate); FS. seekg (0x20); FS. read (char *) & mW. blockalign, sizeof (MW. blockalign); FS. seekg (0x22); FS. read (char *) & mW. bitspersampel, sizeof (MW. bitspersampel); FS. seekg (0x24); FS. read (char *) & mW. subchunk2id, sizeof (MW. subchunk2id); FS. seekg (0x28); FS. read (char *) & mW. subchunk2size, sizeof (MW. subchunk2size); mW. z_data = new unsigned char [mW. subchunk2size]; FS. seekg (0x2c); FS. read (char *) mW. z_data, sizeof (char) * mW. subchunk2size); // output all fields of the wav file for testing the cout <mW. chunckid <Endl; cout <mW. chunksize <Endl; cout <mW. format <Endl; cout <mW. subchunk1id <Endl; cout <mW. subchunk1size <Endl; cout <mW. audioformat <Endl; cout <mW. numchannels <Endl; cout <mW. samplerate <Endl; cout <mW. byterate <Endl; cout <mW. blockalign <Endl; cout <mW. bitspersampel <Endl; cout <mW. subchunk2id <Endl; cout <mW. subchunk2size <Endl; cout <"sample data:" <Endl; For (int K = 0; k <mW. subchunk2size; k ++) {printf ("% x", mW. z_data [k]);} // write each field to the OFS in The New. Wav file. write (char *) & mW. chunckid, sizeof (MW. chunckid); ofs. write (char *) & mW. chunksize, sizeof (MW. chunksize); ofs. write (char *) & mW. format, sizeof (MW. format); ofs. write (char *) & mW. subchunk1id, sizeof (MW. subchunk1id); ofs. write (char *) & mW. subchunk1size, sizeof (MW. subchunk1size); ofs. write (char *) & mW. audioformat, sizeof (MW. audioformat); ofs. write (char *) & mW. numchannels, sizeof (MW. numchannels); ofs. write (char *) & mW. samplerate, sizeof (MW. samplerate); ofs. write (char *) & mW. byterate, sizeof (MW. byterate); ofs. write (char *) & mW. blockalign, sizeof (MW. blockalign); ofs. write (char *) & mW. bitspersampel, sizeof (MW. bitspersampel); ofs. write (char *) & mW. subchunk2id, sizeof (MW. subchunk2id); ofs. write (char *) & mW. subchunk2size, sizeof (MW. subchunk2size); ofs. write (char *) mW. z_data, sizeof (char) * mW. subchunk2size); FS. close (); ofs. close (); Delete [] mW. z_data; System ("pause ");}

Wav file parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.