//Use all available sources in the pool by filling them with either high or low priority sounds
    //Then use the playNonFilling to attempt to add the other type
    //This tests that high-priority voices can appropriately steal sources from low priorities
    //and that low-priority voices cannot steal from a source with high priority
    //(Note that 'high priority' refers to lower priority numbers)

    private void Awake()
    {
        new GameObject("sound").AddComponent <AudioPlayer>();

        AudioAsset asset = CreateAsset(fillWithLowPriority ? low : high);

        asset.Looping    = true;
        asset.VolumeBase = AudioUtil.DecibelToVolumeFactor(-50);

        for (int i = 0; i < AudioSettings.GetConfiguration().numRealVoices; i++)
        {
            asset.Play();
        }
    }
Exemple #2
0
    public Voice Play(AudioAsset sound)
    {
        Voice voice = new Voice()
        {
            Asset = sound
        };

        voice.Fader.Reset();
        voice.Priority = sound.Priority;
        // get AudioClip
        voice.Clip             = GetNextClip(sound);
        voice.PlaybackPosition = 0f;
        // fading in
        if (sound.FadeTypeIn != FadeType.NONE)
        {
            voice.Fader.StartFade(0f, 1f, sound.FadeTimeIn, sound.FadeTypeIn);
        }
        // apply pitch and volume randomization
        float pitchSemitones = sound.PitchBase + Random.Range(-sound.PitchOffset / 2f, sound.PitchOffset / 2f);
        float volumeDecibels = sound.VolumeBase + Random.Range(-sound.VolumeOffset / 2f, sound.VolumeOffset / 2f);

        voice.Pitch   = AudioUtil.SemitoneToPitchFactor(pitchSemitones);
        voice.Volume  = AudioUtil.DecibelToVolumeFactor(volumeDecibels);
        voice.Looping = sound.Looping;
        // set distance attentuation properties
        voice.Attenuation = sound.Attenuation;
        voice.MinDistance = sound.MinimumDistance;
        voice.MaxDistance = sound.MaximumDistance;

        float distanceVolume = CalculateDistanceVolume(voice);

        voice.OutputVolume = (voice.Volume * voice.Fader.Volume * distanceVolume);
        // assign AudioSource
        if (voice.OutputVolume > cullingVolume &&
            pool.GetAvaialbleCount() > 0)
        {
            voice.Source = pool.Get();
            AssignSourceProperties(voice);
            voice.Source.Play();
        }
        else if (DebugMode)
        {
            Debug.LogWarning("can't assign AudioSource for sound asset " + sound + " (" + voice.Priority + ")");
        }

        activeAudio.Add(voice);
        activeAudioAdded = true;
        return(voice);
    }