Skip to content

Commit 6b6e2f1

Browse files
committed
GeneratorFromArray
1 parent fb61674 commit 6b6e2f1

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file streams-generator_fromarray-audiokit.ino
3+
* @brief Tesing GeneratorFromArray with output on audiokit
4+
* @author Phil Schatzmann
5+
* @copyright GPLv3
6+
*/
7+
8+
#include "AudioTools.h"
9+
#include "AudioLibs/AudioKit.h"
10+
11+
uint16_t sample_rate=44100
12+
;
13+
uint8_t channels = 2; // The stream will have 2 channels
14+
int16_t sine_array[] = {0, 2285, 4560, 6812, 9031, 11206, 13327, 15383, 17363, 19259, 21062, 22761, 24350, 25820, 27165, 28377, 29450, 30381, 31163, 31793, 32269, 32587, 32747, 32747, 32587, 32269, 31793, 31163, 30381, 29450, 28377, 27165, 25820, 24350, 22761, 21062, 19259, 17363, 15383, 13327, 11206, 9031, 6812, 4560, 2285, 0, -2285, -4560, -6812, -9031, -11206, -13327, -15383, -17363, -19259, -21062, -22761, -24350, -25820, -27165, -28377, -29450, -30381, -31163, -31793, -32269, -32587, -32747, -32747, -32587, -32269, -31793, -31163, -30381, -29450, -28377, -27165, -25820, -24350, -22761, -21062, -19259, -17363, -15383, -13327, -11206, -9031, -6812, -4560, -2285 };
15+
GeneratorFromArray<int16_t> sineWave(sine_array,0,false);
16+
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
17+
AudioKitStream out;
18+
19+
StreamCopy copier(out, sound); // copies sound into i2s
20+
21+
// Arduino Setup
22+
void setup(void) {
23+
// Open Serial
24+
Serial.begin(115200);
25+
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
26+
27+
// start I2S
28+
Serial.println("starting I2S...");
29+
auto config = out.defaultConfig(TX_MODE);
30+
config.sample_rate = sample_rate;
31+
config.channels = channels;
32+
config.bits_per_sample = 16;
33+
out.begin(config);
34+
35+
// Setup sine wave
36+
auto cfg = sineWave.defaultConfig();
37+
cfg.channels = channels;
38+
cfg.sample_rate = sample_rate;
39+
sineWave.begin(cfg);
40+
Serial.println("started...");
41+
}
42+
43+
// Arduino loop - copy sound to out
44+
void loop() {
45+
copier.copy();
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# I2S Analog Output Test
2+
3+
This is a simple basic test for the ESP32 __analog output__ using I2S.
4+
5+
We just send a generated sine wave and expect to hear a clean signal.
6+
Please note the log level should be set so that there is no disturbing output!
7+
8+
9+
### Output Device: Piezo Electric Element
10+
11+
To test the output I am using a piezo electric element
12+
13+
![DAC](https://pschatzmann.github.io/arduino-audio-tools/resources/piezo.jpeg)
14+
15+
It should also be possible to connect a headphone to the output pins...
16+
17+
18+
On the ESP32 the output is on the Pins GPIO26 and GPIO27
19+
20+
| PIEZO | ESP32
21+
| --------| ---------------
22+
| + | GPIO25 / GPIO26
23+
| - | GND
24+
25+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file streams-generator_fromarray-analog.ino
3+
* @author Phil Schatzmann
4+
* @brief Example for GeneratorFromArray
5+
* @version 0.1
6+
* @date 2022-03-22
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
*/
11+
#include "AudioTools.h"
12+
#include "AudioLibs/AudioKit.h"
13+
14+
uint16_t sample_rate=44100;
15+
uint8_t channels = 1; // The stream will have 2 channels
16+
int16_t sine_array[] = {0, 4560, 9031, 13327, 17363, 21062, 24350, 27165, 29450, 31163, 32269, 32747, 32587, 31793, 30381, 28377, 25820, 22761, 19259, 15383, 11206, 6812, 2285, -2285, -6812, -11206, -15383, -19259, -22761, -25820, -28377, -30381, -31793, -32587, -32747, -32269, -31163, -29450, -27165, -24350, -21062, -17363, -13327, -9031, -4560 };
17+
GeneratorFromArray<int16_t> sineWave(sine_array,0,false);
18+
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
19+
AnalogAudioStream out;
20+
21+
StreamCopy copier(out, sound); // copies sound into i2s
22+
23+
// Arduino Setup
24+
void setup(void) {
25+
// Open Serial
26+
Serial.begin(115200);
27+
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
28+
29+
// start I2S
30+
Serial.println("starting I2S...");
31+
auto config = out.defaultConfig(TX_MODE);
32+
config.sample_rate = sample_rate;
33+
config.channels = channels;
34+
config.bits_per_sample = 16;
35+
out.begin(config);
36+
37+
// Setup sine wave
38+
auto cfg = sineWave.defaultConfig();
39+
cfg.channels = channels;
40+
cfg.sample_rate = sample_rate;
41+
sineWave.begin(cfg);
42+
Serial.println("started...");
43+
}
44+
45+
// Arduino loop - copy sound to out
46+
void loop() {
47+
copier.copy();
48+
}

src/AudioEffects/SoundGenerator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ class GeneratorFromArray : public SoundGenerator<T> {
415415
LOGI("table_length: %d", (int)size);
416416
}
417417

418+
virtual void begin(AudioBaseInfo info) {
419+
SoundGenerator<T>::begin(info);
420+
}
421+
418422
/// Starts the generation of samples
419423
void begin() override {
420424
LOGI(LOG_METHOD);

0 commit comments

Comments
 (0)