PDA

View Full Version : Doom SND Files and DirectSound



foahchon
19-01-2006, 05:52 PM
Hi, I've been hammering away at a way to get .SND files from Doom to play using DirectX as part of a project I'm doing for "fun" (masochism) using a little bit of code from Delphi Doom (mostly the sampling), and a little code from another article that I've seen floating around here, but I'm having no success so far. The following code does not produce any DirectX errors (other than a lost buffer, which is restored), but it doesn't produce any sound either. Also, when I close the application, it crashes. Anyway, here's what I have in a test form's Create event:

procedure TForm1.FormCreate(Sender: TObject);
var
BufferDesc: DSBUFFERDESC;
AudioPtr1,AudioPtr2,Temp: Pointer;
AudioBytes1,AudioBytes2: DWORD;
SNDSound: PChar;
FName: TFileStream;
DataSize: Integer;
begin
FName := TFileStream.Create('d:\DSSHOTGN.snd',fmOpenRead);
DataSize := FName.Size - 8;
FName.Seek(8,soFromBeginning); //8-byte header

GetMem(SNDSound,DataSize);
FName.Read(SNDSound^, DataSize);
FName.Free;
FreeMem(SNDSound,DataSize);

if DirectSoundCreate(nil, DirectSound, nil) <> DS_OK then
begin
DirectSound := nil;
ShowMessage('Could not create DS.');
exit;
end;

if DirectSound.SetCooperativeLevel(Self.Handle, DSSCL_PRIORITY) <> DS_OK then ShowMessage('Cannot set coop.');

PCMFormat.wFormatTag := WAVE_FORMAT_PCM;
PCMFormat.nChannels := 1;
PCMFormat.nSamplesPerSec := 11025;
PCMFormat.cbSize := 0;
PCMFormat.nBlockAlign := 1;
PCMFormat.nAvgBytesPerSec := 11025;
PCMFormat.wBitsPerSample := 8;

ZeroMemory(@BufferDesc, SizeOf(DSBUFFERDESC));
BufferDesc.dwSize := SizeOf(DSBUFFERDESC);
BufferDesc.dwFlags := DSBCAPS_PRIMARYBUFFER;
BufferDesc.dwBufferBytes := 0;
BufferDesc.lpwfxFormat := nil;

if DirectSound.CreateSoundBuffer(BufferDesc, DirectSoundBuffer, nil) <> DS_OK then
begin
ShowMessage('Cannot create sound buffer.');
DirectSoundBuffer := nil;
end
else
begin
if DirectSoundBuffer.SetFormat(PCMFormat) <> DS_OK then ShowMessage('Cannot set format.');
Temp := @SNDSound;
if DirectSoundBuffer.Lock(0,Length(SNDSound),AudioPtr 1,AudioBytes1,AudioPtr2,AudioBytes2,0) = DSERR_BUFFERLOST then
begin
DirectSoundBuffer.Restore;
if DirectSoundBuffer.Lock(0,DataSize,AudioPtr1,AudioB ytes1,AudioPtr2,AudioBytes2,0) <> DS_OK then ShowMessage('Cannot lock.');
Move(Temp^,AudioPtr1^,AudioBytes1);
if DirectSoundBuffer.Unlock(AudioPtr1,AudioBytes1,Aud ioPtr2,AudioBytes2) <> DS_OK then ShowMessage('Not unlocking.');
end;
if DirectSoundBuffer.Play(0, 0, 0) <> DS_OK then ShowMessage('Cannot play.');
end;

PCMFormat.wFormatTag := WAVE_FORMAT_PCM;
PCMFormat.nChannels := 1;
PCMFormat.nSamplesPerSec := 11025;
PCMFormat.nBlockAlign := 1;
PCMFormat.nAvgBytesPerSec := 11025;
PCMFormat.wBitsPerSample := 8;
PCMFormat.cbSize := 0;
end;

Can anyone offer any insight as to what I'm doing wrong here?
Thanks.

WILL
22-01-2006, 06:41 PM
Hey, nice! :)

People here that might be able to help you best may be, BeRo (maker of his own BeRo Tracker, made with Delphi/Free Pascal), Clootie (DirectX expert extrodinate) and Jimmy Valavanis (known for all his Total Conversion series of id Software game engines).