unit ddsdtest;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DDSD, ComCtrls, ExtCtrls, DSound, Menus;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    DDSD1: TDDSD;
    Button3: TButton;
    Button4: TButton;
    TrackBar1: TTrackBar;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    TrackBar2: TTrackBar;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit3: TEdit;
    TrackBar3: TTrackBar;
    Label7: TLabel;
    Edit4: TEdit;
    Timer1: TTimer;
    GroupBox1: TGroupBox;
    Label10: TLabel;
    Label11: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Button5: TButton;
    MainMenu1: TMainMenu;
    K1: TMenuItem;
    KR1: TMenuItem;
    Exit1: TMenuItem;
    KDirectSoundI1: TMenuItem;
    N1: TMenuItem;
    Bevel1: TBevel;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure TrackBar2Change(Sender: TObject);
    procedure TrackBar3Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure KR1Click(Sender: TObject);
    procedure KDirectSoundI1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
    Wav:TDDSDWaveData;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

//ロード
//再生
procedure TForm1.Button2Click(Sender: TObject);
begin
  DDSD1[0].Play;
end;

//停止
procedure TForm1.Button3Click(Sender: TObject);
var
  i:Integer;
begin
  For i:=0 to DDSD1.ChannelCount-1 do
    DDSD1[i].Stop;
end;

//ループ再生
procedure TForm1.Button4Click(Sender: TObject);
begin
  DDSD1[0].LoopPlay;
end;

//パン
procedure TForm1.TrackBar1Change(Sender: TObject);
var
  i:Integer;
begin
  Edit2.Text:=IntToStr(TrackBar1.Position-10000);
  For i:=0 to DDSD1.ChannelCount-1 do
    DDSD1[i].Pan:=TrackBar1.Position-10000;
end;

//ボリューム
procedure TForm1.TrackBar2Change(Sender: TObject);
var
  i:Integer;
begin
  Edit3.Text:=IntToStr(TrackBar2.Position-10000);
  For i:=0 to DDSD1.ChannelCount-1 do
    DDSD1[i].Volume:=TrackBar2.Position-10000;
end;

//周波数の比率
procedure TForm1.TrackBar3Change(Sender: TObject);
var
  rate:Double;
  i:Integer;
begin
  if DDSD1[0].SrcFreq=0 then
    Exit;
  rate:=TrackBar3.Position;
  rate:=rate / DDSD1[0].SrcFreq;
  Edit4.Text:=FloatToStr(rate);
  For i:=0 to DDSD1.ChannelCount-1 do
    DDSD1[i].FreqRate:=rate;
end;

//演奏状態
procedure TForm1.Timer1Timer(Sender: TObject);
var
  status:DWORD;
begin
  status:=DDSD1[0].Status;

  label10.Visible:= (status And DSBSTATUS_PLAYING) <> 0;
  label11.Visible:= (status And DSBSTATUS_LOOPING) <> 0;

end;

//空いているチャンネルを探して再生
procedure TForm1.Button5Click(Sender: TObject);
var
  i:Integer;
begin
  for i:=0 to DDSD1.ChannelCount-1 do begin
    if DDSD1[i].Status = 0 then begin
      DDSD1[i].Play;
      Exit;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if not DDSD1.Initialized then
    MessageDlg('DirectSoundの初期化に失敗しました。'+#13+#10+'Waveデバイスが占有されていたり、サウンドカードが無かったりしませんか？',mtWarning,[mbOK],0);
end;

procedure TForm1.KR1Click(Sender: TObject);
var
  i:Integer;
begin
  with OpenDialog1 do begin
    if Execute then begin
      //WAVEファイルの読み込み
      if Assigned(Wav) then
        Wav.Free;
      Wav:=TDDSDWaveData.Create(DDSD1,FileName);
      //チャンネルに対し、読み込んだWaveDataを割り当てる
      For i:=0 to DDSD1.ChannelCount-1 do
        DDSD1[i].WaveData:=Wav;
    end;
  end;
end;

procedure TForm1.KDirectSoundI1Click(Sender: TObject);
begin
  DDSD1.ForceInitialize();
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.
