/
beatDetect.cpp
178 lines (153 loc) · 5.98 KB
/
beatDetect.cpp
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
#include "beatDetect.h"
#include "fft.h"
// FFT BIN = 256
// サブバンド数 = FFT_SUBBANDS 32 -> subbands
// ヒストリ数 = ENERGY_HISTORY 43 -> energyHistory
int fft_size=512;
int buffer_size = 1024;
beatDetect::beatDetect()
{
//int fft_size=512;
//int buffer_size = 1024;
for(int i = 0; i < fft_size; i++)
fftSmoothed[i] = 0;
// 各周波数サブバンド32個には43個のエネルギーヒストリが含まれている
for(int i = 0; i < FFT_SUBBANDS; i++)
{
for(int l = 0; l < ENERGY_HISTORY; l++){
energyHistory[i][l] = 0;
}
fftSubbands[i] = 0;
averageEnergy[i] = 0;
fftVariance[i] = 0;
beatValueArray[i] = 0;
}
// fftオブジェクト用の配列を宣言
audio_input = new float[buffer_size];
magnitude = new float[fft_size];
phase = new float[fft_size];
power = new float[fft_size];
magnitude_average = new float[fft_size]; //FFTの配列の平均
magnitude_average_snapshot = new float[fft_size];
for (int i = 0; i < fft_size; i++) {
magnitude[i] = 0;
phase[i] = 0;
power[i] = 0;
magnitude_average_snapshot[i] = 0;
magnitude_average[i] = 0;
}
historyPos = 0;
fftInit = true;
beatValue = 1.08;
enableBeatDetect();
printf("beatDetect setup OK!! \n");
}
//void beatDetect::updateFFT(float* in_fft, int infft_size)
void beatDetect::updateFFT()
{
if(fftInit) {
//fft = in_fft;
// audioReceivedで取得したmagnitude配列をfftへ渡す
in_fft = magnitude;
for (int i = 0; i < fft_size; i++) {
// take the max, either the smoothed or the incoming:
// 0.9倍したものよりも大きければ値を更新する
if (fftSmoothed[i] < in_fft[i]) {
fftSmoothed[i] = in_fft[i];
}
// let the smoothed value sink to zero:
// 取得した周波数の振幅値に0.9倍にして、徐々に0に減らしていく
// (古い振幅値の影響をなくす)
fftSmoothed[i] *= 0.90f;
}
if(bDetectBeat) {
// サブバンドの計算
for(int i = 0; i < FFT_SUBBANDS; i++) {
fftSubbands[i] = 0;
for(int b = 0; b < fft_size/FFT_SUBBANDS; b++) {
// [i * 8 + 1~8] -> 8刻みで,0から7,8->15,16->23という感じで和をとる
fftSubbands[i] += in_fft[i*(fft_size/FFT_SUBBANDS)+b];
}
// サブバンド値の和をとった後, 加算した回数分割って平均値にする
fftSubbands[i] = fftSubbands[i]*(float)FFT_SUBBANDS/(float)fft_size;
// サブバンドの分散値の計算
for(int b=0; b < fft_size/FFT_SUBBANDS; b++) {
fftVariance[i] += pow(in_fft[i*(fft_size/FFT_SUBBANDS)+b] - fftSubbands[i], 2);
}
fftVariance[i] = fftVariance[i]*(float)FFT_SUBBANDS/(float)fft_size;
// 定数Cを分散値によって動的に変更する
// Cは、検出の閾値の係数になる
beatValueArray[i] = (-0.0025714*fftVariance[i])+1.35;
}
// エネルギ平均の計算
for(int i = 0; i < FFT_SUBBANDS; i++) {
averageEnergy[i] = 0;
for(int h = 0; h < ENERGY_HISTORY; h++) {
// 全体のエネルギーの平均 += 各サブバンドのエネルギヒストリ
averageEnergy[i] += energyHistory[i][h];
}
// 総和をヒストリ数で割って加重平均にする
averageEnergy[i] /= ENERGY_HISTORY;
}
// put new values into energy history
for(int i = 0; i < FFT_SUBBANDS; i++) {
// 算出したサブバンドをエネルギヒストリのhistoryPosの位置に追加
energyHistory[i][historyPos] = fftSubbands[i];
}
// historyPos = (現在のpos+1)を43で割った余り
// 配列の添字がループすることによって擬似的な循環リストを表現している
historyPos = (historyPos+1) % ENERGY_HISTORY; // forward pointer and rotate if necessary
}
}
}
// 音声入力を受け取ったとき, FFT解析を行いmagnitude配列を更新
void beatDetect::audioReceived(float* input, int bufferSize){
memcpy(audio_input, input, sizeof(float) * bufferSize);
float avg_power = 0.0f;
// アドオンのFFTクラスを使ってスペクトルを解析、 magnitude が欲しかったFFT振幅の値になる。
myfft.powerSpectrum(0, (int)fft_size, audio_input, buffer_size,
magnitude, phase, power, &avg_power);
// 結果の値の大小が激しかったので平方根をとった
for (int i = 0; i < fft_size; i++) {
magnitude[i] = powf(magnitude[i], 0.5);
}
// FFT振幅の平均値を計算
for (int i = 0; i < fft_size; i++) {
float x = 0.085;
magnitude_average[i] = (magnitude[i] * x) + (magnitude_average[i] * (1 - x));
}
}
// ビートそのものがあったか
bool beatDetect::isBeat(int subband)
{
return fftSubbands[subband] > averageEnergy[subband]*beatValueArray[subband];
}
bool beatDetect::isKick()
{
return isBeat(0);
}
bool beatDetect::isSnare()
{
int low = 1;
int hi = FFT_SUBBANDS/3;
int thresh = (hi-low)/3;
return isBeatRange(low, hi, thresh);
}
bool beatDetect::isHat()
{
int low = FFT_SUBBANDS/2;
int hi = FFT_SUBBANDS-1;
int thresh = (hi-low)/3;
return isBeatRange(low, hi, thresh);
}
// 検出に用いるサブバンド幅を指定するタイプ
bool beatDetect::isBeatRange(int low, int high, int threshold)
{
int num = 0;
for(int i = low; i < high+1; i++) {
if(isBeat(i)) {
num++;
}
}
return num > threshold;
}