hedgewars/uSound.pas
changeset 2443 fececcbc2189
parent 2418 538a777f90c4
child 2494 1e10a47cabea
equal deleted inserted replaced
2442:228757f6c54d 2443:fececcbc2189
    56 
    56 
    57 function ChangeVolume(voldelta: LongInt): LongInt;
    57 function ChangeVolume(voldelta: LongInt): LongInt;
    58 function AskForVoicepack(name: shortstring): PVoicepack;
    58 function AskForVoicepack(name: shortstring): PVoicepack;
    59 function soundFadeOut(snd: TSound; qt: LongInt; voicepack: PVoicepack): LongInt;
    59 function soundFadeOut(snd: TSound; qt: LongInt; voicepack: PVoicepack): LongInt;
    60 
    60 
    61 {*remember: LongInt = 32bit; integer = 16bit; byte = 8bit*}
    61 function oalb_init(const app: PChar; const usehardware: Byte): Byte; cdecl; external OpenALBridge;
    62 function openal_init		(filename: PChar; hardware: boolean; memsize: LongInt)	: boolean; cdecl; external OpenALBridge;
    62 procedure oalb_close; cdecl; external OpenALBridge;
    63 function openal_close							: boolean; cdecl; external OpenALBridge;
    63 function oalb_loadfile(const filename: PChar): LongInt; cdecl; external OpenALBridge;
    64 function openal_loadfile	(const filename: PChar)			: LongInt; cdecl; external OpenALBridge;
    64 procedure oalb_playsound(const idx: LongInt; const loop: Byte); cdecl; external OpenALBridge;
    65 function openal_toggleloop	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
    65 procedure oalb_stopsound(const idx: LongInt); cdecl; external OpenALBridge;
    66 function openal_setvolume	(index: LongInt; percentage: byte)	: boolean; cdecl; external OpenALBridge;
    66 procedure oalb_pausesound(const idx: LongInt); cdecl; external OpenALBridge;
    67 function openal_setglobalvolume	(percentage: byte)			: boolean; cdecl; external OpenALBridge;
    67 procedure oalb_continuesound(const idx: LongInt); cdecl; external OpenALBridge;
    68 function openal_fadeout		(index: LongInt; quantity: SmallInt)	: boolean; cdecl; external OpenALBridge;
    68 procedure oalb_setvolume(const idx: LongInt; const percentage: Byte); cdecl; external OpenALBridge;
    69 function openal_fadein		(index: LongInt; quantity: SmallInt)	: boolean; cdecl; external OpenALBridge;
    69 procedure oalb_setglobalvolume(const percentage: Byte); cdecl; external OpenALBridge;
    70 function openal_fade		(index: LongInt; quantity: SmallInt;
    70 
    71 				 direction: boolean)			: boolean; cdecl; external OpenALBridge;
       
    72 function openal_playsound	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
       
    73 function openal_pausesound	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
       
    74 function openal_stopsound	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
       
    75 
    71 
    76 var MusicFN: shortstring = '';
    72 var MusicFN: shortstring = '';
    77 
    73 
    78 implementation
    74 implementation
    79 
    75 
    99 voicepacks[i].name:= name;
    95 voicepacks[i].name:= name;
   100 AskForVoicepack:= @voicepacks[i]
    96 AskForVoicepack:= @voicepacks[i]
   101 end;
    97 end;
   102 
    98 
   103 procedure InitSound;
    99 procedure InitSound;
   104 const numSounds = 80;
       
   105 begin
   100 begin
   106 if not isSoundEnabled then exit;
   101 if not isSoundEnabled then exit;
   107 {*sound works in ipodtouch only if LAND_WIDTH  = 1024;   LAND_HEIGHT = 512; 
   102 {*sound works in ipodtouch only if LAND_WIDTH  = 1024;   LAND_HEIGHT = 512; 
   108 or if ogg are loaded in stream or if sound is loaded by demand*}
   103 or if ogg are loaded in stream or if sound is loaded by demand*}
   109 WriteToConsole('Init OpenAL sound...');
   104 WriteToConsole('Init OpenAL sound...');
   110 
   105 
   111 isSoundEnabled:= openal_init(str2pchar(ParamStr(0)), isSoundHardware, numSounds);
   106 isSoundEnabled:= oalb_init('hwengine', Byte(isSoundHardware)) = 1;
   112 if isSoundEnabled then WriteLnToConsole(msgOK)
   107 if isSoundEnabled then WriteLnToConsole(msgOK)
   113                   else WriteLnToConsole(msgFailed);
   108                   else WriteLnToConsole(msgFailed);
   114 
   109 
   115 Volume:=0;
   110 Volume:=0;
   116 ChangeVolume(cInitVolume);
   111 ChangeVolume(cInitVolume);
   117 end;
   112 end;
   118 
   113 
   119 procedure ReleaseSound;
   114 procedure ReleaseSound;
   120 begin
   115 begin
   121 if isMusicEnabled then openal_fadeout(Mus, 30);
   116 //if isMusicEnabled then openal_fadeout(Mus, 30);
   122 openal_close();
   117 oalb_close();
   123 end;
   118 end;
   124 
   119 
   125 procedure SoundLoad;
   120 procedure SoundLoad;
   126 var i: TSound;
   121 var i: TSound;
   127 	s: shortstring;
   122 	s: shortstring;
   134 for i:= Low(TSound) to High(TSound) do
   129 for i:= Low(TSound) to High(TSound) do
   135 	if Soundz[i].Path <> ptVoices then
   130 	if Soundz[i].Path <> ptVoices then
   136 		begin
   131 		begin
   137 		s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   132 		s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   138 		WriteToConsole(msgLoading + s + ' ');
   133 		WriteToConsole(msgLoading + s + ' ');
   139 		defVoicepack^.chunks[i]:= openal_loadfile (Str2PChar(s));
   134 		defVoicepack^.chunks[i]:= oalb_loadfile(Str2PChar(s));
   140 		TryDo(defVoicepack^.chunks[i] >= 0, msgFailed, true);
   135 		TryDo(defVoicepack^.chunks[i] >= 0, msgFailed, true);
   141 		WriteLnToConsole(msgOK);
   136 		WriteLnToConsole(msgOK);
   142 		end;
   137 		end;
   143 
   138 
   144 for t:= 0 to cMaxTeams do
   139 for t:= 0 to cMaxTeams do
   146 		for i:= Low(TSound) to High(TSound) do
   141 		for i:= Low(TSound) to High(TSound) do
   147 			if Soundz[i].Path = ptVoices then
   142 			if Soundz[i].Path = ptVoices then
   148 				begin
   143 				begin
   149 				s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName;
   144 				s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName;
   150 				WriteToConsole(msgLoading + s + ' ');
   145 				WriteToConsole(msgLoading + s + ' ');
   151 				voicepacks[t].chunks[i]:= openal_loadfile (Str2PChar(s));
   146 				voicepacks[t].chunks[i]:= oalb_loadfile(Str2PChar(s));
   152 				if voicepacks[t].chunks[i] < 0 then
   147 				if voicepacks[t].chunks[i] < 0 then
   153 					WriteLnToConsole(msgFailed)
   148 					WriteLnToConsole(msgFailed)
   154 				else
   149 				else
   155 					WriteLnToConsole(msgOK)
   150 					WriteLnToConsole(msgOK)
   156 				end;
   151 				end;
   157 end;
   152 end;
   158 
   153 
   159 function soundFadeOut(snd: TSound; qt: LongInt; voicepack: PVoicepack): LongInt;
   154 function soundFadeOut(snd: TSound; qt: LongInt; voicepack: PVoicepack): LongInt;
   160 begin
   155 begin
   161 if not isSoundEnabled then exit(0);
   156 if not isSoundEnabled then exit(0);
   162 if (voicepack <> nil) and (voicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt)
   157 //if (voicepack <> nil) and (voicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt)
   163 else if (defVoicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt);
   158 //else if (defVoicepack^.chunks[snd] >= 0) then openal_fadeout(defVoicepack^.chunks[snd], qt);
   164 end;
   159 end;
   165 
   160 
   166 procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack);
   161 procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack);
   167 begin
   162 begin
   168 if (not isSoundEnabled) or fastUntilLag then exit;
   163 if (not isSoundEnabled) or fastUntilLag then exit;
   169 
   164 
   170 if (voicepack <> nil) then
   165 if voicepack = nil then voicepack:= defVoicepack;
   171 begin
   166 
   172 if voicepack^.chunks[snd] >= 0 then
   167 if voicepack^.chunks[snd] >= 0 then
   173 	begin
   168 	begin
   174 	if infinite then openal_toggleloop(voicepack^.chunks[snd]);
   169 	oalb_playsound(voicepack^.chunks[snd], Byte(infinite));
   175 	openal_playsound(voicepack^.chunks[snd]);
       
   176 	lastChan[snd]:=voicepack^.chunks[snd];
   170 	lastChan[snd]:=voicepack^.chunks[snd];
   177 	end
   171 	end
   178 end
       
   179 else
       
   180 begin
       
   181 if (defVoicepack^.chunks[snd] >= 0) then
       
   182 	begin
       
   183 	if infinite then openal_toggleloop(defVoicepack^.chunks[snd]);
       
   184 	openal_playsound(defVoicepack^.chunks[snd]);
       
   185 	lastChan[snd]:=defVoicepack^.chunks[snd];
       
   186 	end
       
   187 end
       
   188 end;
   172 end;
   189 
   173 
   190 procedure StopSound(snd: TSound);
   174 procedure StopSound(snd: TSound);
   191 begin
   175 begin
   192 if isSoundEnabled then
   176 if isSoundEnabled then
   193 	openal_stopsound(lastChan[snd])
   177 	oalb_stopsound(lastChan[snd])
   194 end;
   178 end;
   195 
   179 
   196 procedure PlayMusic;
   180 procedure PlayMusic;
   197 var s: string;
   181 var s: string;
   198 begin
   182 begin
   201 	or (not isMusicEnabled) then exit;
   185 	or (not isMusicEnabled) then exit;
   202 
   186 
   203 s:= PathPrefix + '/Music/' + MusicFN;
   187 s:= PathPrefix + '/Music/' + MusicFN;
   204 WriteToConsole(msgLoading + s + ' ');
   188 WriteToConsole(msgLoading + s + ' ');
   205 
   189 
   206 Mus:= openal_loadfile(Str2PChar(s));
   190 Mus:= oalb_loadfile(Str2PChar(s));
   207 TryDo(Mus >= 0, msgFailed, false);
   191 TryDo(Mus >= 0, msgFailed, false);
   208 WriteLnToConsole(msgOK);
   192 WriteLnToConsole(msgOK);
   209 
   193 
   210 openal_fadein(Mus, 20);
   194 //openal_fadein(Mus, 20);
   211 openal_toggleloop(Mus);
   195 //openal_toggleloop(Mus);
       
   196 oalb_playsound(Mus, 1);
   212 end;
   197 end;
   213 
   198 
   214 function ChangeVolume(voldelta: LongInt): LongInt;
   199 function ChangeVolume(voldelta: LongInt): LongInt;
   215 begin
   200 begin
   216 if not isSoundEnabled then exit(0);
   201 if not isSoundEnabled then exit(0);
   217 
   202 
   218 inc(Volume, voldelta);
   203 inc(Volume, voldelta);
   219 if Volume < 0 then Volume:= 0;
   204 if Volume < 0 then Volume:= 0;
   220 if Volume > 100 then Volume:= 100;
   205 if Volume > 100 then Volume:= 100;
   221 
   206 
   222 openal_setglobalvolume(Volume);
   207 oalb_setglobalvolume(Volume);
   223 if isMusicEnabled then openal_setvolume(Mus, Volume shr 1);
   208 if isMusicEnabled then oalb_setvolume(Mus, Volume shr 1);
   224 ChangeVolume:= Volume;
   209 ChangeVolume:= Volume;
   225 end;
   210 end;
   226 
   211 
   227 procedure PauseMusic;
   212 procedure PauseMusic;
   228 begin
   213 begin
   229 if (MusicFN = '') or (not isMusicEnabled) then exit;
   214 if (MusicFN = '') or (not isMusicEnabled) then exit;
   230 openal_pausesound(Mus);
   215 oalb_stopsound(Mus)
   231 end;
   216 end;
   232 
   217 
   233 procedure ResumeMusic;
   218 procedure ResumeMusic;
   234 begin
   219 begin
   235 if (MusicFN = '') or (not isMusicEnabled) then exit;
   220 if (MusicFN = '') or (not isMusicEnabled) then exit;
   236 openal_playsound(Mus);
   221 oalb_playsound(Mus, 0)
   237 end;
   222 end;
   238 
   223 
   239 
   224 
   240 var i: LongInt;
   225 var i: LongInt;
   241 	c: TSound;
   226 	c: TSound;