pschatzmann / arduino-audio-tools

Arduino Audio Tools (a powerful Audio library not only for Arduino)
GNU General Public License v3.0
1.27k stars 199 forks source link

Setting volume for MemoryStream doesn't work #1539

Closed SalahuddinAsh closed 1 month ago

SalahuddinAsh commented 1 month ago

Problem Description

I'm trying to control the volume of an mp3 music saved as a memorystream.

Setting volume to 1.0 works and the audio get played. but setting it to other values between 0 and 1 makes the audio not get played. I'm not sure how to control volume here.

I checked the wiki page for set-volume but it doesn't explain how to use it for memorystream: https://github.com/pschatzmann/arduino-audio-tools

I checked this issue # 17 but don't understand how to use it in my case. https://github.com/pschatzmann/arduino-audio-tools/issues/17

Device Description

esp32-s3

Sketch

MemoryStream mp3_notify(ding_mp3, ding_mp3_len);
I2SStream i2s;
MP3DecoderHelix helix;
EncodedAudioStream out(&i2s, &helix); // output to decoder
StreamCopy copier_notify(out, mp3_notify); // copy in to i2s
ConverterScaler<int16_t> volume(1.0, 0, 32767);

void initAudio()
{
  auto config = i2s.defaultConfig(TX_MODE);
  config.sample_rate = 48000;
  config.channels = 2;
  config.bits_per_sample = 16;
  config.i2s_format = I2S_STD_FORMAT;
  config.pin_ws = AUDIO_WS_LRC; // LRC
  config.pin_bck = AUDIO_BCK;
  config.pin_data = AUDIO_DATA;
  config.use_apll = true;
  i2s.begin(config);
  out.begin();
}

void playSound()
{
    // volume.setFactor(0.8); 
    mp3_notify.begin();
    while (mp3_notify.available())
    {
      copier_notify.copy(volume);
    }
    mp3_notify.end();
}

Other Steps to Reproduce

No response

What is your development environment

No response

I have checked existing issues, discussions and online documentation

pschatzmann commented 1 month ago

It is a big mistake to multiply unencoded MP3 data with a values! You can only adjust the volume on PCM data with the help of multiply as described in the Wiki.

I would recommend to use a VolumeStream e.g. with the newly supported pipelines

You could also change the input chain so that you copy the decoded data: MemoryStream -> EncodedAudioStream - copy -> I2SStream

SalahuddinAsh commented 1 month ago

Thanks for your quick response. I have no idea about the audio concept and I only use an example code from your great library.

Therefore I would be grateful if you give me a link for an example or document to explain what you mean with VolumeStream and the newly supported pipelines.

Also, does changing the input change will allow me to change the volume?

Thanks

SalahuddinAsh commented 1 month ago

I'll try going through your blog to learn more about the library.