Skip to content

Commit

Permalink
HRTFu V2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
arevaloarboled committed Jul 28, 2021
1 parent f53c74a commit 382e5e6
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 170 deletions.
21 changes: 6 additions & 15 deletions Assets/HRTFu/Eigen_HRTF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
157 changes: 45 additions & 112 deletions Assets/HRTFu/HRTFu.cs
Original file line number Diff line number Diff line change
@@ -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<List<float>> positions;
List<float> samplesl;
List<float> 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<float>();
samplesr = new List<float>();
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<bool> tmp_status = new List<bool>();
// List<int> tmp_samples = new List<int>();
// foreach (var i in FindObjectsOfType<AudioSource>())
// {
// 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<AudioSource>().Play();
// int c = 0;
// foreach (var i in FindObjectsOfType<AudioSource>())
// {
// 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
Expand All @@ -98,44 +69,33 @@ void Awake()
listener = listeners[0].gameObject;
}
}
Eigen_HRTF.eigen_init();
_isPlaying = audio_source.isPlaying;
//positions = new List<List<float>>();
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);
Expand All @@ -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<float>() {distance,elevation,azimuth,Time.time});
//Debug.Log(idxs[0].ToString()+" "+idxs[1].ToString()+ "\tE: " + elevation.ToString() + "\tA: " + azimuth.ToString());

}

private void CheckAudioSource()
Expand All @@ -173,26 +124,8 @@ private void CheckAudioSource()
audio_source = this.GetComponent<AudioSource>();
}
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);
}

}
Binary file modified Assets/HRTFu/Plugins/x64/HRTFu_plugin.bundle
Binary file not shown.
Binary file added Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll
Binary file not shown.
69 changes: 69 additions & 0 deletions Assets/HRTFu/Plugins/x64/HRTFu_plugin.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/HRTFu/Plugins/x64/libfftw3f-3.dll
Binary file not shown.
69 changes: 69 additions & 0 deletions Assets/HRTFu/Plugins/x64/libfftw3f-3.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed Assets/Plugins/HRTFu.dll
Binary file not shown.
Loading

0 comments on commit 382e5e6

Please sign in to comment.