-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOBuffer_Wave.pas
215 lines (172 loc) · 6.73 KB
/
OBuffer_Wave.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
(*
* File: $RCSfile: OBuffer_Wave.pas,v $
* Revision: $Revision: 1.1.1.1 $
* Version : $Id: OBuffer_Wave.pas,v 1.1.1.1 2002/04/21 12:57:22 fobmagog Exp $
* Author: $Author: fobmagog $
* Homepage: http://delphimpeg.sourceforge.net/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*)
{$DEFINE SEEK_STOP}
unit OBuffer_Wave;
interface
uses
Windows, SysUtils, MMSystem, Classes, Shared, OBuffer, Player;
type
TOBuffer_Wave = class(TOBuffer)
private
FBufferP: array[0..MAX_CHANNELS-1] of Cardinal;
FChannels: Cardinal;
FDataSize: Cardinal;
FTemp: PByteArray;
hmmioOut: HMMIO;
mmioinfoOut: MMIOINFO;
ckOutRIFF: MMCKINFO;
ckOut: MMCKINFO;
public
constructor Create(NumberOfChannels: Cardinal; Player: TPlayer; Filename: String);
destructor Destroy; override;
procedure Append(Channel: Cardinal; Value: SmallInt); override;
procedure WriteBuffer; override;
{$IFDEF SEEK_STOP}
procedure ClearBuffer; override;
procedure SetStopFlag; override;
{$ENDIF}
end;
function CreateWaveFileOBffer(Player: TPlayer; Filename: String): TOBuffer;
implementation
uses
Math, Header;
function CreateWaveFileOBffer(Player: TPlayer; Filename: String): TOBuffer;
var Mode: TMode;
WhichChannels: TChannels;
begin
Mode := Player.Mode;
WhichChannels := Player.Channels;
try
if ((Mode = SingleChannel) or (WhichChannels <> both)) then
Result := TOBuffer_Wave.Create(1, Player, Filename) // mono
else
Result := TOBuffer_Wave.Create(2, Player, Filename); // stereo
except
on E: Exception do
Result := nil;
end;
end;
{ TOBuffer_Wave }
// Need to break up the 32-bit integer into 2 8-bit bytes.
// (ignore the first two bytes - either 0x0000 or 0xffff)
// Note that Intel byte order is backwards!!!
procedure TOBuffer_Wave.Append(Channel: Cardinal; Value: SmallInt);
begin
FTemp[FBufferP[Channel]] := (Value and $ff);
FTemp[FBufferP[Channel]+1] := (Value shr 8);
inc(FBufferP[Channel], FChannels shl 1);
end;
procedure TOBuffer_Wave.ClearBuffer;
begin
// Since we write each frame, and seeks and stops occur between
// frames, nothing is needed here.
end;
constructor TOBuffer_Wave.Create(NumberOfChannels: Cardinal; Player: TPlayer; Filename: String);
var pwf: TWAVEFORMATEX;
i: Cardinal;
begin
FChannels := NumberOfChannels;
FDataSize := FChannels * OBUFFERSIZE;
if (Player.Version = MPEG2_LSF) then
FDataSize := FDataSize shr 1;
if (Player.Layer = 1) then
FDataSize := FDataSize div 3;
FTemp := AllocMem(FDataSize);
hmmioOut := mmioOpen(PChar(FileName), nil, MMIO_ALLOCBUF or MMIO_WRITE or MMIO_CREATE);
if (hmmioOut = 0) then
raise Exception.Create('Output device failure');
// Create the output file RIFF chunk of form type WAVE.
ckOutRIFF.fccType := ord('W') or (ord('A') shl 8) or (ord('V') shl 16) or (ord('E') shl 24);
ckOutRIFF.cksize := 0;
if (mmioCreateChunk(hmmioOut, @ckOutRIFF, MMIO_CREATERIFF) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Initialize the WAVEFORMATEX structure
pwf.wBitsPerSample := 16; // No 8-bit support yet
pwf.wFormatTag := WAVE_FORMAT_PCM;
pwf.nChannels := FChannels;
pwf.nSamplesPerSec := Player.Frequency;
pwf.nAvgBytesPerSec := (FChannels * Player.Frequency shl 1);
pwf.nBlockAlign := (FChannels shl 1);
pwf.cbSize := 0;
// Create the fmt chunk
ckOut.ckid := ord('f') or (ord('m') shl 8) or (ord('t') shl 16) or (ord(' ') shl 24);
ckOut.cksize := sizeof(pwf);
if (mmioCreateChunk(hmmioOut, @ckOut, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Write the WAVEFORMATEX structure to the fmt chunk.
if (mmioWrite(hmmioOut, @pwf, sizeof(pwf)) <> sizeof(pwf)) then
raise Exception.Create('Output device failure');
// Ascend out of the fmt chunk, back into the RIFF chunk.
if (mmioAscend(hmmioOut, @ckOut, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Create the data chunk that holds the waveform samples.
ckOut.ckid := ord('d') or (ord('a') shl 8) or (ord('t') shl 16) or (ord('a') shl 24);
ckOut.cksize := 0;
if (mmioCreateChunk(hmmioOut, @ckOut, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
mmioGetInfo(hmmioOut, @mmioinfoOut, 0);
for i := 0 to FChannels-1 do
FBufferP[i] := i * FChannels;
end;
destructor TOBuffer_Wave.Destroy;
begin
// Mark the current chunk as dirty and flush it
mmioinfoOut.dwFlags := mmioinfoOut.dwFlags or MMIO_DIRTY;
if (mmioSetInfo(hmmioOut, @mmioinfoOut, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Ascend out of data chunk
if (mmioAscend(hmmioOut, @ckOut, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Ascend out of RIFF chunk
if (mmioAscend(hmmioOut, @ckOutRIFF, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Close the file
if (mmioClose(hmmioOut, 0) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
// Free the buffer memory
try
FreeMem(FTemp);
except
on E: Exception do;
end;
end;
procedure TOBuffer_Wave.SetStopFlag;
begin
end;
procedure TOBuffer_Wave.WriteBuffer;
var Write, i: Cardinal;
begin
Write := Min(FDataSize, Cardinal(mmioinfoOut.pchEndWrite) - Cardinal(mmioinfoOut.pchNext));
Move(FTemp^, mmioinfoOut.pchNext^, Write);
inc(Cardinal(mmioinfoOut.pchNext), Write);
if (Write < FDataSize) then begin
mmioinfoOut.dwFlags := mmioinfoOut.dwFlags or MMIO_DIRTY;
if (mmioAdvance(hmmioOut, @mmioinfoOut, MMIO_WRITE) <> MMSYSERR_NOERROR) then
raise Exception.Create('Output device failure');
end;
Move(FTemp[Write], mmioinfoOut.pchNext^, FDataSize - Write);
inc(Cardinal(mmioinfoOut.pchNext), FDataSize - Write);
// Reset buffer pointers
for i := 0 to FChannels-1 do
FBufferP[i] := i * FChannels;
end;
end.