How to apply a Note with Duration to the I2S-Output Stream #880
-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Why don't just just create your own method like play(float note, int duration) in your sketch ? void play(float frequency, int durationMs){
sine.setFrequency(frequency);
copier.copyMs(durationMs, audioInfo);
} You will need to use the latest update or replace copyMs() with CopyN and calculate the number of "pages" yourself |
Beta Was this translation helpful? Give feedback.
-
😊 Thanks for the response to my question! |
Beta Was this translation helpful? Give feedback.
-
Not really sure what your question is: if you want to play the tone for 1 second you would call copyMs(1000.. as 1 second has 1000 milliseconds. To use copyN you just need to calculate the number of bytes that you need to generate. If you use a sample rate with 44100 samples per second, 2 channels and 16 bits per sample it is quite simple to calculate the bytes per second: If you look at the constructor of StreamCopy you can indicate the number of bytes that are processed with each copy call. If you don't specify anything it is using the default value of 1024. So calling copyN(176400/1024) also plays for 1 second. Here is a complete simple sketch #include "AudioTools.h"
AudioInfo info(32000,2, 16);
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
I2SStream out;
StreamCopy copier(out, sound); // copies sound into i2s
void play(float frequency, int durationMs){
sine.setFrequency(frequency);
copier.copyMs(durationMs, info);
// same as
// copy.copyN(info.sample_rate*info.channels*(info.bits_per_sample/8)*durationMs/1000 / copy.bufferSize());
}
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
// start I2S
Serial.println("starting I2S...");
auto config = out.defaultConfig(TX_MODE);
config.copyFrom(info);
out.begin(config);
// Setup sine wave
sineWave.begin(info, N_B4);
Serial.println("started...");
}
// Arduino loop
void loop() {
play(C4, 4000); // play frequency of C4 (261.63 hz) for 4 seconds
play(G3, 8000);
} |
Beta Was this translation helpful? Give feedback.
-
😊 Thanks for a wonderful reply back 👍 |
Beta Was this translation helpful? Give feedback.
Why don't just just create your own method like play(float note, int duration) in your sketch ?
You will need to use the latest update or replace copyMs() with CopyN and calculate the number of "pages" yourself