hedgewars/uSound.pas
branchwebgl
changeset 7977 4987f6f83cdf
parent 7896 67217e6108fd
child 8018 091293bc974f
equal deleted inserted replaced
7975:1a8308265fdd 7977:4987f6f83cdf
   168 end;
   168 end;
   169 
   169 
   170 procedure InitSound;
   170 procedure InitSound;
   171 const channels: LongInt = {$IFDEF MOBILE}1{$ELSE}2{$ENDIF};
   171 const channels: LongInt = {$IFDEF MOBILE}1{$ELSE}2{$ENDIF};
   172 begin
   172 begin
   173     if not isSoundEnabled then
   173     if (not isSoundEnabled) then
   174         exit;
   174         exit;
   175     WriteToConsole('Init sound...');
   175     WriteToConsole('Init sound...');
   176     isSoundEnabled:= SDL_InitSubSystem(SDL_INIT_AUDIO) >= 0;
   176     isSoundEnabled:= SDL_InitSubSystem(SDL_INIT_AUDIO) >= 0;
   177 
   177 
   178     if isSoundEnabled then
   178     if isSoundEnabled then
   266     if (voicepack <> nil) then
   266     if (voicepack <> nil) then
   267         begin
   267         begin
   268         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
   268         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
   269             begin
   269             begin
   270             s:= UserPathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   270             s:= UserPathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   271             if not FileExists(s) then
   271             if (not FileExists(s)) then
   272                 s:= Pathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   272                 s:= Pathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   273             WriteToConsole(msgLoading + s + ' ');
   273             WriteToConsole(msgLoading + s + ' ');
   274             voicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   274             voicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   275             if voicepack^.chunks[snd] = nil then
   275             if voicepack^.chunks[snd] = nil then
   276                 WriteLnToConsole(msgFailed)
   276                 WriteLnToConsole(msgFailed)
   282     else
   282     else
   283         begin
   283         begin
   284         if (defVoicepack^.chunks[snd] = nil) and (Soundz[snd].Path <> ptVoices) and (Soundz[snd].FileName <> '') then
   284         if (defVoicepack^.chunks[snd] = nil) and (Soundz[snd].Path <> ptVoices) and (Soundz[snd].FileName <> '') then
   285             begin
   285             begin
   286             s:= UserPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   286             s:= UserPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   287             if not FileExists(s) then
   287             if (not FileExists(s)) then
   288                 s:= Pathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   288                 s:= Pathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   289             WriteToConsole(msgLoading + s + ' ');
   289             WriteToConsole(msgLoading + s + ' ');
   290             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   290             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   291             SDLTry(defVoicepack^.chunks[snd] <> nil, true);
   291             SDLTry(defVoicepack^.chunks[snd] <> nil, true);
   292             WriteLnToConsole(msgOK);
   292             WriteLnToConsole(msgOK);
   366     if (voicepack <> nil) then
   366     if (voicepack <> nil) then
   367         begin
   367         begin
   368         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
   368         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
   369            begin
   369            begin
   370             s:= UserPathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   370             s:= UserPathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   371             if not FileExists(s) then
   371             if (not FileExists(s)) then
   372                 s:= Pathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   372                 s:= Pathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
   373             WriteToConsole(msgLoading + s + ' ');
   373             WriteToConsole(msgLoading + s + ' ');
   374             voicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   374             voicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   375             if voicepack^.chunks[snd] = nil then
   375             if voicepack^.chunks[snd] = nil then
   376                 WriteLnToConsole(msgFailed)
   376                 WriteLnToConsole(msgFailed)
   382     else
   382     else
   383         begin
   383         begin
   384         if (defVoicepack^.chunks[snd] = nil) and (Soundz[snd].Path <> ptVoices) and (Soundz[snd].FileName <> '') then
   384         if (defVoicepack^.chunks[snd] = nil) and (Soundz[snd].Path <> ptVoices) and (Soundz[snd].FileName <> '') then
   385             begin
   385             begin
   386             s:= UserPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   386             s:= UserPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   387             if not FileExists(s) then
   387             if (not FileExists(s)) then
   388                 s:= Pathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   388                 s:= Pathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
   389             WriteToConsole(msgLoading + s + ' ');
   389             WriteToConsole(msgLoading + s + ' ');
   390             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   390             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), _P'rb'), 1);
   391             SDLTry(defVoicepack^.chunks[snd] <> nil, true);
   391             SDLTry(defVoicepack^.chunks[snd] <> nil, true);
   392             WriteLnToConsole(msgOK);
   392             WriteLnToConsole(msgOK);
   398         end;
   398         end;
   399 end;
   399 end;
   400 
   400 
   401 procedure StopSound(snd: TSound);
   401 procedure StopSound(snd: TSound);
   402 begin
   402 begin
   403     if not isSoundEnabled then
   403     if (not isSoundEnabled) then
   404         exit;
   404         exit;
   405 
   405 
   406     if (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   406     if (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   407         begin
   407         begin
   408         Mix_HaltChannel(lastChan[snd]);
   408         Mix_HaltChannel(lastChan[snd]);
   410         end;
   410         end;
   411 end;
   411 end;
   412 
   412 
   413 procedure StopSoundChan(chn: LongInt);
   413 procedure StopSoundChan(chn: LongInt);
   414 begin
   414 begin
   415     if not isSoundEnabled then
   415     if (not isSoundEnabled) then
   416         exit;
   416         exit;
   417 
   417 
   418     if (chn <> -1) and (Mix_Playing(chn) <> 0) then
   418     if (chn <> -1) and (Mix_Playing(chn) <> 0) then
   419         Mix_HaltChannel(chn);
   419         Mix_HaltChannel(chn);
   420 end;
   420 end;
   421 
   421 
   422 procedure StopSoundChan(chn, fadems: LongInt);
   422 procedure StopSoundChan(chn, fadems: LongInt);
   423 begin
   423 begin
   424     if not isSoundEnabled then
   424     if (not isSoundEnabled) then
   425         exit;
   425         exit;
   426 
   426 
   427     if (chn <> -1) and (Mix_Playing(chn) <> 0) then
   427     if (chn <> -1) and (Mix_Playing(chn) <> 0) then
   428         Mix_FadeOutChannel(chn, fadems);
   428         Mix_FadeOutChannel(chn, fadems);
   429 end;
   429 end;
   433 begin
   433 begin
   434     if (not isSoundEnabled) or (MusicFN = '') or (not isMusicEnabled) then
   434     if (not isSoundEnabled) or (MusicFN = '') or (not isMusicEnabled) then
   435         exit;
   435         exit;
   436 
   436 
   437     s:= UserPathPrefix + '/Data/Music/' + MusicFN;
   437     s:= UserPathPrefix + '/Data/Music/' + MusicFN;
   438     if not FileExists(s) then
   438     if (not FileExists(s)) then
   439         s:= PathPrefix + '/Music/' + MusicFN;
   439         s:= PathPrefix + '/Music/' + MusicFN;
   440     WriteToConsole(msgLoading + s + ' ');
   440     WriteToConsole(msgLoading + s + ' ');
   441 
   441 
   442     Mus:= Mix_LoadMUS(Str2PChar(s));
   442     Mus:= Mix_LoadMUS(Str2PChar(s));
   443     SDLTry(Mus <> nil, false);
   443     SDLTry(Mus <> nil, false);
   452 end;
   452 end;
   453 
   453 
   454 function ChangeVolume(voldelta: LongInt): LongInt;
   454 function ChangeVolume(voldelta: LongInt): LongInt;
   455 begin
   455 begin
   456     ChangeVolume:= 0;
   456     ChangeVolume:= 0;
   457     if (not isSoundEnabled) or ((voldelta = 0) and not (cInitVolume = 0)) then
   457     if (not isSoundEnabled) or ((voldelta = 0) and (not (cInitVolume = 0))) then
   458         exit;
   458         exit;
   459 
   459 
   460     inc(Volume, voldelta);
   460     inc(Volume, voldelta);
   461     if Volume < 0 then
   461     if Volume < 0 then
   462         Volume:= 0;
   462         Volume:= 0;