unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
windows, mmsystem, ayfly;
const
REPLAY_RATE = 48000;
CHANNELS = 2;
REPLAY_DEPTH = 16;
BuffSize = 65536*2; // multiple of 2
BufferCount = 2;
type
{ TForm1 }
TForm1 = class(TForm)
Memo1: TMemo;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
private
buffers: array[0..BufferCount-1] of array[0..BuffSize-1] of UInt32;
waveHeaders: array[0..BufferCount-1] of TWaveHdr;
currentBuffer: Integer;
public
end;
var
Form1: TForm1;
waveOut: HWAVEOUT;
waveHeader: TWaveHdr;
playing: Boolean = true;
// ayfly
songInfo: PAYSongInfo;
implementation
{$R *.lfm}
{ TForm1 }
/// audio init et le reste !!
procedure HandleError(const Str: PAnsiChar);
begin
if Str <> nil then
begin
ShowMessage('Error: Wrong Format ? '+ Str);
Halt(1);
end;
end;
procedure FillBuffer(bufferIndex: Integer);
begin
if playing then
begin
//le buffer avec des données audio
bufferIndex := Form1.currentBuffer;
ay_rendersongbuffer(songInfo, @Form1.buffers[bufferIndex][0], BuffSize * SizeOf(UInt32) );
end;
end;
function WaveOutCallback(hwo: HWAVEOUT; uMsg: UINT; dwInstance, dwParam1, dwParam2: DWORD_PTR): DWORD; stdcall;
begin
if uMsg = WOM_DONE then
begin
FillBuffer(Form1.currentBuffer);
waveOutWrite(waveOut, @Form1.waveHeaders[Form1.currentBuffer], SizeOf(TWaveHdr));
Form1.currentBuffer := (Form1.currentBuffer + 1) mod BufferCount;
end;
Result := 0;
end;
procedure InitAudio;
var
wFormat: TWaveFormatEx;
i: Integer;
begin
// SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_LOWEST); // if needed
with wFormat do
begin
wFormatTag := WAVE_FORMAT_PCM; // 1
nChannels := CHANNELS; // stereo 2
nSamplesPerSec := REPLAY_RATE; // 48000
wBitsPerSample := REPLAY_DEPTH; // 16 bits
nBlockAlign := nChannels * (wBitsPerSample div 8); // Taille echantillon
nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
cbSize := 0;
end;
if waveOutOpen(@waveOut, WAVE_MAPPER, @wFormat, QWORD(@WaveOutCallback), 0, CALLBACK_FUNCTION) <> MMSYSERR_NOERROR then
ShowMessage('Error: Audio initialization failed');
// buffers
for i := 0 to BufferCount - 1 do
begin
ZeroMemory(@Form1.waveHeaders[i], SizeOf(TWaveHdr));
with Form1.waveHeaders[i] do
begin
lpData := @Form1.buffers[i][0];
dwBufferLength := BuffSize * SizeOf(UInt32); // ** to-do type ! Unsigned int
dwFlags := 0 ;
end;
waveOutPrepareHeader(waveOut, @Form1.waveHeaders[i], SizeOf(TWaveHdr));
end;
Form1.currentBuffer := 0;
end;
procedure CloseAudio;
begin
waveOutUnprepareHeader(waveOut, @waveHeader, SizeOf(TWaveHdr));
waveOutClose(waveOut);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InitAudio;
// Init song
SongInfo := ay_initsong('cs.pt3', REPLAY_RATE,SongInfo);
fillbuffer(0);
waveOutWrite(waveOut, @waveHeaders[0], SizeOf(TWaveHdr)); // start audio !
// song infos
Memo1.Clear;
Memo1.Lines.Add('Song name : ' + (SongInfo^.Name));
Memo1.Lines.Add('Song author : ' + (SongInfo^.Author));
end;
end.