-
-
Notifications
You must be signed in to change notification settings - Fork 278
Converting the Data Format
I am currently providing quite a few audio processing classes which can be used to change the audio format.
You can use the more generic FormatConverterStream to change of the
- bits_per_sample
- number of channels
- sample_rate
This class is supporting both: the conversion on the input and on the output side.
This is the recommended way and the propagation of changes of the AudioInfo should work automatically in all the cases.
#include "AudioTools.h"
SineWaveGenerator<int32_t> sine_wave; // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int32_t> in_stream(sine_wave); // Stream generated from sine wave
CsvStream<int16_t> out(Serial); // Output to Serial
AudioInfo from(44100, 2, 32);
AudioInfo to(44100, 2, 16);
FormatConverterStream conv(out);
StreamCopy copier(conv, in_stream); // copies sound to out
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
sine_wave.begin(from, N_B4);
in_stream.begin(from);
conv.begin(from, to);
out.begin(to);
}
void loop(){
copier.copy();
}
Here you need to be a bit more careful: If the input of the FormatConverterStream is an AudioStream you need to deactivate the automatic notification by casting the parameter to a Stream. This is necessary because the system can not know if you intend to use the Stream as input (w/o notification) or as output (with notification)!
#include "AudioTools.h"
SineWaveGenerator<int32_t> sine_wave; // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int32_t> in_stream(sine_wave); // Stream generated from sine wave
CsvStream<int16_t> out(Serial); // Output to Serial
AudioInfo from(44100, 2, 32);
AudioInfo to(44100, 2, 16);
FormatConverterStream conv((Stream&)in_stream);
StreamCopy copier(out, conv); // copies converted sound to out
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
sine_wave.begin(from, N_B4);
in_stream.begin(from);
conv.begin(from, to);
out.begin(to);
}
void loop(){
copier.copy();
}
This class is very flexible and convenient to use. However if you need to save some milliseconds in your processing, it is a bit more efficient to use the dedicated class if you need to change only a single parameter: e.g. the sample_rate: