unit GSound;

interface
uses
  Bass;

procedure InitGS;

procedure PlaySound( I : Byte);
procedure StopSound( I : Byte);
procedure TrackPlay( I : Integer);
procedure TrackStop;

procedure DJ;
function Sd_Level : Single;

const
  MofTrack = 1;
  NTrackMusicBox = 1;
var
  SoundEn : Boolean = true;
  MuzEn : Boolean = true;
implementation
const
  MofSound = 10;

const
  NSound : array[1..MofSound] of string =
  ('click.ogg', 'gun1.ogg', 'gun2.ogg', 'gun3.ogg', 'gun4.ogg', 'regun1.ogg', 'regun2.ogg', 'regun3.ogg', 'regun4.ogg', 'oh.ogg');

  NTrack : array[1..MofTrack] of string =
  ('muz.ogg');



var
  S_Gn   : array[1..MofSound ] of Cardinal;
  S_Intro,
  S_MZT  : array[1..MofTrack ] of Cardinal;
  NowTrack : Integer = -1;
  WSD    : Boolean;
  Sd_LostLevel : Single;
  Sd_Channel : Cardinal;

function Sd_Level : Single;
var
  L : Cardinal;
begin
  if BASS_ChannelIsActive(Sd_Channel) = BASS_ACTIVE_PLAYING then
  begin
    L := Bass_ChannelGetLevel(Sd_Channel);
    Result :=  (L shr 16) / 32768;
  end
  else
    Result := 0;
  Sd_LostLevel := Result;
end;
  
procedure LoadSounds;
var
  I : integer;
begin
  for I := 0 to MofTrack do
    S_MZT[I] := BASS_StreamCreateFile(False, PChar('Data\' + NTrack[I]), 0, 0, BASS_SAMPLE_LOOP);

  for I := 1 to MofSound do
    S_Gn[I] := BASS_StreamCreateFile(False, PChar('Data\' + NSound[I]), 0, 0, 0);

  TrackPlay(1);
end;

procedure DJ;
var
  I : Integer;
begin
 { if  BASS.BASS_ChannelGetPosition(S_MZT[NowTrack],0) >=
      BASS.BASS_ChannelGetLength(S_MZT[NowTrack],0)
  then
  begin
    I := random(MofTrack - NowTrack + 1) + NowTrack + 1;
    if I > MofTrack then
      I := 1;
    TrackPlay(1);
  end; }
end;

procedure TrackPlay(I : Integer);
begin
  if (WSD) then
  if NowTrack <> I then
  begin
    if NowTrack >= 0 then
      BASS_ChannelStop(S_MZT[NowTrack]);
    BASS_ChannelPlay(S_MZT[I], TRUE);
    NowTrack := I;
    Sd_Channel := S_MZT[I];
  end;
end;

procedure TrackStop;
begin
  if (WSD) then
  begin
    if NowTrack >= 0 then
      BASS_ChannelStop(S_MZT[NowTrack]);
    Sd_Channel := 0;
    NowTrack := 0;
  end;
end;

procedure InitGS;
begin
  if BASS_Init(-1, 44100, 0, 0, nil) then
  begin
    WSD := true;
    LoadSounds;
  end
  else
    WSD:=false;
end;

procedure PlaySound;
begin
  if (WSD) and (SoundEn) then
  begin
    BASS_ChannelStop(S_Gn[I]);
    BASS_ChannelPlay(S_Gn[I], False);
  end;
end;

procedure StopSound;
begin
  if (WSD) then
  begin
    BASS_ChannelStop(S_Gn[I]);
  end;
end;

end.
