FastLED 3.9.15
Loading...
Searching...
No Matches
fft.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/scoped_ptr.h"
4#include "fl/slice.h"
5#include "fl/vector.h"
6
7namespace fl {
8
9class FFTImpl;
10class AudioSample;
11
12struct FFTBins {
13 public:
14 FFTBins(size_t n) : mSize(n) {
15 bins_raw.reserve(n);
16 bins_db.reserve(n);
17 }
18
19 void clear() {
20 bins_raw.clear();
21 bins_db.clear();
22 }
23
24 size_t size() const { return mSize; }
25
26 // The bins are the output of the FFTImpl.
28 // The frequency range of the bins.
30
31 private:
32 size_t mSize;
33};
34
35struct FFT_Args {
36 static int DefaultSamples() { return 512; }
37 static int DefaultBands() { return 16; }
38 static float DefaultMinFrequency() { return 174.6f; }
39 static float DefaultMaxFrequency() { return 4698.3f; }
40 static int DefaultSampleRate() { return 44100; }
41
43 int bands;
44 float fmin;
45 float fmax;
47
49 float fmin = DefaultMinFrequency(),
50 float fmax = DefaultMaxFrequency(),
52 // Memset so that this object can be hashed without garbage from packed
53 // in data.
54 memset(this, 0, sizeof(FFT_Args));
55 this->samples = samples;
56 this->bands = bands;
57 this->fmin = fmin;
58 this->fmax = fmax;
59 this->sample_rate = sample_rate;
60 }
61
62 bool operator==(const FFT_Args &other) const {
63 return samples == other.samples && bands == other.bands &&
64 fmin == other.fmin && fmax == other.fmax &&
65 sample_rate == other.sample_rate;
66 }
67 bool operator!=(const FFT_Args &other) const { return !(*this == other); }
68};
69
70class FFT {
71 public:
72 FFT();
74
75 void run(const Slice<const int16_t> &sample, FFTBins *out,
76 const FFT_Args &args = FFT_Args());
77
78 void clear();
79 size_t size() const;
80
81 // FFT's are expensive to create, so we cache them. This sets the size of
82 // the cache. The default is 8.
83 void setFFTCacheSize(size_t size);
84
85 private:
86 // Get the FFTImpl for the given arguments.
87 FFTImpl &get_or_create(const FFT_Args &args);
88
89 // using HashMap = fl::HashMapLru<FFT_Args, Ptr<FFTImpl>>;
90 struct HashMap;
91 // HashMap mMap = HashMap(8);
93};
94
95}; // namespace fl
void clear()
Definition fft.cpp:30
void setFFTCacheSize(size_t size)
Definition fft.cpp:34
FFTImpl & get_or_create(const FFT_Args &args)
Definition fft.cpp:36
scoped_ptr< HashMap > mMap
Definition fft.h:92
size_t size() const
Definition fft.cpp:32
void run(const Slice< const int16_t > &sample, FFTBins *out, const FFT_Args &args=FFT_Args())
Definition fft.cpp:23
FFT()
Definition fft.cpp:19
HeapVector< T > vector
Definition vector.h:1028
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
bool operator==(const FFT_Args &other) const
Definition fft.h:62
static int DefaultSampleRate()
Definition fft.h:40
static int DefaultBands()
Definition fft.h:37
static float DefaultMinFrequency()
Definition fft.h:38
FFT_Args(int samples=DefaultSamples(), int bands=DefaultBands(), float fmin=DefaultMinFrequency(), float fmax=DefaultMaxFrequency(), int sample_rate=DefaultSampleRate())
Definition fft.h:48
float fmax
Definition fft.h:45
int samples
Definition fft.h:42
float fmin
Definition fft.h:44
int sample_rate
Definition fft.h:46
static int DefaultSamples()
Definition fft.h:36
static float DefaultMaxFrequency()
Definition fft.h:39
bool operator!=(const FFT_Args &other) const
Definition fft.h:67
int bands
Definition fft.h:43
FFTBins(size_t n)
Definition fft.h:14
fl::vector< float > bins_raw
Definition fft.h:27
void clear()
Definition fft.h:19
fl::vector< float > bins_db
Definition fft.h:29
size_t size() const
Definition fft.h:24
size_t mSize
Definition fft.h:32