I have been trying to get some sort of db SPL conversion for the output volume coming from the android device. I know it is pretty much impossible for it to be a perfect db SPL level because of hardware and calibrations and stuff, but I just need a rough cut off so that I can give the user a warning when the volume of the media they are playing is too loud.
I found the getStreamVolumedB function in AudioManager, but I cannot interpret the results. All floats returned seem to be on a scale from 0 to 1 or 0 to 0.7ish, with 0 to 1 applying to voice calls, and 0 to 0.7 applying to media. I used my galaxy s8 to test it by playing spotify while I run the function, and instead of changing its float db calculation based on what is being outputted, it only seems to change based on the volume settings on the device.
Can someone help me interpret what exactly the function is outputting and how I can somehow convert that to db SPL, or get a safe hearing threshold? Also I'm totally new to android programming and also Java, so I could be using the function wrong or something. If so, please let me know. Thanks!
I have attached code to the relevant class below
public class AudioStream {
private AudioManager audioManager;
//private AudioStream audioStreamVol;
private static final String TAG = "AudioStream";
private Set<Integer> deviceTypes =
new HashSet<>(Arrays.asList(AudioDeviceInfo.TYPE_BUILTIN_EARPIECE,
AudioDeviceInfo.TYPE_BUILTIN_SPEAKER,
AudioDeviceInfo.TYPE_WIRED_HEADSET,
AudioDeviceInfo.TYPE_WIRED_HEADPHONES,
AudioDeviceInfo.TYPE_LINE_ANALOG,
AudioDeviceInfo.TYPE_LINE_DIGITAL,
AudioDeviceInfo.TYPE_BLUETOOTH_SCO,
AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
AudioDeviceInfo.TYPE_HDMI,
AudioDeviceInfo.TYPE_HDMI_ARC,
AudioDeviceInfo.TYPE_USB_DEVICE,
AudioDeviceInfo.TYPE_USB_ACCESSORY,
AudioDeviceInfo.TYPE_DOCK,
AudioDeviceInfo.TYPE_FM,
AudioDeviceInfo.TYPE_BUILTIN_MIC,
AudioDeviceInfo.TYPE_TELEPHONY,
AudioDeviceInfo.TYPE_AUX_LINE,
AudioDeviceInfo.TYPE_IP,
AudioDeviceInfo.TYPE_BUS,
AudioDeviceInfo.TYPE_USB_HEADSET,
AudioDeviceInfo.TYPE_HEARING_AID));
public int findDeviceType() {
final AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo device : devices) {
final int type = device.getType();
if (deviceTypes.contains(type)) {
return type;
}
}
return -1;
}
public float audioStreamVol(Context context){
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int curType = (int) audioManager.STREAM_MUSIC;
int vol = audioManager.getStreamVolume(curType);
int deviceType = findDeviceType();
float volumedB = audioManager.getStreamVolumeDb(curType, vol, deviceType);
return volumedB;
}
}