Skip to content

Commit

Permalink
Merge pull request #3 from voxell-tech/develop
Browse files Browse the repository at this point in the history
v1.2.1
  • Loading branch information
nixon-voxell committed Nov 1, 2022
2 parents 06f97fb + 21ed6b3 commit 2d26150
Show file tree
Hide file tree
Showing 24 changed files with 10,533 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ crashlytics-build.properties
AudioClips/
*.mp3
*.wav
*.audio

# license
LICENSE.meta
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [1.2.1]

### Changes

Add in VX.Audio (remove *.audio from `.gitignore`).

## [1.2.0]

### Changes
Expand Down
69 changes: 69 additions & 0 deletions VX.Audio/AudioCore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The Original Code is Copyright (C) 2020 Voxell Technologies.
All rights reserved.
*/

using UnityEngine;
using UnityEngine.VFX;

namespace Voxell.Audio
{
[RequireComponent(typeof(VisualEffect))]
[RequireComponent(typeof(AudioSource))]
public sealed partial class AudioCore : MonoBehaviour
{
private const float EPSILON = 0.001f;

void Start()
{
InitAgentInteraction();
InitAudioVisualizer();
}

void Update() => UpdateAgentInteraction();
void FixedUpdate() => UpdateAudioVisualizer();

internal static class ShaderPropertyId
{
public static readonly int sampleCount = Shader.PropertyToID("sampleCount");
public static readonly int transform = Shader.PropertyToID("transform");
public static readonly int oldTransform = Shader.PropertyToID("oldTransform");
public static readonly int framRate = Shader.PropertyToID("framRate");
}

internal static class ShaderBufferId
{
public static readonly int cb_samplePoints = Shader.PropertyToID("cb_samplePoints");
public static readonly int cb_position = Shader.PropertyToID("cb_position");
public static readonly int cb_oldPosition = Shader.PropertyToID("cb_oldPosition");
public static readonly int cb_normal = Shader.PropertyToID("cb_normal;");
public static readonly int tex_positionMap = Shader.PropertyToID("tex_positionMap");
public static readonly int tex_velocityMap = Shader.PropertyToID("tex_velocityMap");
public static readonly int tex_normalMap = Shader.PropertyToID("tex_normalMap");
}

internal static class VFXPropertyId
{
public static readonly int mesh_sampleMesh = Shader.PropertyToID("SampleMesh");
public static readonly int float_forceMultiplier = Shader.PropertyToID("ForceMultiplier");
public static readonly int int_triangleCount = Shader.PropertyToID("TriangleCount");
public static readonly int float_idleNoiseSpeed = Shader.PropertyToID("IdleNoiseSpeed");
public static readonly int float_noiseIntensity = Shader.PropertyToID("NoiseIntensity");
public static readonly int float3_noiseScale = Shader.PropertyToID("NoiseScale");
}
}
}
11 changes: 11 additions & 0 deletions VX.Audio/AudioCore.cs.meta

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

77 changes: 77 additions & 0 deletions VX.Audio/AudioInteraction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The Original Code is Copyright (C) 2020 Voxell Technologies.
All rights reserved.
*/

using UnityEngine;

namespace Voxell.Audio
{
public partial class AudioCore
{
[Header("Audio Visualizer Interaction")]
[Tooltip("Velocity of audio visualizer when there is no interaction")]
public Vector2 idleVelocity = new Vector2(1.0f, -1.0f);

[Tooltip("Minimum turbulence for VFX graph when there is no interaction or extra force")]
public float idleNoiseIntensity = 0.1f;
public float maxNoiseIntensity = 0.3f;

[Tooltip("Sensitivity of the audio visualizer on mouse drag")]
public float rotationMultiplier = 500;

[Range(0.8f, 0.99f), Tooltip("A number that multiplies the velocity of the audio visualizer on each update")]
public float velocityDamping = 0.9f;

[Tooltip("Multiplier of noise intensity before sending it to the VFX graph")]
public float intensityCoefficient = 0.1f;

private Vector2 _rotationVelocity;

private void InitAgentInteraction() => _rotationVelocity = Vector2.zero;

private void UpdateAgentInteraction()
{
// if (Input.GetMouseButton(0)) OnMouseDrag();

if (Vector3.Dot(transform.up, Vector3.up) >= 0)
transform.Rotate(Camera.main.transform.up, -Vector3.Dot(_rotationVelocity, Camera.main.transform.right), Space.World);
else
transform.Rotate(Camera.main.transform.up, -Vector3.Dot(_rotationVelocity, Camera.main.transform.right), Space.World);

transform.Rotate(Camera.main.transform.right, Vector3.Dot(_rotationVelocity, Camera.main.transform.up), Space.World);
_rotationVelocity *= velocityDamping;
_rotationVelocity += idleVelocity*Time.deltaTime;

if (_rotationVelocity.magnitude <= EPSILON) _rotationVelocity = Vector2.zero;

float intensity = Mathf.Clamp(_rotationVelocity.magnitude * intensityCoefficient, idleNoiseIntensity, maxNoiseIntensity);
audioVFX.SetFloat(VFXPropertyId.float_noiseIntensity, intensity);
}

/// <summary>
/// Calcualte mouse drag force and apply rotational force accordingly
/// </summary>
private void OnMouseDrag()
{
float rotationX = Input.GetAxis("Mouse X")*rotationMultiplier*Mathf.Deg2Rad;
float rotationY = Input.GetAxis("Mouse Y")*rotationMultiplier*Mathf.Deg2Rad;

_rotationVelocity = new Vector2(rotationX, rotationY);
}
}
}
11 changes: 11 additions & 0 deletions VX.Audio/AudioInteraction.cs.meta

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

63 changes: 63 additions & 0 deletions VX.Audio/AudioProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The Original Code is Copyright (C) 2020 Voxell Technologies.
All rights reserved.
*/

using UnityEngine;

namespace Voxell.Audio
{
public class AudioProcessor
{
public AudioSource source;
public AudioProfile profile;

public float[] samples;
public int freqSize;
public int bandAverage;
public int[] bandDistribution;

public AudioProcessor(ref AudioSource source, ref AudioProfile profile)
{
Debug.Assert(profile.frequencyRange > profile.bandSize, "Number of triangles should not exceed frequency range.");
this.source = source;
this.profile = profile;

samples = new float[profile.sampleSize];

float freqInterval = AudioSettings.outputSampleRate/profile.sampleSize;
float currFreq = 0.0f;
freqSize = 0;

for (int s=0; s < profile.sampleSize; s++)
{
currFreq += freqInterval;
if (currFreq < profile.frequencyRange) freqSize ++;
else break;
}

bandAverage = Mathf.Max(1, freqSize/profile.bandSize);

bandDistribution = new int[profile.bandSize+1];
bandDistribution[0] = 0;
for (int b=0; b < profile.bandSize; b++)
bandDistribution[b+1] = bandAverage + b*bandAverage;
}

public void SampleSpectrum() => source.GetSpectrumData(samples, profile.channel, profile.window);
}
}
11 changes: 11 additions & 0 deletions VX.Audio/AudioProcessor.cs.meta

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

36 changes: 36 additions & 0 deletions VX.Audio/AudioProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The Original Code is Copyright (C) 2020 Voxell Technologies.
All rights reserved.
*/

using UnityEngine;

namespace Voxell.Audio
{
[System.Serializable]
public class AudioProfile
{
public int channel;
public FFTWindow window;
public int sampleSize;
public float power;
public float scale;
public int smoothingIterations;
[Range(5000, 20000)] public int frequencyRange;
[HideInInspector] public int bandSize;
}
}
11 changes: 11 additions & 0 deletions VX.Audio/AudioProfile.cs.meta

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

Loading

0 comments on commit 2d26150

Please sign in to comment.