Author: Richard Funke
How do i write a wave file?
Answer:
1 type
2 TPCMWaveHeader = record
3 rID: array[0..3] of char; { 'RIFF' Identifier }
4 rLen: longint;
5 wID: array[0..3] of char; { 'WAVE' Identifier }
6 fId: array[0..3] of char; { 'fmt ' Identifier }
7 fLen: longint; { Fixed, must be 16 }
8 wFormatTag: word; { Fixed, must be 1 }
9 nChannels: word; { Mono=1, Stereo=2 }
10 nSamplesPerSec: longint; { SampleRate in Hertz }
11 nAvgBytesPerSec: longint;
12 nBlockAlign: word;
13 nBitsPerSample: word; { Resolution, e.g. 8 or 16 }
14 dId: array[0..3] of char; { 'data' Identifier }
15 dLen: longint; { Number of following data bytes }
16 end;
17
18 procedure WritePCMWaveFile(Filename: string; Resolution, Channels, Samplerate,
19 Samples: integer; Data: Pointer);
20 var
21 h: TPCMWaveHeader;
22 f: file;
23 databytes: integer;
24 begin
25 DataBytes := Samples;
26 DataBytes := DataBytes * Channels; { double if stereo }
27 DataBytes := DataBytes * (Resolution div 8); { double if 16 Bit }
28
29 FillChar(h, SizeOf(TPCMWaveHeader), #0);
30 with h do
31 begin
32 rID[0] := 'R';
33 rID[1] := 'I';
34 rID[2] := 'F';
35 rID[3] := 'F'; { 1st identifier }
36 rLen := DataBytes + 36;
37 wID[0] := 'W';
38 wID[1] := 'A';
39 wID[2] := 'V';
40 wID[3] := 'E'; { 2nd identifier }
41 fId[0] := 'f';
42 fId[1] := 'm';
43 fId[2] := 't';
44 fID[3] := Chr($20); { 3rdidentifier ends with a space character }
45 fLen := $10; { Fixed, must be 16 }
46 wFormatTag := 1; { Fixed, must be 1 }
47 nChannels := Channels; { Channels }
48 nSamplesPerSec := SampleRate; { Sample rate in Hertz }
49 nAvgBytesPerSec := SampleRate * Channels * trunc(Resolution div 8);
50 nBlockAlign := Channels * (Resolution div 8); { Byte order, see below }
51 nBitsPerSample := Resolution;
52 dId[0] := 'd';
53 dId[1] := 'a';
54 dId[2] := 't';
55 dId[3] := 'a'; { Data identifier }
56 dLen := DataBytes; { number of following data bytes }
57 end;
58 AssignFile(f, filename);
59 ReWrite(f, 1);
60 BlockWrite(f, h, SizeOf(h));
61 BlockWrite(f, pbytearray(data), databytes);
62 CloseFile(f);
63 { The rest of the file is the wave data. Order is low-high for left channel,
64 low-high for right channel, and so on.
65 For mono or 8 bit files make the respective changes. }
66 end;
|