public void Destroy() { if (environment != IntPtr.Zero) { PhononCore.iplDestroyEnvironment(ref environment); } }
// Initializes various Phonon API objects in a lazy fashion. // Safe to call this every frame. public void LazyInitialize(BinauralRenderer binauralRenderer, bool directBinauralEnabled) { if (directBinauralEffect == IntPtr.Zero && outputFormat.channelLayout == ChannelLayout.Stereo && directBinauralEnabled && binauralRenderer.GetBinauralRenderer() != IntPtr.Zero) { // Create object based binaural effect for direct sound if the output format is stereo. if (PhononCore.iplCreateBinauralEffect(binauralRenderer.GetBinauralRenderer(), inputFormat, outputFormat, ref directBinauralEffect) != Error.None) { Debug.Log("Unable to create binaural effect. Please check the log file for details."); return; } } if (directCustomPanningEffect == IntPtr.Zero && outputFormat.channelLayout == ChannelLayout.Custom && !directBinauralEnabled && binauralRenderer.GetBinauralRenderer() != IntPtr.Zero) { // Panning effect for direct sound (used for rendering only for custom speaker layout, // otherwise use default Unity panning) if (PhononCore.iplCreatePanningEffect(binauralRenderer.GetBinauralRenderer(), inputFormat, outputFormat, ref directCustomPanningEffect) != Error.None) { Debug.Log("Unable to create custom panning effect. Please check the log file for details."); return; } } }
public void Destroy() { if (device != IntPtr.Zero) { PhononCore.iplDestroyComputeDevice(ref device); } }
public void Destroy() { if (scene != IntPtr.Zero) { PhononCore.iplDestroyScene(ref scene); } }
public void Destroy() { #if !UNITY_ANDROID PhononCore.iplDestroyConvolutionEffect(ref propagationAmbisonicsEffect); propagationAmbisonicsEffect = IntPtr.Zero; PhononCore.iplDestroyAmbisonicsBinauralEffect(ref propagationBinauralEffect); propagationBinauralEffect = IntPtr.Zero; PhononCore.iplDestroyAmbisonicsPanningEffect(ref propagationPanningEffect); propagationPanningEffect = IntPtr.Zero; wetData = null; if (wetDataMarshal != null) { for (int i = 0; i < outputFormat.numSpeakers; ++i) { Marshal.FreeCoTaskMem(wetDataMarshal[i]); } } wetDataMarshal = null; if (wetAmbisonicsDataMarshal != null) { for (int i = 0; i < ambisonicsFormat.numSpeakers; ++i) { Marshal.FreeCoTaskMem(wetAmbisonicsDataMarshal[i]); } } wetAmbisonicsDataMarshal = null; #endif }
void OnDestroy() { if (environmentalRenderer != IntPtr.Zero && !usedByMixer) { PhononCore.iplDestroyEnvironmentalRenderer(ref environmentalRenderer); } }
// // Draws the inspector. // public override void OnInspectorGUI() { serializedObject.Update(); PhononGUI.SectionHeader("Quality Preset"); EditorGUILayout.PropertyField(serializedObject.FindProperty("Preset")); if (serializedObject.FindProperty("Preset").enumValueIndex < 3) { SimulationSettingsValue actualValue = ((SimulationSettings)target).Value; actualValue.CopyFrom(SimulationSettingsPresetList.PresetValue(serializedObject.FindProperty("Preset").enumValueIndex)); } else { EditorGUILayout.PropertyField(serializedObject.FindProperty("Value")); if (Application.isEditor && EditorApplication.isPlayingOrWillChangePlaymode) { SimulationSettingsValue actualValue = ((SimulationSettings)target).Value; EnvironmentComponent editorRuntimeEnvComponent = FindObjectOfType <EnvironmentComponent>(); if (editorRuntimeEnvComponent != null) { IntPtr environment = editorRuntimeEnvComponent.Environment().GetEnvironment(); PhononCore.iplSetNumBounces(environment, actualValue.RealtimeBounces); } } } EditorGUILayout.Space(); serializedObject.ApplyModifiedProperties(); }
// // Helper function to change the name of the BakedSource or BakedStaticListener // used by the effect. // public void UpdateEffectName(string effectName) { if (propagationAmbisonicsEffect != IntPtr.Zero) { PhononCore.iplSetConvolutionEffectName(propagationAmbisonicsEffect, Common.ConvertString(effectName)); } }
void DisplayProgressBarAndCancel() { BakedReverb bakedReverb = serializedObject.targetObject as BakedReverb; if (bakedReverb.bakeStatus != BakeStatus.InProgress) { return; } float progress = bakedReverb.probeBoxBakingProgress + .01f; //Adding an offset because progress bar when it is exact 0 has some non-zero progress. int progressPercent = Mathf.FloorToInt(Mathf.Min(progress * 100.0f, 100.0f)); string progressString = "Baking " + bakedReverb.probeBoxBakingCurrently + "/" + bakedReverb.totalProbeBoxes + " Probe Box (" + progressPercent.ToString() + "% complete)"; EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(" "); EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), progress, progressString); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(" "); if (GUILayout.Button("Cancel Bake")) { bakedReverb.cancelBake = true; // Ensures partial baked data is not serialized and that bake is properly cancelled for multiple probe boxes. PhononCore.iplCancelBake(); bakedReverb.EndBake(); oneBakeActive = false; bakedReverb.cancelBake = false; Debug.Log("CANCELLED: Baking reverb effect."); } EditorGUILayout.EndHorizontal(); Repaint(); }
public void FrameUpdate(IntPtr envRenderer, Vector3 sourcePosition, Vector3 listenerPosition, Vector3 listenerAhead, Vector3 listenerUp, float partialOcclusionRadius, OcclusionMode directOcclusionMode, OcclusionMethod directOcclusionMethod) { directSoundPath = PhononCore.iplGetDirectSoundPath(envRenderer, listenerPosition, listenerAhead, listenerUp, sourcePosition, partialOcclusionRadius, directOcclusionMode, directOcclusionMethod); }
public void Destroy() { if (binauralRenderer != IntPtr.Zero) { PhononCore.iplDestroyBinauralRenderer(ref binauralRenderer); } }
public void CancelBake() { cancelBake = true; // Ensures partial baked data is not serialized and that bake is properly cancelled for multiple probe boxes. PhononCore.iplCancelBake(); EndBake(); oneBakeActive = false; cancelBake = false; }
public void Create(Environment environment, RenderingSettings renderingSettings, GlobalContext globalContext) { var error = PhononCore.iplCreateBinauralRenderer(globalContext, renderingSettings, null, ref binauralRenderer); if (error != Error.None) { throw new Exception("Unable to create binaural renderer [" + error.ToString() + "]"); } }
// // Applies the Phonon effect to audio. // void OnAudioFilterRead(float[] data, int channels) { mutex.WaitOne(); if (!initialized || destroying) { mutex.ReleaseMutex(); return; } if ((data == null) || (environmentRenderer == IntPtr.Zero) || (!processMixedAudio) || (wetData == null) || (wetAmbisonicsDataMarshal == null)) { mutex.ReleaseMutex(); return; } #if !UNITY_ANDROID AudioBuffer wetAmbisonicsBuffer; wetAmbisonicsBuffer.audioFormat = ambisonicsFormat; wetAmbisonicsBuffer.numSamples = data.Length / channels; wetAmbisonicsBuffer.deInterleavedBuffer = wetAmbisonicsDataMarshal; wetAmbisonicsBuffer.interleavedBuffer = null; PhononCore.iplGetMixedEnvironmentalAudio(environmentRenderer, 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); #endif for (int i = 0; i < data.Length; ++i) { data[i] += wetData[i]; } mutex.ReleaseMutex(); }
public void Destroy() { directAttnLerp.Reset(); PhononCore.iplDestroyBinauralEffect(ref directBinauralEffect); directBinauralEffect = IntPtr.Zero; PhononCore.iplDestroyPanningEffect(ref directCustomPanningEffect); directCustomPanningEffect = IntPtr.Zero; }
public void LazyInitialize(BinauralRenderer binauralRenderer, bool acceleratedMixing, bool indirectBinauralEnabled, RenderingSettings renderingSettings) { #if !UNITY_ANDROID AudioFormat ambisonicsBinauralFormat = outputFormat; ambisonicsBinauralFormat.channelOrder = ChannelOrder.Deinterleaved; // Set up propagationPanningEffect if (acceleratedMixing && propagationPanningEffect == IntPtr.Zero && binauralRenderer.GetBinauralRenderer() != IntPtr.Zero) { if (PhononCore.iplCreateAmbisonicsPanningEffect(binauralRenderer.GetBinauralRenderer(), ambisonicsFormat, ambisonicsBinauralFormat, ref propagationPanningEffect) != Error.None) { Debug.Log("Unable to create Ambisonics panning effect. Please check the log file for details."); return; } } if (outputFormat.channelLayout == ChannelLayout.Stereo && acceleratedMixing && indirectBinauralEnabled && propagationBinauralEffect == IntPtr.Zero && binauralRenderer.GetBinauralRenderer() != IntPtr.Zero) { // Create ambisonics based binaural effect for indirect sound if the output format is stereo. if (PhononCore.iplCreateAmbisonicsBinauralEffect(binauralRenderer.GetBinauralRenderer(), ambisonicsFormat, ambisonicsBinauralFormat, ref propagationBinauralEffect) != Error.None) { Debug.Log("Unable to create propagation binaural effect. Please check the log file for details."); return; } } if (acceleratedMixing && wetData == null) { wetData = new float[renderingSettings.frameSize * outputFormat.numSpeakers]; } if (acceleratedMixing && wetAmbisonicsDataMarshal == null) { wetAmbisonicsDataMarshal = new IntPtr[ambisonicsFormat.numSpeakers]; for (int i = 0; i < ambisonicsFormat.numSpeakers; ++i) { wetAmbisonicsDataMarshal[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(float)) * renderingSettings.frameSize); } } if (acceleratedMixing && wetDataMarshal == null) { wetDataMarshal = new IntPtr[outputFormat.numSpeakers]; for (int i = 0; i < outputFormat.numSpeakers; ++i) { wetDataMarshal[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(float)) * renderingSettings.frameSize); } } #endif }
public void Destroy() { PhononCore.iplDestroyBinauralEffect(ref directBinauralEffect); directBinauralEffect = IntPtr.Zero; PhononCore.iplDestroyPanningEffect(ref directCustomPanningEffect); directCustomPanningEffect = IntPtr.Zero; PhononCore.iplDestroyDirectSoundEffect(ref directSoundEffect); directSoundEffect = IntPtr.Zero; }
// // Updates the direction of the source relative to the listener. // Wait until the end of the frame to update the position to get latest information. // private void UpdateRelativeDirection() { sourcePosition = Common.ConvertVector(transform.position); listenerPosition = Common.ConvertVector(listener.transform.position); listenerAhead = Common.ConvertVector(listener.transform.forward); listenerUp = Common.ConvertVector(listener.transform.up); IntPtr envRenderer = (environmentalRenderer != null) ? environmentalRenderer.GetEnvironmentalRenderer() : IntPtr.Zero; directSoundPath = PhononCore.iplGetDirectSoundPath(envRenderer, listenerPosition, listenerAhead, listenerUp, sourcePosition, partialOcclusionRadius, directOcclusionOption); }
public Error Create(ComputeDevice computeDevice, SimulationSettings simulationSettings, Scene scene, ProbeManager probeManager, GlobalContext globalContext) { var error = PhononCore.iplCreateEnvironment(globalContext, computeDevice.GetDevice(), simulationSettings, scene.GetScene(), probeManager.GetProbeManager(), ref environment); if (error != Error.None) { throw new Exception("Unable to create environment [" + error.ToString() + "]"); } return(error); }
public Error Create(ComputeDevice computeDevice, SimulationSettings simulationSettings, GlobalContext globalContext) { string fileName = SceneFileName(); if (!File.Exists(fileName)) { return(Error.Fail); } var error = PhononCore.iplLoadFinalizedScene(globalContext, simulationSettings, fileName, computeDevice.GetDevice(), null, ref scene); return(error); }
// // Destroys the effect. // void OnDestroy() { mutex.WaitOne(); destroying = true; errorLogged = false; if (audioEngine == AudioEngine.Unity) { directAttnInterlop.Reset(); #if !UNITY_ANDROID PhononCore.iplDestroyConvolutionEffect(ref propagationAmbisonicsEffect); propagationAmbisonicsEffect = IntPtr.Zero; PhononCore.iplDestroyAmbisonicsBinauralEffect(ref propagationBinauralEffect); propagationBinauralEffect = IntPtr.Zero; PhononCore.iplDestroyAmbisonicsPanningEffect(ref propagationPanningEffect); propagationPanningEffect = IntPtr.Zero; #endif PhononCore.iplDestroyBinauralEffect(ref directBinauralEffect); directBinauralEffect = IntPtr.Zero; PhononCore.iplDestroyPanningEffect(ref directCustomPanningEffect); directCustomPanningEffect = IntPtr.Zero; wetData = null; if (wetDataMarshal != null) { for (int i = 0; i < outputFormat.numSpeakers; ++i) { Marshal.FreeCoTaskMem(wetDataMarshal[i]); } } wetDataMarshal = null; if (wetAmbisonicsDataMarshal != null) { for (int i = 0; i < ambisonicsFormat.numSpeakers; ++i) { Marshal.FreeCoTaskMem(wetAmbisonicsDataMarshal[i]); } } wetAmbisonicsDataMarshal = null; } mutex.ReleaseMutex(); }
public void Destroy() { if (probeBatch != IntPtr.Zero) { PhononCore.iplDestroyProbeBatch(ref probeBatch); probeBatch = IntPtr.Zero; } if (probeManager != IntPtr.Zero) { PhononCore.iplDestroyProbeManager(ref probeManager); probeManager = IntPtr.Zero; } }
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 Error Create() { var error = PhononCore.iplCreateProbeBatch(ref probeBatch); if (error != Error.None) { throw new Exception("Unable to create probe batch."); } error = PhononCore.iplCreateProbeManager(ref probeManager); if (error != Error.None) { throw new Exception("Unable to create probe batch."); } //Add all probes from all probe boxes to the probe batch. ProbeBox[] allProbeBoxes = GameObject.FindObjectsOfType <ProbeBox>() as ProbeBox[]; foreach (ProbeBox probeBox in allProbeBoxes) { if (probeBox.probeBoxData == null || probeBox.probeBoxData.Length == 0) { continue; } IntPtr probeBoxPtr = IntPtr.Zero; try { PhononCore.iplLoadProbeBox(probeBox.probeBoxData, probeBox.probeBoxData.Length, ref probeBoxPtr); } catch (Exception e) { Debug.LogError(e.Message); } int numProbes = PhononCore.iplGetProbeSpheres(probeBoxPtr, null); for (int i = 0; i < numProbes; ++i) { PhononCore.iplAddProbeToBatch(probeBatch, probeBoxPtr, i); } PhononCore.iplDestroyProbeBox(ref probeBoxPtr); } //Add probe batch to probe manager. PhononCore.iplAddProbeBatch(probeManager, probeBatch); PhononCore.iplFinalizeProbeBatch(probeBatch); return(error); }
public Error Create(GlobalContext globalContext, bool useOpenCL, ComputeDeviceType deviceType, int numComputeUnits) { var error = Error.None; if (useOpenCL) { error = PhononCore.iplCreateComputeDevice(globalContext, deviceType, numComputeUnits, ref device); if (error != Error.None) { throw new Exception("Unable to create OpenCL compute device (" + deviceType.ToString() + ", " + numComputeUnits.ToString() + " CUs): [" + error.ToString() + "]"); } } return(error); }
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); } }
public void SetEnvironment(Environment environment, PropagationSettings simulationSettings) { ambisonicsFormat.channelLayoutType = ChannelLayoutType.Ambisonics; ambisonicsFormat.ambisonicsOrder = simulationSettings.ambisonicsOrder; ambisonicsFormat.numSpeakers = (ambisonicsFormat.ambisonicsOrder + 1) * (ambisonicsFormat.ambisonicsOrder + 1); ambisonicsFormat.ambisonicsOrdering = AmbisonicsOrdering.ACN; ambisonicsFormat.ambisonicsNormalization = AmbisonicsNormalization.N3D; ambisonicsFormat.channelOrder = ChannelOrder.Deinterleaved; var error = PhononCore.iplCreateEnvironmentalRenderer(Context.GetContext(), environment.GetEnvironment(), RenderingSettings(), ambisonicsFormat, ref environmentalRenderer); if (error != Error.None) { throw new Exception("Unable to create environment renderer [" + error.ToString() + "]"); } }
public void BakeEffectThread() { BakingSettings bakeSettings; bakeSettings.bakeConvolution = bakeConvolution ? Bool.True : Bool.False; bakeSettings.bakeParametric = bakeParameteric ? Bool.True : Bool.False; foreach (ProbeBox probeBox in duringBakeProbeBoxes) { if (cancelBake) { return; } IntPtr probeBoxPtr = IntPtr.Zero; try { PhononCore.iplLoadProbeBox(probeBox.probeBoxData, probeBox.probeBoxData.Length, ref probeBoxPtr); probeBoxBakingCurrently++; } catch (Exception e) { Debug.LogError(e.Message); } PhononCore.iplBakeStaticListener(duringBakeEnvComponent.Environment().GetEnvironment(), probeBoxPtr, duringBakeSphere, uniqueIdentifier, bakeSettings, bakeCallback); if (cancelBake) { return; } int probeBoxSize = PhononCore.iplSaveProbeBox(probeBoxPtr, null); probeBox.probeBoxData = new byte[probeBoxSize]; PhononCore.iplSaveProbeBox(probeBoxPtr, probeBox.probeBoxData); int probeBoxEffectSize = PhononCore.iplGetBakedDataSizeByName(probeBoxPtr, bakedListenerPrefix + uniqueIdentifier); probeBox.UpdateProbeDataMapping(bakedListenerPrefix + uniqueIdentifier, probeBoxEffectSize); PhononCore.iplDestroyProbeBox(ref probeBoxPtr); } bakeStatus = BakeStatus.Complete; }
public void DeleteBakedDataByName(string name) { IntPtr probeBox = IntPtr.Zero; try { PhononCore.iplLoadProbeBox(probeBoxData, probeBoxData.Length, ref probeBox); PhononCore.iplDeleteBakedDataByName(probeBox, Common.ConvertString(name)); UpdateProbeDataMapping(name, -1); int probeBoxSize = PhononCore.iplSaveProbeBox(probeBox, null); probeBoxData = new byte[probeBoxSize]; PhononCore.iplSaveProbeBox(probeBox, probeBoxData); } catch (Exception e) { Debug.LogError(e.Message); } }
public void Create(Environment environment, RenderingSettings renderingSettings, GlobalContext globalContext) { HRTFParams hrtfParams = new HRTFParams { type = HRTFDatabaseType.Default, hrtfData = IntPtr.Zero, numHrirSamples = 0, loadCallback = null, unloadCallback = null, lookupCallback = null }; var error = PhononCore.iplCreateBinauralRenderer(globalContext, renderingSettings, hrtfParams, ref binauralRenderer); if (error != Error.None) { throw new Exception("Unable to create binaural renderer [" + error.ToString() + "]"); } }