Skip to content

Commit c5347c3

Browse files
authored
Add left and right volume balance enhancement (#53)
Enhancement for adjusting the left and right volume balance. by: fabitom
1 parent 0d3e4a4 commit c5347c3

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/VS1053.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,32 @@ void VS1053::begin() {
189189
void VS1053::setVolume(uint8_t vol) {
190190
// Set volume. Both left and right.
191191
// Input value is 0..100. 100 is the loudest.
192-
uint16_t value; // Value to send to SCI_VOL
192+
uint8_t valueL, valueR; // Values to send to SCI_VOL
193193

194194
curvol = vol; // Save for later use
195-
value = map(vol, 0, 100, 0xFF, 0x00); // 0..100% to one channel
196-
value = (value << 8) | value;
197-
write_register(SCI_VOL, value); // Volume left and right
195+
valueL = vol;
196+
valueR = vol;
197+
198+
if (curbalance < 0) {
199+
valueR = max(0, vol + curbalance);
200+
} else if (curbalance > 0) {
201+
valueL = max(0, vol - curbalance);
202+
}
203+
204+
valueL = map(valueL, 0, 100, 0xFE, 0x00); // 0..100% to left channel
205+
valueR = map(valueR, 0, 100, 0xFE, 0x00); // 0..100% to right channel
206+
207+
write_register(SCI_VOL, (valueL << 8) | valueR); // Volume left and right
208+
}
209+
210+
void VS1053::setBalance(int8_t balance) {
211+
if (balance > 100) {
212+
curbalance = 100;
213+
} else if (balance < -100) {
214+
curbalance = -100;
215+
} else {
216+
curbalance = balance;
217+
}
198218
}
199219

200220
void VS1053::setTone(uint8_t *rtone) { // Set bass/treble (4 nibbles)
@@ -212,6 +232,10 @@ uint8_t VS1053::getVolume() { // Get the currenet volume setting.
212232
return curvol;
213233
}
214234

235+
int8_t VS1053::getBalance() { // Get the currenet balance setting.
236+
return curbalance;
237+
}
238+
215239
void VS1053::startSong() {
216240
sdi_send_fillers(10);
217241
}

src/VS1053.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class VS1053 {
4444
uint8_t dcs_pin; // Pin where DCS line is connected
4545
uint8_t dreq_pin; // Pin where DREQ line is connected
4646
uint8_t curvol; // Current volume setting 0..100%
47+
int8_t curbalance = 0; // Current balance setting -100..100
48+
// (-100 = right channel silent, 100 = left channel silent)
4749
const uint8_t vs1053_chunk_size = 32;
4850
// SCI Register
4951
const uint8_t SCI_MODE = 0x0;
@@ -127,12 +129,18 @@ class VS1053 {
127129
// Set the player volume.Level from 0-100, higher is louder
128130
void setVolume(uint8_t vol);
129131

132+
// Adjusting the left and right volume balance, higher to enhance the right side, lower to enhance the left side.
133+
void setBalance(int8_t balance);
134+
130135
// Set the player baas/treble, 4 nibbles for treble gain/freq and bass gain/freq
131136
void setTone(uint8_t *rtone);
132137

133138
// Get the currenet volume setting, higher is louder
134139
uint8_t getVolume();
135140

141+
// Get the currenet balance setting (-100..100)
142+
int8_t getBalance();
143+
136144
// Print configuration details to serial output.
137145
void printDetails(const char *header);
138146

0 commit comments

Comments
 (0)