Wave audio file parsing program

Source: Internet
Author: User
Tags fread

<This article cannot be used for commercial purposes without permission. For more information, see the source and link of this Article.>

I. Description:

Recently I want to test the driver, so I am working as a wave file parser to obtain PCM audio data in the wave file.

 

 

Ii. program code:

# Include <stdio. h>

# Include <string. h>

Typedef int DWORD;

Typedef unsigned short word;

Typedef struct _ riff_header

{

Char riffid [4]; // 'R', 'I', 'F', 'F'

DWORD riffsize;

Char riffformat [4]; // 'w', 'A', 'V', 'E'

} _ Riff_header;

Typedef struct _ fmt_block

{

Char fmtid [4]; // 'F', 'M','t ',''

DWORD fmtsize;

Word formattag;

Word channels;

DWORD samplespersec;

DWORD avgbytespersec;

Word blockalign;

Word bitspersample;

} _ Fmt_block;

 
Typedef struct _ fact_block

{

Char factid [4]; // 'F', 'A', 'C', 'T'

DWORD factsize;

DWORD factdata;

} _ Fact_block;

 
Typedef struct _ data_block

{

Char dataid [4]; // 'D', 'A', 't', 'A'

DWORD datasize;

} _ Data_block;

 
Typedef struct _ fill_struct

{

_ Riff_header;

_ Fmt_block;

_ Fact_block;

_ Data_block;

} _ Fill_struct;/* 56bytes */

 
 
 
Int main (INT argc, char * argv [])

{

_ Fill_struct waveheader;

Char * PT;

Int ret;

File * FP;

Printf ("sizeriff = % d, sizefmt = % d, sizefact = % d, sizedata = % d, sizefill = % d/N", sizeof (_ riff_header ), sizeof (_ fmt_block ),

Sizeof (_ fact_block), sizeof (_ data_block), sizeof (_ fill_struct ));

Fp = fopen (argv [1], "R ");

If (FP = NULL)

{

Printf ("fileopen error/N ");

Return-1;

}

PT = (char *) & waveheader;

Ret = fread (PT, sizeof (char), sizeof (_ fill_struct), FP );

Printf ("fread % d Bytes/N", RET );

/* Riff */
Printf ("------------------------------------------/N ");

Printf ("szriffid = % C/N", waveheader. riff_header.riffid [0], waveheader. riff_header.riffid [1],

Waveheader. riff_header.riffid [2], waveheader. riff_header.riffid [3]);

Printf ("riffsize = % d/N", waveheader. riff_header.riffsize );

Printf ("riffformat = % C/N", waveheader. riff_header.riffformat [0], waveheader. riff_header.riffformat [1],

Waveheader. riff_header.riffformat [2], waveheader. riff_header.riffformat [3]);

/* Format */
Printf ("------------------------------------------/N ");

Printf ("szfmtid = % C/N", waveheader. fmt_block.fmtid [0], waveheader. fmt_block.fmtid [1],

Waveheader. fmt_block.fmtid [2], waveheader. fmt_block.fmtid [3]);

Printf ("fmtsize = % d/N", waveheader. fmt_block.fmtsize );

Printf ("formattag = % d/N", waveheader. fmt_block.formattag );

Printf ("channels = % d/N", waveheader. fmt_block.channels );

Printf ("samplespersec = % d/N", waveheader. fmt_block.samplespersec );

Printf ("avgbytespersec = % d/N", waveheader. fmt_block.avgbytespersec );

Printf ("blockalign = % d/N", waveheader. fmt_block.blockalign );

Printf ("bitspersample = % d/N", waveheader. fmt_block.bitspersample );

/* Fact. Some wave files do not have fact information, so here is the start of _ data_block,

Therefore, you must determine whether szfactid is "fact" or "data "*/
Printf ("------------------------------------------/N ");

Char strtmp [10];

Memcpy (strtmp, waveheader. fact_block.factid, 4 );

Strtmp [4] = '/0 ';

Printf ("strtmp = % s/n", strtmp );

If (strcmp (strtmp, "fact") = 0) {/* determine whether fact information exists */

Printf ("szfactid = % C/N", waveheader. fact_block.factid [0], waveheader. fact_block.factid [1],

Waveheader. fact_block.factid [2], waveheader. fact_block.factid [3]);

Printf ("factsize = % d/N", waveheader. fact_block.factsize );

Printf ("factdata = % d/N", waveheader. fact_block.factdata );

} Else {/* If factid is not equal to "fact", it indicates that there is no fact information, and the following will fill it with the _ data_block struct */

Memcpy (waveheader. data_block.dataid, waveheader. fact_block.factid, 4 );

Waveheader. data_block.datasize = waveheader. fact_block.factsize;

}

/* Data */
Printf ("------------------------------------------/N ");

Printf ("szdataid = % C/N", waveheader. data_block.dataid [0], waveheader. data_block.dataid [1],

Waveheader. data_block.dataid [2], waveheader. data_block.dataid [3]);

Printf ("datasize = % d/N", waveheader. data_block.datasize );

/* Audio data start */

/* The current file Pointer Points to the data zone. Read the file from here.

Let's talk more about it. Just find it online, mainly about the difference between single and dual channels */
Fclose (FP );

Return 0;

}

 

 

Iii. Test

Read the data, set the hardware code stream and other parameters, and directly put the data into the audio buffer! You can perform related tests on Linux.

Compile the code and run the following code:

./Test audio.wav

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.