unit Sound;

interface

Uses
  SysUtils,
  System.IO,
  Objet,
  GamePlay,
  Microsoft.DirectX.DirectSound,
  Microsoft.DirectX.AudioVideoPlayback,
  System.Windows.Forms,
  System.Drawing;

type TSound = class(System.Windows.Forms.Form)
  public
    Fallbool : boolean;
    FDevice : Device;
    FSecondaryBuffer : array of SecondaryBuffer;
    FNumberSound : Integer;
    FBufferDescription : BufferDescription;
    FSound : String;
    constructor Create;
    procedure AddSound(FSound : string);
    procedure PlayNormal(ID : integer);
    procedure PlayLoop(ID : integer);
    procedure Stop(ID : integer);
    procedure Volume(FVolume, ID : integer);
    procedure Balance(FBalance, ID : integer);
    procedure StopAll;
    procedure PlayAllSound(var pos : Tpos; var touches : TTouches);
    procedure Reinit(var touches : TTouches);
  private
    procedure CreateDevice;
    procedure SetCooperativeLevel;
    procedure CreateBufferDescription;
    procedure CreateSound(FSound : string);
end;

implementation
//////////////////////////////////////////////////////////////////////////////
constructor TSound.Create;
begin
  inherited Create;
  // Nombre de morceaux
  FNumberSound := 0;

  // Set up DirectSound
  CreateDevice;

  // Set the cooperative level
  SetCooperativeLevel;
end;
//////////////////////////////////////////////////////////////////////////////
procedure TSound.AddSound(FSound : string);
begin
  // Buffer and its descriptor
  CreateBufferDescription;

  // Load the sound
  CreateSound(FSound);
end;
///////////////////////////////////////////////////////////////////////////////
procedure TSound.CreateDevice;
begin
  FDevice := Device.Create();
end;
///////////////////////////////////////////////////////////////////////////////
procedure TSound.SetCooperativeLevel;
begin
  FDevice.SetCooperativeLevel(Self, CooperativeLevel.Priority);
end;
////////////////////////////////////////////////////////////////////////////////
procedure TSound.CreateBufferDescription;
begin
  FBufferDescription := BufferDescription.Create;

  // Permet de controler la balance (droite/gauche du son).
  FBufferDescription.ControlPan := true;
  // Permet de controler le volume.
  FBufferDescription.ControlVolume := true;
  // Permet de controler la frequence du bruit.
  FBufferDescription.ControlFrequency := true;
  // Permet d'appliquer des effets au son.
  FBufferDescription.ControlEffects := true;
end;
///////////////////////////////////////////////////////////////////////////////
procedure TSound.CreateSound(FSound : string);
begin
  FNumberSound := FNumberSound + 1;
  SetLength(FSecondaryBuffer, FNumberSound);
  // Cree le buffer.
  FSecondaryBuffer[FNumberSound - 1] := SecondaryBuffer.Create(FSound, FBufferDescription, Fdevice);
end;
////////////////////////////////////////////////////////////////////////////////
procedure TSound.PlayNormal(ID : integer);
var
  FFlags : BufferPlayFlags;
begin
  // Lit le son normalement.
  FFlags := BufferPlayFlags.Default;

  // Le 0 se refere à la prioritée du son.
  FSecondaryBuffer[ID].Play(0, FFlags);
end;
///////////////////////////////////////////////////////////////////////////////
procedure TSound.PlayLoop(ID : integer);
var
  FFlags : BufferPlayFlags;
begin
  // Loop le son.
  FFlags := BufferPlayFlags.Looping;

  // Le 0 se refere à la prioritée du son.
  FSecondaryBuffer[ID].Play(0, FFlags);
end;
//////////////////////////////////////////////////////////////////////////////
procedure TSound.Stop(ID : integer);
begin
  if (FSecondaryBuffer <> nil) then
    begin
      FSecondaryBuffer[ID].Stop;
    end;
end;
///////////////////////////////////////////////////////////////////////////////
procedure TSound.Volume(FVolume, ID : integer);
begin
  if (FSecondaryBuffer <> nil) then
    begin
      FSecondaryBuffer[ID].Volume := FVolume;
    end;
end;
///////////////////////////////////////////////////////////////////////////////
procedure TSound.Balance(FBalance, ID : integer);
begin
  if (FSecondaryBuffer <> nil) then
    begin
      FSecondaryBuffer[ID].Pan := FBalance;
    end;
end;
//////////////////////////////////////////////////////////////////////////////
procedure TSound.StopAll;
var
  ID : integer;
begin
  for ID := 0 to FNumberSound - 1 do
    begin
      FSecondaryBuffer[ID].Stop;
    end;
end;
//////////////////////////////////////////////////////////////////////////////
procedure TSound.PlayAllSound(var pos : Tpos; var touches : TTouches);
var
  r : integer;
begin
  reinit(touches);
  // Chute
 if (pos.y<0) and FallBool then
    begin
      FallBool := false;
      r := Random(3)+1;
      case r of
        1 : Playnormal(1);
        2 : Playnormal(2);
        3 : Playnormal(3);
      end;
    end;

 // Saut
  if pos.jump then
    begin
      Playnormal(3);
      pos.jump := false;
    end;
end;
/////////////////////////////////////////////////////////////////////////////
procedure TSound.reinit (var touches : TTouches);
begin
  if touches.R then
  begin
    Fallbool := true;
    stop(1);
    stop(2);
    stop(3);
  end;
end;

end.


