Skip to content

Commit 3318758

Browse files
committed
Filter tests
1 parent 9ec6da4 commit 3318758

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

src/AudioTools/MusicalNotes.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,19 @@ class MusicalNotes {
128128
/// Determines the frequency of the indicate note and octave (0-8)
129129
int frequency(MusicalNotesEnum note, uint8_t octave){
130130
if (note>11) return 0;
131-
if (octave>7) return 0;
131+
if (octave>8) return 0;
132132
return notes[octave][note];
133133
}
134134

135135
/// Determines the frequency of the indicate note index from 0 to 107
136136
int frequency(uint16_t idx){
137137
MusicalNotesEnum mainNote = (MusicalNotesEnum) (idx % 12);
138-
uint8_t level = idx / 12;
139-
return frequency(mainNote, level);
138+
uint8_t octave = idx / 12;
139+
return frequency(mainNote, octave);
140+
}
141+
142+
int frequencyCount() {
143+
return 108;
140144
}
141145

142146
/// Determines the frequency of the indicate main note index (0-6) and octave (0-8)
File renamed without changes.

tests/filter-wav/filter-wav.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,58 @@
55
#include "SdFat.h"
66

77
// define FIR filter
8-
float coef[] = {
9-
-0.018296746249137946,
10-
-0.056723974384224739,
11-
0.018540799820324621,
12-
0.097644454515593698,
13-
-0.018688161556077588,
14-
-0.297627121039396536,
15-
0.550301497785836702,
16-
-0.297627121039396536,
17-
-0.018688161556077588,
18-
0.097644454515593698,
19-
0.018540799820324621,
20-
-0.056723974384224739,
21-
-0.018296746249137946
8+
int16_t coef[] = {
9+
-600,
10+
-1859,
11+
608,
12+
3200,
13+
-612,
14+
-9752,
15+
18032,
16+
-9752,
17+
-612,
18+
3200,
19+
608,
20+
-1859,
21+
-600
2222
};
2323
//
2424
uint16_t sample_rate=44100;
2525
uint8_t channels = 2; // The stream will have 2 channels
26-
NoiseGenerator<int16_t> noise(32000); // subclass of SoundGenerator with max amplitude of 32000
27-
GeneratedSoundStream<int16_t> in_stream(noise); // Stream generated from sine wave
28-
FilteredStream<int16_t, float> in_filtered(in_stream, channels); // Defiles the filter as BaseConverter
26+
SineWaveGenerator<int16_t> wave(32000); // subclass of SoundGenerator with max amplitude of 32000
27+
GeneratedSoundStream<int16_t> in_stream(wave); // Stream generated from sine wave
28+
FilteredStream<int16_t, int16_t> in_filtered(in_stream, channels); // Defiles the filter as BaseConverter
2929
SdFat sd;
3030
SdFile file;
3131
EncodedAudioStream out(&file, new WAVEncoder()); // encode as wav file
3232
StreamCopy copier(out, in_filtered); // copies sound to out
33+
MusicalNotes notes;
3334

3435
void setup(){
3536
Serial.begin(115200);
3637
AudioLogger::instance().begin(Serial, AudioLogger::Info);
3738

38-
auto cfg = noise.defaultConfig();
39+
auto cfg = wave.defaultConfig();
3940
cfg.sample_rate = sample_rate;
4041
cfg.channels = channels;
4142
cfg.bits_per_sample = 16;
42-
noise.begin(cfg);
43+
wave.begin(cfg, 20);
4344
in_stream.begin();
4445
out.begin();
4546

4647
// setup filters on channel 1 only
47-
in_filtered.setFilter(1, new FIR<float>(coef));
48+
in_filtered.setFilter(1, new FIR<int16_t>(coef));
4849

4950
if (!sd.begin(SS, SPI_HALF_SPEED)) sd.initErrorHalt();
50-
if (!file.open("noise.wav", O_RDWR | O_CREAT)) {
51-
sd.errorHalt("opening noise.wav for write failed");
51+
if (!file.open("wave.wav", O_RDWR | O_CREAT)) {
52+
sd.errorHalt("opening wave.wav for write failed");
5253
}
5354

54-
for (int j=0;j<1024;j++){
55-
copier.copy();
55+
for (int j=0;j<notes.frequencyCount();j++){
56+
wave.setFrequency(notes.frequency(j));
57+
for (int i=0;i<20;i++){
58+
copier.copy();
59+
}
5660
}
5761
file.close();
5862
stop();

0 commit comments

Comments
 (0)