Author: Kniebusch Horst
I have a very big wave file and I want to split it into two separately files. But,
this two files have to stand alone. You should can play and edit them like another
wave files.
Answer:
If you know how wave files work, this is very easy.
First you need the complete file header of the old wave file:
1 type
2 TWaveHeader = record
3 ident1: array[0..3] of Char; // Must be "RIFF"
4 len: DWORD; // remaining length after this header
5 ident2: array[0..3] of Char; // Must be "WAVE"
6 ident3: array[0..3] of Char; // Must be "fmt "
7 reserv: DWORD; // Reserved Size
8 wFormatTag: Word; // format type
9 nChannels: Word; // number of channels (i.e. mono, stereo,
10 etc.)
11 nSamplesPerSec: DWORD; //sample rate
12 nAvgBytesPerSec: DWORD; //for buffer estimation
13 nBlockAlign: Word; //block size of data
14 wBitsPerSample: Word; //number of bits per sample of mono data
15 cbSize: Word; //the count in bytes of the size of
16 ident4: array[0..3] of Char; //Must be "data"
17 end;
18
19
20 //You can load the file header with this function:
21
22
23 function GetWaveHeader(FileName: TFilename): TWaveHeader;
24 const
25 riff = 'RIFF';
26 wave = 'WAVE';
27 var
28 f: TFileStream;
29 w: TWaveHeader;
30 begin
31 if not FileExists(Filename) then
32 exit;
33
34 try
35 f := TFileStream.create(Filename, fmOpenRead);
36 f.read(w, Sizeof(w));
37
38 if w.ident1 <> riff then
39 begin
40 Showmessage('This is not a RIFF File');
41 exit;
42 end;
43
44 if w.ident2 <> wave then
45 begin
46 Showmessage('This is not a valid wave file');
47 exit;
48 end;
49
50 finally
51 f.free;
52 end;
53
54 Result := w;
55 end;
56
57
58 //Now we have all for creating the code for spliting the wave file:
59
60
61
62 function SplitWave(Source, Dest1, Dest2: TFileName; Pos: Integer): Boolean;
63 var
64 f1, f2, f3: TfileStream;
65 w: TWaveHeader;
66 p: Integer;
67 begin
68 Result:=False
69
70 if not FileExists(Source) then
71 exit;
72
73 try
74 w := GetWaveHeader(Source);
75
76 p := Pos - Sizeof(TWaveHeader);
77
78 f1 := TFileStream.create(Source, fmOpenRead);
79 f2 := TFileStream.create(Dest1, fmCreate);
80 f3 := TFileStream.create(Dest2, fmCreate);
81
82 {++++++++++Create file 1 ++++++++++++++++}
83 w.len := p;
84 f2.write(w, Sizeof(w));
85 f1.position := Sizeof(w);
86 f2.CopyFrom(f1, p);
87 {++++++++++++++++++++++++++++++++++++++++}
88
89 {+++++++++++Create file 2 +++++++++++++++}
90 w.len := f1.size - Pos;
91 f3.write(w, Sizeof(w));
92 f1.position := Pos;
93 f3.CopyFrom(f1, f1.size - pos);
94 {++++++++++++++++++++++++++++++++++++++++}
95 finally
96 f1.free;
97 f2.free;
98 f3.free;
99 end;
100
101 Result:=True;
102 end;
|