-
Notifications
You must be signed in to change notification settings - Fork 2
Output of Sound
Phil Schatzmann edited this page Dec 25, 2021
·
24 revisions
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. In order
You can use the ArdStreamTextOut to visualize the generated sound in the Arduino Serial Plotter: The overall program logic looks as follows:
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.
## Output of Sound using ArdStreamOut
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:
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
## Arduino Audio Tools
In order to bring more standardisation across processors I started the [Arduino Audio Tools](https://github.com/pschatzmann/arduino-audio-tools) project which will support different output methods and microcontroller Architectures.
int channels = 2
I2SStream i2s
ArdStreamOut out(i2s, 1);
Clarinette clarinette;
...
setup(){
auto cfg = i2s.defaultConfig()
cfg.channels = 1;
cfg.sample_rate = clarinette.sampleRate();
i2s.begin(cfg);
}
loop(){
out.tick(clarinette.tick())
}