| 27 | #include "fl/math/math.h" |
| 28 | |
| 29 | FL_TEST_FILE(FL_FILEPATH) { |
| 30 | |
| 31 | using namespace fl; |
| 32 | using Diag = fl::audio::detector::VocalDetectorDiagnostics; |
| 33 | using fl::audio::test::makeSample; |
| 34 | using fl::audio::test::generateDC; |
| 35 | using fl::audio::test::generateSine; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | constexpr float PI = 3.14159265358979f; |
| 40 | |
| 41 | } // anonymous namespace |
| 42 | |
| 43 | // ============================================================================= |
| 44 | // 1. Signal conditioning SHOULD be enabled by default |
| 45 | // ============================================================================= |
| 46 | // A user who creates an audio::Processor and calls update() should get |
| 47 | // conditioned audio (DC removed, spikes filtered) without needing to know |
| 48 | // about setSignalConditioningEnabled(). MEMS microphones like the INMP441 |
| 49 | // always have DC offset and occasional I2S glitches. |
| 50 | |
| 51 | FL_TEST_CASE("Audio fix - DC offset removed by default") { |
| 52 | audio::Processor processor; |
| 53 | |
| 54 | // Sample with large DC offset (3000) — common with INMP441 MEMS mic |
| 55 | vector<i16> pcm; |
| 56 | generateSine(pcm, 512, 440.0f, 44100.0f, 5000); |
| 57 | // Add DC offset |
| 58 | for (auto& sample : pcm) { |
| 59 | i32 val = static_cast<i32>(sample) + 3000; |
| 60 | sample = static_cast<i16>(fl::max(-32768, fl::min(32767, val))); |
| 61 | } |
| 62 | |
| 63 | processor.update(makeSample(pcm, 1000)); |
| 64 | |
| 65 | // Measure DC offset of the sample that reached detector |
| 66 | const auto &processed = processor.getSample().pcm(); |
| 67 | i64 sum = 0; |
| 68 | for (size i = 0; i < processed.size(); ++i) { |
| 69 | sum += processed[i]; |
| 70 | } |
| 71 | float meanDC = |
| 72 | static_cast<float>(sum) / static_cast<float>(processed.size()); |
| 73 | |
| 74 | // DESIRED: DC offset should be removed — mean should be near zero |
| 75 | FL_CHECK_LT(fl::abs(meanDC), 500.0f); |
| 76 | } |
| 77 | |
| 78 | FL_TEST_CASE("Audio fix - I2S spike filtered by default") { |
| 79 | audio::Processor processor; |
| 80 | |
| 81 | // Sample with a large spike (I2S glitch) |
| 82 | vector<i16> pcm; |
| 83 | generateDC(pcm, 512, 0); |
| 84 | pcm[100] = 30000; // spike well above normal signal range |
| 85 | |
| 86 | processor.update(makeSample(pcm, 1000)); |
nothing calls this directly
no test coverage detected