-
-
Notifications
You must be signed in to change notification settings - Fork 309
Volume Control
Sound is propagating in the form of waves. When talking about sound waves, the volume is the perception of loudness from the intensity of a sound wave. The higher the intensity of a sound, the louder it is perceived in our ears, and the higher volume it has.
If you represent the sound with an int16_t then at maximum volume the values as oscillating between +-32767. To decrease the volume you can just decrease this amplitude by multiplying it with a value < 1.0. If you multiply with 0.0 you have no sound - if you multiply by 0.5 you have halve the volume.
You can use the ConverterScaler class to set the volume. Here is an example sketch which just sends the output :
#include "AudioTools.h"
using namespace audio_tools;
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
CsvStream<sound_t> printer(Serial, channels); // ASCII stream
StreamCopy copier(printer, sound); // copies sound into printer
ConverterScaler<int16_t> volume(1.0, 0, 32767); // volume control
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
// Setup sine wave
sineWave.begin(channels, sample_rate, N_B4);
}
// Arduino loop - repeated processing
void loop() {
copier.copy(volume);
}
You can adjust the volume now dynamically by calling volume.setFactor(0.0)
to mute the output or volume.setFactor(0.5)
to halve the volume...