pschatzmann / arduino-audio-tools

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

Feature Request #17

Closed Dweaver309 closed 3 years ago

Dweaver309 commented 3 years ago

Your libs are great I am using them in my new "Jukebox" music play biased on your file_mp3-a2dp.ino sketch. Is it possible to control the volume to the Bluetooth device? If it can be done please post an example. Thanks for the great work.

pschatzmann commented 3 years ago

The generic method to set the volume is by multiplying the input data with a factor (between 0 and 1).

Have a look at the ConverterScaler class. This can be passed as argument to the copy method. The volume can then be controlled by calling setFactor(0.0) for mute or setFactor(1.0) to set the max volume. Tomorrow I might be able to make an example...

Dweaver309 commented 3 years ago

Thanks, I'm looking forward to seeing the example.

On Sat, Sep 18, 2021 at 12:31 PM Phil Schatzmann @.***> wrote:

Have a look at the ConverterScaler class. This can be passed as argument to the copy method. The volume can then be controlled by calling setFactor(0.0) for mute or setFactor(1.0) to set the max volume. Tomorrow I might be able to make an example...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pschatzmann/arduino-audio-tools/issues/17#issuecomment-922343926, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5AFHGCS6LMDHESXMQQJBTUCTEHDANCNFSM5EHTZBCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

pschatzmann commented 3 years ago

Have a look at https://github.com/pschatzmann/arduino-audio-tools/wiki/Volume-Control

Dweaver309 commented 3 years ago

I am not sure I understand the example if you have time could you post an example using your file_mp3-a2dp.ino sketch. Thanks again.

pschatzmann commented 3 years ago

Oh, I see you did not use the Stream API. But for file_mp3-a2dp.ino it is easy as well:

You just change the sketch to add

float volume = 1.0

// callback used by A2DP to provide the sound data
int32_t get_sound_data(Channels* data, int32_t len) {  
  // set volume
  for (int j=0;j<len;j++){
    data[j].channel1 = data[j].channel1 * volume;
    data[j].channel2 = data[j].channel2 * volume;
  }
  return out == nullptr ? 0 : out->read(data, len);
}

Changing the volume is then just setting the volume variable to a value between 1.0 and 1.0

Dweaver309 commented 3 years ago

I set the volume to 0.0 and that to 1.0 using a float volume = 0.0 then float volume = 1.0. The loudness did not change. Do you think the two bluetooth devices I am using do not work with the call back?

Thanks for all the help

On Mon, Sep 20, 2021 at 1:37 PM Phil Schatzmann @.***> wrote:

Oh, I see you did not use the Stream API. But for file_mp3-a2dp.ino it is easy as well:

You just change the sketch to add

float volume = 1.0

// callback used by A2DP to provide the sound data int32_t get_sound_data(Channels data, int32_t len) { // set volume for (int j=0;j<len;j++){ data[j].channel1 = data[j].channel1 volume; data[j].channel2 = data[j].channel2 * volume; } return out == nullptr ? 0 : out->read(data, len); }

Changing the volume is then just setting the volume variable to a value between 1.0 and 1.0

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pschatzmann/arduino-audio-tools/issues/17#issuecomment-923179704, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5AFHGQKUMNIGUXB6KSTULUC55NZANCNFSM5EHTZBCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

pschatzmann commented 3 years ago

I hope the following works:

volatile float volume = 1.0

// callback used by A2DP to provide the sound data
int32_t get_sound_data(Channels* data, int32_t len) {  
  // get data
  if out == 0 return 0;
  int32_t result = out->read(data, len)
  // set volume
  for (int j=0;j<len;j++){
    data[j].channel1 = data[j].channel1 * volume;
    data[j].channel2 = data[j].channel2 * volume;
  }
  return result;
}
Dweaver309 commented 3 years ago

Tried volatile ... Serial.print(volume) returns the correct change?

On Tue, Sep 21, 2021 at 2:09 AM Phil Schatzmann @.***> wrote:

Does it help if you use volatile float volume = 1.0 ? If not try to to check with Serial.print(volume) that the values are really updated

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pschatzmann/arduino-audio-tools/issues/17#issuecomment-923701773, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5AFHAOWZRC5JV5PGRW2L3UDAVSLANCNFSM5EHTZBCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

pschatzmann commented 3 years ago

Did you use the corrected version of get_sound_data() ?

Dweaver309 commented 3 years ago

I will update all my libs now and try. Thanks

On Tue, Sep 21, 2021 at 8:53 AM Phil Schatzmann @.***> wrote:

Did you use the corrected version of get_sound_data() ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pschatzmann/arduino-audio-tools/issues/17#issuecomment-924012233, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5AFHBJXA4GMPWE36ILVV3UDCE5XANCNFSM5EHTZBCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Dweaver309 commented 3 years ago

I just updated my arduino-audio-tools lib This is my get_sound_data:

// callback used by A2DP to provide the sound data int32_t get_sound_data(Channels data, int32_t len) { // set volume for (int j = 0; j < len; j++) { data[j].channel1 = data[j].channel1 volume; data[j].channel2 = data[j].channel2 * volume; }

return out == nullptr ? 0 : out->read(data, len); }

On Tue, Sep 21, 2021 at 8:54 AM david weaver @.***> wrote:

I will update all my libs now and try. Thanks

On Tue, Sep 21, 2021 at 8:53 AM Phil Schatzmann @.***> wrote:

Did you use the corrected version of get_sound_data() ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pschatzmann/arduino-audio-tools/issues/17#issuecomment-924012233, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5AFHBJXA4GMPWE36ILVV3UDCE5XANCNFSM5EHTZBCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Dweaver309 commented 3 years ago

I just updated my arduino-audio-tools lib This is my get_sound_data: // callback used by A2DP to provide the sound data int32_t get_sound_data(Channels data, int32_t len) { // set volume for (int j = 0; j < len; j++) { data[j].channel1 = data[j].channel1 volume; data[j].channel2 = data[j].channel2 * volume; }

return out == nullptr ? 0 : out->read(data, len); }

pschatzmann commented 3 years ago

Nop that won't work.... I suggest that you use the version from 7 hours ago

Dweaver309 commented 3 years ago

The version posted 7 hours ago works great!

Thanks for all your help