forked from mihow/bpmdj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpm-analyzer-env.cpp
147 lines (137 loc) · 4.61 KB
/
bpm-analyzer-env.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
/****
BpmDj v4.2-pl4: Free Dj Tools
Copyright (C) 2001-2012 Werner Van Belle
http://bpmdj.yellowcouch.org/
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.
See the authors.txt for a full list of people involved.
****/
#ifndef __loaded__bpm_analyzer_env_cpp__
#define __loaded__bpm_analyzer_env_cpp__
using namespace std;
#include <assert.h>
#include <math.h>
#include <fftw3.h>
#include "signals.h"
#include "bpm-analyzer-env.h"
BpmAnalyzerEnv::BpmAnalyzerEnv(stereo_sample2 *block, unsigned4 n,
float8 lower_boundary, float8 higher_boundary):
BpmAnalyzerImpl(44100,true), audiorate(11025), spectrum_shifter(2)
{
input_audio=block;
input_samples=n;
startbpm=lower_boundary;
stopbpm=higher_boundary;
startshift = 0;
stopshift = 0;
freq = NULL;
audio = NULL;
}
bool BpmAnalyzerEnv::step()
{
status("Reading audio");
signed8 audiosize=input_samples;
audiosize>>=spectrum_shifter;
windowsize=lower_power_of_two(audiosize);
signed4 blocksize = 1 << spectrum_shifter;
stereo_sample2 *block = bpmdj_allocate(blocksize,stereo_sample2);
fft_type *audio = (fft_type*)fftw_malloc(audiosize*sizeof(fft_type));
freq = (fft_type*)fftw_malloc(windowsize*sizeof(fft_type));
fftw_plan plan = fftw_plan_r2r_1d(windowsize,audio,freq,FFTW_R2HC,
FFTW_ESTIMATE);
for(signed4 i = 0 ; i < audiosize; i++)
{
signed8 sum = 0;
memcpy(block,input_audio+i*blocksize,sizeof(stereo_sample2)*blocksize);
for (signed4 j = 0 ; j < blocksize ; j ++)
sum+=abs(block[j].left);
sum/=blocksize;
audio[i]=sum;
}
status("FFT");
fftw_execute(plan);
// rescale the entire thing
for(signed4 i = 0 ; i <windowsize/2 ; i ++)
{
fft_type bpm = Index_to_frequency(windowsize,i); // tov samplerate
bpm*=(fft_type)samplerate; // in Hz tov non collapsed samplerate
// uitgedrukt in collapsed samplerate
for(signed4 j = 0 ; j < spectrum_shifter; j ++) bpm/=2.0;
bpm*=60.0; // uitgedrukt in BPM.
if (bpm<startbpm) continue;
if (bpm>stopbpm) break;
float8 re=freq[i];
float8 im=freq[windowsize-1-i];
freq[i]=log(sqrt(re*re+im*im));
// freq[i]=fabs(freq[i]);
}
// detect peak bpm's
peaks = 10;
peak_bpm = bpmdj_allocate(peaks, fft_type);
peak_energy = bpmdj_allocate(peaks, fft_type);
fft_type *copy = bpmdj_allocate(windowsize / 2, fft_type);
for(signed4 i = 0 ; i < windowsize/2 ; i++) copy[i]=freq[i];
fft_type range = 0.5; // BPM left and right...
for(signed4 j = 0 ; j < peaks ; j ++)
{
fft_type energy = 0, at = 0;
for(signed4 i = 0 ; i <windowsize/2 ; i ++)
{
// tov samplerate
fft_type bpm = Index_to_frequency(windowsize,i);
// in Hz tov non collapsed samplerate
bpm*=(fft_type)samplerate;
// uitgedrukt in collapsed samplerate
for(signed4 j = 0 ; j < spectrum_shifter; j ++) bpm/=2.0;
bpm*=60.0; // uitgedrukt in BPM.
// skip or break ?
if (bpm<startbpm) continue;
if (bpm>stopbpm) break;
// is larger than any of the known peaks ?
if (copy[i]>energy)
{
energy = copy[i];
at = bpm;
}
}
// store peak
peak_bpm[j]=at;
peak_energy[j]=energy;
status("Peak %d at %g with strength %g",j,at,energy);
if (j == 0)
tempo=at;
// clear neighbors
for(signed4 i = 0 ; i <windowsize/2 ; i ++)
{
// obtain BPM
// relatief tov samplerate
fft_type bpm = Index_to_frequency(windowsize,i);
bpm*=(float8)samplerate; // in Hz tov non collapsed samplerate
// in collapsed samplerate
for(signed4 j = 0 ; j < spectrum_shifter; j ++) bpm/=2.0;
bpm*=60.0; // in BPM.
if (bpm>=at-range && bpm<=at+range)
copy[i]=0;
if (bpm>at+range) break;
}
}
assert(freq);
if (plot)
{
axes(0,horizontal(),axis("relative value"),"HalfReal FFTW Envelope Spectrum");
for(signed4 i = 1 ; i <windowsize/2; i ++)
if (freq[i]>0)
period_point(0,(samplerate*(double)windowsize/(double)(i*audiorate))/4.,freq[i]);
}
fftw_destroy_plan(plan);
fftw_free(audio);
finished();
return false;
}
#endif // __loaded__bpm_analyzer_env_cpp__