unit IDX_sound;
///////////////////////////////////////
//   Unit : IDX_Sound
//   Date : 02/04/2005
// Author : Amosow 'Gambit_OZ' Evgeniy
// e-Mail : greenrat@mail333.com
///////////////////////////////////////

interface
  uses Fmod,Fmodtypes,Fmoderrors,SysUtils,Log;
var
playingmusic:boolean=false;
playingsound:boolean=true;
Music, Effect: PFMusicModule;
EffectStream,MusicStream : PFSoundStream;
//GlobalPath:string;
procedure PlayEffect(FileName : string);
procedure PlayMusic(FileName : string);
procedure InitSoundEngine;
procedure StopMusic;
procedure StopEffect;
procedure FreeSoundEngine;

implementation

procedure InitSoundEngine;
begin
if not FSOUND_Init(44100, 32, 0) then
begin
FSOUND_Close();
addtolog('Ошибка инициализации звука!',true,true);
playingmusic:=false;
playingsound:=false;
exit;
end;
FSOUND_SetSFXMasterVolume(200);
end;

procedure LoopMusic;
begin
if playingmusic then
begin
try
if (FMUSIC_IsFinished(Music)) and (not FMUSIC_IsPlaying(Music)) and (music<>nil) then FMUSIC_PlaySong(Music);

except
AddToLog('Ошибка FMod',true,true);
end;
end;
end;

procedure PauseMusic;
begin
if (FMUSIC_IsPlaying(Music)) and (playingmusic) then
FMUSIC_SetPaused(Music, true) else
FMUSIC_SetPaused(Music, false);
end;

procedure FreeSoundEngine;
begin
  FMUSIC_StopSong(Effect);
  FMUSIC_FreeSong(Effect);
  Effect:= nil;

  FMUSIC_StopSong(Music);
  FMUSIC_FreeSong(Music);
  Music:= nil;
  FSOUND_Close;

end;

procedure StopMusic;
begin
try
FMUSIC_StopSong(Music);

FMUSIC_FreeSong(Music);
FSOUND_Stream_Stop(MusicStream);
musicstream:=nil;
music:=nil;
except
AddToLog('Ошибка FMod',true,true);
end;
end;

procedure StopEffect;
begin
if (playingsound) then
begin
try
  if FMUSIC_IsPlaying(Effect) then
  begin
  FMUSIC_StopSong(Effect);
  FMUSIC_FreeSong(Effect);
  end else
  FSOUND_Stream_Stop(EffectStream);
except
AddToLog('Ошибка FMod',true,true);
end;
end;
end;

procedure PlayMusic(FileName : string);
var
FilePath : string;
Chanel:integer;
begin
try
FilePath := 'data\music\'+FileName;
if fileexists(FilePath)  then
begin
if playingmusic then
begin
Music := FMUSIC_LoadSong(pchar(FilePath));
if  Music <> nil then
begin
FMUSIC_PlaySong(Music);
FMUSIC_SetMasterVolume(Music,100);
end
 else
begin
MusicStream := FSOUND_Stream_OpenFile(PChar(FilePath), FSOUND_LOOP_NORMAL , 0);
Chanel:=FSOUND_Stream_Play(FSOUND_FREE, MusicStream);
end;
end;
end else
begin
addtolog('Ошибка FMod: Файл ["data\music\'+FilePath+'"] не найден!',true,true);
end;
except
addtolog('Ошибка FMod',true,true);
end;
end;

procedure PlayEffect(FileName : string);
var
FilePath : string;
begin
try
if Effect<>nil then
begin
  FMUSIC_StopSong(Effect);
  FMUSIC_FreeSong(Effect);
  Effect:= nil;
end;

FilePath := 'data\sound\' + FileName;
if fileexists(FilePath)  then
begin
if playingsound then
begin
Effect := FMUSIC_LoadSong(pchar(FilePath));
if Effect = nil then
begin
   EffectStream := FSOUND_Stream_OpenFile(PChar(FilePath), FSOUND_NORMAL , 0);
   FSOUND_Stream_Play(FSOUND_FREE, EffectStream);
end
else
FMUSIC_PlaySong(Effect);
end;
end else
begin
addtolog('Ошибка FMod: File not found ["'+FilePath+'"]',true,true);
end;
except
addtolog('Ошибка FMod',true,true);
end;

end;


end.
