使用java从手机或pc中采样db
我很难从电脑或手机的麦克风中提取分贝,我只是想确保我在数学上和物理上都做对了 代码如下:使用java从手机或pc中采样db,java,audio,bytearray,microphone,decibel,Java,Audio,Bytearray,Microphone,Decibel,我很难从电脑或手机的麦克风中提取分贝,我只是想确保我在数学上和物理上都做对了 代码如下: package soundscale; import java.io.ByteArrayOutputStream; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.Li
package soundscale;
import java.io.ByteArrayOutputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class SoundScale{
public static void main(String[] args) {
AudioFormat format = new AudioFormat(8000f, 16, 1, true, false); TargetDataLine microphone;
SourceDataLine speakers;
try {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
int CHUNK_SIZE = 512;
byte[] data = new byte[microphone.getBufferSize()/5];
microphone.start();
int bytesRead = 0;
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
speakers.open(format);
speakers.start();
long number=0;
while (bytesRead < 10000000) {
numBytesRead = microphone.read(data, 0, CHUNK_SIZE);
bytesRead += numBytesRead;
out.write(data, 0, numBytesRead);
speakers.write(data, 0, numBytesRead);
number=convertToLong(data);//converting to unsigned long from data(amplitude)
number=(long) (20*Math.log10(number));//converting to db using amplitude
System.out.println(" db is "+(number));
}
speakers.drain();
speakers.close();
microphone.close();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static final long convertToLong(byte[] bytes) {//better way not to lose data
byte sum = 0;
long sum1=0;
for (byte b : bytes) {
sum ^= b;
}
return sum1=Byte.toUnsignedLong(sum);
}
}
包装音阶;
导入java.io.ByteArrayOutputStream;
导入javax.sound.sampled.AudioFormat;
导入javax.sound.sampled.AudioSystem;
导入javax.sound.sampled.DataLine;
导入javax.sound.sampled.LineUnavailableException;
导入javax.sound.sampled.SourceDataLine;
导入javax.sound.sampled.TargetDataLine;
公共级声级{
公共静态void main(字符串[]args){
AudioFormat格式=新的AudioFormat(8000f,16,1,真,假);TargetDataLine话筒;
数据线扬声器;
试一试{
DataLine.Info=newdataline.Info(TargetDataLine.class,格式);
麦克风=(TargetDataLine)AudioSystem.getLine(信息);
麦克风。打开();
ByteArrayOutputStream out=新建ByteArrayOutputStream();
int numBytesRead;
int CHUNK_SIZE=512;
byte[]data=新字节[micromic.getBufferSize()/5];
麦克风。开始();
int字节读取=0;
DataLine.Info dataLineInfo=新的DataLine.Info(SourceDataLine.class,格式);
扬声器=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
发言者:公开(格式);
扬声器。开始();
长数=0;
而(字节读取<10000000){
numBytesRead=麦克风读取(数据,0,块大小);
字节读取+=字节读取数;
out.write(数据,0,numbytes读取);
扬声器。写入(数据,0,numBytesRead);
number=convertToLong(数据);//从数据(振幅)转换为无符号长
number=(长)(20*Math.log10(number));//使用振幅转换为db
System.out.println(“db是”+(数字));
}
扬声器。排水管();
扬声器。关闭();
麦克风关闭();
}捕获(LineUnavailableException e){
e、 printStackTrace();
}
}
publicstaticfinallongconverttolong(byte[]bytes){//不丢失数据的更好方法
字节和=0;
长sum1=0;
for(字节b:字节){
总和^=b;
}
返回sum1=Byte.toUnsignedLong(总和);
}
}
我的问题是,我似乎得到了一个样本,但我不确定我是否使用了正确的公式来计算这个样本
我希望同时懂物理、数学和java的人能给出答案
感谢您的帮助:)您的公式看起来很好,因为您正确获得了振幅。Stackoverflow并不是一个真正的代码审查网站。你不能基于标准化输入测试你自己的代码吗?我将其与一个手机应用程序进行比较,该应用程序发出的声音达到20+