Skip to content

Instantly share code, notes, and snippets.

@royaltm
Created August 21, 2018 14:49
Show Gist options
  • Save royaltm/87cca05dcd6b706ea16228d8dd454174 to your computer and use it in GitHub Desktop.
Save royaltm/87cca05dcd6b706ea16228d8dd454174 to your computer and use it in GitHub Desktop.
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;
}
}
@royaltm
Copy link
Author

royaltm commented Aug 21, 2018

The output is:

javac TestDBVolume.java
java TestDBVolume

volume: 0, db = -100
volume: 1, db = -100
volume: 2, db = -100
volume: 3, db = -100
volume: 4, db = -100
volume: 5, db = -100
volume: 6, db = -100
volume: 7, db = -100
volume: 8, db = -100
volume: 9, db = -100
volume: 10, db = -100
volume: 11, db = -100
volume: 12, db = -100
volume: 13, db = -100
volume: 14, db = -100
volume: 15, db = -100
volume: 16, db = -100
volume: 17, db = -100
volume: 18, db = -100
volume: 19, db = -100
volume: 20, db = 0

@royaltm
Copy link
Author

royaltm commented Aug 21, 2018

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