From 33187580ea65987457e7b8f81ab5bb72cb359f30 Mon Sep 17 00:00:00 2001 From: Phil Schatzmann Date: Tue, 8 Feb 2022 14:52:31 +0100 Subject: [PATCH] Filter tests --- src/AudioTools/MusicalNotes.h | 10 ++-- .../noise.wav => src/AudioTools/wave.wav | 0 tests/filter-wav/filter-wav.cpp | 52 ++++++++++--------- 3 files changed, 35 insertions(+), 27 deletions(-) rename tests/filter-wav/noise.wav => src/AudioTools/wave.wav (100%) diff --git a/src/AudioTools/MusicalNotes.h b/src/AudioTools/MusicalNotes.h index 18c487ade4..ff6805183f 100644 --- a/src/AudioTools/MusicalNotes.h +++ b/src/AudioTools/MusicalNotes.h @@ -128,15 +128,19 @@ class MusicalNotes { /// Determines the frequency of the indicate note and octave (0-8) int frequency(MusicalNotesEnum note, uint8_t octave){ if (note>11) return 0; - if (octave>7) return 0; + if (octave>8) return 0; return notes[octave][note]; } /// Determines the frequency of the indicate note index from 0 to 107 int frequency(uint16_t idx){ MusicalNotesEnum mainNote = (MusicalNotesEnum) (idx % 12); - uint8_t level = idx / 12; - return frequency(mainNote, level); + uint8_t octave = idx / 12; + return frequency(mainNote, octave); + } + + int frequencyCount() { + return 108; } /// Determines the frequency of the indicate main note index (0-6) and octave (0-8) diff --git a/tests/filter-wav/noise.wav b/src/AudioTools/wave.wav similarity index 100% rename from tests/filter-wav/noise.wav rename to src/AudioTools/wave.wav diff --git a/tests/filter-wav/filter-wav.cpp b/tests/filter-wav/filter-wav.cpp index b6121f1619..cc25c763f6 100644 --- a/tests/filter-wav/filter-wav.cpp +++ b/tests/filter-wav/filter-wav.cpp @@ -5,54 +5,58 @@ #include "SdFat.h" // define FIR filter -float coef[] = { --0.018296746249137946, --0.056723974384224739, -0.018540799820324621, -0.097644454515593698, --0.018688161556077588, --0.297627121039396536, -0.550301497785836702, --0.297627121039396536, --0.018688161556077588, -0.097644454515593698, -0.018540799820324621, --0.056723974384224739, --0.018296746249137946 +int16_t coef[] = { +-600, +-1859, +608, +3200, +-612, +-9752, +18032, +-9752, +-612, +3200, +608, +-1859, +-600 }; // uint16_t sample_rate=44100; uint8_t channels = 2; // The stream will have 2 channels -NoiseGenerator noise(32000); // subclass of SoundGenerator with max amplitude of 32000 -GeneratedSoundStream in_stream(noise); // Stream generated from sine wave -FilteredStream in_filtered(in_stream, channels); // Defiles the filter as BaseConverter +SineWaveGenerator wave(32000); // subclass of SoundGenerator with max amplitude of 32000 +GeneratedSoundStream in_stream(wave); // Stream generated from sine wave +FilteredStream in_filtered(in_stream, channels); // Defiles the filter as BaseConverter SdFat sd; SdFile file; EncodedAudioStream out(&file, new WAVEncoder()); // encode as wav file StreamCopy copier(out, in_filtered); // copies sound to out +MusicalNotes notes; void setup(){ Serial.begin(115200); AudioLogger::instance().begin(Serial, AudioLogger::Info); - auto cfg = noise.defaultConfig(); + auto cfg = wave.defaultConfig(); cfg.sample_rate = sample_rate; cfg.channels = channels; cfg.bits_per_sample = 16; - noise.begin(cfg); + wave.begin(cfg, 20); in_stream.begin(); out.begin(); // setup filters on channel 1 only - in_filtered.setFilter(1, new FIR(coef)); + in_filtered.setFilter(1, new FIR(coef)); if (!sd.begin(SS, SPI_HALF_SPEED)) sd.initErrorHalt(); - if (!file.open("noise.wav", O_RDWR | O_CREAT)) { - sd.errorHalt("opening noise.wav for write failed"); + if (!file.open("wave.wav", O_RDWR | O_CREAT)) { + sd.errorHalt("opening wave.wav for write failed"); } - for (int j=0;j<1024;j++){ - copier.copy(); + for (int j=0;j