hedgewars/uSound.pas
changeset 7442 9bb6abdb5675
parent 7423 ec8f690f3e0f
child 7848 775a72905708
equal deleted inserted replaced
7392:bc3306c59a08 7442:9bb6abdb5675
    45 
    45 
    46 // MUSIC
    46 // MUSIC
    47 
    47 
    48 // Obvious music commands for music track
    48 // Obvious music commands for music track
    49 procedure SetMusic(enabled: boolean);           // Enable/disable music.
    49 procedure SetMusic(enabled: boolean);           // Enable/disable music.
    50 procedure SetMusicName(musicname: shortstring);     // Enable/disable music and set name of musicfile to play.
    50 procedure SetMusicName(musicname: shortstring); // Enable/disable music and set name of the file to play.
    51 procedure PlayMusic;                            // Play music from the start.
    51 procedure PlayMusic;                            // Play music from the start.
    52 procedure PauseMusic;                           // Pause music.
    52 procedure PauseMusic;                           // Pause music.
    53 procedure ResumeMusic;                          // Resume music from pause point.
    53 procedure ResumeMusic;                          // Resume music from pause point.
    54 procedure ChangeMusic(musicname: shortstring);  // Replaces music track with musicname and plays it.
    54 procedure ChangeMusic(musicname: shortstring);  // Replaces music track with musicname and plays it.
    55 procedure StopMusic;                            // Stops and releases the current track.
    55 procedure StopMusic;                            // Stops and releases the current track.
    80 
    80 
    81 procedure AddVoice(snd: TSound; voicepack: PVoicepack);
    81 procedure AddVoice(snd: TSound; voicepack: PVoicepack);
    82 procedure PlayNextVoice;
    82 procedure PlayNextVoice;
    83 
    83 
    84 
    84 
    85 // MISC
    85 // GLOBAL FUNCTIONS
    86 
       
    87 // Set the initial volume
       
    88 procedure SetVolume(volume: LongInt);
       
    89 
       
    90 // Modifies the sound volume of the game by voldelta and returns the new volume level.
       
    91 function  ChangeVolume(voldelta: LongInt): LongInt;
       
    92 
       
    93 // Returns a pointer to the voicepack with the given name.
       
    94 function  AskForVoicepack(name: shortstring): Pointer;
       
    95 
    86 
    96 // Drastically lower the volume when we lose focus (and restore the previous value).
    87 // Drastically lower the volume when we lose focus (and restore the previous value).
    97 procedure DampenAudio;
    88 procedure DampenAudio;
    98 procedure UndampenAudio;
    89 procedure UndampenAudio;
    99 
    90 
       
    91 // Mute/Unmute audio
       
    92 procedure MuteAudio;
       
    93 
       
    94 
       
    95 // MISC
       
    96 
       
    97 // Set the initial volume
       
    98 procedure SetVolume(volume: LongInt);
       
    99 
       
   100 // Modifies the sound volume of the game by voldelta and returns the new volume level.
       
   101 function  ChangeVolume(voldelta: LongInt): LongInt;
       
   102 
       
   103 // Returns a pointer to the voicepack with the given name.
       
   104 function  AskForVoicepack(name: shortstring): Pointer;
       
   105 
       
   106 
   100 implementation
   107 implementation
   101 uses uVariables, uConsole, uUtils, uCommands, uDebug;
   108 uses uVariables, uConsole, uUtils, uCommands, uDebug;
   102 
   109 
   103 const chanTPU = 32;
   110 const chanTPU = 32;
   104 var Volume: LongInt;
   111 var Volume: LongInt;
       
   112     cInitVolume: LongInt;
       
   113     previousVolume: LongInt; // cached volume value
   105     lastChan: array [TSound] of LongInt;
   114     lastChan: array [TSound] of LongInt;
   106     voicepacks: array[0..cMaxTeams] of TVoicepack;
   115     voicepacks: array[0..cMaxTeams] of TVoicepack;
   107     defVoicepack: PVoicepack;
   116     defVoicepack: PVoicepack;
   108     Mus: PMixMusic = nil;
   117     Mus: PMixMusic = nil; // music pointer
   109     MusicFN: shortstring; // music file name
   118     MusicFN: shortstring; // music file name
   110     previousVolume: LongInt; // cached volume value
       
   111     isMusicEnabled: boolean;
   119     isMusicEnabled: boolean;
   112     isSoundEnabled: boolean;
   120     isSoundEnabled: boolean;
   113     isSEBackup: boolean;
   121     isSEBackup: boolean;
   114     cInitVolume: LongInt;
       
   115 
   122 
   116 
   123 
   117 function  AskForVoicepack(name: shortstring): Pointer;
   124 function  AskForVoicepack(name: shortstring): Pointer;
   118 var i: Longword;
   125 var i: Longword;
   119     locName, path: shortstring;
   126     locName, path: shortstring;
   178     WriteToConsole('Init SDL_mixer... ');
   185     WriteToConsole('Init SDL_mixer... ');
   179     SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, true);
   186     SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, true);
   180     WriteLnToConsole(msgOK);
   187     WriteLnToConsole(msgOK);
   181 
   188 
   182     Mix_AllocateChannels(Succ(chanTPU));
   189     Mix_AllocateChannels(Succ(chanTPU));
   183     ChangeVolume(cInitVolume);
   190     ChangeVolume(cInitVolume);	
   184 end;
   191 end;
   185 
   192 
   186 procedure ResetSound;
   193 procedure ResetSound;
   187 begin
   194 begin
   188     isSoundEnabled:= isSEBackup;
   195     isSoundEnabled:= isSEBackup;
   444 end;
   451 end;
   445 
   452 
   446 function ChangeVolume(voldelta: LongInt): LongInt;
   453 function ChangeVolume(voldelta: LongInt): LongInt;
   447 begin
   454 begin
   448     ChangeVolume:= 0;
   455     ChangeVolume:= 0;
   449     if not isSoundEnabled then
   456     if (not isSoundEnabled) or (voldelta = 0) then
   450         exit;
   457         exit;
   451 
   458 
   452     inc(Volume, voldelta);
   459     inc(Volume, voldelta);
   453     if Volume < 0 then
   460     if Volume < 0 then
   454         Volume:= 0;
   461         Volume:= 0;
   456     Mix_Volume(-1, Volume);
   463     Mix_Volume(-1, Volume);
   457     // get assigned Volume
   464     // get assigned Volume
   458     Volume:= Mix_Volume(-1, -1);
   465     Volume:= Mix_Volume(-1, -1);
   459     if isMusicEnabled then
   466     if isMusicEnabled then
   460         Mix_VolumeMusic(Volume * 4 div 8);
   467         Mix_VolumeMusic(Volume * 4 div 8);
   461     ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME
   468     ChangeVolume:= Volume * 100 div MIX_MAX_VOLUME;
       
   469 
       
   470     if (isMusicEnabled) then
       
   471         if (Volume = 0) then
       
   472             PauseMusic
       
   473             else
       
   474             ResumeMusic;
       
   475 
       
   476     isAudioMuted:= (Volume = 0);
   462 end;
   477 end;
   463 
   478 
   464 procedure DampenAudio;
   479 procedure DampenAudio;
   465 begin
   480 begin
       
   481     if (isAudioMuted) then
       
   482         exit;
   466     previousVolume:= Volume;
   483     previousVolume:= Volume;
   467     ChangeVolume(-Volume * 7 div 9);
   484     ChangeVolume(-Volume * 7 div 9);
   468 end;
   485 end;
   469 
   486 
   470 procedure UndampenAudio;
   487 procedure UndampenAudio;
   471 begin
   488 begin
       
   489      if (isAudioMuted) then
       
   490         exit;
   472     ChangeVolume(previousVolume - Volume);
   491     ChangeVolume(previousVolume - Volume);
       
   492 end;
       
   493 
       
   494 procedure MuteAudio;
       
   495 begin
       
   496     if (not isSoundEnabled) then
       
   497     exit;
       
   498 
       
   499     if (isAudioMuted) then
       
   500     begin
       
   501         ResumeMusic;
       
   502         ChangeVolume(previousVolume);
       
   503     end
       
   504     else
       
   505     begin
       
   506         PauseMusic;
       
   507         previousVolume:= Volume;
       
   508         ChangeVolume(-Volume);
       
   509     end;
       
   510 
       
   511     // isAudioMuted is updated in ChangeVolume
   473 end;
   512 end;
   474 
   513 
   475 procedure SetMusic(enabled: boolean);
   514 procedure SetMusic(enabled: boolean);
   476 begin
   515 begin
   477     isMusicEnabled:= enabled;
   516     isMusicEnabled:= enabled;
   532     if s[byte(s[0])]='"' then
   571     if s[byte(s[0])]='"' then
   533         Delete(s, byte(s[0]), 1);
   572         Delete(s, byte(s[0]), 1);
   534     CurrentTeam^.voicepack:= AskForVoicepack(s)
   573     CurrentTeam^.voicepack:= AskForVoicepack(s)
   535 end;
   574 end;
   536 
   575 
       
   576 procedure chMute(var s: shortstring);
       
   577 begin
       
   578     s:= s; // avoid compiler hint
       
   579     MuteAudio;
       
   580 end;
       
   581 
   537 procedure initModule;
   582 procedure initModule;
   538 var t: LongInt;
   583 var t: LongInt;
   539     i: TSound;
   584     i: TSound;
   540 begin
   585 begin
   541     RegisterVariable('voicepack', @chVoicepack, false);
   586     RegisterVariable('voicepack', @chVoicepack, false);
       
   587     RegisterVariable('mute'     , @chMute     , true );
   542 
   588 
   543     MusicFN:='';
   589     MusicFN:='';
   544     isMusicEnabled:= true;
   590     isAudioMuted:= false;
   545     isSoundEnabled:= true;
       
   546     isSEBackup:= isSoundEnabled;
   591     isSEBackup:= isSoundEnabled;
   547     cInitVolume:= 100;
       
   548     Volume:= 0;
   592     Volume:= 0;
   549     defVoicepack:= AskForVoicepack('Default');
   593     defVoicepack:= AskForVoicepack('Default');
   550 
   594 
   551     for i:= Low(TSound) to High(TSound) do
   595     for i:= Low(TSound) to High(TSound) do
   552         lastChan[i]:= -1;
   596         lastChan[i]:= -1;
   566 
   610 
   567 procedure freeModule;
   611 procedure freeModule;
   568 begin
   612 begin
   569     if isSoundEnabled then
   613     if isSoundEnabled then
   570         ReleaseSound(true);
   614         ReleaseSound(true);
       
   615     // koda still needs to fix this properly.  when he rearranged things, he made these variables get
       
   616     // reset after argparsers picks them up
       
   617     isMusicEnabled:= true;
       
   618     isSoundEnabled:= true;
       
   619     cInitVolume:= 100;
   571 end;
   620 end;
   572 
   621 
   573 end.
   622 end.
   574 
   623