-
Notifications
You must be signed in to change notification settings - Fork 2
Output of Sound
Here is the class diagram of the output classes.
You can call the tick() method on any STK Instrument to provide the next sound sample. The value is a float in the range of -1.0 to 1.0. that you can use as basis for the audio input of your microcontroller.
Unfortunately the audio output classes have not been standardized across the different microcontrollers. But usually you can use a Stream class accepting int16_t values, so I creates some Arduino specific classes to simplify the output. Here is the overview:
You can use the ArdStreamTextOut to visualize the generated sound in the Arduino Serial Plotter: The overall program logic looks as follows:
#include "StkAll.h"
int channels = 1
ArdStreamTextOut out(Serial, channels);
Clarinette clarinette;
...
loop(){
out.tick(clarinette.tick())
}
This will output 1 channel of audio data. If you increase the value to 2 the clarinette samples are copied into 2 output channels.
The ArdStreamOut class is used to convert the float values into int16_t values with n channels. In Arduino the I2S class is usually implemented as a subclass of Stream. So we can use it as follows:
#include "StkAll.h"
int channels = 2
ArdStreamOut out(I2S, 2);
Clarinette clarinette;
...
loop(){
out.tick(clarinette.tick())
}
This naturally only works if your Arduino implementation provides the I2S class and in your specific case it might be called slightly different. Do not forget to configure I2S as sample size of 16 and the sample rate that you determine with the help of the stk sampleRate() method!
In order to bring more standardisation across processors I started the Arduino Audio Tools project which will support different output methods (I2S, Analog, PWM) and microcontroller Architectures.
#include "AudioTools.h
#include "AudioLibs/AudioSTK.h"
Clarinette clarinette;
STKStream<Instrmnt> in(clarinette);
I2SStream out
StreamCopy copier(out, in);
...
setup(){
// setup input
auto icfg = in.defaultConfig();
in.begin(icfg);
// setup output
auto ocfg = out.defaultConfig(TX_MODE);
ocfg.copyFrom(icfg);
out.begin(ocfg);
}
loop(){
copier.copy();
}
Here the Stk framework is generating 1 channel of sound data to the I2SStream of the audiotools. Fortunately, we have a standardized way to configure output classes across processors.