时间: 2021-07-31 作者:daque
type tpcmwaveheader = record rid : array[0..3] of char; { 'riff' identifier } rlen : longint; wid : array[0..3] of char; { 'wave' identifier } fid : array[0..3] of char; { 'fmt ' identifier } flen : longint; { fixed, must be 16 } wformattag : word; { fixed, must be 1 } nchannels : word; { mono=1, stereo=2 } nsamplespersec : longint; { samplerate in hertz } navgbytespersec : longint; nblockalign : word; nbitspersample : word; { resolution, e.g. 8 or 16 } did : array[0..3]of char; { 'data' identifier } dlen : longint; { number of following data bytes } end; procedure writepcmwavefile(filename : string; resolution, channels, samplerate, samples : integer; data : pointer); var h : tpcmwaveheader; f : file; databytes : integer; begin databytes:=samples; databytes:=databytes*channels; { double if stereo } databytes:=databytes*(resolution div 8); { double if 16 bit } fillchar(h,sizeof(tpcmwaveheader),#0); with h do begin rid[0]:='r'; rid[1]:='i'; rid[2]:='f'; rid[3]:='f'; { 1st identifier } rlen:=databytes+36; wid[0]:='w'; wid[1]:='a'; wid[2]:='v'; wid[3]:='e'; { 2nd identifier } fid[0]:='f'; fid[1]:='m'; fid[2]:='t'; fid[3]:=chr($20); { 3rdidentifier ends with a space character } flen:=$10; { fixed, must be 16 } wformattag:=1; { fixed, must be 1 } nchannels:=channels; { channels } nsamplespersec:=samplerate; { sample rate in hertz } navgbytespersec:=samplerate*channels*trunc(resolution div 8); nblockalign:=channels*(resolution div 8); { byte order, see below } nbitspersample:=resolution; did[0]:='d'; did[1]:='a'; did[2]:='t'; did[3]:='a'; { data identifier } dlen:=databytes; { number of following data bytes } end; assignfile(f,filename); rewrite(f,1); blockwrite(f,h,sizeof(h)); blockwrite(f,pbytearray(data),databytes); closefile(f); { the rest of the file is the wave data. order is low-high for left channel, low-high for right channel, and so on. for mono or 8 bit files make the respective changes. } end;