Skip to content

Commit 4efa292

Browse files
committed
ifft example
1 parent e75fd15 commit 4efa292

File tree

4 files changed

+92708
-0
lines changed

4 files changed

+92708
-0
lines changed

src/AudioTools/AudioLibs/AudioFFT.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ class AudioFFTBase : public AudioStream {
312312
size_t readBytes(uint8_t *data, size_t len) override {
313313
TRACED();
314314
if (rfft_data.size() == 0) return 0;
315+
316+
// get data via callback if there is no more data
317+
if (cfg.rxtx_mode == RX_MODE && cfg.callback != nullptr && rfft_data.available() == 0) {
318+
cfg.callback(*this);
319+
}
320+
315321
// execute rfft when we consumed all data
316322
if (has_rfft_data && rfft_data.available() == 0) {
317323
rfft();
@@ -443,6 +449,14 @@ class AudioFFTBase : public AudioStream {
443449
/// gets the value of a bin
444450
bool getBin(int pos, FFTBin &bin) { return p_driver->getBin(pos, bin); }
445451

452+
/// clears the fft data
453+
void clearBins(){
454+
FFTBin empty{0,0};
455+
for (int j=0; j< size(); j++){
456+
setBin(j, empty);
457+
}
458+
}
459+
446460
/// Provides the actual configuration
447461
AudioFFTConfig &config() { return cfg; }
448462

tests-cmake/ifft/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
4+
# set the project name
5+
project(ifft)
6+
set (CMAKE_CXX_STANDARD 11)
7+
set (DCMAKE_CXX_FLAGS "-Werror")
8+
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -ldl -lpthread -lm")
9+
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -ldl -lpthread -lm")
10+
11+
# Emulator is not necessary for -DIS_MIN_DESKTOP
12+
set(ADD_ARDUINO_EMULATOR OFF CACHE BOOL "Add Arduino Emulator Library")
13+
set(ADD_PORTAUDIO OFF CACHE BOOL "No Portaudio")
14+
15+
# Build with arduino-audio-tools
16+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
17+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/arduino-audio-tools )
18+
endif()
19+
20+
# Download miniaudio.h
21+
file(DOWNLOAD https://raw.githubusercontent.com/mackron/miniaudio/master/miniaudio.h
22+
${CMAKE_CURRENT_SOURCE_DIR}/miniaudio.h)
23+
24+
25+
# build sketch as executable
26+
add_executable (ifft ifft.cpp)
27+
28+
# set preprocessor defines
29+
target_compile_definitions(ifft PUBLIC -DIS_MIN_DESKTOP)
30+
31+
# specify libraries
32+
target_link_libraries(ifft arduino-audio-tools)
33+
34+
# access to miniaudio in sketch directory
35+
target_include_directories(ifft PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

tests-cmake/ifft/ifft.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "AudioTools.h"
2+
#include "AudioTools/AudioLibs/AudioRealFFT.h" // using RealFFT
3+
#include "AudioTools/AudioLibs/MiniAudioStream.h"
4+
5+
AudioInfo info(44100, 2, 16);
6+
AudioRealFFT afft; // or AudioKissFFT
7+
//CsvOutput<int16_t> out(Serial);
8+
MiniAudioStream out;
9+
StreamCopy copier(out, afft);
10+
int bin_idx = 0;
11+
12+
// privide fft data
13+
void fftFillData(AudioFFTBase &fft) {
14+
fft.clearBins();
15+
FFTBin bin{1,1};
16+
fft.setBin(bin_idx, bin);
17+
18+
// restart from first bin
19+
if (++bin_idx>=fft.size()) bin_idx = 0;
20+
}
21+
22+
void setup() {
23+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
24+
25+
// Setup FFT
26+
auto tcfg = afft.defaultConfig(RX_MODE);
27+
tcfg.copyFrom(info);
28+
tcfg.length = 1024;
29+
tcfg.callback = fftFillData;
30+
afft.begin(tcfg);
31+
32+
// setup output
33+
auto ocfg = out.defaultConfig(TX_MODE);
34+
ocfg.copyFrom(info);
35+
out.begin(ocfg);
36+
}
37+
38+
void loop() { copier.copy(); }

0 commit comments

Comments
 (0)