arduino-audio-tools
AudioKissFFT.h
1 #pragma once
2 
3 #include "kiss_fix.h"
4 #include "AudioFFT.h"
5 
12 namespace audio_tools {
13 
20 class FFTDriverKissFFT : public FFTDriver {
21  public:
22  bool begin(int len) override {
23  this->len = len;
24  if (p_fft_object==nullptr) p_fft_object = cpp_kiss_fft_alloc(len,false,nullptr,nullptr);
25  if (p_data==nullptr) p_data = new kiss_fft_cpx[len];
26  assert(p_fft_object!=nullptr);
27  assert(p_data!=nullptr);
28  return p_fft_object!=nullptr && p_data!=nullptr;
29  }
30 
31  void end() override {
32  if (p_fft_object!=nullptr) kiss_fft_free(p_fft_object);
33  if (p_fft_object_inv!=nullptr) kiss_fft_free(p_fft_object_inv);
34 
35  if (p_data!=nullptr) delete[] p_data;
36  p_fft_object = nullptr;
37  p_data = nullptr;
38  }
39  void setValue(int idx, float value) override {
40  p_data[idx].r = value;
41  }
42 
43  void fft() override {
44  cpp_kiss_fft (p_fft_object, p_data, p_data);
45  };
46 
47  void rfft() override {
48  if(p_fft_object_inv==nullptr) {
49  p_fft_object_inv = cpp_kiss_fft_alloc(len,true,nullptr,nullptr);
50  }
51  cpp_kiss_fft (p_fft_object_inv, p_data, p_data);
52  };
53 
54  float magnitude(int idx) override {
55  return sqrt(magnitudeFast(idx));
56  }
57 
59  float magnitudeFast(int idx) override {
60  return (p_data[idx].r * p_data[idx].r + p_data[idx].i * p_data[idx].i);
61  }
62 
63  bool isValid() override{ return p_fft_object!=nullptr; }
64 
65  bool isReverseFFT() override {return true;}
66 
67  float getValue(int idx) override { return p_data[idx].r; }
68 
69  bool setBin(int pos, FFTBin &bin) { return FFTDriver::setBin(pos, bin);}
70 
71  bool setBin(int pos, float real, float img) override {
72  if (pos>=len) return false;
73  p_data[pos].r = real;
74  p_data[pos].i = img;
75  return true;
76  }
77  bool getBin(int pos, FFTBin &bin) override {
78  if (pos>=len) return false;
79  bin.real = p_data[pos].r;
80  bin.img = p_data[pos].i;
81  return true;
82  }
83 
84  kiss_fft_cfg p_fft_object=nullptr;
85  kiss_fft_cfg p_fft_object_inv=nullptr;
86  kiss_fft_cpx *p_data = nullptr; // real
87  int len = 0;
88 
89 };
96 class AudioKissFFT : public AudioFFTBase {
97  public:
99 
101  kiss_fft_cpx *dataArray() {
102  return driverEx()->p_data;
103  }
104 
105  FFTDriverKissFFT* driverEx() {
106  return (FFTDriverKissFFT*)driver();
107  }
108 };
109 
110 
111 }
Executes FFT using audio data. The Driver which is passed in the constructor selects a specifc FFT im...
Definition: AudioFFT.h:125
AudioFFTBase(FFTDriver *driver)
Default Constructor. The len needs to be of the power of 2 (e.g. 512, 1024, 2048, 4096,...
Definition: AudioFFT.h:128
FFTDriver * driver()
provides access to the FFTDriver which implements the basic FFT functionality
Definition: AudioFFT.h:289
AudioFFT using FFTReal. The only specific functionality is the access to the dataArray.
Definition: AudioKissFFT.h:96
kiss_fft_cpx * dataArray()
Provides the complex array returned by the FFT
Definition: AudioKissFFT.h:101
Abstract Class which defines the basic FFT functionality.
Definition: AudioFFT.h:92
virtual bool setBin(int idx, float real, float img)
sets the value of a bin
Definition: AudioFFT.h:112
Driver for RealFFT.
Definition: AudioKissFFT.h:20
float magnitudeFast(int idx) override
magnitude w/o sqrt
Definition: AudioKissFFT.h:59
void setValue(int idx, float value) override
Sets the real value.
Definition: AudioKissFFT.h:39
bool isReverseFFT() override
Returns true if reverse FFT is supported.
Definition: AudioKissFFT.h:65
bool setBin(int pos, float real, float img) override
sets the value of a bin
Definition: AudioKissFFT.h:71
bool getBin(int pos, FFTBin &bin) override
gets the value of a bin
Definition: AudioKissFFT.h:77
float magnitude(int idx) override
Calculate the magnitude (fft result) at index (sqr(i² + r²))
Definition: AudioKissFFT.h:54
void fft() override
Perform FFT.
Definition: AudioKissFFT.h:43
void rfft() override
Calculate reverse FFT.
Definition: AudioKissFFT.h:47
float getValue(int idx) override
Get result value from Reverse FFT.
Definition: AudioKissFFT.h:67
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
And individual FFT Bin.
Definition: AudioFFT.h:61