Esempio n. 1
0
    //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    // OnAudioFilterRead (separate thread)
    //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    /// <summary>
    /// Raises the audio filter read event.
    /// We will spatialize the audio here
    /// NOTE: It's important that we get the audio attenuation curve for volume,
    /// that way we can feed it into our spatializer and have things sound match
    /// with bypass on/off
    /// </summary>
    /// <param name="data">Data.</param>
    /// <param name="channels">Channels.</param>
    ///
    void OnAudioFilterRead(float[] data, int channels)
    {
#if DEBUG_AudioSource
        if (debugOn)
        {
            if (audioFrames > 0)
            {
                audioFrames--;
            }
        }
#endif
        // Problem: We cannot read timer here.
        // However, we can read time-stamp via plugin
        // This is required to smooth out the position,
        // since the main loop is only allowed to update position
        // of sound, but is running at a different frequency then
        // the audio loop.
        //
        // Therefore, we will need to dead reckon the position of the sound.

        // Do not spatialize if we are not playing
        if ((isPlaying == false) ||
            (Bypass == true) ||
            (OSPManager.GetBypass() == true) ||
            (OSPManager.IsInitialized() == false))
        {
            return;
        }


        float dTime = GetTime(true) - relPosTime;
        lock (this)
        {
            relPos    += relVel * dTime;
            relPosTime = GetTime(true);
        }

        // TODO: Need to ensure that sound is not played before context is
        // legal
        if (context != sNoContext)
        {
            // Set the position for this context and sound
            OSPManager.SetPositionRel(context, relPos.x, relPos.y, relPos.z);
            //Spatialize (local override of InvSq is passed in)
            OSPManager.Spatialize(context, data, useInverseSquare, falloffNear, falloffFar);
        }
    }
Esempio n. 2
0
    // Token: 0x06003728 RID: 14120 RVA: 0x0011AAB0 File Offset: 0x00118EB0
    private void OnAudioFilterRead(float[] data, int channels)
    {
        if (!this.isPlaying || this.Bypass || OSPManager.GetBypass() || !OSPManager.IsInitialized())
        {
            return;
        }
        float d = this.GetTime(true) - this.relPosTime;

        lock (this)
        {
            this.relPos    += this.relVel * d;
            this.relPosTime = this.GetTime(true);
        }
        if (this.context != -1)
        {
            OSPManager.SetPositionRel(this.context, this.relPos.x, this.relPos.y, this.relPos.z);
            OSPManager.Spatialize(this.context, data, this.useInverseSquare, this.falloffNear, this.falloffFar);
        }
    }
Esempio n. 3
0
 // Token: 0x06003719 RID: 14105 RVA: 0x0011A360 File Offset: 0x00118760
 private void Update()
 {
     if (!this.audioSource)
     {
         return;
     }
     if (!this.isPlaying && this.audioSource.isPlaying)
     {
         if (!OSPManager.IsInitialized())
         {
             return;
         }
         this.Acquire();
         this.SetRelativeSoundPos(true);
         lock (this)
         {
             this.isPlaying = true;
         }
         this.drain = false;
     }
     if (this.isPlaying)
     {
         if (!this.audioSource.isPlaying)
         {
             lock (this)
             {
                 this.isPlaying = false;
             }
             this.Release();
             return;
         }
         if (this.Bypass || OSPManager.GetBypass())
         {
             this.audioSource.spatialBlend = this.panLevel;
             this.audioSource.spread       = this.spread;
         }
         else
         {
             float spatialBlend = 1f;
             if (OSPManager.GetUseInverseSquareAttenuation() || this.useInverseSquare)
             {
                 spatialBlend = 0f;
             }
             this.audioSource.spatialBlend = spatialBlend;
             this.audioSource.spread       = 180f;
         }
         if (this.context != -1)
         {
             OSPManager.SetDisableReflectionsOnSound(this.context, this.disableReflections);
         }
         if (this.audioSource.time == 0f && this.audioSource.loop)
         {
             this.SetRelativeSoundPos(false);
         }
         else if (this.audioSource.time >= this.audioSource.clip.length && !this.audioSource.loop)
         {
             this.drainTime = OSPManager.GetDrainTime(this.context);
             this.drain     = true;
         }
         else
         {
             this.SetRelativeSoundPos(false);
         }
         if (this.drain)
         {
             this.drainTime -= Time.deltaTime;
             if (this.drainTime < 0f)
             {
                 this.drain = false;
                 lock (this)
                 {
                     this.isPlaying = false;
                 }
                 this.Stop();
             }
         }
     }
 }
    /// <summary>
    /// Update this instance.
    /// </summary>
    void Update()
    {
        if (!audioSource)
        {
            return;
        }

        // If user called Play on the acutal AudioSource, account for this
        // NOTE: There may be a potential issue with calling AudioSource.Stop on a
        // looping sound, since the Update may not catch this. We might need to catch
        // this edge case at some point; a one-shot sound will stop automatically and
        // return spatialization resources.
        if ((isPlaying == false) && (audioSource.isPlaying))
        {
            // This is a copy of OSPAudioSource Play function, minus
            // calling Play on audio source since it's already been called

            // Bail if manager has not even started
            if (OSPManager.IsInitialized() == false)
            {
                return;
            }

            // We will grab a context at this point, and set the right values
            // to allow for spatialization to take effect
            Aquire();

            // We will set the relative position of this sound now before we start playing
            SetRelativeSoundPos(true);

            // We are ready to play the sound
            // Commented out, kept for readability
            // audioSource.Play();

            lock (this) isPlaying = true;
        }

        // If sound is playing on it's own and dies off, we need to
        // Reset
        if (isPlaying == true)
        {
            // If we stopped the sound using AudioSource, Release resources
            if (audioSource.isPlaying == false)
            {
                lock (this) isPlaying = false;
                Release();
                return;
            }

            // We will go back and forth between spatial processing
            // and native 2D panning
            if ((Bypass == true) || (OSPManager.GetBypass() == true))
            {
#if (!UNITY5)
                audioSource.spatialBlend = panLevel;
#else
                audioSource.spatialBlend = panLevel;
#endif
                audioSource.spread = spread;
            }
            else
            {
                // We want to remove FMod curve attenuation if we are doing our
                // own internal calc
                float pl = sSetPanLevel;
                if (OSPManager.GetUseInverseSquareAttenuation() == true)
                {
                    pl = sSetPanLevelInvSq;
                }
#if (!UNITY5)
                audioSource.spatialBlend = pl;
#else
                audioSource.spatialBlend = pl;
#endif
                audioSource.spread = sSetSpread;
            }

            // Update FrequencyHints and local reflection enable/disable
            if (context != sNoContext)
            {
                OSPManager.SetFrequencyHint(context, frequencyHint);
                OSPManager.SetDisableReflectionsOnSound(context, disableReflections);
            }

            // return the context when the sound has finished playing
            if ((audioSource.time >= audioSource.clip.length) && (audioSource.loop == false))
            {
                // One last pass to allow for the tail portion of the sound
                // to finish
                drainTime = OSPManager.GetDrainTime(context);
                drain     = true;
            }
            else
            {
                // We must set all positional properties here, not in
                // OnAudioFilterRead. We might consider a better approach
                // to interpolate the current location for better localization,
                // should the resolution of setting it here sound jittery due
                // to thread frequency mismatch.
                SetRelativeSoundPos(false);

#if DEBUG_AudioSource
                // Time main thread and audio thread
                if (debugOn)
                {
                    // Get audio frequency
                    if (audioFrames == 0)
                    {
                        float ct = 1.0f / (GetTime(false) - dTimeMainLoop);
                        Debug.LogWarning(System.String.Format("U: {0:F0}", ct));
                        ct = 100.0f / (GetTime(true) - PrevAudioTime);
                        Debug.LogWarning(System.String.Format("A: {0:F0}", ct));
                        audioFrames   = 100;
                        PrevAudioTime = (float)GetTime(true);
                    }

                    dTimeMainLoop = (float)GetTime(false);
                }
#endif
            }

            if (drain == true)
            {
                // Keep playing until we safely drain out the early reflections
                drainTime -= Time.deltaTime;
                if (drainTime < 0.0f)
                {
                    drain = false;

                    lock (this) isPlaying = false;
                    // Stop will both stop audio from playing (keeping OnAudioFilterRead from
                    // running with a held audio source) as well as release the spatialization
                    // resources via Release()
                    Stop();
                }
            }
        }
    }