baldram/ESP_VS1053_Library

setVolume values / question

h1aji opened this issue · 1 comments

h1aji commented

I was comparing setVolume

void VS1053::setVolume(uint8_t vol) {
// Set volume. Both left and right.
// Input value is 0..100. 100 is the loudest.
uint8_t valueL, valueR; // Values to send to SCI_VOL
curvol = vol; // Save for later use
valueL = vol;
valueR = vol;
if (curbalance < 0) {
valueR = max(0, vol + curbalance);
} else if (curbalance > 0) {
valueL = max(0, vol - curbalance);
}
valueL = map(valueL, 0, 100, 0xFE, 0x00); // 0..100% to left channel
valueR = map(valueR, 0, 100, 0xFE, 0x00); // 0..100% to right channel
writeRegister(SCI_VOL, (valueL << 8) | valueR); // Volume left and right
}
void VS1053::setBalance(int8_t balance) {
if (balance > 100) {
curbalance = 100;
} else if (balance < -100) {
curbalance = -100;
} else {
curbalance = balance;
}
}

with the code from Adafruit

void Adafruit_VS1053::setVolume(uint8_t left, uint8_t right) {
  // accepts values between 0 and 255 for left and right.
  uint16_t v;
  v = left;
  v <<= 8;
  v |= right;

  noInterrupts(); // cli();
  sciWrite(VS1053_REG_VOLUME, v);
  interrupts(); // sei();
}

And found that maximum value for setVolume is 254. So, which value would be correct in this case?

The map function maps the values from 0-100 to 0-254 (= 0xFE ), see
https://www.arduino.cc/reference/de/language/functions/math/map/
So basically both funtions do the same. Using a range from 0-100 is of course coarser, but imho is more intuitive and does not really make a difference.