forked from arduino-libraries/ArduinoSound
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmplitudeAnalyzer.cpp
More file actions
74 lines (57 loc) · 1.66 KB
/
Copy pathAmplitudeAnalyzer.cpp
File metadata and controls
74 lines (57 loc) · 1.66 KB
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
/*
Copyright (c) 2016 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "AudioIn.h"
#include "AmplitudeAnalyzer.h"
AmplitudeAnalyzer::AmplitudeAnalyzer() :
_bitsPerSample(-1),
_available(0),
_analysis(0)
{
}
AmplitudeAnalyzer::~AmplitudeAnalyzer()
{
}
int AmplitudeAnalyzer::available()
{
return _available;
}
int AmplitudeAnalyzer::read()
{
if (_available) {
_available = 0;
return _analysis;
}
return 0;
}
int AmplitudeAnalyzer::configure(AudioIn* input)
{
int bitsPerSample = input->bitsPerSample();
if (bitsPerSample != 16 && bitsPerSample != 32) {
return 0;
}
_bitsPerSample = bitsPerSample;
return 1;
}
void AmplitudeAnalyzer::update(const void* buffer, size_t size)
{
int analysis = 0;
if (_bitsPerSample == 16) {
arm_rms_q15((q15_t*)buffer, size / 2, (q15_t*)&analysis);
} else if (_bitsPerSample == 32) {
arm_rms_q31((q31_t*)buffer, size / 4, (q31_t*)&analysis);
}
_analysis = analysis;
_available = 1;
}