Skip to content

Commit e8a237f

Browse files
AR for eyewear devices
1 parent 47c4dee commit e8a237f

File tree

1,381 files changed

+162801
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,381 files changed

+162801
-0
lines changed

Voice-over-vuzix/.DS_Store

6 KB
Binary file not shown.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class AudioVisualizer : MonoBehaviour
6+
{
7+
private const int SAMPLE_SIZE = 1024;
8+
public float visualModifier = 50.0f;
9+
public float smoothSpeed = 10.0f;
10+
public float maxVisualScale = 25.0f;
11+
public float keePercentage = 0.5f;
12+
public float rmsValue;
13+
public float dbValue;
14+
public float pitchValue;
15+
private AudioSource source;
16+
private float[] samples;
17+
private float[] spectrum;
18+
private float sampleRate;
19+
20+
private Transform[] visualList;
21+
private float[] visualScale;
22+
private int amnVisual = 64;
23+
24+
25+
private void Start()
26+
{
27+
{
28+
//Assuming audio source on top of the object
29+
source = GetComponent<AudioSource>();
30+
samples = new float[1024];
31+
spectrum = new float[1024];
32+
sampleRate = AudioSettings.outputSampleRate;
33+
SpawnLine();
34+
}
35+
}
36+
37+
private void SpawnLine()
38+
{
39+
visualScale = new float[amnVisual];
40+
visualList = new Transform[amnVisual];
41+
for (int i = 0;i< amnVisual; i++)
42+
{
43+
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube) as GameObject;
44+
visualList[i] = go.transform;
45+
visualList[i].position = Vector3.right * i;
46+
47+
}
48+
}
49+
50+
private void Update()
51+
{
52+
AnalyzeSound();
53+
UpdateVisual();
54+
}
55+
56+
private void UpdateVisual()
57+
{
58+
int visualIndex = 0;
59+
int spectrumIndex = 0;
60+
int averageSize = (int)((SAMPLE_SIZE * keePercentage) / amnVisual);
61+
62+
while(visualIndex < amnVisual)
63+
{
64+
int j = 0;
65+
float sum = 0;
66+
while(j < averageSize)
67+
{
68+
sum += spectrum[spectrumIndex];
69+
spectrumIndex++;
70+
j++;
71+
}
72+
float scaleY = sum / averageSize * visualModifier;
73+
visualScale[visualIndex] -= Time.deltaTime * smoothSpeed;
74+
if (visualScale[visualIndex] < scaleY)
75+
visualScale[visualIndex] = scaleY;
76+
visualList[visualIndex].localScale = Vector3.one + Vector3.up * visualScale[visualIndex];
77+
visualIndex++;
78+
79+
if (visualScale[visualIndex] > maxVisualScale)
80+
visualScale[visualIndex] = maxVisualScale;
81+
82+
}
83+
84+
}
85+
86+
private void AnalyzeSound()
87+
{
88+
source.GetOutputData(samples, 0);
89+
// Fetching RMS Value
90+
91+
int i = 0;
92+
float sum = 0;
93+
for (;i<SAMPLE_SIZE;i++)
94+
{
95+
sum = samples[i] * samples[i];
96+
}
97+
98+
rmsValue = Mathf.Sqrt(sum / SAMPLE_SIZE);
99+
//Get db Value
100+
dbValue = 20 * Mathf.Log10(rmsValue / 0.1f);
101+
102+
//Fetching Sound spextrum
103+
source.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
104+
105+
//Finding pitch
106+
float maxV = 0;
107+
var maxN = 0;
108+
for (i =0; i<SAMPLE_SIZE; i++)
109+
{
110+
if (!(spectrum[i] > maxV) || !(spectrum[i] > 0.0f))
111+
continue;
112+
maxV = spectrum[i];
113+
maxN = i;
114+
115+
}
116+
117+
float freqN = maxN;
118+
if(maxN > 0 && maxN < SAMPLE_SIZE - 1)
119+
{
120+
var dL = spectrum[maxN - 1] / spectrum[maxN];
121+
var dR = spectrum[maxN + 1] / spectrum[maxN];
122+
freqN += 0.5f * (dR * dR - dL * dL);
123+
}
124+
pitchValue = freqN * (sampleRate / 2) / SAMPLE_SIZE;
125+
}
126+
}

Voice-over-vuzix/Speech to text/Assets/AudioVisualizer.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)