Created
August 21, 2018 14:49
-
-
Save royaltm/87cca05dcd6b706ea16228d8dd454174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestDBVolume { | |
static int maxVolumeLevel = 20; | |
static int minDBLevel = -100; | |
public static void main(String[] args) { | |
for (int volume = 0; volume <= maxVolumeLevel; ++volume) { | |
int volumeInDecibels = calulateDSPVolume(volume); | |
String output = String.format("volume: %d, db = %d", volume, volumeInDecibels); | |
System.out.println(output); | |
} | |
} | |
static private int calulateDSPVolume(int volume) { | |
if (maxVolumeLevel == 0) { | |
return 0; | |
} | |
int volumeInDecibels = (int) ((1 - Math.pow((volume / maxVolumeLevel), 0.125)) * (minDBLevel)); | |
return volumeInDecibels; | |
} | |
} |
when fixed:
int volumeInDecibels = (int) ((1 - Math.pow(((float)volume / maxVolumeLevel), 0.125)) * (minDBLevel));
$ java TestDBVolume
volume: 0, db = -100
volume: 1, db = -31
volume: 2, db = -25
volume: 3, db = -21
volume: 4, db = -18
volume: 5, db = -15
volume: 6, db = -13
volume: 7, db = -12
volume: 8, db = -10
volume: 9, db = -9
volume: 10, db = -8
volume: 11, db = -7
volume: 12, db = -6
volume: 13, db = -5
volume: 14, db = -4
volume: 15, db = -3
volume: 16, db = -2
volume: 17, db = -2
volume: 18, db = -1
volume: 19, db = 0
volume: 20, db = 0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output is: