Skip to content

Commit 2d26150

Browse files
authored
Merge pull request #3 from voxell-tech/develop
v1.2.1
2 parents 06f97fb + 21ed6b3 commit 2d26150

24 files changed

+10533
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ crashlytics-build.properties
101101
AudioClips/
102102
*.mp3
103103
*.wav
104-
*.audio
105104

106105
# license
107106
LICENSE.meta

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [1.2.1]
2+
3+
### Changes
4+
5+
Add in VX.Audio (remove *.audio from `.gitignore`).
6+
17
## [1.2.0]
28

39
### Changes

VX.Audio/AudioCore.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
This program is free software; you can redistribute it and/or
3+
modify it under the terms of the GNU General Public License
4+
as published by the Free Software Foundation; either version 2
5+
of the License, or (at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software Foundation,
14+
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15+
16+
The Original Code is Copyright (C) 2020 Voxell Technologies.
17+
All rights reserved.
18+
*/
19+
20+
using UnityEngine;
21+
using UnityEngine.VFX;
22+
23+
namespace Voxell.Audio
24+
{
25+
[RequireComponent(typeof(VisualEffect))]
26+
[RequireComponent(typeof(AudioSource))]
27+
public sealed partial class AudioCore : MonoBehaviour
28+
{
29+
private const float EPSILON = 0.001f;
30+
31+
void Start()
32+
{
33+
InitAgentInteraction();
34+
InitAudioVisualizer();
35+
}
36+
37+
void Update() => UpdateAgentInteraction();
38+
void FixedUpdate() => UpdateAudioVisualizer();
39+
40+
internal static class ShaderPropertyId
41+
{
42+
public static readonly int sampleCount = Shader.PropertyToID("sampleCount");
43+
public static readonly int transform = Shader.PropertyToID("transform");
44+
public static readonly int oldTransform = Shader.PropertyToID("oldTransform");
45+
public static readonly int framRate = Shader.PropertyToID("framRate");
46+
}
47+
48+
internal static class ShaderBufferId
49+
{
50+
public static readonly int cb_samplePoints = Shader.PropertyToID("cb_samplePoints");
51+
public static readonly int cb_position = Shader.PropertyToID("cb_position");
52+
public static readonly int cb_oldPosition = Shader.PropertyToID("cb_oldPosition");
53+
public static readonly int cb_normal = Shader.PropertyToID("cb_normal;");
54+
public static readonly int tex_positionMap = Shader.PropertyToID("tex_positionMap");
55+
public static readonly int tex_velocityMap = Shader.PropertyToID("tex_velocityMap");
56+
public static readonly int tex_normalMap = Shader.PropertyToID("tex_normalMap");
57+
}
58+
59+
internal static class VFXPropertyId
60+
{
61+
public static readonly int mesh_sampleMesh = Shader.PropertyToID("SampleMesh");
62+
public static readonly int float_forceMultiplier = Shader.PropertyToID("ForceMultiplier");
63+
public static readonly int int_triangleCount = Shader.PropertyToID("TriangleCount");
64+
public static readonly int float_idleNoiseSpeed = Shader.PropertyToID("IdleNoiseSpeed");
65+
public static readonly int float_noiseIntensity = Shader.PropertyToID("NoiseIntensity");
66+
public static readonly int float3_noiseScale = Shader.PropertyToID("NoiseScale");
67+
}
68+
}
69+
}

VX.Audio/AudioCore.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VX.Audio/AudioInteraction.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
This program is free software; you can redistribute it and/or
3+
modify it under the terms of the GNU General Public License
4+
as published by the Free Software Foundation; either version 2
5+
of the License, or (at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software Foundation,
14+
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15+
16+
The Original Code is Copyright (C) 2020 Voxell Technologies.
17+
All rights reserved.
18+
*/
19+
20+
using UnityEngine;
21+
22+
namespace Voxell.Audio
23+
{
24+
public partial class AudioCore
25+
{
26+
[Header("Audio Visualizer Interaction")]
27+
[Tooltip("Velocity of audio visualizer when there is no interaction")]
28+
public Vector2 idleVelocity = new Vector2(1.0f, -1.0f);
29+
30+
[Tooltip("Minimum turbulence for VFX graph when there is no interaction or extra force")]
31+
public float idleNoiseIntensity = 0.1f;
32+
public float maxNoiseIntensity = 0.3f;
33+
34+
[Tooltip("Sensitivity of the audio visualizer on mouse drag")]
35+
public float rotationMultiplier = 500;
36+
37+
[Range(0.8f, 0.99f), Tooltip("A number that multiplies the velocity of the audio visualizer on each update")]
38+
public float velocityDamping = 0.9f;
39+
40+
[Tooltip("Multiplier of noise intensity before sending it to the VFX graph")]
41+
public float intensityCoefficient = 0.1f;
42+
43+
private Vector2 _rotationVelocity;
44+
45+
private void InitAgentInteraction() => _rotationVelocity = Vector2.zero;
46+
47+
private void UpdateAgentInteraction()
48+
{
49+
// if (Input.GetMouseButton(0)) OnMouseDrag();
50+
51+
if (Vector3.Dot(transform.up, Vector3.up) >= 0)
52+
transform.Rotate(Camera.main.transform.up, -Vector3.Dot(_rotationVelocity, Camera.main.transform.right), Space.World);
53+
else
54+
transform.Rotate(Camera.main.transform.up, -Vector3.Dot(_rotationVelocity, Camera.main.transform.right), Space.World);
55+
56+
transform.Rotate(Camera.main.transform.right, Vector3.Dot(_rotationVelocity, Camera.main.transform.up), Space.World);
57+
_rotationVelocity *= velocityDamping;
58+
_rotationVelocity += idleVelocity*Time.deltaTime;
59+
60+
if (_rotationVelocity.magnitude <= EPSILON) _rotationVelocity = Vector2.zero;
61+
62+
float intensity = Mathf.Clamp(_rotationVelocity.magnitude * intensityCoefficient, idleNoiseIntensity, maxNoiseIntensity);
63+
audioVFX.SetFloat(VFXPropertyId.float_noiseIntensity, intensity);
64+
}
65+
66+
/// <summary>
67+
/// Calcualte mouse drag force and apply rotational force accordingly
68+
/// </summary>
69+
private void OnMouseDrag()
70+
{
71+
float rotationX = Input.GetAxis("Mouse X")*rotationMultiplier*Mathf.Deg2Rad;
72+
float rotationY = Input.GetAxis("Mouse Y")*rotationMultiplier*Mathf.Deg2Rad;
73+
74+
_rotationVelocity = new Vector2(rotationX, rotationY);
75+
}
76+
}
77+
}

VX.Audio/AudioInteraction.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VX.Audio/AudioProcessor.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This program is free software; you can redistribute it and/or
3+
modify it under the terms of the GNU General Public License
4+
as published by the Free Software Foundation; either version 2
5+
of the License, or (at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software Foundation,
14+
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15+
16+
The Original Code is Copyright (C) 2020 Voxell Technologies.
17+
All rights reserved.
18+
*/
19+
20+
using UnityEngine;
21+
22+
namespace Voxell.Audio
23+
{
24+
public class AudioProcessor
25+
{
26+
public AudioSource source;
27+
public AudioProfile profile;
28+
29+
public float[] samples;
30+
public int freqSize;
31+
public int bandAverage;
32+
public int[] bandDistribution;
33+
34+
public AudioProcessor(ref AudioSource source, ref AudioProfile profile)
35+
{
36+
Debug.Assert(profile.frequencyRange > profile.bandSize, "Number of triangles should not exceed frequency range.");
37+
this.source = source;
38+
this.profile = profile;
39+
40+
samples = new float[profile.sampleSize];
41+
42+
float freqInterval = AudioSettings.outputSampleRate/profile.sampleSize;
43+
float currFreq = 0.0f;
44+
freqSize = 0;
45+
46+
for (int s=0; s < profile.sampleSize; s++)
47+
{
48+
currFreq += freqInterval;
49+
if (currFreq < profile.frequencyRange) freqSize ++;
50+
else break;
51+
}
52+
53+
bandAverage = Mathf.Max(1, freqSize/profile.bandSize);
54+
55+
bandDistribution = new int[profile.bandSize+1];
56+
bandDistribution[0] = 0;
57+
for (int b=0; b < profile.bandSize; b++)
58+
bandDistribution[b+1] = bandAverage + b*bandAverage;
59+
}
60+
61+
public void SampleSpectrum() => source.GetSpectrumData(samples, profile.channel, profile.window);
62+
}
63+
}

VX.Audio/AudioProcessor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VX.Audio/AudioProfile.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
This program is free software; you can redistribute it and/or
3+
modify it under the terms of the GNU General Public License
4+
as published by the Free Software Foundation; either version 2
5+
of the License, or (at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software Foundation,
14+
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15+
16+
The Original Code is Copyright (C) 2020 Voxell Technologies.
17+
All rights reserved.
18+
*/
19+
20+
using UnityEngine;
21+
22+
namespace Voxell.Audio
23+
{
24+
[System.Serializable]
25+
public class AudioProfile
26+
{
27+
public int channel;
28+
public FFTWindow window;
29+
public int sampleSize;
30+
public float power;
31+
public float scale;
32+
public int smoothingIterations;
33+
[Range(5000, 20000)] public int frequencyRange;
34+
[HideInInspector] public int bandSize;
35+
}
36+
}

VX.Audio/AudioProfile.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)