Skip to content

Commit 2e14a9c

Browse files
committed
2 parents 63143d2 + a69cdb1 commit 2e14a9c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Driver.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ class AudioDriver {
322322
DriverPins *p_pins = nullptr;
323323
int i2c_default_address = -1;
324324

325+
int mapVolume(int x, int in_min, int in_max, int out_min, int out_max) {
326+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
327+
}
328+
325329
/// Determine the TwoWire object from the I2C config or use Wire
326330
virtual i2c_bus_handle_t getI2C() {
327331
if (p_pins == nullptr) return DEFAULT_WIRE;
@@ -557,7 +561,7 @@ class AudioDriverCS42448Class : public AudioDriver {
557561

558562
int getVolume() override { return volume; }
559563
bool setInputVolume(int volume) override {
560-
int vol = map(volume, 0, 100, -128, 127);
564+
int vol = mapVolume(volume, 0, 100, -128, 127);
561565
return cs42448.setVolumeADC(vol);
562566
}
563567
bool isVolumeSupported() override { return true; }
@@ -834,7 +838,7 @@ class AudioDriverES8388Class : public AudioDriver {
834838
}
835839

836840
bool setInputVolume(int volume) {
837-
// map values from 0 - 100 to 0 to 8
841+
// mapVolume values from 0 - 100 to 0 to 8
838842

839843
// es_mic_gain_t: MIC_GAIN_MIN = -1, 0,3,6,9,12,15,18,21,24 MIC_GAIN_MAX =
840844
// 25
@@ -861,7 +865,7 @@ class AudioDriverES8388Class : public AudioDriver {
861865
MIC_GAIN_18DB, MIC_GAIN_21DB, MIC_GAIN_24DB};
862866

863867
int vol = limitValue(volume, 0, 100);
864-
int idx = map(vol, 0, 100, 0, 8);
868+
int idx = mapVolume(vol, 0, 100, 0, 8);
865869

866870
es_mic_gain_t gain = gains[idx];
867871
AD_LOGD("input volume: %d -> gain %d [dB] (idx: %d of 0..8)", volume, gain,
@@ -964,15 +968,15 @@ class AudioDriverWM8960Class : public AudioDriver {
964968
/// Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
965969
bool setVolume(int volume) {
966970
volume_out = limitValue(volume, 0, 100);
967-
int vol_int = volume_out == 0.0 ? 0 : map(volume_out, 0, 100, 30, 0x7F);
971+
int vol_int = volume_out == 0.0 ? 0 : mapVolume(volume_out, 0, 100, 30, 0x7F);
968972
return mtb_wm8960_set_output_volume(vol_int);
969973
}
970974

971975
int getVolume() { return volume_out; }
972976

973977
bool setInputVolume(int volume) {
974978
volume_in = limitValue(volume, 0, 100);
975-
int vol_int = map(volume_in, 0, 100, 0, 30);
979+
int vol_int = mapVolume(volume_in, 0, 100, 0, 30);
976980
return mtb_wm8960_adjust_input_volume(vol_int);
977981
}
978982
bool isVolumeSupported() { return true; }
@@ -1147,7 +1151,7 @@ class AudioDriverWM8978Class : public AudioDriver {
11471151

11481152
/// Mute line 0 = speaker, line 1 = headphones
11491153
bool setMute(bool mute, int line) override {
1150-
int scaled = mute ? 0 : map(volume, 0, 100, 0, 63);
1154+
int scaled = mute ? 0 : mapVolume(volume, 0, 100, 0, 63);
11511155
switch (line) {
11521156
case 0:
11531157
wm8078.setSPKvol(scaled);
@@ -1175,7 +1179,7 @@ class AudioDriverWM8978Class : public AudioDriver {
11751179

11761180
bool setVolume(int volume) override {
11771181
this->volume = volume;
1178-
int scaled = map(volume, 0, 100, 0, 63);
1182+
int scaled = mapVolume(volume, 0, 100, 0, 63);
11791183
wm8078.setSPKvol(scaled);
11801184
wm8078.setHPvol(scaled, scaled);
11811185
return true;
@@ -1184,7 +1188,7 @@ class AudioDriverWM8978Class : public AudioDriver {
11841188
int getVolume() override { return volume; }
11851189

11861190
bool setInputVolume(int volume) override {
1187-
int scaled = map(volume, 0, 100, 0, 63);
1191+
int scaled = mapVolume(volume, 0, 100, 0, 63);
11881192
wm8078.setMICgain(scaled);
11891193
wm8078.setAUXgain(scaled);
11901194
wm8078.setLINEINgain(scaled);
@@ -1254,7 +1258,7 @@ class AudioDriverWM8994Class : public AudioDriver {
12541258
setPAPower(true);
12551259
delay(10);
12561260
p_pins = &pins;
1257-
int vol = map(volume, 0, 100, DEFAULT_VOLMIN, DEFAULT_VOLMAX);
1261+
int vol = mapVolume(volume, 0, 100, DEFAULT_VOLMIN, DEFAULT_VOLMAX);
12581262
uint32_t freq = codecCfg.getRateNumeric();
12591263
uint16_t outputDevice = getOutput(codec_cfg.output_device);
12601264

@@ -1269,7 +1273,7 @@ class AudioDriverWM8994Class : public AudioDriver {
12691273

12701274
bool setVolume(int volume) {
12711275
this->volume = volume;
1272-
int vol = map(volume, 0, 100, DEFAULT_VOLMIN, DEFAULT_VOLMAX);
1276+
int vol = mapVolume(volume, 0, 100, DEFAULT_VOLMIN, DEFAULT_VOLMAX);
12731277
return wm8994_SetVolume(getI2CAddress(), vol) == 0;
12741278
}
12751279
int getVolume() { return volume; }

0 commit comments

Comments
 (0)