FastLED 3.9.15
Loading...
Searching...
No Matches
fft.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/unique_ptr.h"
4#include "fl/span.h"
5#include "fl/vector.h"
6#include "fl/move.h"
7#include "fl/memfill.h"
8
9namespace fl {
10
11class FFTImpl;
12class AudioSample;
13
14struct FFTBins {
15 public:
16 FFTBins(fl::size n) : mSize(n) {
17 bins_raw.reserve(n);
18 bins_db.reserve(n);
19 }
20
21 // Copy constructor and assignment
22 FFTBins(const FFTBins &other) : bins_raw(other.bins_raw), bins_db(other.bins_db), mSize(other.mSize) {}
23 FFTBins &operator=(const FFTBins &other) {
24 if (this != &other) {
25 mSize = other.mSize;
26 bins_raw = other.bins_raw;
27 bins_db = other.bins_db;
28 }
29 return *this;
30 }
31
32 // Move constructor and assignment
33 FFTBins(FFTBins &&other) noexcept
34 : bins_raw(fl::move(other.bins_raw)), bins_db(fl::move(other.bins_db)), mSize(other.mSize) {}
35
36 FFTBins &operator=(FFTBins &&other) noexcept {
37 if (this != &other) {
38 bins_raw = fl::move(other.bins_raw);
39 bins_db = fl::move(other.bins_db);
40 mSize = other.mSize;
41 }
42 return *this;
43 }
44
45 void clear() {
46 bins_raw.clear();
47 bins_db.clear();
48 }
49
50 fl::size size() const { return mSize; }
51
52 // The bins are the output of the FFTImpl.
54 // The frequency range of the bins.
56
57 private:
58 fl::size mSize;
59};
60
61struct FFT_Args {
62 static int DefaultSamples() { return 512; }
63 static int DefaultBands() { return 16; }
64 static float DefaultMinFrequency() { return 174.6f; }
65 static float DefaultMaxFrequency() { return 4698.3f; }
66 static int DefaultSampleRate() { return 44100; }
67
69 int bands;
70 float fmin;
71 float fmax;
73
75 float fmin = DefaultMinFrequency(),
76 float fmax = DefaultMaxFrequency(),
78 // Memset so that this object can be hashed without garbage from packed
79 // in data.
80 fl::memfill(this, 0, sizeof(FFT_Args));
81 this->samples = samples;
82 this->bands = bands;
83 this->fmin = fmin;
84 this->fmax = fmax;
85 this->sample_rate = sample_rate;
86 }
87
88 // Rule of 5 for POD data
89 FFT_Args(const FFT_Args &other) = default;
90 FFT_Args &operator=(const FFT_Args &other) = default;
91 FFT_Args(FFT_Args &&other) noexcept = default;
92 FFT_Args &operator=(FFT_Args &&other) noexcept = default;
93
94 bool operator==(const FFT_Args &other) const ;
95 bool operator!=(const FFT_Args &other) const { return !(*this == other); }
96};
97
98class FFT {
99 public:
100 FFT();
102
103 FFT(FFT &&) = default;
104 FFT &operator=(FFT &&) = default;
105 FFT(const FFT & other);
106 FFT &operator=(const FFT & other);
107
108 void run(const span<const i16> &sample, FFTBins *out,
109 const FFT_Args &args = FFT_Args());
110
111 void clear();
112 fl::size size() const;
113
114 // FFT's are expensive to create, so we cache them. This sets the size of
115 // the cache. The default is 8.
116 void setFFTCacheSize(fl::size size);
117
118 private:
119 // Get the FFTImpl for the given arguments.
121 struct HashMap;
123};
124
125}; // namespace fl
void clear()
Definition fft.cpp:45
FFTImpl & get_or_create(const FFT_Args &args)
Definition fft.cpp:51
scoped_ptr< HashMap > mMap
Definition fft.h:122
fl::size size() const
Definition fft.cpp:47
FFT()
Definition fft.cpp:22
void setFFTCacheSize(fl::size size)
Definition fft.cpp:49
FFT(FFT &&)=default
void run(const span< const i16 > &sample, FFTBins *out, const FFT_Args &args=FFT_Args())
FFT & operator=(FFT &&)=default
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
unique_ptr< T, Deleter > scoped_ptr
Definition scoped_ptr.h:12
Slice< T > span
Definition span.h:8
void * memfill(void *ptr, int value, fl::size num)
Definition memfill.h:11
HeapVector< T, Allocator > vector
Definition vector.h:1214
IMPORTANT!
Definition crgb.h:20
corkscrew_args args
Definition old.h:150
FFT_Args(const FFT_Args &other)=default
bool operator==(const FFT_Args &other) const
Definition fft.cpp:63
static int DefaultSampleRate()
Definition fft.h:66
FFT_Args & operator=(FFT_Args &&other) noexcept=default
static int DefaultBands()
Definition fft.h:63
static float DefaultMinFrequency()
Definition fft.h:64
FFT_Args(int samples=DefaultSamples(), int bands=DefaultBands(), float fmin=DefaultMinFrequency(), float fmax=DefaultMaxFrequency(), int sample_rate=DefaultSampleRate())
Definition fft.h:74
float fmax
Definition fft.h:71
FFT_Args & operator=(const FFT_Args &other)=default
int samples
Definition fft.h:68
float fmin
Definition fft.h:70
int sample_rate
Definition fft.h:72
FFT_Args(FFT_Args &&other) noexcept=default
static int DefaultSamples()
Definition fft.h:62
static float DefaultMaxFrequency()
Definition fft.h:65
bool operator!=(const FFT_Args &other) const
Definition fft.h:95
int bands
Definition fft.h:69
FFTBins & operator=(const FFTBins &other)
Definition fft.h:23
fl::size mSize
Definition fft.h:58
FFTBins(const FFTBins &other)
Definition fft.h:22
FFTBins(fl::size n)
Definition fft.h:16
fl::vector< float > bins_raw
Definition fft.h:53
fl::size size() const
Definition fft.h:50
void clear()
Definition fft.h:45
FFTBins & operator=(FFTBins &&other) noexcept
Definition fft.h:36
FFTBins(FFTBins &&other) noexcept
Definition fft.h:33
fl::vector< float > bins_db
Definition fft.h:55