Exemple #1
0
        public void AudioFrameUpdate(float[] data, int channels, bool physicsBasedAttenuation, float directMixFraction,
                                     bool directBinauralEnabled, HRTFInterpolation hrtfInterpolation)
        {
            float distanceAttenuation = (physicsBasedAttenuation) ? directSoundPath.distanceAttenuation : 1f;

            directAttnLerp.Set(directSoundPath.occlusionFactor * directMixFraction * distanceAttenuation);

            float   perSampleIncrement;
            int     numFrames       = data.Length / channels;
            float   attnFactor      = directAttnLerp.Update(out perSampleIncrement, numFrames);
            Vector3 directDirection = directSoundPath.direction;

            AudioBuffer inputBuffer;

            inputBuffer.audioFormat         = inputFormat;
            inputBuffer.numSamples          = data.Length / channels;
            inputBuffer.deInterleavedBuffer = null;
            inputBuffer.interleavedBuffer   = data;

            AudioBuffer outputBuffer;

            outputBuffer.audioFormat         = outputFormat;
            outputBuffer.numSamples          = data.Length / channels;
            outputBuffer.deInterleavedBuffer = null;
            outputBuffer.interleavedBuffer   = data;

            if ((outputFormat.channelLayout == ChannelLayout.Stereo) && directBinauralEnabled)
            {
                // Apply binaural audio to direct sound.
                PhononCore.iplApplyBinauralEffect(directBinauralEffect, inputBuffer, directDirection, hrtfInterpolation,
                                                  outputBuffer);
            }
            else if (outputFormat.channelLayout == ChannelLayout.Custom)
            {
                // Apply panning fo custom speaker layout.
                PhononCore.iplApplyPanningEffect(directCustomPanningEffect, inputBuffer, directDirection, outputBuffer);
            }

            // Process direct sound occlusion
            for (int i = 0, count = 0; i < numFrames; ++i)
            {
                for (int j = 0; j < channels; ++j, ++count)
                {
                    data[count] *= attnFactor;
                }

                attnFactor += perSampleIncrement;
            }
        }
        public void AudioFrameUpdate(float[] data, int channels, bool physicsBasedAttenuation, float directMixFraction,
                                     bool directBinauralEnabled, bool applyAirAbsorption, HRTFInterpolation hrtfInterpolation,
                                     OcclusionMode directOcclusionMode, OcclusionMethod directOcclusionMethod)
        {
            DirectSoundEffectOptions directSoundEffectOptions;

            directSoundEffectOptions.applyDistanceAttenuation = physicsBasedAttenuation ? Bool.True : Bool.False;
            directSoundEffectOptions.applyAirAbsorption       = applyAirAbsorption ? Bool.True : Bool.False;
            directSoundEffectOptions.occlusionMode            = directOcclusionMode;

            AudioBuffer inputBuffer;

            inputBuffer.audioFormat         = inputFormat;
            inputBuffer.numSamples          = data.Length / channels;
            inputBuffer.deInterleavedBuffer = null;
            inputBuffer.interleavedBuffer   = data;

            AudioBuffer outputBuffer;

            outputBuffer.audioFormat         = outputFormat;
            outputBuffer.numSamples          = data.Length / channels;
            outputBuffer.deInterleavedBuffer = null;
            outputBuffer.interleavedBuffer   = data;

            for (int i = 0; i < data.Length; i++)
            {
                data[i] *= directMixFraction;
            }

            PhononCore.iplApplyDirectSoundEffect(directSoundEffect, inputBuffer, directSoundPath,
                                                 directSoundEffectOptions, outputBuffer);

            Vector3 directDirection = directSoundPath.direction;

            if ((outputFormat.channelLayout == ChannelLayout.Stereo) && directBinauralEnabled)
            {
                // Apply binaural audio to direct sound.
                PhononCore.iplApplyBinauralEffect(directBinauralEffect, outputBuffer, directDirection,
                                                  hrtfInterpolation, outputBuffer);
            }
            else if (outputFormat.channelLayout == ChannelLayout.Custom)
            {
                // Apply panning fo custom speaker layout.
                PhononCore.iplApplyPanningEffect(directCustomPanningEffect, outputBuffer, directDirection,
                                                 outputBuffer);
            }
        }
Exemple #3
0
        //
        // Applies the Phonon 3D effect to dry audio.
        //
        void OnAudioFilterRead(float[] data, int channels)
        {
            mutex.WaitOne();

            if (!initialized || destroying)
            {
                mutex.ReleaseMutex();
                Array.Clear(data, 0, data.Length);
                return;
            }

            if (data == null)
            {
                mutex.ReleaseMutex();
                Array.Clear(data, 0, data.Length);
                return;
            }

            float distanceAttenuation = (physicsBasedAttenuation) ? directSoundPath.distanceAttenuation : 1f;

            directAttnInterlop.Set(directSoundPath.occlusionFactor * directMixFraction);
            float   occlusionFactor = directAttnInterlop.Update();
            Vector3 directDirection = directSoundPath.direction;

            AudioBuffer inputBuffer;

            inputBuffer.audioFormat         = inputFormat;
            inputBuffer.numSamples          = data.Length / channels;
            inputBuffer.deInterleavedBuffer = null;
            inputBuffer.interleavedBuffer   = data;

            AudioBuffer outputBuffer;

            outputBuffer.audioFormat         = outputFormat;
            outputBuffer.numSamples          = data.Length / channels;
            outputBuffer.deInterleavedBuffer = null;
            outputBuffer.interleavedBuffer   = data;

            // Input data is sent (where it is copied) for indirect propagation effect processing.
            // This data must be sent before applying any other effect to the input audio.
#if !UNITY_ANDROID
            if (enableReflections && (wetData != null) && (wetDataMarshal != null) && (wetAmbisonicsDataMarshal != null) && (propagationAmbisonicsEffect != IntPtr.Zero))
            {
                for (int i = 0; i < data.Length; ++i)
                {
                    wetData[i] = data[i] * indirectMixFraction;
                }

                AudioBuffer propagationInputBuffer;
                propagationInputBuffer.audioFormat         = inputFormat;
                propagationInputBuffer.numSamples          = wetData.Length / channels;
                propagationInputBuffer.deInterleavedBuffer = null;
                propagationInputBuffer.interleavedBuffer   = wetData;

                PhononCore.iplSetDryAudioForConvolutionEffect(propagationAmbisonicsEffect, sourcePosition, propagationInputBuffer);
            }
#endif

            if ((outputFormat.channelLayout == ChannelLayout.Stereo) && directBinauralEnabled)
            {
                // Apply binaural audio to direct sound.
                PhononCore.iplApplyBinauralEffect(directBinauralEffect, inputBuffer, directDirection, hrtfInterpolation, outputBuffer);
            }
            else if (outputFormat.channelLayout == ChannelLayout.Custom)
            {
                // Apply panning fo custom speaker layout.
                PhononCore.iplApplyPanningEffect(directCustomPanningEffect, inputBuffer, directDirection, outputBuffer);
            }

            // Process direct sound occlusion
            for (int i = 0; i < data.Length; ++i)
            {
                data[i] *= occlusionFactor * distanceAttenuation;
            }

#if !UNITY_ANDROID
            if (enableReflections && (wetData != null) && (wetDataMarshal != null) && (wetAmbisonicsDataMarshal != null) && (propagationAmbisonicsEffect != IntPtr.Zero))
            {
                if (fourierMixingEnabled)
                {
                    phononMixer.processMixedAudio = true;
                    mutex.ReleaseMutex();
                    return;
                }

                AudioBuffer wetAmbisonicsBuffer;
                wetAmbisonicsBuffer.audioFormat         = ambisonicsFormat;
                wetAmbisonicsBuffer.numSamples          = data.Length / channels;
                wetAmbisonicsBuffer.deInterleavedBuffer = wetAmbisonicsDataMarshal;
                wetAmbisonicsBuffer.interleavedBuffer   = null;
                PhononCore.iplGetWetAudioForConvolutionEffect(propagationAmbisonicsEffect, listenerPosition, listenerAhead, listenerUp, wetAmbisonicsBuffer);

                AudioBuffer wetBufferMarshal;
                wetBufferMarshal.audioFormat = outputFormat;
                wetBufferMarshal.audioFormat.channelOrder = ChannelOrder.Deinterleaved;     // Set format to deinterleave.
                wetBufferMarshal.numSamples          = data.Length / channels;
                wetBufferMarshal.deInterleavedBuffer = wetDataMarshal;
                wetBufferMarshal.interleavedBuffer   = null;

                if ((outputFormat.channelLayout == ChannelLayout.Stereo) && indirectBinauralEnabled)
                {
                    PhononCore.iplApplyAmbisonicsBinauralEffect(propagationBinauralEffect, wetAmbisonicsBuffer, wetBufferMarshal);
                }
                else
                {
                    PhononCore.iplApplyAmbisonicsPanningEffect(propagationPanningEffect, wetAmbisonicsBuffer, wetBufferMarshal);
                }

                AudioBuffer wetBuffer;
                wetBuffer.audioFormat         = outputFormat;
                wetBuffer.numSamples          = data.Length / channels;
                wetBuffer.deInterleavedBuffer = null;
                wetBuffer.interleavedBuffer   = wetData;
                PhononCore.iplInterleaveAudioBuffer(wetBufferMarshal, wetBuffer);

                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] += wetData[i];
                }
            }
#endif

            mutex.ReleaseMutex();
        }