Operate the wave file (2): Determine whether a file is a wave file

Source: Internet
Author: User
The first 12 bytes of the wave file can be described as follows:
Triff = record ckid: DWORD; {'riff'} cksize: DWORD; {file size, excluding the first 8 bytes} fcctype: DWORD; {'wave '} end;
 

We can read the first 12 bytes of a file to determine whether it is a wave file.

Uses mmsystem, ioutils; {ioutils is used here. tfile. openread easily creates a file stream} procedure tform1.formcreate (Sender: tobject); var Riff: Record ckid, cksize, fcctype: DWORD; end; {You can define the structure and declare the structure variable} begin with tfile. openread ('C: \ windows \ media \ Windows XP start .wav ') Do begin read (riff, sizeof (riff); free; end; If (riff. ckid = fourcc_riff) and (riff. fcctype = mmiostringtofourcc ('wave ', 0) Then showmessagefmt ('this is a wave file, and its size is % d byte', [riff. cksize + 8]); end;
 

Write it as a function. It is better not to reference the mmsystem unit.

{If it is a wave file, the file size is returned. If not, the system returns 0} function iswave (filepath: string): integer; function mmiofourcc (chr0, chr1, chr2, chr3: ansichar ): DWORD; begin result: = DWORD (chr0) + DWORD (chr1) SHL 8 + DWORD (chr2) SHL 16 + DWORD (chr3) SHL 24; end; var Riff: Record ckid, cksize, fcctype: DWORD; end; begin result: = 0; with tfilestream. create (filepath, fmopenread) Do begin read (riff, sizeof (riff); free; end; If (riff. ckid = mmiofourcc ('R', 'I', 'F', 'F') and (riff. fcctype = mmiofourcc ('w', 'A', 'V', 'E') then result: = riff. cksize + 8; end;
 

In turn, you can also determine the specific format of a riff file.

{A function in the riff file format is returned. If it is not a riff file, 'noniff '} function getrifftype (filepath: string): string; function mmiofourcc (chr0, chr1, chr2, chr3: ansichar): DWORD; begin result: = DWORD (chr0) + DWORD (chr1) SHL 8 + DWORD (chr2) SHL 16 + DWORD (chr3) SHL 24; end; var Riff: record ckid, cksize, fcctype: DWORD; end; Type tchars = array [0 .. 3] of ansichar; {used for type conversion} begin result: = 'noniff '; with tfilestream. create (filepath, fmopenread) Do begin read (riff, sizeof (riff); free; end; If (riff. ckid = mmiofourcc ('R', 'I', 'F', 'F') then result: = tchars (riff. fcctype); end; // test: Begin showmessage (getrifftype ('C: \ windows \ media \ Windows XP start .wav '); {wave} showmessage (getrifftype ('C: \ windows \ clock. avi '); {Avi} showmessage (getrifftype ('C: \ windows \ notepad.exe'); {noneriff} end;
 

About fourcc_riff, mmiofourcc, and mmiostringtofourcc:

Riff files are composed of several "blocks", each of which starts with four characters (less than four characters are supplemented by spaces );
The four consecutive bytes are exactly the size of a 32-bit integer, so they are often read as an integer to judge.

You can use mmsystem. mmiostringtofourcc to obtain such an integer.

From C/C ++ code, we often see that mmiofourcc is not a function in the winmm. dll library, but a macro defined in C/C ++.
Here, we use Delphi to simulate and implement this function. Its function is similar to mmiostringtofourcc.

Mmsystem. fourcc_riff is a constant. It is used directly when an integer corresponding to "riff" is required. For example:

Uses mmsystem; {custom mmiofourcc function} function mmiofourcc (chr0, chr1, chr2, chr3: ansichar): DWORD; begin result: = DWORD (chr0) + DWORD (chr1) SHL 8 + DWORD (chr2) SHL 16 + DWORD (chr3) SHL 24; end; Procedure tform1.formcreate (Sender: tobject); var F1, F2, F3, F4: fourcc; {fourcc = DWORD;} begin F1: = mmiostringtofourcc ('riff', 0); F2: = mmiostringtofourcc ('riff', mmio_toupper ); {The second parameter can be used to convert the string to uppercase.} F3: = mmiofourcc ('R', 'I', 'F', 'F'); F4: = fourcc_riff; showmessagefmt ('% d, % d', [F1, F2, F3, F4]); {1179011410,117 9011410, 1179011410,117 9011410} end;
 

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.