Skip to content

Commit

Permalink
Filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Feb 8, 2022
1 parent 9ec6da4 commit 3318758
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
10 changes: 7 additions & 3 deletions src/AudioTools/MusicalNotes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
File renamed without changes.
52 changes: 28 additions & 24 deletions tests/filter-wav/filter-wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int16_t> noise(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(noise); // Stream generated from sine wave
FilteredStream<int16_t, float> in_filtered(in_stream, channels); // Defiles the filter as BaseConverter
SineWaveGenerator<int16_t> wave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in_stream(wave); // Stream generated from sine wave
FilteredStream<int16_t, int16_t> 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<float>(coef));
in_filtered.setFilter(1, new FIR<int16_t>(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<notes.frequencyCount();j++){
wave.setFrequency(notes.frequency(j));
for (int i=0;i<20;i++){
copier.copy();
}
}
file.close();
stop();
Expand Down

0 comments on commit 3318758

Please sign in to comment.