-
-
Notifications
You must be signed in to change notification settings - Fork 286
Resampling
Phil Schatzmann edited this page Mar 7, 2022
·
28 revisions
We have implemented some simple functionality to resample from an input rate to a output rate. This can be achieved with the help of the ResampleStream class. We can resample both on the intput and on the output side:
We wrap the original input stream in a ResampleStream. In the configuration we indicate the from rate and the target rate. In this example we perform a upsampling because the target rate is double the input rate.
uint16_t sample_rate=44100;
uint8_t channels = 2; // The stream will have 2 channels
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
ResampleStream<int16_t> resample(sound);
CsvStream<int16_t> out(Serial);
StreamCopy copier(out, resample); // copies sound to out
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
// define resample
auto cfgr = resample.defaultConfig();
cfgr.channels = channels;
cfgr.sample_rate_from = sample_rate;
cfgr.sample_rate = 88200;
resample.begin(cfgr);
// Define CSV Output
auto config = out.defaultConfig();
config.sample_rate = sample_rate;
config.channels = channels;
out.begin(config);
// Setup sine wave
sineWave.begin(channels, sample_rate, N_B4);
Serial.println("started...");
}
// Arduino loop - copy sound to out
void loop() {
copier.copy();
}
We can achieve the same result on the output side: We wrap the target output stream in a ResampleStream. In the configuration we indicate the from rate and the target rate. In this example we perform a upsampling because the target rate is double the input rate.
uint16_t sample_rate=44100;
uint8_t channels = 2; // The stream will have 2 channels
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> in(sineWave); // Stream generated from sine wave
CsvStream<int16_t> out(Serial);
ResampleStream<int16_t> resample(out);
StreamCopy copier(resample, in); // copies sound to out
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
// define resample
auto cfgr = resample.defaultConfig();
cfgr.channels = channels;
cfgr.sample_rate_from = sample_rate;
cfgr.sample_rate = 88200;
resample.begin(cfgr);
// Define CSV Output
auto config = out.defaultConfig();
config.sample_rate = sample_rate;
config.channels = channels;
out.begin(config);
// Setup sine wave
sineWave.begin(channels, sample_rate, N_B4);
Serial.println("started...");
}
// Arduino loop - copy sound to out
void loop() {
copier.copy();
}