hedgewars/uSound.pas
changeset 2603 abed6070a669
parent 2602 3deb9ff104da
child 2630 079ef82eac75
equal deleted inserted replaced
2602:3deb9ff104da 2603:abed6070a669
    29 
    29 
    30 procedure InitSound;
    30 procedure InitSound;
    31 procedure ReleaseSound;
    31 procedure ReleaseSound;
    32 procedure SoundLoad;
    32 procedure SoundLoad;
    33 procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack);
    33 procedure PlaySound(snd: TSound; infinite: boolean; voicepack: PVoicepack);
    34 procedure LoopSound(snd: TSound; voicepack: PVoicepack);
       
    35 procedure PlayMusic;
    34 procedure PlayMusic;
    36 procedure PauseMusic;
    35 procedure PauseMusic;
    37 procedure ResumeMusic;
    36 procedure ResumeMusic;
    38 procedure StopSound(snd: TSound);
    37 procedure StopSound(snd: TSound);
    39 function  ChangeVolume(voldelta: LongInt): LongInt;
    38 function  ChangeVolume(voldelta: LongInt): LongInt;
    65 voicepacks[i].name:= name;
    64 voicepacks[i].name:= name;
    66 AskForVoicepack:= @voicepacks[i]
    65 AskForVoicepack:= @voicepacks[i]
    67 end;
    66 end;
    68 
    67 
    69 procedure InitSound;
    68 procedure InitSound;
    70 var i: TSound;
       
    71 begin
    69 begin
    72 if not isSoundEnabled then exit;
    70 if not isSoundEnabled then exit;
    73 WriteToConsole('Init sound...');
    71 WriteToConsole('Init sound...');
    74 isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
    72 isSoundEnabled:= SDL_Init(SDL_INIT_AUDIO) >= 0;
    75 if isSoundEnabled then
    73 if isSoundEnabled then
    76    isSoundEnabled:= Mix_OpenAudio(44100, $8010, 2, 1024) = 0;
    74    isSoundEnabled:= Mix_OpenAudio(44100, $8010, 2, 1024) = 0;
    77 if isSoundEnabled then WriteLnToConsole(msgOK)
    75 if isSoundEnabled then WriteLnToConsole(msgOK)
    78                   else WriteLnToConsole(msgFailed);
    76                   else WriteLnToConsole(msgFailed);
    79 Mix_AllocateChannels(Succ(chanTPU));
    77 Mix_AllocateChannels(Succ(chanTPU));
    80 if isMusicEnabled then Mix_VolumeMusic(50);
    78 if isMusicEnabled then Mix_VolumeMusic(50);
    81 
       
    82 for i:= Low(TSound) to High(TSound) do
       
    83 	lastChan[i]:= -1;
       
    84 
    79 
    85 Volume:= 0;
    80 Volume:= 0;
    86 ChangeVolume(cInitVolume)
    81 ChangeVolume(cInitVolume)
    87 end;
    82 end;
    88 
    83 
   108 if not isSoundEnabled then exit;
   103 if not isSoundEnabled then exit;
   109 
   104 
   110 defVoicepack:= AskForVoicepack('Default');
   105 defVoicepack:= AskForVoicepack('Default');
   111 
   106 
   112 for i:= Low(TSound) to High(TSound) do
   107 for i:= Low(TSound) to High(TSound) do
   113 	if (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
   108 	if Soundz[i].Path <> ptVoices then
   114 		begin
   109 		begin
   115 		s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   110 		s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
   116 		WriteToConsole(msgLoading + s + ' ');
   111 		WriteToConsole(msgLoading + s + ' ');
   117 		defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   112 		defVoicepack^.chunks[i]:= Mix_LoadWAV_RW(SDL_RWFromFile(Str2PChar(s), 'rb'), 1);
   118 		TryDo(defVoicepack^.chunks[i] <> nil, msgFailed, true);
   113 		TryDo(defVoicepack^.chunks[i] <> nil, msgFailed, true);
   120 		end;
   115 		end;
   121 
   116 
   122 for t:= 0 to cMaxTeams do
   117 for t:= 0 to cMaxTeams do
   123 	if voicepacks[t].name <> '' then
   118 	if voicepacks[t].name <> '' then
   124 		for i:= Low(TSound) to High(TSound) do
   119 		for i:= Low(TSound) to High(TSound) do
   125 			if (Soundz[i].Path = ptVoices) and (Soundz[i].FileName <> '') then
   120 			if Soundz[i].Path = ptVoices then
   126 				begin
   121 				begin
   127 				s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName;
   122 				s:= Pathz[Soundz[i].Path] + '/' + voicepacks[t].name + '/' + Soundz[i].FileName;
   128 				WriteToConsole(msgLoading + s + ' ');
   123 				WriteToConsole(msgLoading + s + ' ');
   129 			//	{$IFNDEF IPHONEOS}
   124 			//	{$IFNDEF IPHONEOS}
   130 				//broken for unknown reasons (most likely poor SDL_Mixer)
   125 				//broken for unknown reasons (most likely poor SDL_Mixer)
   147 	lastChan[snd]:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], loops, -1)
   142 	lastChan[snd]:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], loops, -1)
   148 else
   143 else
   149 	lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], loops, -1)
   144 	lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], loops, -1)
   150 end;
   145 end;
   151 
   146 
   152 procedure LoopSound(snd: TSound; voicepack: PVoicepack);
       
   153 begin
       
   154 if (not isSoundEnabled) or fastUntilLag then exit;
       
   155 if lastChan[snd] <> -1 then exit;
       
   156 
       
   157 if (voicepack <> nil) and (voicepack^.chunks[snd] <> nil) then
       
   158 	lastChan[snd]:= Mix_PlayChannelTimed(-1, voicepack^.chunks[snd], -1, -1)
       
   159 else
       
   160 	lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], -1, -1)
       
   161 end;
       
   162 
       
   163 procedure StopSound(snd: TSound);
   147 procedure StopSound(snd: TSound);
   164 begin
   148 begin
   165 if not isSoundEnabled then exit;
   149 if not isSoundEnabled then exit;
   166 if (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
   150 if Mix_Playing(lastChan[snd]) <> 0 then
   167 	begin
   151 	Mix_HaltChannel(lastChan[snd])
   168 	Mix_HaltChannel(lastChan[snd]);
       
   169 	lastChan[snd]:= -1;
       
   170 	end;
       
   171 end;
   152 end;
   172 
   153 
   173 procedure PlayMusic;
   154 procedure PlayMusic;
   174 var s: string;
   155 var s: string;
   175 begin
   156 begin