DEADSOFTWARE

simplify TSoundLoader interface
[d2df-sdl.git] / src / engine / e_soundfile_fluid.pas
index da79055db8e9d30ffabfc3e5eb9fcfbb74be978c..fba2726d408d211e3fd32b453cf70dce4ab62513 100644 (file)
@@ -2,8 +2,7 @@
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation, version 3 of the License ONLY.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -25,11 +24,11 @@ type
 
   TFluidLoader = class (TSoundLoader)
   public
-    function Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean; override; overload;
-    function Load(FName: string; SStreaming: Boolean): Boolean; override; overload;
-    function SetPosition(Pos: LongWord): Boolean; override;
+    function Load(Data: Pointer; Len: LongWord; Loop: Boolean): Boolean; override; overload;
+    function Load(FName: string; Loop: Boolean): Boolean; override; overload;
+    function Finished(): Boolean; override;
+    function Restart(): Boolean; override;
     function FillBuffer(Buf: Pointer; Len: LongWord): LongWord; override;
-    function GetAll(var OutPtr: Pointer): LongWord; override;
     procedure Free(); override;
 
   private
@@ -47,9 +46,6 @@ type
 const
   DEFAULT_SOUNDFONT = 'data/soundfont.sf2';
 
-var
-  e_Soundfont: string = '';
-
 implementation
 
 uses sysutils, utils, e_sound, e_log, ctypes{$IFDEF WINDOWS}, windirs{$ENDIF};
@@ -112,13 +108,13 @@ end;
 
 function TFluidLoaderFactory.GetLoader(): TSoundLoader;
 begin
-  if e_Soundfont = '' then e_Soundfont := FindDefaultSoundfont();
+  if e_SoundFont = '' then e_SoundFont := FindDefaultSoundfont();
   Result := TFluidLoader.Create();
 end;
 
 (* TFluidLoader *)
 
-function TFluidLoader.Load(Data: Pointer; Len: LongWord; SStreaming: Boolean): Boolean;
+function TFluidLoader.Load(Data: Pointer; Len: LongWord; Loop: Boolean): Boolean;
 var
   Ret: cint;
 begin
@@ -128,7 +124,7 @@ begin
     FSynth := new_fluid_synth(FluidSettings);
     if FSynth = nil then
       raise Exception.Create('new_fluid_synth failed');
-    Ret := fluid_synth_sfload(FSynth, PChar(e_Soundfont), 1);
+    Ret := fluid_synth_sfload(FSynth, PChar(e_SoundFont), 1);
     if Ret = FLUID_FAILED then
       raise Exception.Create('fluid_synth_sfload failed');
     FPlayer := new_fluid_player(FSynth);
@@ -150,8 +146,9 @@ begin
     end;
   end;
 
-  if FLooping then
+  if Loop then
     fluid_player_set_loop(FPlayer, -1);
+
   FFormat.SampleRate := 44100;
   FFormat.SampleBits := 16;
   FFormat.Channels := 2;
@@ -160,7 +157,7 @@ begin
   Result := True;
 end;
 
-function TFluidLoader.Load(FName: string; SStreaming: Boolean): Boolean;
+function TFluidLoader.Load(FName: string; Loop: Boolean): Boolean;
 var
   Ret: cint;
 begin
@@ -170,7 +167,7 @@ begin
     FSynth := new_fluid_synth(FluidSettings);
     if FSynth = nil then
       raise Exception.Create('new_fluid_synth failed');
-    Ret := fluid_synth_sfload(FSynth, PChar(e_Soundfont), 1);
+    Ret := fluid_synth_sfload(FSynth, PChar(e_SoundFont), 1);
     if Ret = FLUID_FAILED then
       raise Exception.Create('fluid_synth_sfload failed');
     FPlayer := new_fluid_player(FSynth);
@@ -192,8 +189,9 @@ begin
     end;
   end;
 
-  if FLooping then
+  if Loop then
     fluid_player_set_loop(FPlayer, -1);
+
   FFormat.SampleRate := 44100;
   FFormat.SampleBits := 16;
   FFormat.Channels := 2;
@@ -202,9 +200,22 @@ begin
   Result := True;
 end;
 
-function TFluidLoader.SetPosition(Pos: LongWord): Boolean;
+function TFluidLoader.Finished(): Boolean;
 begin
-  Result := False; // unsupported?
+  Result := fluid_player_get_status(FPlayer) = FLUID_PLAYER_DONE;
+end;
+
+function TFluidLoader.Restart(): Boolean;
+begin
+  Result := False;
+  // fluid_player_seek() is only supported in full 2.x.x, and I ain't compiling that shit
+  // if (FSynth <> nil) and (FPlayer <> nil) then
+  // begin
+  //   fluid_synth_system_reset(FSynth);
+  //   fluid_player_seek(FPlayer, 0);
+  //   fluid_player_play(FPlayer);
+  //   Result := True;
+  // end;
 end;
 
 function TFluidLoader.FillBuffer(Buf: Pointer; Len: LongWord): LongWord;
@@ -217,14 +228,13 @@ begin
   if Ret = FLUID_OK then Result := Len;
 end;
 
-function TFluidLoader.GetAll(var OutPtr: Pointer): LongWord;
-begin
-  Result := 0; // midis are always streaming, so this don't make sense
-end;
-
 procedure TFluidLoader.Free();
 begin
-  if FPlayer <> nil then delete_fluid_player(FPlayer);
+  if FPlayer <> nil then 
+  begin
+    fluid_player_stop(FPlayer);
+    delete_fluid_player(FPlayer);
+  end;
   if FSynth <> nil then delete_fluid_synth(FSynth);
   FPlayer := nil;
   FSynth := nil;
@@ -235,9 +245,11 @@ initialization
   if FluidSettings <> nil then
   begin
     fluid_settings_setint(FluidSettings, PChar('synth.midi-channels'), 16);
-    fluid_settings_setint(FluidSettings, PChar('synth.cpu-cores'), 0);
+    fluid_settings_setint(FluidSettings, PChar('synth.cpu-cores'), 1);
     fluid_settings_setnum(FluidSettings, PChar('synth.sample-rate'), 44100);
     fluid_settings_setnum(FluidSettings, PChar('synth.gain'), 1);
+    fluid_settings_setint(FluidSettings, PChar('synth.reverb.active'), 0);
+    fluid_settings_setint(FluidSettings, PChar('synth.chorus.active'), 0);
     fluid_settings_setstr(FluidSettings, PChar('player.timing-source'), PChar('sample'));
     e_AddSoundLoader(TFluidLoaderFactory.Create());
   end;