-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeatMapping.cs
182 lines (167 loc) · 6.47 KB
/
BeatMapping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EssentialMechanics.Sound
{
public class BeatMapping
{
public enum samplesCountPresets { _256 = 256, _512 = 512, _1024 = 1024, _2048 = 2048 };
public struct Note
{
public string name;
public float frequency;
public int multiplier;
}
Note createNote(string name, float frequency, int multiplier = 0)
{
Note note = new Note();
note.name = name;
note.frequency = frequency;
note.multiplier = multiplier;
return note;
}
float[] getSamples(AudioSource audioSource, int samplesCount)
{
return audioSource.GetOutputData(samplesCount,0);
}
float getMaxFrequency()
{
return (float)AudioSettings.outputSampleRate / 2f;
}
float getCurrentFrequency(float[] Samples, int samplesCount, float maxFrequency)
{
float maxAmplitude = 0;
int index = 0;
for(int i = 0; i< samplesCount; i++)
{
if (Samples[i] > maxAmplitude)
{
maxAmplitude = Samples[i];
index = i;
}
}
float frequencyIndex = index;
if (maxAmplitude > 0 && maxAmplitude < (int)samplesCount - 1)
{
float dL = Samples[index - 1] / Samples[index];
float dR = Samples[index + 1] / Samples[index];
frequencyIndex += 0.5f * (dR * dR - dL * dL);
}
return frequencyIndex * (maxFrequency / 2) / (int)samplesCount;
}
List<Note> getStandardNotes()
{
Note C = createNote("C", 16.35f);
Note Cs = createNote("C#", 17.32f); ;
Note D = createNote("D", 18.35f);
Note Eb = createNote("Eb", 19.45f);
Note E = createNote("E", 20.6f);
Note F = createNote("F", 21.83f);
Note Fs = createNote("F#", 23.12f);
Note G = createNote("G", 24.5f);
Note Gs = createNote("G#", 25.96f);
Note A = createNote("A", 27.5f);
Note Bb = createNote("Bb", 29.14f);
Note B = createNote("B", 30.87f);
List<Note> Notes = new List<Note>();
Notes.Add(C); Notes.Add(Cs); Notes.Add(D); Notes.Add(Eb); Notes.Add(E); Notes.Add(F);
Notes.Add(Fs); Notes.Add(G); Notes.Add(Gs); Notes.Add(A); Notes.Add(Bb); Notes.Add(B);
return Notes;
}
List<List<int>> getAudioToneChannels(int samplesCount)
{
List<List<int>> returnList = new List<List<int>>();
List<int> SubBass = new List<int>();//List Index = 0
List<int> Bass = new List<int>();//List Index = 1
List<int> LowMiddle = new List<int>();//List Index = 2
List<int> Middle = new List<int>();//List Index = 3
List<int> HighMiddle = new List<int>();//List Index = 4
List<int> High = new List<int>();//List Index = 5
int hertzPerBin = (int)getMaxFrequency() / (int)samplesCount;
for (int i = 0; i < (int)samplesCount / hertzPerBin; i++)
{
if ((i * hertzPerBin) < 63)
SubBass.Add(i);
if ((i * hertzPerBin) > 63 && (i * hertzPerBin) < 250)
Bass.Add(i);
if ((i * hertzPerBin) > 250 && (i * hertzPerBin) < 640)
LowMiddle.Add(i);
if ((i * hertzPerBin) > 640 && (i * hertzPerBin) < 2500)
Middle.Add(i);
if ((i * hertzPerBin) > 2500 && (i * hertzPerBin) < 5000)
HighMiddle.Add(i);
if ((i * hertzPerBin) > 5000)
High.Add(i);
}
returnList.Add(SubBass);
returnList.Add(Bass);
returnList.Add(LowMiddle);
returnList.Add(Middle);
returnList.Add(HighMiddle);
returnList.Add(High);
return returnList;
}
List<float> createAudioHistory(int samplesCount, float[] samples)
{
List<float> History = new List<float>();
List<List<int>> AudioToneChannels = getAudioToneChannels(samplesCount);
for(int i = 0; i < AudioToneChannels.Count; i++)
{
int g = 0;
float average = 0;
int index = AudioToneChannels[i][g];
for(int e = 0; e < samples.Length; e++)
{
if(e == index)
{
average += samples[e];
g++;
index = AudioToneChannels[i][g];
}
}
average /= g;
History.Add(average);
}
return History;
}
bool fullBeatDetector(float[] history, float[] oldhistory, float tolerance = 0)
{
float cteOldHistory = 0;
float cteHistory = 0;
foreach (float i in oldhistory)
cteOldHistory += i;
foreach (float i in history)
cteHistory += i;
if ((cteHistory / history.Length - cteOldHistory / oldhistory.Length) > tolerance)
return true;
else
return false;
}
bool beatDetector(float[] history, float[] oldhistory, int index, float tolerance = 0)
{
float cteOldHistory = oldhistory[index];
float cteHistory = history[index];
if ((cteHistory - cteOldHistory) > tolerance)
return true;
else
return false;
}
Note getAudioNote(float frequency)
{
Note nearNote = createNote("", 0f);
List<Note> Notes = getStandardNotes();
for (int i = 0; i < Notes.Count; i++)
for (int j = 1; j < 8; j++)
{
if (Mathf.Abs(frequency - nearNote.frequency) > Mathf.Abs(frequency - Notes[i].frequency * j) || nearNote.frequency == 0)
{
nearNote = createNote(Notes[i].name, Notes[i].frequency * j, j);
}
}
if (Mathf.Abs(frequency - nearNote.frequency) < 2)
return nearNote;
else
return createNote("", 0f);
}
}
}