Skip to content

Commit b732c48

Browse files
committed
DRAFT Support for filters
1 parent c638d28 commit b732c48

File tree

16 files changed

+760
-242
lines changed

16 files changed

+760
-242
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ Here is an simple example which streams a file from the Flash Memory and writes
5454
#include "AudioTools.h"
5555
#include "StarWars30.h"
5656
57-
using namespace audio_tools;
58-
5957
uint8_t channels = 2;
6058
uint16_t sample_rate = 22050;
6159
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file streams-kit-filter-kit.ino
3+
* @brief Copy audio from I2S to I2S using an FIR filter
4+
* @author Phil Schatzmann
5+
* @copyright GPLv3
6+
* #TODO testing is outstanding
7+
*/
8+
9+
#include "AudioTools.h"
10+
#include "AudioLibs/AudioKit.h"
11+
12+
uint16_t sample_rate=44100;
13+
uint16_t channels = 2;
14+
AudioKitStream kit;
15+
16+
// copy filtered values
17+
FilteredStream<int16_t, float> inFiltered(kit, channels); // Defiles the filter as BaseConverter
18+
StreamCopy copier(kit, inFiltered); // copies sound into i2s
19+
20+
// define FIR filter
21+
float coef[] = { 0.021, 0.096, 0.146, 0.096, 0.021};
22+
23+
// Arduino Setup
24+
void setup(void) {
25+
// Open Serial
26+
Serial.begin(115200);
27+
// change to Warning to improve the quality
28+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
29+
30+
// setup filters for all available channels
31+
inFiltered.setFilter(0, new FIR<float>(coef));
32+
inFiltered.setFilter(1, new FIR<float>(coef));
33+
34+
// start I2S in
35+
Serial.println("starting KIT...");
36+
auto config = kit.defaultConfig(RX_TX_MODE);
37+
config.sample_rate = sample_rate;
38+
config.sd_active = false;
39+
config.input_device = AUDIO_HAL_ADC_INPUT_LINE2;
40+
kit.begin(config);
41+
42+
Serial.println("KIT started...");
43+
}
44+
45+
// Arduino loop - copy sound to out
46+
void loop() {
47+
copier.copy();
48+
}

examples/examples-basic-api/base-adc-a2dp/base-adc-a2dp.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*
66
* @author Phil Schatzmann
77
* @copyright GPLv3
8-
*
8+
* #TODO retest is outstanding
9+
*
910
*/
1011
// Add this in your sketch or change the setting in AudioConfig.h
1112
#define USE_A2DP
@@ -24,11 +25,13 @@ BluetoothA2DPSource a2dp_source;
2425
ConverterScaler<int16_t> scaler(1.0, -26427, 32700 );
2526

2627
// callback used by A2DP to provide the sound data
27-
int32_t get_sound_data(Frame* data, int32_t len) {
28-
arrayOf2int16_t *data_arrays = (arrayOf2int16_t *) data;
28+
int32_t get_sound_data(Frame* frames, int32_t count) {
29+
uint8_t *data = (uint8_t*)frames;
30+
int channels = 2;
31+
size_t len = count * channels * sizeof(int16_t);
2932
// the ADC provides data in 16 bits
30-
size_t result_len = adc.read(data_arrays, len);
31-
scaler.convert(data_arrays, result_len);
33+
size_t result_len = adc.readBytes(data, len);
34+
scaler.convert(data, result_len);
3235
return result_len;
3336
}
3437

examples/examples-basic-api/base-adc-serial/base-adc-serial.ino

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* @author Phil Schatzmann
77
* @copyright GPLv3
8+
* #TODO retest is outstanding
89
*/
910

1011
// Add this in your sketch or change the setting in AudioConfig.h
@@ -19,8 +20,8 @@
1920
*/
2021

2122
AnalogAudio adc;
22-
const int32_t max_buffer_len = 512;
23-
int16_t buffer[max_buffer_len][2];
23+
const int32_t max_buffer_len = 1024;
24+
uint8_t buffer[max_buffer_len];
2425
// The data has a center of around 26427, so we we need to shift it down to bring the center to 0
2526
ConverterScaler<int16_t> scaler(1.0, -26427, 32700 );
2627

@@ -36,13 +37,15 @@ void setup(void) {
3637

3738
// Arduino loop - repeated processing
3839
void loop() {
39-
size_t len = adc.read(buffer, max_buffer_len);
40+
size_t len = adc.readBytes(buffer, max_buffer_len);
4041
// move center to 0 and scale the values
4142
scaler.convert(buffer, len);
4243

43-
for (int j=0;j<len;j++){
44-
Serial.print(buffer[j][0]);
44+
int16_t *sample = (int16_t*) buffer;
45+
int size = len / 4;
46+
for (int j=0; j<size; j++){
47+
Serial.print(*sample++);
4548
Serial.print(", ");
46-
Serial.println(buffer[j][1]);
49+
Serial.println(*sample++);
4750
}
4851
}

examples/examples-basic-api/base-i2s-a2dp/base-i2s-a2dp.ino

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* @file base-i2s-a2dp.ino
33
* @author Phil Schatzmann
44
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/base-i2s-a2dp/README.md
5-
*
6-
* @author Phil Schatzmann
7-
* @copyright GPLv3
8-
*/
5+
* @copyright GPLv3
6+
* #TODO retest is outstanding
7+
*/
98

109
// Add this in your sketch or change the setting in AudioConfig.h
1110
#define USE_A2DP
@@ -29,19 +28,15 @@ int32_t buffer[max_buffer_len][2];
2928

3029
// callback used by A2DP to provide the sound data
3130
int32_t get_sound_data(Frame* data, int32_t len) {
32-
size_t req_len = min(max_buffer_len,(size_t) len);
33-
34-
// the microphone provides data in int32_t -> we read it into the buffer of int32_t data
35-
size_t result_len = i2s.read(buffer, req_len);
36-
31+
uint8_t* data8 = (uint8_t*)data;
32+
int bytes = len * 4;
33+
size_t result_len = i2s.readBytes(data8, bytes);
3734
// we have data only in 1 channel but we want to fill both
38-
bothChannels.convert(buffer, result_len);
39-
40-
// convert buffer to int16 for A2DP
41-
converter.convert(buffer, data, result_len);
42-
return result_len;
35+
bothChannels.convert(data8, bytes);
36+
return result_len / 4;
4337
}
4438

39+
4540
// Arduino Setup
4641
void setup(void) {
4742
Serial.begin(115200);

examples/examples-stream/streams-adc-serial/streams-adc-serial.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-adc-serial/README.md
55
* @author Phil Schatzmann
66
* @copyright GPLv3
7+
* #TODO retest is outstanding
78
*/
89

910
#include "AudioTools.h"
1011

1112

1213

1314
uint8_t channels = 2;
14-
AnalogAudioStream microphone; // analog microphone
15-
CsvStream<int16_t> printer(Serial, channels); // ASCII output stream
16-
StreamCopy copier(printer, microphone); // copies microphone into printer
17-
ConverterAutoCenter<int16_t> center; // make sure the avg of the signal is 0
15+
AnalogAudioStream microphone; // analog microphone
16+
CsvStream<int16_t> printer(Serial, channels); // ASCII output stream
17+
StreamCopy copier(printer, microphone); // copies microphone into printer
18+
ConverterAutoCenter<int16_t> center(channels); // make sure the avg of the signal is 0
1819

1920
// Arduino Setup
2021
void setup(void) {

examples/examples-stream/streams-analog-serial/streams-analog-serial.ino

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22
* @file streams-analog-serial.ino
33
* @author Phil Schatzmann
44
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-analog-serial/README.md
5-
*
6-
* @author Phil Schatzmann
75
* @copyright GPLv3
6+
* #TODO retest is outstanding
87
*/
98

10-
119
#include "Arduino.h"
1210
#include "AudioTools.h"
1311

14-
15-
1612
const uint16_t sample_rate = 44100;
1713
const uint8_t channels = 2;
1814
AnalogAudioStream in;
1915
CsvStream<int16_t> out(Serial, channels); // ASCII output stream
2016
StreamCopy copier(out, in); // copy i2sStream to CsvStream
21-
ConverterAutoCenter<int16_t> center; // set avg to 0
17+
ConverterAutoCenter<int16_t> center(channels); // set avg to 0
2218

2319
// Arduino Setup
2420
void setup(void) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @file streams-i2s-filter-i2s.ino
3+
* @brief Copy audio from I2S to I2S using an FIR filter
4+
* @author Phil Schatzmann
5+
* @copyright GPLv3
6+
*/
7+
8+
#include "AudioTools.h"
9+
10+
uint16_t sample_rate=44100;
11+
uint16_t channels = 2;
12+
I2SStream in;
13+
I2SStream out;
14+
15+
// copy filtered values
16+
ConverterNChannels<int16_t> converter(channels); // Defiles the filter as BaseConverter
17+
FilteredStream<int16_t, float> inFiltered(in); //
18+
StreamCopy copier(out, inFiltered); // copies sound into i2s
19+
20+
// define FIR filter parameters
21+
float coef[] = { 0.021, 0.096, 0.146, 0.096, 0.021};
22+
23+
24+
// Arduino Setup
25+
void setup(void) {
26+
// Open Serial
27+
Serial.begin(115200);
28+
// change to Warning to improve the quality
29+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
30+
31+
// setup filters for all available channels
32+
inFiltered.setFilter(0, new FIR<float>(coef));
33+
inFiltered.setFilter(1, new FIR<float>(coef));
34+
35+
// start I2S in
36+
Serial.println("starting I2S...");
37+
auto config_in = in.defaultConfig(RX_MODE);
38+
config_in.sample_rate = sample_rate;
39+
config_in.bits_per_sample = 16;
40+
config_in.i2s_format = I2S_STD_FORMAT;
41+
config_in.is_master = true;
42+
config_in.port_no = 0;
43+
config_in.pin_ws = 14;
44+
config_in.pin_bck = 15;
45+
config_in.pin_data = 16;
46+
// config_in.fixed_mclk = sample_rate * 256
47+
// config_in.pin_mck = 2
48+
49+
in.begin(config_in);
50+
51+
// start I2S out
52+
auto config_out = out.defaultConfig(TX_MODE);
53+
config_out.sample_rate = sample_rate;
54+
config_out.bits_per_sample = 16;
55+
config_out.i2s_format = I2S_STD_FORMAT;
56+
config_out.is_master = true;
57+
config_out.port_no = 1;
58+
config_out.pin_ws = 17;
59+
config_out.pin_bck = 18;
60+
config_out.pin_data = 19;
61+
out.begin(config_out);
62+
63+
Serial.println("I2S started...");
64+
}
65+
66+
// Arduino loop - copy sound to out
67+
void loop() {
68+
copier.copy();
69+
}
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/**
2-
* @file streams-i2s-i2s.ino
3-
* @brief Copy audio from I2S to I2S
2+
* @file streams-i2s-filter-i2s.ino
3+
* @brief Copy audio from I2S to I2S using an FIR filter
44
* @author Phil Schatzmann
55
* @copyright GPLv3
66
*/
7+
8+
#include "AudioTools.h"
79

8-
uint16_t sample_rate=96000;
10+
uint16_t sample_rate=44100;
911
uint16_t channels = 2;
1012
I2SStream in;
1113
I2SStream out;
12-
StreamCopy copier(out, in); // copies sound into i2s
13-
ConverterScaler<int16_t> scaler(0.8, 0, 1000);
14+
StreamCopy copier(out, in); // copies sound into i2s
15+
1416

1517
// Arduino Setup
1618
void setup(void) {
@@ -25,13 +27,13 @@ void setup(void) {
2527
config_in.sample_rate = sample_rate;
2628
config_in.bits_per_sample = 16;
2729
config_in.i2s_format = I2S_STD_FORMAT;
30+
config_in.is_master = true;
2831
config_in.port_no = 0;
29-
config_in.pin_ws = 12;
30-
config_in.pin_bck = 13;
31-
config_in.pin_data = 14;
32-
config_in.is_master = false;
33-
//config.pin_mck = 2; // wenn ESP master
34-
//config.fixed_mclk = ? //wenn ESP master
32+
config_in.pin_ws = 14;
33+
config_in.pin_bck = 15;
34+
config_in.pin_data = 16;
35+
// config_in.fixed_mclk = sample_rate * 256
36+
// config_in.pin_mck = 2
3537
in.begin(config_in);
3638

3739
// start I2S out
@@ -41,15 +43,15 @@ void setup(void) {
4143
config_out.i2s_format = I2S_STD_FORMAT;
4244
config_out.is_master = true;
4345
config_out.port_no = 1;
44-
config_out.pin_ws = 32;
45-
config_out.pin_bck = 35;
46-
config_out.pin_data = 34;
46+
config_out.pin_ws = 17;
47+
config_out.pin_bck = 18;
48+
config_out.pin_data = 19;
4749
out.begin(config_out);
4850

4951
Serial.println("I2S started...");
5052
}
5153

5254
// Arduino loop - copy sound to out
5355
void loop() {
54-
copier.copy(scaler);
56+
copier.copy();
5557
}

examples/sandbox/streams-adc-a2dp/streams-adc-a2dp.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* @author Phil Schatzmann
44
* @brief We use a mcp6022 analog microphone as input and send the data to A2DP
55
* see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/examples-stream/streams-adc-a2dp/README.md
6-
*
7-
* @author Phil Schatzmann
86
* @copyright GPLv3
7+
* #TODO retest is outstanding
98
*
109
*/
1110
// Add this in your sketch or change the setting in AudioConfig.h
@@ -18,7 +17,7 @@
1817
AnalogAudioStream in; // analog mic
1918
A2DPStream out = A2DPStream::instance() ; // A2DP output - A2DPStream is a singleton!
2019
StreamCopy copier(out, in); // copy in to out
21-
ConverterAutoCenter<int16_t> center; // The data has a center of around 26427, so we we need to shift it down to bring the center to 0
20+
ConverterAutoCenter<int16_t> center(2); // The data has a center of around 26427, so we we need to shift it down to bring the center to 0
2221

2322
// Arduino Setup
2423
void setup(void) {

0 commit comments

Comments
 (0)