diff --git a/Assets/HRTFu/Eigen_HRTF.cs b/Assets/HRTFu/Eigen_HRTF.cs index d6a4bc64..143ab9df 100644 --- a/Assets/HRTFu/Eigen_HRTF.cs +++ b/Assets/HRTFu/Eigen_HRTF.cs @@ -6,25 +6,16 @@ namespace Eigen_HRTF_plugin { static class Eigen_HRTF { [DllImport("HRTFu_plugin", EntryPoint = "get_filters", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern void get_filters([In]float d,[In]float e,[In]float a,[In]int pinna, [Out] float[] filter_l, [Out] float[] filter_r,[Out] int[] delays, [Out] int[] idxs); - - [DllImport("HRTFu_plugin", EntryPoint = "get_filters", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern unsafe void get_filters(float d, float e, float a, int pinna, float* filter_l, float* filter_r, int* delays, int* idxs); - - [DllImport("HRTFu_plugin", EntryPoint = "convolution", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern void convolution([Out] float[] signal, [In] float[] filter); - - [DllImport("HRTFu_plugin", EntryPoint = "convolution", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern unsafe void convolution( float* signal, float* filter); + public static extern void get_filters( float d, float e, float a, int pinna, float[] filter_l, float[] filter_r, int[] delays, int[] idxs); [DllImport("HRTFu_plugin", EntryPoint = "eigen_init", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern void eigen_init(); + public static extern void eigen_init([In] [MarshalAs(UnmanagedType.LPStr)] string path); + + [DllImport("HRTFu_plugin", EntryPoint = "full_DSP", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] + public unsafe static extern void full_DSP(float[] data, int data_size, float[] signal_buffer, float[] filter_l, float[] filter_r, float[] buffer_l, float[] buffer_r, int[] delays, float gain); [DllImport("HRTFu_plugin", EntryPoint = "DSP", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern void DSP([Out] float[] data, int data_size,[In] float[] filter_l,[In] float[] filter_r,[Out] float[] buffer_l,[Out] float[] buffer_r,[Out] int[] prev_delays,[Out] int[] delays); + public unsafe static extern void DSP(float[] data, int data_size, float[] filter_l, float[] filter_r, float[] filter_l2, float[] filter_r2, float[] buffer_l, float[] buffer_r, int[] prev_delays, int[] delays, int[] prev_ids, int[] ids, float gain); - [DllImport("HRTFu_plugin", EntryPoint = "DSP_delay", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] - public static extern void DSP_delay([Out] float[] data,[In] int data_size,[In] bool first, [In] float[] filter_l, [In] float[] filter_r, [Out] float[] buffer_signal, [Out] float[] buffer_l, [Out] float[] buffer_r, [Out] int[] delays); - //float* data,int data_size,bool first,float* filter_l,float* filter_r,float* buffer_signal,float* buffer_l,float* buffer_r,int* delays } } diff --git a/Assets/HRTFu/HRTFu.cs b/Assets/HRTFu/HRTFu.cs index 30366bfa..41c2be43 100644 --- a/Assets/HRTFu/HRTFu.cs +++ b/Assets/HRTFu/HRTFu.cs @@ -1,88 +1,59 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; -using UnityEngine.Audio; using Eigen_HRTF_plugin; -using System.Text; -using System.IO; +using UnityEngine.Profiling; public class HRTFu : MonoBehaviour { private float[] filter_l; private float[] filter_r; + private float[] filter_l2; + private float[] filter_r2; private float[] buffer_l; private float[] buffer_r; - private float[] buffer_signal; - private bool first=true; - //private float[][] previous; + //private float[] signal_buffer; private int[] delays; - private int[] prev_delays; - private int[] idxs; - public float distance = 20; - public float elevation = 0; - public float azimuth = 0; + private int[] prev_delays; + private int[] idxs; + private int[] prev_idxs; + private float distance = 0; + private float elevation = 0; + private float azimuth = 0; public float scale = 0.01f; public enum Pinnas : int { Small = 0, Large = 1 }; public Pinnas pinna; public GameObject listener = null; + [Tooltip("Gain in dB. Modification of the gain may change the subjective perception of distance. We strongly recommend do not modify this parameter. Use it at your own discretion. Default value 0 dB")] + public float gain = 0f; private AudioSource audio_source; - private bool _isPlaying=false; - //private List> positions; - List samplesl; - List samplesr; - //Listener object - //public AudioMixerGroup MixerChannel=null; - // Start is called before the first frame update - + private bool _isPlaying = false; + //Listener object + // Start is called before the first frame update void Awake() { - filter_l = new float[1024]; - filter_r = new float[1024]; - buffer_l = new float[1024]; - buffer_r = new float[1024]; - buffer_signal = new float[256]; - samplesl = new List(); - samplesr = new List(); + filter_l = new float[257*2]; + filter_r = new float[257*2]; + filter_l2 = new float[257 * 2]; + filter_r2 = new float[257 * 2]; + buffer_l = new float[589]; + buffer_r = new float[589]; + //signal_buffer = new float[128]; delays = new int[2]; - prev_delays = new int[2]; + prev_delays = new int[2]; idxs = new int[2]; + prev_idxs = new int[2]; idxs[0] = -2; idxs[1] = -2; + prev_idxs[0] = -1; + prev_idxs[1] = -1; delays[0] = 0; delays[1] = 0; prev_delays[0] = 0; - prev_delays[1] = 0; - AudioConfiguration config = AudioSettings.GetConfiguration(); - CheckAudioSource(); - //if (config.dspBufferSize != 256 || config.sampleRate != 48000 || config.speakerMode != AudioSpeakerMode.Stereo) - //{ - // List tmp_status = new List(); - // List tmp_samples = new List(); - // foreach (var i in FindObjectsOfType()) - // { - // tmp_status.Add(i.isPlaying); - // tmp_samples.Add(i.timeSamples); - - // } - // config.dspBufferSize = 256; - // config.sampleRate = 48000; - // config.speakerMode = AudioSpeakerMode.Stereo; - // config.numRealVoices = 512; - // config.numVirtualVoices = 1024; - // AudioSettings.Reset(config); - // //if (tmp) this.GetComponent().Play(); - // int c = 0; - // foreach (var i in FindObjectsOfType()) - // { - // if (tmp_status[c]) { i.PlayDelayed(0.005f); i.timeSamples = tmp_samples[c]; } - // c++; - // } - //} + prev_delays[1] = 0; - //AudioMixerGroup mixer = Instantiate(audio_source.outputAudioMixerGroup); - //mixer.name = "HRTFu[" + this.GetInstanceID().ToString() + "]"; - //audio_source.outputAudioMixerGroup = mixer; + + CheckAudioSource(); if (listener == null) { //Seek audio listeners in scene @@ -98,44 +69,33 @@ void Awake() listener = listeners[0].gameObject; } } - Eigen_HRTF.eigen_init(); - _isPlaying = audio_source.isPlaying; - //positions = new List>(); + Eigen_HRTF.eigen_init(Application.streamingAssetsPath+"/HRTFu/"); + _isPlaying = audio_source.isPlaying; } // Update is called once per frame - void Update() - { - //AudioConfiguration config = AudioSettings.GetConfiguration(); + void LateUpdate() + { + float tmp_d = distance; float tmp_e = elevation; float tmp_a = azimuth; //Calculate distance between listener and sound source distance = Mathf.Abs(Vector3.Distance(transform.position, listener.transform.position)) / scale; //Calculate diretion vector between listener and sound source - Vector3 dir = (transform.position - listener.transform.position).normalized; - //Debug.DrawRay(listener.transform.position,dir*distance,Color.blue,0.1f,false); - //Calculate angle of elevation between listener and sound source - //Vector3 cross = Vector3.Cross(dir, listener.transform.right); + Vector3 dir = (transform.position - listener.transform.position).normalized; + //Calculate angle of elevation between listener and sound source if (Vector3.Cross(listener.transform.right, Vector3.ProjectOnPlane(dir, listener.transform.up)) == Vector3.zero) { Vector3 dirE = Vector3.ProjectOnPlane(dir, listener.transform.forward); elevation = Vector3.SignedAngle(listener.transform.right, dirE, listener.transform.forward); - } - //if (Vector3.Cross(-listener.transform.right, Vector3.ProjectOnPlane(dir, listener.transform.up)) == Vector3.zero) - //{ - // Vector3 dirE = Vector3.ProjectOnPlane(dir, -listener.transform.right); - // elevation = -Vector3.SignedAngle(-listener.transform.right, dirE, listener.transform.forward); - // Debug.LogWarning("2 " + dirE.ToString()); - //} + } else { Vector3 dirE = Vector3.ProjectOnPlane(dir, listener.transform.right); elevation = -Vector3.SignedAngle(listener.transform.forward, dirE, listener.transform.right); - } - //elevation = Mathf.Acos(dir.y) * Mathf.Rad2Deg + 90; - elevation = elevation % 180 == 0 ? 0 : elevation; - //Debug.Log(-Vector3.SignedAngle(listener.transform.forward, dirE, listener.transform.right)); + } + elevation = elevation % 180 == 0 ? 0 : elevation; if (elevation < -90f) { elevation = -90 - (elevation % 90); @@ -150,20 +110,11 @@ void Update() if (azimuth < 0f) { azimuth = 360f + azimuth; - } - ////Debug.Log ("E: "+elevation.ToString()+"\tA: "+azimuth.ToString()); - //Vector3 dir = transform.position- listener.transform.position; - //////////Debug.Log(dir.ToString()+"\t"+idxs[0].ToString() + " " + idxs[1].ToString() + "\tD: " + distance.ToString() + "\tE: " + elevation.ToString() + "\tA: " + azimuth.ToString()); - //azimuth = dir.z!=0?Mathf.Atan(dir.x / dir.z)*Mathf.Rad2Deg:270; - //if (azimuth < 0) azimuth=360 + azimuth; - //elevation = Mathf.Acos(dir.y/distance) * Mathf.Rad2Deg-90; + } _isPlaying = audio_source.isPlaying; if (tmp_d == distance && tmp_e == elevation && tmp_a == azimuth) return; - //previous_delays = delays; Eigen_HRTF.get_filters(distance, elevation, azimuth, (int)pinna, filter_l, filter_r, delays, idxs); - //Debug.Log((int)pinna); - ///positions.Add(new List() {distance,elevation,azimuth,Time.time}); - //Debug.Log(idxs[0].ToString()+" "+idxs[1].ToString()+ "\tE: " + elevation.ToString() + "\tA: " + azimuth.ToString()); + } private void CheckAudioSource() @@ -173,26 +124,8 @@ private void CheckAudioSource() audio_source = this.GetComponent(); } void OnAudioFilterRead(float[] data, int channels) - { - if(_isPlaying) - Eigen_HRTF.DSP(data, data.Length, filter_l, filter_r, buffer_l, buffer_r, prev_delays, delays); - //Eigen_HRTF.DSP_delay(data, data.Length, first, filter_l, filter_r,buffer_signal, buffer_l, buffer_r, delays); - //if (first) first = false; - //for (int i = 0; i < data.Length; i = i + 2) - //{ - // samplesl.Add(data[i]); - // samplesr.Add(data[i + 1]); - //} - } - private void OnDestroy() - { - //AudioClip recordl = AudioClip.Create("output", samplesl.Count, 1, 48000, false); - //AudioClip recordr = AudioClip.Create("output", samplesl.Count, 1, 48000, false); - //recordl.SetData(samplesl.ToArray(), 0); - //recordr.SetData(samplesr.ToArray(), 0); - //SavWav.Save("outputl", recordl); - //SavWav.Save("outputr", recordr); - - } + { + if (_isPlaying) Eigen_HRTF.DSP(data, data.Length, filter_l, filter_r, filter_l2, filter_r2, buffer_l, buffer_r, prev_delays, delays, prev_idxs, idxs, gain); + } } \ No newline at end of file diff --git a/Assets/HRTFu/Plugins/x64/HRTFu_plugin.bundle b/Assets/HRTFu/Plugins/x64/HRTFu_plugin.bundle index b9af0687..441d401e 100755 Binary files a/Assets/HRTFu/Plugins/x64/HRTFu_plugin.bundle and b/Assets/HRTFu/Plugins/x64/HRTFu_plugin.bundle differ diff --git a/Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll b/Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll new file mode 100755 index 00000000..1662fa4a Binary files /dev/null and b/Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll differ diff --git a/Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll.meta b/Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll.meta new file mode 100644 index 00000000..e300a76c --- /dev/null +++ b/Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll.meta @@ -0,0 +1,69 @@ +fileFormatVersion: 2 +guid: 9b9541fc6b7184da6995266a6588d809 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 1 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + : Any + second: + enabled: 0 + settings: + Exclude Editor: 0 + Exclude Linux64: 0 + Exclude OSXUniversal: 0 + Exclude WebGL: 0 + Exclude Win: 0 + Exclude Win64: 0 + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 1 + settings: + CPU: x86_64 + DefaultValueInitialized: true + OS: AnyOS + - first: + Standalone: Linux64 + second: + enabled: 1 + settings: + CPU: AnyCPU + - first: + Standalone: OSXUniversal + second: + enabled: 1 + settings: + CPU: x86_64 + - first: + Standalone: Win + second: + enabled: 1 + settings: + CPU: None + - first: + Standalone: Win64 + second: + enabled: 1 + settings: + CPU: AnyCPU + - first: + WebGL: WebGL + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/HRTFu/Plugins/x64/libfftw3f-3.dll b/Assets/HRTFu/Plugins/x64/libfftw3f-3.dll new file mode 100755 index 00000000..32a3fcf1 Binary files /dev/null and b/Assets/HRTFu/Plugins/x64/libfftw3f-3.dll differ diff --git a/Assets/HRTFu/Plugins/x64/libfftw3f-3.dll.meta b/Assets/HRTFu/Plugins/x64/libfftw3f-3.dll.meta new file mode 100644 index 00000000..ec813e11 --- /dev/null +++ b/Assets/HRTFu/Plugins/x64/libfftw3f-3.dll.meta @@ -0,0 +1,69 @@ +fileFormatVersion: 2 +guid: dcde16fa230fb4ba2933031d226660af +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 1 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + : Any + second: + enabled: 0 + settings: + Exclude Editor: 0 + Exclude Linux64: 0 + Exclude OSXUniversal: 0 + Exclude WebGL: 0 + Exclude Win: 0 + Exclude Win64: 0 + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 1 + settings: + CPU: x86_64 + DefaultValueInitialized: true + OS: AnyOS + - first: + Standalone: Linux64 + second: + enabled: 1 + settings: + CPU: AnyCPU + - first: + Standalone: OSXUniversal + second: + enabled: 1 + settings: + CPU: x86_64 + - first: + Standalone: Win + second: + enabled: 1 + settings: + CPU: None + - first: + Standalone: Win64 + second: + enabled: 1 + settings: + CPU: AnyCPU + - first: + WebGL: WebGL + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/HRTFu.dll b/Assets/Plugins/HRTFu.dll deleted file mode 100644 index cde61978..00000000 Binary files a/Assets/Plugins/HRTFu.dll and /dev/null differ diff --git a/Assets/Plugins/HRTFu.dll.meta b/Assets/Plugins/HRTFu.dll.meta deleted file mode 100644 index 4ebc3fa6..00000000 --- a/Assets/Plugins/HRTFu.dll.meta +++ /dev/null @@ -1,33 +0,0 @@ -fileFormatVersion: 2 -guid: af5f0f0ed28854d138e992012a02dcf7 -PluginImporter: - externalObjects: {} - serializedVersion: 2 - iconMap: {} - executionOrder: {} - defineConstraints: [] - isPreloaded: 0 - isOverridable: 0 - isExplicitlyReferenced: 0 - validateReferences: 1 - platformData: - - first: - Any: - second: - enabled: 1 - settings: {} - - first: - Editor: Editor - second: - enabled: 0 - settings: - DefaultValueInitialized: true - - first: - Windows Store Apps: WindowsStoreApps - second: - enabled: 0 - settings: - CPU: AnyCPU - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Prefabs/Dead.prefab b/Assets/Prefabs/Dead.prefab index 95706b14..51c00838 100644 --- a/Assets/Prefabs/Dead.prefab +++ b/Assets/Prefabs/Dead.prefab @@ -130,10 +130,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 42aa8d3e47e784e579c2974d53743c3d, type: 3} m_Name: m_EditorClassIdentifier: - x: 0 - y: 0 - z: 0 - neighbor: 0 --- !u!82 &156523269763131348 AudioSource: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Normal.unity b/Assets/Scenes/Normal.unity index 62f4eba6..1ba745e3 100644 --- a/Assets/Scenes/Normal.unity +++ b/Assets/Scenes/Normal.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 170076734} - m_IndirectSpecularColor: {r: 0.44657838, g: 0.49641234, b: 0.57481676, a: 1} + m_IndirectSpecularColor: {r: 0.44657874, g: 0.49641275, b: 0.5748172, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: diff --git a/Assets/Plugins.meta b/Assets/StreamingAssets/HRTFu.meta similarity index 77% rename from Assets/Plugins.meta rename to Assets/StreamingAssets/HRTFu.meta index d1921d2a..2909a637 100644 --- a/Assets/Plugins.meta +++ b/Assets/StreamingAssets/HRTFu.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: b353c8a87209949f78e093aa9728e1eb +guid: d91feb493f46044e0acb3de702f84bc7 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/StreamingAssets/HRTFu/Eigen@48000.dat b/Assets/StreamingAssets/HRTFu/Eigen@48000.dat new file mode 100644 index 00000000..d080fbdb Binary files /dev/null and b/Assets/StreamingAssets/HRTFu/Eigen@48000.dat differ diff --git a/Assets/StreamingAssets/HRTFu/Eigen@48000.dat.meta b/Assets/StreamingAssets/HRTFu/Eigen@48000.dat.meta new file mode 100644 index 00000000..9dc42404 --- /dev/null +++ b/Assets/StreamingAssets/HRTFu/Eigen@48000.dat.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2a650f53c8730477fa2744c6c00dcdea +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/StreamingAssets/HRTFu/interpolation.dat b/Assets/StreamingAssets/HRTFu/interpolation.dat new file mode 100755 index 00000000..4eb6aa35 Binary files /dev/null and b/Assets/StreamingAssets/HRTFu/interpolation.dat differ diff --git a/Assets/StreamingAssets/HRTFu/interpolation.dat.meta b/Assets/StreamingAssets/HRTFu/interpolation.dat.meta new file mode 100755 index 00000000..e0717221 --- /dev/null +++ b/Assets/StreamingAssets/HRTFu/interpolation.dat.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2414e208d44cc4ffdbdc128bd10d06b5 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/AudioManager.asset b/ProjectSettings/AudioManager.asset index 20b24af5..9b4e2611 100644 --- a/ProjectSettings/AudioManager.asset +++ b/ProjectSettings/AudioManager.asset @@ -9,11 +9,11 @@ AudioManager: Doppler Factor: 1 Default Speaker Mode: 2 m_SampleRate: 0 - m_DSPBufferSize: 256 - m_VirtualVoiceCount: 512 - m_RealVoiceCount: 32 + m_DSPBufferSize: 1024 + m_VirtualVoiceCount: 255 + m_RealVoiceCount: 255 m_SpatializerPlugin: m_AmbisonicDecoderPlugin: m_DisableAudio: 0 m_VirtualizeEffects: 1 - m_RequestedDSPBufferSize: 256 + m_RequestedDSPBufferSize: 1024