unit ddsdtut31;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ddsd, StdCtrls;

type
  TForm1 = class(TForm)
    DDSD1: TDDSD;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private 宣言 }
    procedure UpdateWav(Sender:TDDSDGenWave; Player:TDDSDChannel; ofs,len:Cardinal);
  public
    { Public 宣言 }
    wav:TDDSDWaveData;
    count:Integer;
  end;

const
  samples:Array[0..47] of Integer =
    ( 84,75,67,67,  //さいた
      84,75,67,67,  //さいた
      56,67,75,84,  //チューリップの
      75,67,75,75,  //はなが

      84,75,67,67,  //ならんだ
      84,75,67,67,  //ならんだ
      56,67,75,84,  //あかしろ
      75,67,84,84,  //きいろ

      56,56,67,56,  //どのはな
      50,50,56,56,  //みても
      67,67,75,75,  //きれいだ
      84,84,84,84   //な
    );

var
  Form1: TForm1;

implementation



procedure TForm1.UpdateWav(Sender:TDDSDGenWave; Player:TDDSDChannel; ofs,len:Cardinal);
var
  i:Integer;
  buffer:Array[0..11024] of Byte;
begin

  for i:=0 to 11024 do begin
    buffer[i]:= ((i div samples[count]) And 1) * 64;  //サンプル数がsamples[idx] * 2の矩形波を作る
  end;

  //転送
  wav.BlockCopy(ofs,@buffer,len);

  //カウンタを増やす
  Inc(count);

  //ループさせる
  if count > High(samples) then
    count:=0;
end;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  //wav:=TDDSDWaveData.CreateEmptyBuffer(DDSD1, 22050, 8, False, 22050, False);
  wav:=TDDSDWaveData.CreateStream(DDSD1, 22050, 8, False, 22050);

  wav.OnUpdate:=UpdateWav;
  DDSD1[0].WaveData:=wav;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  buffer:Array[0..11024] of Byte;
begin
  DDSD1[0].Stop;

  //バッファの前半分を予め埋める
  for i:=0 to 11024 do begin
    buffer[i]:= ((i div samples[0]) And 1) * 64;  //サンプル数がsamples[idx] * 2の矩形波を作る
  end;
  count:=1;
  wav.BlockCopy(0, @buffer, 11025);


  DDSD1[0].LoopPlay;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DDSD1[0].Stop;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  DDSD1[0].Stop;
  wav.Free;

  wav:=TDDSDWaveData.CreateStream(DDSD1, 22050, 8, False, 22050);
  wav.OnUpdate:=UpdateWav;
  DDSD1[0].WaveData:=wav;
end;


end.
