FastLED 3.9.15
Loading...
Searching...
No Matches
audio.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/fft.h"
4#include "fl/math.h"
5#include "fl/ptr.h"
6#include "fl/slice.h"
7#include "fl/vector.h"
8#include <math.h>
9#include <stdint.h>
10
11namespace fl {
12
13class AudioSampleImpl;
14
16
17// AudioSample is a wrapper around AudioSampleImpl, hiding the reference
18// counting so that the api object can be simple and have standard object
19// semantics.
21 public:
25 AudioSample(const AudioSample &other) : mImpl(other.mImpl) {}
26 AudioSample(AudioSampleImplPtr impl) : mImpl(impl) {}
27 AudioSample &operator=(const AudioSample &other);
28 bool isValid() const { return mImpl != nullptr; }
29
30 size_t size() const;
31 // Raw pcm levels.
32 const VectorPCM &pcm() const;
33 // Zero crossing factor between 0.0f -> 1.0f, detects "hiss"
34 // and sounds like cloths rubbing. Useful for sound analysis.
35 float zcf() const;
36 float rms() const;
37
38 void fft(FFTBins *out);
39
40 const_iterator begin() const { return pcm().begin(); }
41 const_iterator end() const { return pcm().end(); }
42 const int16_t &at(size_t i) const;
43 const int16_t &operator[](size_t i) const;
44 operator bool() const { return isValid(); }
45 bool operator==(const AudioSample &other) const;
46 bool operator!=(const AudioSample &other) const;
47
48 private:
49 static const VectorPCM &empty();
50 AudioSampleImplPtr mImpl;
51};
52
53// Sound level meter is a persistant measuring class that will auto-tune the
54// microphone to real world SPL levels. It will adapt to the noise floor of the
55// environment. Note that the microphone only ever outputs DBFS (dB Full Scale)
56// values, which are collected over a stream of samples. The sound level meter
57// will convert this to SPL (Sound Pressure Level) values, which are the real
58// world values.
60 public:
64 SoundLevelMeter(double spl_floor = 33.0f, double smoothing_alpha = 0.0);
65
67 void processBlock(const int16_t *samples, size_t count);
69 processBlock(samples.data(), samples.size());
70 }
71
73 double getDBFS() const { return current_dbfs_; }
74
76 double getSPL() const { return current_spl_; }
77
79 void setFloorSPL(double spl_floor) {
80 spl_floor_ = spl_floor;
82 }
83
85 void resetFloor() {
86 dbfs_floor_global_ = INFINITY_DOUBLE; // infinity<double>
87 offset_ = 0.0;
88 }
89
90 private:
91 double spl_floor_; // e.g. 33.0 dB SPL
92 double smoothing_alpha_; // 0 = pure min, >0 = slow adapt
93 double dbfs_floor_global_; // lowest dBFS seen so far
94 double offset_; // spl_floor_ − dbfs_floor_global_
95 double current_dbfs_; // last block’s dBFS
96 double current_spl_; // last block’s estimated SPL
97};
98
99// Implementation details.
101 public:
104 template <typename It> void assign(It begin, It end) {
105 mSignedPcm.assign(begin, end);
106 // calculate zero crossings
108 }
109 const VectorPCM &pcm() const { return mSignedPcm; }
110
111 // "Zero crossing factor". High values > .4 indicate hissing
112 // sounds. For example a microphone rubbing against a clothing.
113 // These types of signals indicate the audio should be ignored.
114 // Low zero crossing factors (with loud sound) indicate that there
115 // is organized sound like that coming from music. This is so cheap
116 // to calculate it's done automatically. It should be one of the first
117 // signals to reject or accept a sound signal.
118 //
119 // Returns: a value -> [0.0f, 1.0f)
120 float zcf() const {
121 const size_t n = pcm().size();
122 if (n < 2) {
123 return 0.f;
124 }
125 return float(mZeroCrossings) / (n - 1);
126 }
127
128 private:
130 mZeroCrossings = 0;
131 if (mSignedPcm.size() > 1) {
132 for (size_t i = 1; i < mSignedPcm.size(); ++i) {
133 const bool crossed =
134 (mSignedPcm[i - 1] < 0 && mSignedPcm[i] >= 0) ||
135 (mSignedPcm[i - 1] >= 0 && mSignedPcm[i] < 0);
136 if (crossed) {
138 }
139 }
140 }
141 }
142
144 int16_t mZeroCrossings = 0;
145};
146
147} // namespace fl
AudioAnalyzeFFT1024 fft
fl::vector< int16_t > VectorPCM
Definition audio.h:22
const VectorPCM & pcm() const
Definition audio.cpp:6
AudioSampleImplPtr mImpl
Definition audio.h:50
const int16_t & at(size_t i) const
Definition audio.cpp:26
VectorPCM::const_iterator const_iterator
Definition audio.h:23
float zcf() const
Definition audio.cpp:63
AudioSample(const AudioSample &other)
Definition audio.h:25
bool operator!=(const AudioSample &other) const
Definition audio.cpp:53
bool isValid() const
Definition audio.h:28
AudioSample(AudioSampleImplPtr impl)
Definition audio.h:26
bool operator==(const AudioSample &other) const
Definition audio.cpp:35
float rms() const
Definition audio.cpp:65
const int16_t & operator[](size_t i) const
Definition audio.cpp:33
size_t size() const
Definition audio.cpp:19
const_iterator begin() const
Definition audio.h:40
const_iterator end() const
Definition audio.h:41
AudioSample & operator=(const AudioSample &other)
Definition audio.cpp:14
static const VectorPCM & empty()
Definition audio.cpp:57
int16_t mZeroCrossings
Definition audio.h:144
float zcf() const
Definition audio.h:120
void assign(It begin, It end)
Definition audio.h:104
void initZeroCrossings()
Definition audio.h:129
fl::vector< int16_t > VectorPCM
Definition audio.h:102
VectorPCM mSignedPcm
Definition audio.h:143
const VectorPCM & pcm() const
Definition audio.h:109
size_t size() const
Definition vector.h:435
iterator end()
Definition vector.h:466
iterator begin()
Definition vector.h:464
const T * const_iterator
Definition vector.h:314
const T * data() const
Definition slice.h:86
size_t size() const
Definition slice.h:90
double getDBFS() const
Definition audio.h:73
double dbfs_floor_global_
Definition audio.h:93
void processBlock(const int16_t *samples, size_t count)
Process a block of int16 PCM samples.
Definition audio.cpp:84
SoundLevelMeter(double spl_floor=33.0f, double smoothing_alpha=0.0)
Definition audio.cpp:79
double current_dbfs_
Definition audio.h:95
void setFloorSPL(double spl_floor)
change your known noise-floor SPL at runtime
Definition audio.h:79
void processBlock(fl::Slice< const int16_t > samples)
Definition audio.h:68
double getSPL() const
Definition audio.h:76
double smoothing_alpha_
Definition audio.h:92
double spl_floor_
Definition audio.h:91
double current_spl_
Definition audio.h:96
void resetFloor()
reset so the next quiet block will re-initialize your floor
Definition audio.h:85
#define INFINITY_DOUBLE
Definition math_macros.h:45
HeapVector< T > vector
Definition vector.h:1028
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_SMART_PTR(type)
Definition ptr.h:31