hedgewars/uSound.pas
changeset 3664 f5bdf26c843e
parent 3615 b78d7959540a
child 3667 9359a70df013
equal deleted inserted replaced
3663:8c28abf427f5 3664:f5bdf26c843e
   128     Mix_CloseAudio();
   128     Mix_CloseAudio();
   129 end;
   129 end;
   130 
   130 
   131 procedure SoundLoad;
   131 procedure SoundLoad;
   132 var i: TSound;
   132 var i: TSound;
   133     s: shortstring;
       
   134     t: Longword;
   133     t: Longword;
   135 begin
   134 begin
   136     if not isSoundEnabled then exit;
   135     if not isSoundEnabled then exit;
   137 
   136 
   138     defVoicepack:= AskForVoicepack('Default');
   137     defVoicepack:= AskForVoicepack('Default');
   139 
   138 
   140     for i:= Low(TSound) to High(TSound) do
   139     for i:= Low(TSound) to High(TSound) do
   141         if (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
   140         defVoicepack^.chunks[i]:= nil;
   142         begin
   141  
   143             s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
       
   144             WriteToConsole(msgLoading + s + ' ');
       
   145             defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   146             TryDo(defVoicepack^.chunks[i] <> nil, msgFailed, true);
       
   147             WriteLnToConsole(msgOK);
       
   148         end;
       
   149 
       
   150     for t:= 0 to cMaxTeams do
   142     for t:= 0 to cMaxTeams do
   151         if voicepacks[t].name <> '' then
   143         if voicepacks[t].name <> '' then
   152             for i:= Low(TSound) to High(TSound) do
   144             for i:= Low(TSound) to High(TSound) do
   153                 if (Soundz[i].Path = ptVoices) and (Soundz[i].FileName <> '') then
   145                     voicepacks[t].chunks[i]:= nil;
   154                 begin
   146 
   155                     s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName;
       
   156                     WriteToConsole(msgLoading + s + ' ');
       
   157                     voicepacks[t].chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   158                     if voicepacks[t].chunks[i] = nil then
       
   159                         WriteLnToConsole(msgFailed)
       
   160                     else
       
   161                         WriteLnToConsole(msgOK)
       
   162                 end;
       
   163 end;
   147 end;
   164 
   148 
   165 procedure PlaySound(snd: TSound);
   149 procedure PlaySound(snd: TSound);
   166 begin
   150 begin
   167     PlaySound(snd, nil, false);
   151     PlaySound(snd, nil, false);
   176 begin
   160 begin
   177     PlaySound(snd, voicepack, false);
   161     PlaySound(snd, voicepack, false);
   178 end;
   162 end;
   179 
   163 
   180 procedure PlaySound(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
   164 procedure PlaySound(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
       
   165 var s:shortstring;
   181 begin
   166 begin
   182     if (not isSoundEnabled) or fastUntilLag then
   167     if (not isSoundEnabled) or fastUntilLag then
   183         exit;
   168         exit;
   184 
   169 
   185     if keepPlaying and (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   170     if keepPlaying and (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   186         exit;
   171         exit;
   187 
   172 
   188     if (voicepack <> nil) and (voicepack^.chunks[snd] <> nil) then
   173     if (voicepack <> nil) then
       
   174     begin
       
   175         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
       
   176         begin
       
   177             s:= Pathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
       
   178             WriteToConsole(msgLoading + s + ' ');
       
   179             voicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   180             if voicepack^.chunks[snd] = nil then
       
   181                 WriteLnToConsole(msgFailed)
       
   182             else
       
   183                 WriteLnToConsole(msgOK)
       
   184         end;
   189         lastChan[snd]:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], 0, -1)
   185         lastChan[snd]:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], 0, -1)
       
   186     end
   190     else
   187     else
       
   188     begin
       
   189         if (defVoicepack^.chunks[snd] = nil) and (Soundz[snd].Path <> ptVoices) and (Soundz[snd].FileName <> '') then
       
   190         begin
       
   191             s:= Pathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
       
   192             WriteToConsole(msgLoading + s + ' ');
       
   193             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   194             TryDo(defVoicepack^.chunks[snd] <> nil, msgFailed, true);
       
   195             WriteLnToConsole(msgOK);
       
   196         end;
   191         lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], 0, -1)
   197         lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], 0, -1)
       
   198     end;
   192 end;
   199 end;
   193 
   200 
   194 function LoopSound(snd: TSound): LongInt;
   201 function LoopSound(snd: TSound): LongInt;
   195 begin
   202 begin
   196     LoopSound:= LoopSound(snd, nil)
   203     LoopSound:= LoopSound(snd, nil)
   197 end;
   204 end;
   198 
   205 
   199 function LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
   206 function LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
       
   207 var s: shortstring;
   200 begin
   208 begin
   201     if (not isSoundEnabled) or fastUntilLag then
   209     if (not isSoundEnabled) or fastUntilLag then
   202     begin
   210     begin
   203         LoopSound:= -1;
   211         LoopSound:= -1;
   204         exit
   212         exit
   205     end;
   213     end;
   206 
   214 
   207     if (voicepack <> nil) and (voicepack^.chunks[snd] <> nil) then
   215     if (voicepack <> nil) then
       
   216     begin
       
   217         if (voicepack^.chunks[snd] = nil) and (Soundz[snd].Path = ptVoices) and (Soundz[snd].FileName <> '') then
       
   218         begin
       
   219             s:= Pathz[Soundz[snd].Path] + '/' + voicepack^.name + '/' + Soundz[snd].FileName;
       
   220             WriteToConsole(msgLoading + s + ' ');
       
   221             voicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   222             if voicepack^.chunks[snd] = nil then
       
   223                 WriteLnToConsole(msgFailed)
       
   224             else
       
   225                 WriteLnToConsole(msgOK)
       
   226         end;
   208         LoopSound:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], -1, -1)
   227         LoopSound:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], -1, -1)
       
   228     end
   209     else
   229     else
   210         LoopSound:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], -1, -1)
   230     begin
       
   231         if (defVoicepack^.chunks[snd] = nil) and (Soundz[snd].Path <> ptVoices) and (Soundz[snd].FileName <> '') then
       
   232         begin
       
   233             s:= Pathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
       
   234             WriteToConsole(msgLoading + s + ' ');
       
   235             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
       
   236             TryDo(defVoicepack^.chunks[snd] <> nil, msgFailed, true);
       
   237             WriteLnToConsole(msgOK);
       
   238         end;
       
   239         LoopSound:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], -1, -1);
       
   240     end;
   211 end;
   241 end;
   212 
   242 
   213 procedure StopSound(snd: TSound);
   243 procedure StopSound(snd: TSound);
   214 begin
   244 begin
   215     if not isSoundEnabled then exit;
   245     if not isSoundEnabled then exit;