-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundleveldb.cpp
62 lines (53 loc) · 1.64 KB
/
soundleveldb.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
#include <pxt.h>
#if MICROBIT_CODAL
#include "LevelDetectorSPL.h"
#endif
namespace input {
/**
* IGNORE THIS VERSION.
*/
//%
int soundLevelDbOriginal() {
#if MICROBIT_CODAL
int originalUnit = uBit.audio.levelSPL->unit;
float soundDb = uBit.audio.levelSPL->getValue(LEVEL_DETECTOR_SPL_DB);
uBit.audio.levelSPL->unit = originalUnit;
return (int)soundDb;
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
return 0;
#endif
}
int scaleLowerDbValues(int value) {
// From values about 70 db y is accurate, for values below we need to weight it
if (value > 65) {
return value;
}
const int MAX_DB = 65;
const int MIN_DB = 30;
const int MIN_VALUE = 51;
const int RANGE_VALUE = MAX_DB - MIN_VALUE;
const int RANGE_DB = MAX_DB - MIN_DB;
// Original linear decay
// return (int)MAX_DB - ((MAX_DB - value) * EXTRA_DECAY);
float fraction = (float)(value - MIN_VALUE) / (float)RANGE_VALUE;
float nonLinearFraction = sqrtf(fraction);
return MIN_DB + (int)(RANGE_DB * nonLinearFraction);
}
//% blockId=device_get_sound_level_db
int soundLevelDbC() {
#if MICROBIT_CODAL
static LevelDetectorSPL *localLevelSPL = NULL;
const int DEVICE_ID_UNUSED = 50;
if (localLevelSPL == NULL) {
// Lower gain and lower min value than default uBit.audio.levelSPL
localLevelSPL = new LevelDetectorSPL(*(uBit.audio.rawSplitter->createChannel()), 200.0, 1.0, 12.0, 50.0, DEVICE_ID_UNUSED, true);
}
int localLevelValue = localLevelSPL->getValue(LEVEL_DETECTOR_SPL_DB);
return scaleLowerDbValues(localLevelValue);
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
return 0;
#endif
}
}