Im my last couple of Blogs, I have demonstrated how we can use the low level API of my Arduino Audio Tools library. Currently I am busy working on a high level API using Arduino Streams.
I did not commit my latest changes yet but I want to take the opportunity to share some more examples: We all know the Arduino Streams. We use them to write out print messages and sometimes we use them to read the output from Serial devices.
The same thing applies to my “Audio Streams”: You can read audio data from “Audio Sources” and you write them to “Audio Sinks”.
Sometimes it is quite useful to be able to generate a test tone. We can use the GeneratedSoundStream class together with an implementation of the SoundGenerator class. In my example I use a SineWaveGenerator.
In order to make sure that we use a consistent data type in the sound processing I also used a typedef int16_t sound_t;
. You could experiment with some other data types.
To be able to verify the result I used CvsStream<sound_t> printer(Serial, channels);
which generates CSV output and sends it to Serial.
#include "AudioTools.h"
using namespace audio_tools;
typedef int16_t sound_t; // sound will be represented as int16_t (with 2 bytes)
uint16_t sample_rate=44100;
uint8_t channels = 2; // The stream will have 2 channels
SineWaveGenerator<sound_t> sineWave(32000); // subclass of SoundGenerator
GeneratedSoundStream<sound_t> sound(sineWave, channels); // Stream generated from sine wave
CvsStream<sound_t> printer(Serial, channels); // ASCII stream
StreamCopy copier(printer, sound); // copies sound into printer
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
// Setup sine wave
sineWave.begin(sample_rate, B4);
}
// Arduino loop - repeated processing
void loop() {
copier.copy();
}
Here is the result on the Arduino Serial Plotter:
0 Comments