Skip to content

Commit 3990b63

Browse files
committed
- fixed audio visualizer not moving when frequency range is fairly low
- prevent audio visualizer from running when triangle count of the mesh is higher than frequency range - reset package to v1.0.0, added CHANGELOG.md
1 parent 9292667 commit 3990b63

File tree

9 files changed

+28
-105
lines changed

9 files changed

+28
-105
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [1.0.0]
2+
3+
- Initial release.

CHANGELOG.md.meta

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

Runtime/AudioCore.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ void Start()
3434
InitAudioVisualizer();
3535
}
3636

37-
void Update()
38-
{
39-
UpdateAgentInteraction();
40-
UpdateAudioVisualizer();
41-
}
37+
void Update() => UpdateAgentInteraction();
38+
void FixedUpdate() => UpdateAudioVisualizer();
4239

4340
internal static class ShaderPropertyId
4441
{

Runtime/AudioProcessor.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@ public class AudioProcessor
2525
{
2626
public AudioSource source;
2727
public AudioProfile profile;
28-
public AudioProcessor(ref AudioSource source, ref AudioProfile profile)
29-
{
30-
this.source = source;
31-
this.profile = profile;
32-
33-
Init();
34-
}
3528

3629
public float[] samples;
3730
public int freqSize;
3831
public int bandAverage;
3932
public int[] bandDistribution;
4033

41-
public void Init()
34+
public AudioProcessor(ref AudioSource source, ref AudioProfile profile)
4235
{
36+
Debug.Assert(profile.frequencyRange > profile.bandSize, "Number of triangles should not exceed frequency range.");
37+
this.source = source;
38+
this.profile = profile;
39+
4340
samples = new float[profile.sampleSize];
4441

4542
float freqInterval = AudioSettings.outputSampleRate/profile.sampleSize;
@@ -49,16 +46,16 @@ public void Init()
4946
for (int s=0; s < profile.sampleSize; s++)
5047
{
5148
currFreq += freqInterval;
52-
if (currFreq < profile.freqRange) freqSize ++;
49+
if (currFreq < profile.frequencyRange) freqSize ++;
5350
else break;
5451
}
5552

56-
bandAverage = freqSize/profile.bandSize;
53+
bandAverage = Mathf.Max(1, freqSize/profile.bandSize);
5754

5855
bandDistribution = new int[profile.bandSize+1];
5956
bandDistribution[0] = 0;
60-
for (int b=1; b < profile.bandSize+1; b++)
61-
bandDistribution[b] = bandAverage + b*bandAverage;
57+
for (int b=0; b < profile.bandSize; b++)
58+
bandDistribution[b+1] = bandAverage + b*bandAverage;
6259
}
6360

6461
public void SampleSpectrum() => source.GetSpectrumData(samples, profile.channel, profile.window);

Runtime/AudioProfile.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public class AudioProfile
2929
public int sampleSize;
3030
public float power;
3131
public float scale;
32-
public float sensitivity;
3332
public int smoothingIterations;
34-
[Range(5000, 20000)] public int freqRange;
33+
[Range(5000, 20000)] public int frequencyRange;
3534
[HideInInspector] public int bandSize;
3635
}
3736
}

Runtime/AudioVisualizer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ private void InitAudioVisualizer()
8080
// audio processing attributes
8181
samples = new NativeArray<float>(audioProfile.sampleSize, Allocator.Persistent);
8282
bandDistribution = new NativeArray<int>(audioProfile.bandSize+1, Allocator.Persistent);
83+
bandDistribution.CopyFrom(audioProcessor.bandDistribution);
84+
bandDistribution.AsReadOnly();
8385

8486
prevBands = new NativeArray<float>(totalTriangles, Allocator.Persistent);
85-
MathUtil.SetNativeArray<float>(ref prevBands, 0);
87+
prevBands.CopyFrom(prevBands);
8688

8789
bandVelocities = new NativeArray<float>(totalTriangles, Allocator.Persistent);
88-
MathUtil.SetNativeArray<float>(ref bandVelocities, 0);
90+
bandVelocities.CopyFrom(bandVelocities);
8991

90-
MathUtil.CopyToNativeArray<int>(ref audioProcessor.bandDistribution, ref bandDistribution);
91-
bandDistribution.AsReadOnly();
9292
sampleMeshData.Dispose();
9393
}
9494

@@ -97,7 +97,7 @@ private void UpdateAudioVisualizer()
9797
audioProcessor.SampleSpectrum();
9898

9999
// copy audio samples to native array samples
100-
MathUtil.CopyToNativeArray<float>(ref audioProcessor.samples, ref samples);
100+
samples.CopyFrom(audioProcessor.samples);
101101

102102
AudioMeshVisualizer audioMeshVisualizer = new AudioMeshVisualizer
103103
{

Runtime/SpectrumSmoother.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

Runtime/SpectrumSmoother.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"license": "GPL v3.0",
1212
"unity": "2021.1",
1313
"unityRelease": "0f1",
14-
"version": "1.1.1",
14+
"version": "1.0.0",
1515
"dependencies": {
1616
"voxell.util": "1.1.0",
1717
"com.unity.visualeffectgraph": "10.3.2"

0 commit comments

Comments
 (0)