|
5 | 5 | #include "SdFat.h"
|
6 | 6 |
|
7 | 7 | // 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 |
22 | 22 | };
|
23 | 23 | //
|
24 | 24 | uint16_t sample_rate=44100;
|
25 | 25 | 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 |
29 | 29 | SdFat sd;
|
30 | 30 | SdFile file;
|
31 | 31 | EncodedAudioStream out(&file, new WAVEncoder()); // encode as wav file
|
32 | 32 | StreamCopy copier(out, in_filtered); // copies sound to out
|
| 33 | +MusicalNotes notes; |
33 | 34 |
|
34 | 35 | void setup(){
|
35 | 36 | Serial.begin(115200);
|
36 | 37 | AudioLogger::instance().begin(Serial, AudioLogger::Info);
|
37 | 38 |
|
38 |
| - auto cfg = noise.defaultConfig(); |
| 39 | + auto cfg = wave.defaultConfig(); |
39 | 40 | cfg.sample_rate = sample_rate;
|
40 | 41 | cfg.channels = channels;
|
41 | 42 | cfg.bits_per_sample = 16;
|
42 |
| - noise.begin(cfg); |
| 43 | + wave.begin(cfg, 20); |
43 | 44 | in_stream.begin();
|
44 | 45 | out.begin();
|
45 | 46 |
|
46 | 47 | // 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)); |
48 | 49 |
|
49 | 50 | 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"); |
52 | 53 | }
|
53 | 54 |
|
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 | + } |
56 | 60 | }
|
57 | 61 | file.close();
|
58 | 62 | stop();
|
|
0 commit comments