Encapsulated into WAV Files

Source: Internet
Author: User

Wav. h

// Name: WAV. h <br/> # ifndef _ wav_h _ <br/> # DEFINE _ wav_h _ <br/> # ifndef u8 <br/> # defineu8unsigned char <br/> # endif <br/> # ifndef 2010< br/> # define 2010unsigned short <br/> # endif <br/> # ifndef u32 <br/> # define u32 unsigned int <br/> # endif <br/> # ifndef u64 <br/> # define u64 unsigned long <br/> # endif <br/> struct riff_header <br/> {<br/> u8 szriffid [4]; // 'R', 'I', 'F', 'F' <br/> u32 dwriffsize; // the size of the whole WAV file minus s Number of bytes occupied by zriffid and szriffformat <br/> u8 szriffformat [4]; // 'w', 'A', 'V ', 'E' <br/>}; <br/> struct wave_format <br/>{< br/> b2wformattag; // encoding method, U-law: 7 A-law: 6 <br/> 2010wchannels; // number of audio channels, 1 -- single channel; 2 -- Dual Channel <br/> u32 dwsamplespersec; // sampling frequency <br/> u32 dwavgbytespersec; // number of channels x width of each sample data x sampling frequency/8 <br/> b2wblockalign; // data block alignment unit (the number of bytes required for each sample) PCMU 1 byte <br/> b2wbitspersample; // The number of bits required for each sample PCMU 8bit <br /> U8 pack [2]; // Additional Information <br/>}; <br/> struct fmt_block <br/>{< br/> u8 szfmtid [4]; // 'F', 'M', 't', ''<br/> u32 dwfmtsize; // when additional information is available, no additional information is 16 <br/> struct wave_format wavformat; <br/>}; <br/>/* fact Chunk is an optional field, generally, a WAV file is converted into a chunk by some software. */<Br/> struct fact_block <br/> {<br/> u8 szfactid [4]; // 'F', 'A', 'C ', 'T' <br/> u32 dwfactsize; // number of characters starting from the next byte to szdataid <br/> }; <br/> struct data_block <br/> {<br/> u8 szdataid [4]; // 'D', 'A','t ', 'A' <br/> u32 dwdatasize; <br/>}; </P> <p> # endif

 

 

 

Wav_file_formate.c

// Name: wav_file_formate.c </P> <p> # include <stdio. h> <br/> # include <stdlib. h> // exit () <br/> # include <string. h> // bzero () <br/> # include <sys/time. h> <br/> # include <sys/types. h> <br/> # include <unistd. h> <br/> # include <sys/STAT. h>/* stat used */</P> <p> # include "WAV. H "<br/> # define raw_size 1024*50 <br/> # define tmep_buffer_size 200 <br/> unsigned long get_file_size (const char * filename) <br/>{< br/> struct sta T Buf; <br/> If (STAT (filename, & BUF) <0) <br/>{< br/> return 0; <br/>}< br/> return (unsigned long) BUF. st_size; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> struct riff_header; <br/> struct fmt_block; <br/> struct fact_block; <br/> struct data_block; <br/> file * wav_in; <br/> file * wav_out; <br/> unsigned long raw_size; <br/> If (argc! = 3) <br/>{< br/> printf ("Usage:/n/T % S <intput File> <output file>/N ", argv [0]); <br/> exit (-1); <br/>}< br/> // char name_in [] = "test. g711 "; <br/> wav_in = fopen (argv [1]," rb "); <br/> If (wav_in = NULL) <br/>{< br/> printf ("can't open input file % s/n", argv [1]); <br/> return (-1 ); <br/>}< br/> // char name_out [] = "test.wav"; <br/> wav_out = fopen (argv [2], "WB "); <br/> If (wav_out = NULL) <br/>{< br/> printf ("Can't open output file % s/n", argv [2]); <br/> fclose (wav_in); <br/> return (-1 ); <br/>}</P> <p> raw_size = get_file_size (argv [1]); <br/> If (raw_size = 0) <br/>{< br/> printf ("get_file_size error/N"); <br/> return (-1 ); <br/>}< br/> printf ("raw size is % d/N", raw_size); <br/> unsigned int temp_dwriffsize = sizeof (riff_header) + sizeof (fmt_block)/<br/> + sizeof (data_block) + raw_size-8; <br/> memmove (riff _ Header. szriffid, "riff", 4); <br/> riff_header.dwriffsize = temp_dwriffsize; // ==========< br/> memmove (riff_header.szriffformat, "wave", 4); <br/> memmove (fmt_block.szfmtid, "FMT", 4); <br/> fmt_block.dwfmtsize = 18; /// ===========< br/> fmt_block.wavformat.wformattag = 0x06; <br/> fmt_block.wavformat.wchannels = 0x01; <br/> mask = 0x1f40; <br/> fmt_block.wavformat.dwavgbytesper SEC = 0x1f40; <br/> fmt_block.wavformat.wblockalign = 0x01; <br/> fmt_block.wavformat.wbitspersample = 0x08; <br/> // memmove(fmt_block.wav format. pack, "", 2); <br/> memmove (data_block.szdataid, "data", 4); <br/> data_block.dwdatasize = raw_size; // ===========< br/> int ret; <br/> ret = fwrite (& riff_header, sizeof (riff_header), 1, wav_out ); <br/> If (Ret <0) <br/> {<br/> perror ("riff_header fwrite"); <br/> exit (0); <Br/>}< br/> ret = fwrite (& fmt_block, sizeof (fmt_block)-2, 1, wav_out); <br/> If (Ret <0) <br/>{< br/> perror ("fmt_block fwrite"); <br/> exit (0 ); <br/>}< br/> ret = fwrite (& data_block, sizeof (data_block), 1, wav_out); <br/> If (Ret <0) <br/>{< br/> perror ("data_block fwrite"); <br/> exit (0 ); <br/>}</P> <p> char temp_char; <br/> while (! Feof (wav_in) <br/>{< br/> temp_char = fgetc (wav_in); <br/> If (Ret <0) <br/>{< br/> perror ("fread"); <br/> break; <br/>}< br/> ret = fputc (temp_char, wav_out ); <br/> If (Ret <0) <br/> {<br/> perror ("fwrite"); <br/> break; <br/>}< br/> fclose (wav_in); <br/> fclose (wav_out); <br/> return 0; <br/>}

 

Compile wav_file_formate.c

Yuan @ Yuan :~ /Work/test/wav_file_formate> gcc-O wav_file_formate wav_file_formate.c

 

Run the program to read rawdata from the file test.g711and pack it into the file test.wav.

Yuan @ Yuan :~ /Work/test/wav_file_formate>./wav_file_formate test. g711 test.wav

 

Generate the test.wav file, which can be opened directly with the player.

 

I would like to thank my predecessors for sharing the following information:

Http://blog.csdn.net/pyundeng/archive/2006/10/24/1348936.aspx

Http://www.cnitblog.com/liaoqingshan/archive/2008/09/02/48651.html

Http://www.cnitblog.com/tinnal/archive/2009/01/03/53401.html

Http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html

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.