-
Notifications
You must be signed in to change notification settings - Fork 1
/
AudioAnalyser.cpp
70 lines (54 loc) · 1.16 KB
/
AudioAnalyser.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
/*
* AudioAnalyser2.cpp
*
* Created on: 04.10.2012
* Author: mse
*/
#include "AudioAnalyser.h"
#include "fft.h"
#include <Arduino.h>
AudioAnalyser::AudioAnalyser() {
// TODO Auto-generated constructor stub
}
AudioAnalyser::~AudioAnalyser() {
// TODO Auto-generated destructor stub
}
void AudioAnalyser::sampleAudio() {
int i;
int x[SIZE];
ADCSRA = 0x87;
// turn on adc, freq = 1/128 , 125 kHz.
ADMUX = 0x60;
//Bit 5 ADLAR: ADC Left Adjust Result
ADCSRA |= (1 << ADSC);
// while((ADCSRA&(1<<ADIF)) == 0); //Discard first conversion result.
while (!(ADCSRA & 0x10))
;
for (i = 0; i < SIZE; i++) {
ADCSRA |= (1 << ADSC);
// while((ADCSRA&(1<<ADIF)) == 0);
while (!(ADCSRA & 0x10))
;
x[i] = ADCL;
x[i] += (ADCH << 8);
}
ADCSRA = 0x00;
for (i = 0; i < SIZE; i++) {
x[i] -= sdvig;
if (i & 0x01)
fx[(SIZE + i) >> 1] = x[i];
else
fx[i >> 1] = x[i];
}
}
int * AudioAnalyser::readAndTransform() {
sampleAudio();
fix_fftr(fx, log2N);
// Calculation of the magnitude:
for (int i = 0; i < N / 2; i++) {
fx[i] = sqrt(
(long) fx[i] * (long) fx[i]
+ (long) fx[i + N / 2] * (long) fx[i + N / 2]);
}
return fx;
}