Could anyone help with getting Microphone audio levels on B4J?
Inline Java is also fine.
Been searching for some time, but not getting correct answers....
The below is the closest I've got.
Logs: rms value is: 1.5199067E-5
Errors: javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
Inline Java is also fine.
Been searching for some time, but not getting correct answers....
The below is the closest I've got.
B4X:
public static double GetLevels() {
AudioFormat fmt = new AudioFormat(44100f, 16, 2, true, false);
//AudioFormat fmt = getAudioFormat();
final int bufferByteSize = 2048;
TargetDataLine line;
try {
line = AudioSystem.getTargetDataLine(fmt);
line.open(fmt, bufferByteSize);
} catch(LineUnavailableException e) {
System.err.println(e);
return 0;
}
byte[] buf = new byte[bufferByteSize];
float[] samples = new float[bufferByteSize / 2];
float lastPeak = 0f;
line.start();
for(int b; (b = line.read(buf, 0, buf.length)) > -1;) {
// convert bytes to samples here
for(int i = 0, s = 0; i < b;) {
int sample = 0;
sample |= buf[i++] & 0xFF; // (reverse these two lines
sample |= buf[i++] << 8; // if the format is big endian)
samples[s++] = sample / 32768f; // normalize to range of +/-1.0f
}
float rms = 0f;
float peak = 0f;
for(float sample : samples) {
float abs = Math.abs(sample);
if(abs > peak) {
peak = abs;
}
rms += sample * sample;
}
rms = (float)Math.sqrt(rms / samples.length);
System.out.println("rms value is: " + rms);
return rms;
}
return 0;
}
Logs: rms value is: 1.5199067E-5
Errors: javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.