The structure of the loading format information is as follows:
Twaveformat = packed record wformattag: word; nchannels: word; nsamplespersec: DWORD; navgbytespersec: DWORD; nblockalign: word; end; tags = record WF: twaveformat; wbitspersample: word; End; twaveformatex = packed record wformattag: word; {format type; mainly used: wave_format_pcm} nchannels: word; {number of channels; 1 is single channel; 2 is STEREO} nsamplespersec: DWORD; {sampling frequency} navgbytespersec: DWORD; {transmission rate} nblockalign: word; {size of each sample} wbitspersample: word; {sampling precision} cbsize: word; {size of additional data; the PCM-encoded file does not have this field} end;
We can see that they are incrementing by one Field in sequence, and they are also a component of the wave file. Now we need to extract them from the wave file.
Obtain the function and test code:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; button1: tbutton; procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses mmsystem; // function used to obtain formatted data in the wave; twaveformatex is commonly used, but PCM lacks its function getwavefmt (filepath: string; var FMT: twaveformatex ): boolean; var hfile: hmmio; ckiriff, ckifmt: tmmckinfo; begin result: = false; hfile: = mmioopen (pchar (filepath), nil, mmio_read ); if hfile = 0 Then exit; zeromemory (@ ckiriff, sizeof (tmmckinfo); zeromemory (@ ckifmt, sizeof (tmmckinfo); zeromemory (@ FMT, sizeof (bytes )); {clear the struct to be accepted first} ckifmt. ckid: = mmiostringtofourcc ('fmt', 0); {prepare for the format block} // obtain the information of the main block first mmiodescend (hfile, @ ckiriff, nil, mmio_findriff ); // after obtaining the FMT block information, the pointer automatically points to the start point of the format data, and then reads the format data if (ckiriff. ckid = fourcc_riff) and (ckiriff. fcctype = mmiostringtofourcc ('wave ', 0) and (mmiodescend (hfile, @ ckifmt, @ ckiriff, timeout) = mmsyserr_noerror) then result: = (mmioread (hfile, @ FMT, ckifmt. cksize) = ckifmt. cksize); mmioclose (hfile, 0); end; // call the test procedure tform1.button1click (Sender: tobject); const filepath = 'C: \ windows \ media \ Windows XP start .wav '; vaR waveformat: twaveformatex; begin if getwavefmt (filepath, waveformat) then with memo1.lines do begin clear; add (format ('wformattag: % d', [waveformat. wformattag]); add (format ('nchannels: % d', [waveformat. nchannels]); add (format ('nsamplespersec: % d', [waveformat. nsamplespersec]); add (format ('navgbytespersec: % d', [waveformat. navgbytespersec]); add (format ('nblockalign: % d', [waveformat. nblockalign]); add (format ('wbitspersample: % d', [waveformat. wbitspersample]); add (format ('cbsize: % d', [waveformat. cbsize]); end; {display result: wformattag: 1 nchannels: 2 nsamplespersec: 22050 navgbytespersec: 88200 nblockalign: 4 wbitspersample: 16 cbsize: 0} end; end.