Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

IGSoundEffectsSubsystem.SoundEffectsSubsystem


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
class SoundEffectsSubsystem extends IGEffectsSystem.EffectsSubsystem
    implements Engine.IInterestedActorDestroyed
    config(SoundEffects)
    native;

// =============================================================================
//  SoundEffectsSubsystem
// 
// 
//  The SoundEffectsSubsystem is the manager class for the sound system.  It contains the public 
//  interface that the EffectsSystem deals with.  Most of the functionality is handled in native
//  code, except for overriden script functions from the base class, EffectsSubsystem, though these
//  are kept small and generally rely on native functions anyway.  This class has the responsibility
//  of creating and maintaining the list of currently playing soundinstances.  
//
// ==============================================================================

// Constants
const kMaxSounds        = 80;
const kMaxInstances     = 80;
const kNoTime           = 1000000000.0;

// ==============================================================================
// Variables
// ==============================================================================

// SoundInstanceBank is an ActorBank which handles pooling soundinstances that are no longer in the 
// CurrentSounds array.  Each sound currently playing has a corresponding SoundInstance that encapsulates
// the instance data, and handles updating the sound.  When a new sound plays, a SoundInstance is withdrawn
// from the SoundInstanceBank and added to the CurrentSounds array.  When a sound stops, it is removed from
// the CurrentSounds and deposited into the SoundInstanceBank.  Note: SoundInstances perform no updating 
// functionality while in the SoundInstanceBank.
var private transient ActorBank             SoundInstanceBank;      // Bank of unused SoundInstances.
var private transient array<SoundInstance>  CurrentSounds;          // Currently playing SoundInstances.

// State flags....
var private bool                            bSoundsArePaused;       // Whether all sounds are paused
var private bool                            GameStarted;            // Whether the game has started or not
var private config bool                     bDebugSounds;           // Whether we should log out all sorts of debug info.

// Native functions...
// Tell the sound system that the give sound will loop
native static function                      SetNativeLooping (Level inSoundLevel, sound inSound);
// Return a debug string describing the state of the sound system
native final function string                GetSoundDebugText ();

// Stopping sound functionality....
native final function bool                  StopSound(Actor Source, Sound Sound);
// Stop all SoundInstances being played on the given actor
native final function                       StopMySchemas (Actor inSource);
// Stop all SoundInstances being played currently
native final function                       StopAllSchemas();
// Stop all looping schemas that the specified actor is playing
native final function                       StopMyLoopingSchemas(Actor inSource);
// Returns if the given specification is playing on the given actor
native final function bool                  IsSchemaPlaying (Actor inSource, name SpecificationName);

// These private functions handle instance creation/deletion/updating. 
native private final function bool          CanStartInstance(SoundInstance inInstance);
native private final function               KillInstance(SoundInstance inInstance);
native private final function               AddToCurrentSounds (SoundInstance inInstance);
// CreateSoundInstance withdraws a SoundInstance from a bank, which could possibly cause the creation of a soundinstance
// TODO: Maybe should be called "GetSoundInstance" or something?
native private final function SoundInstance CreateSoundInstance ();

native private final function NativeInitialize();

// Overridable delegate for withdrawn behavior 
function OnBankWithdrawn(Actor inActorWithdrawn)
{
    assertWithDescription( inActorWithdrawn.IsA( 'SoundInstance' ), "Withdrawing a non-SoundInstance from the bank!" );
    inActorWithdrawn.OptimizeIn();
}

// Overridable delegate for deposited behavior 
function OnBankDeposited(Actor inActorDeposited)
{
    local SoundInstance BankedInstance;
    assertWithDescription( inActorDeposited.IsA( 'SoundInstance' ), "Depositing a non-SoundInstance from the bank!" );
    
    // Make sure the sound was stopped before hand...
    BankedInstance = SoundInstance(inActorDeposited);
    if ( BankedInstance.SoundHandle != -1 )
        assertWithDescription( false, "Error, SoundInstance "$BankedInstance$" is being deposited in the bank but it wasn't stopped." );

    if (bDebugSounds)
    {
    	log("Actor: "$BankedInstance$" was deposited");
    }
    inActorDeposited.OptimizeOut();
}

// PreBeginPlay override handles initialization of any necessary data
simulated function PreBeginPlay()
{
    Super.PreBeginPlay();

    RegisterNotifyGameStarted();

	if (Outer.Name != 'Entry')
		Level.RegisterNotifyActorDestroyed(Self);

    // Create sound instance bank
    SoundInstanceBank = Spawn(class'ActorBank');
    SoundInstanceBank.Initialize( class'SoundInstance' );

    SoundInstanceBank.OnWithdrawn = OnBankWithdrawn;
    SoundInstanceBank.OnDeposited = OnBankDeposited;

	NativeInitialize();
}

function OnOtherActorDestroyed(Actor ActorBeingDestroyed)
{
    //log("Actor"$ActorBeingDestroyed$" was destroyed");
    StopMyLoopingSchemas(ActorBeingDestroyed);
}

// GameStarted notification function, sets the GameStarted flag to true...
simulated function OnGameStarted() { GameStarted=true; }

// GetSoundMaterialFlags is used to get the pertinent flag from the Material.  Sometimes the sound designer
// will set the SoundFlag directly, other times, when the MaterialSoundType is not set, the visual effect
// flag will be used. 
simulated function int GetSoundMaterialFlags(Material inMaterial)
{
    // HACK: set the SoundEffectSubsystem's TextureFlags from the passed material (if any)
    if (inMaterial != None)
    {
        if (inMaterial.MaterialSoundType != 0)
            return inMaterial.MaterialSoundType;
        else
            return inMaterial.MaterialVisualType;
    }
    return 0;
}

// This EffectSubsystem overriden function plays the given effect specification.  This will result in the creation and 
// updating of a new SoundInstance if necessary.
simulated event Actor PlayEffectSpecification(EffectSpecification GenericSchema,
                                                 Actor Source,
                                                 optional Actor Target,
                                                 optional Material TargetMaterial,
                                                 optional vector overrideWorldLocation,
                                                 optional rotator overrideWorldRotation,
                                                 optional IEffectObserver Observer)
{
    local SoundInstance NewInstance;
    local SoundEffectSpecification Schema;
    local bool bInstanceValid;

	if (GenericSchema == None)
		return None;

    if (bDebugSounds)
    {
        log( "[SOUNDEFFECTS] - EffectSpecification Triggered!  Specification: "$GenericSchema$", on Actor: "$Source$" and Material: "$TargetMaterial);
    }

    Schema = SoundEffectSpecification(GenericSchema);
    //log("Playeffectspec: "$GenericSchema$" with observer: "$Observer);
    assert(Schema != None);

    // Create a new sound instance
    NewInstance = CreateSoundInstance();
    if (NewInstance != None)
    {
        // Construct the soundinstance based on the Specification...
        bInstanceValid = Schema.ConstructSoundInstance(NewInstance, GetSoundMaterialFlags(TargetMaterial), Source, GameStarted);

#if !IG_TRIBES3 // Alex: sound effect observers were causing clean up crashes - we do not use this functionality so it has been culled
        // SetObserver as soon as Instance is constructed, previously it was set after the next !bInstanceValid || !CanStartInstance() 
        // conditional.  Set it here so OnEffectStopped is called correctly if the NewInstance doesn't play.
        if (Observer != None)
            NewInstance.SetObserver(Observer);
#endif

        if ( !bInstanceValid  || !CanStartInstance(NewInstance) )
        {
            if (bDebugSounds)
            {
                log( "[SOUNDEFFECTS] - ERROR! Sound could not be started!" );
            }
            KillInstance (NewInstance);
            return None;
        }         

        if ( bDebugSounds )
            log( "New Instance:"$NewInstance$" Instantiated: "$NewInstance.toString() );

        // SoundInstance is ok to be played...
        AddToCurrentSounds(NewInstance);
        if ( bDebugSounds )
            log( "About to play New Instance: "$NewInstance.toString() );
               
        // Tell the instance it's been properly initialized...
        NewInstance.OnFinishedInitialized();

        // Play the sound and shiznits....
        NewInstance.Play();
    }
    return NewInstance;    // SoundEffectSubsystems create SoundInstances which are Actors and can be returned here to
                           // implement the normal behavior of an EffectsSubsystem
}

// This EffectSubsystem overriden function stops the given effect specification.  This will result in stopping the 
// sound in the lower level unreal code, removing the SoundInstance from the CurrentSounds.
simulated event StopEffectSpecification(EffectSpecification EffectSpec, Actor Source)
{
    local int i;
    assertWithDescription(source != None, "StopSchema called with NULL source.");

    // Check through currently playing sounds and stop instances
    // with this object and schema.
    for (i = CurrentSounds.Length-1; i >= 0; --i)
    {
        if (CurrentSounds[i] != None)
        {
            if (CurrentSounds[i].Source == source && CurrentSounds[i].SchemaName == EffectSpec.Name)
                KillInstance (CurrentSounds[i]);
        }
    }
}


// These pause functions call a console command which causes the lower level unreal code to pause 
// all sounds.  Note: This functionality is likely to chaange....
simulated function PauseAllSchemas ()
{
        if (!bSoundsArePaused)
        {
                ConsoleCommand ("PauseSounds");
                bSoundsArePaused = true;
        }
}
simulated function UnpauseAllSchemas ()
{
        if (bSoundsArePaused)
        {
                ConsoleCommand ("UnpauseSounds");
                bSoundsArePaused = false;
        }
}


// PlayMarkerSounds plays sounds triggered by walking into a given SoundMarker.  Sounds played will stop the previous
// marker sound and start a new one.
simulated function PlayMarkerSounds (Actor inSource, string inSoundID1, string inSoundID2)
{
    local bool bPlaySound1;
    local bool bPlaySound2;
    local int i;
    
    bPlaySound1 = true;
    bPlaySound2 = true;

    // Stop any marker sounds currently playing (that aren't also played by *this* marker)

    for (i = 0; i < CurrentSounds.Length; ++i)
        if (CurrentSounds[i] != None && CurrentSounds[i].Source != None)
            if (CurrentSounds[i].Source.IsA ('SoundMarker'))
                if (string(CurrentSounds[i].SchemaName) == inSoundID1)
                    // Sound 1 is already playing
                    bPlaySound1 = false;
                else if (string(CurrentSounds[i].SchemaName) == inSoundID2)
                    // Sound 2 is already playing
                    bPlaySound2 = false; 
                else
                    KillInstance (CurrentSounds[i]);

    // Play the new sound(s)
    if (inSoundID1 != "" && GetSpecificationByString(inSoundID1) != None && bPlaySound1)
        PlayEffectSpecification(GetSpecificationByString(inSoundID1), inSource);

    if (inSoundID2 != "" && GetSpecificationByString(inSoundID2) != None && bPlaySound2)
        PlayEffectSpecification(GetSpecificationByString(inSoundID2), inSource);
}

// Print the current sounds and sound instances to the log
simulated function LogState()
{
    local int i;
    local String StateString;

    Log("----------------------------------------------------------------");
    Log("|              SOUND EFFECTS SUBSYSTEM STATE                   |");
    Log("----------------------------------------------------------------");
    Log("| Existing sounds:");
    for (i = 0; i < CurrentSounds.Length; ++i)
    {
        StateString = "None";
        if (CurrentSounds[i] != None) { StateString = CurrentSounds[i].toString(); }
        Log("|   #"$i$": "$StateString);
    }
    Log("----------------------------------------------------------------");
}

// Native c++ .h interface....
cpptext
{
private: 
    void RemoveFromCurrentSounds (ASoundInstance* inInstance);
    // Handles cases where a new sound can overwrite an old sound
    UBOOL HandleOverlap (ASoundInstance* inExistingSound, ASoundInstance* inNewSound);
    UBOOL IsBeingDelayed(ASoundInstance* inInstance);
    UBOOL IsSchemaPlaying(AActor* inSource, FName schemaName);
    class USoundEffectSpecification* GetSpecificationByName( FName SchemaName );
    UBOOL CanStartInstance(ASoundInstance* inInstance);

    // Stop instance will just stop the sample playing, while leaving the SoundInstance updating, this 
    // should NEVER be called by anything outside of ASoundInstance of ASoundEffectsSubsystem.  This is used
    // for non-seamless loops where there is a random delay between loop iterations.  
    void StopInstance (ASoundInstance* inInstance);
    // Requires ASoundInstance to be a friend...
    friend class ASoundInstance;

    // When the UAudioSubsystem stops a sound sample for whatever reason, we need to make sure and cleanup SoundInstance's
    // that reference the sample
    void OnSubsystemStoppedSound( int StoppedHandle );
    // Requires this function to be a friend...
    friend static void GlobalSoundStoppedByAudioSubsystem( int SoundIndex );

    static UAudioSubsystem* AudioSubsystem;

public:
    // Used to register names for events in this package
    void InitExecution();
    void PostScriptDestroyed();
    
    // Returns the duration of the sound
    static FLOAT GetSoundInstanceDuration(ASoundInstance* inInstance);

    // Returns the current UAudioSubsystem...
    static UAudioSubsystem* GetAudio();

    // Stops a sound from playing the actual sound, though it still updates.  Used when a sound is too far away to hear
    void MuteInstance (ASoundInstance* inInstance);
    void UnMuteInstance (ASoundInstance* inInstance);

    // Instance handling functions
    void PlayInstance (ASoundInstance* inInstance);
    void KillInstance (ASoundInstance* inInstance);
}


defaultproperties
{
    EffectSpecificationSubClass=class'SoundEffectSpecification'
    bDebugSounds=false
}


Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: ne 5.9.2004 16:02:12.000 - Creation time: st 23.5.2018 00:10:47.163 - Created with UnCodeX