arduino-audio-tools
AudioEspressifFFT.h
1 #pragma once
2 
3 #include "AudioFFT.h"
4 #include "esp_dsp.h"
5 
12 namespace audio_tools {
13 
21  public:
22  bool begin(int len) override {
23  N = len;
24  if (p_data==nullptr){
25  p_data = new float[len*2];
26  if (p_data==nullptr){
27  LOGE("not enough memory");
28  }
29  }
30  assert(p_data!=nullptr);
31  ret = dsps_fft2r_init_fc32(NULL, CONFIG_DSP_MAX_FFT_SIZE);
32  if (ret != ESP_OK){
33  LOGE("dsps_fft2r_init_fc32 %d", ret);
34  }
35  return p_data!=nullptr && ret == ESP_OK;
36  }
37 
38  void end() override {
39  dsps_fft2r_deinit_fc32();
40  if (p_data==nullptr){
41  delete p_data;
42  p_data = nullptr;
43  }
44  }
45 
46  void setValue(int idx, int value) override {
47  if (idx<N){
48  p_data[idx*2 + 0] = value;
49  p_data[idx*2 + 1] = 0.0f;
50  }
51  }
52 
53  void fft() override {
54  ret = dsps_fft2r_fc32(p_data, N);
55  if (ret != ESP_OK){
56  LOGE("dsps_fft2r_fc32 %d", ret);
57  }
58  // Bit reverse
59  ret = dsps_bit_rev_fc32(p_data, N);
60  if (ret != ESP_OK){
61  LOGE("dsps_bit_rev_fc32 %d", ret);
62  }
63  // Convert one complex vector to two complex vectors
64  ret = dsps_cplx2reC_fc32(p_data, N);
65  if (ret != ESP_OK){
66  LOGE("dsps_cplx2reC_fc32 %d", ret);
67  }
68  };
69 
70  float magnitude(int idx) override {
71  return sqrt(p_data[idx*2] * p_data[idx*2] + p_data[idx*2+1] * p_data[idx*2+1]);
72  }
73 
75  float magnitudeFast(int idx) override {
76  return (p_data[idx*2] * p_data[idx*2] + p_data[idx*2+1] * p_data[idx*2+1]);
77  }
78 
79  virtual bool isValid() override{ return p_data!=nullptr && ret==ESP_OK; }
80 
81  esp_err_t ret;
82  float *p_data = nullptr;
83  int N=0;
84 
85 };
93  public:
95 
97  float *dataArray() {
98  return driverEx()->p_data;
99  }
100 
101  FFTDriverEspressifFFT* driverEx() {
102  return (FFTDriverEspressifFFT*)driver();
103  }
104 };
105 
106 
107 }
AudioFFT using FFTReal. The only specific functionality is the access to the dataArray.
Definition: AudioEspressifFFT.h:92
float * dataArray()
Provides the complex array returned by the FFT
Definition: AudioEspressifFFT.h:97
Executes FFT using audio data. The Driver which is passed in the constructor selects a specifc FFT im...
Definition: AudioFFT.h:82
AudioFFTBase(FFTDriver *driver)
Default Constructor. The len needs to be of the power of 2 (e.g. 512, 1024, 2048, 4096,...
Definition: AudioFFT.h:85
FFTDriver * driver()
provides access to the FFTDriver which implements the basic FFT functionality
Definition: AudioFFT.h:240
fft Driver for espressif dsp library: https://espressif-docs.readthedocs-hosted.com/projects/esp-dsp/...
Definition: AudioEspressifFFT.h:20
float magnitudeFast(int idx) override
magnitude w/o sqrt
Definition: AudioEspressifFFT.h:75
Abstract Class which defines the basic FFT functionality.
Definition: AudioFFT.h:65
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10