【问题标题】:Calculating microphone volume (trying to find max)计算麦克风音量(试图找到最大值)
【发布时间】:2013-03-30 00:53:24
【问题描述】:

我正在尝试查找麦克风输入的最大音量,因为我正在尝试查找某个声音的平均音量,以便我的程序可以通过音量识别它。 RMS计算方法来自这个网站(https://forums.oracle.com/forums/thread.jspa?threadID=1270831),我只是想弄清楚一切是如何工作的......

问题是,无论我发出多大的噪音,RMS 电平每次都输出为 0!所以我要么将我的 targetDataLine 设置完全错误并且它没有捕获音频......要么我在其他地方做错了什么。

这是我目前所拥有的:

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;


public class MicrophoneTesting {

public MicrophoneTesting() {
    // TODO Auto-generated constructor stub
}

protected static int calculateRMSLevel(byte[] audioData)
{ // audioData might be buffered data read from a data line
    long lSum = 0;
    for(int i=0; i<audioData.length; i++)
        lSum = lSum + audioData[i];

    double dAvg = lSum / audioData.length;

    double sumMeanSquare = 0d;
    for(int j=0; j<audioData.length; j++)
        sumMeanSquare = sumMeanSquare + Math.pow(audioData[j] - dAvg, 2d);

    double averageMeanSquare = sumMeanSquare / audioData.length;
    return (int)(Math.pow(averageMeanSquare,0.5d) + 0.5);
}

public static void main(String[] args){

    // Open a TargetDataLine for getting microphone input & sound level
    TargetDataLine line = null;
    AudioFormat format = new AudioFormat(8000, 0, 1, true, true);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); //     format is an AudioFormat object
    if (!AudioSystem.isLineSupported(info)) {
        System.out.println("The line is not supported.");
    }
    // Obtain and open the line.
    try {
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
    } catch (LineUnavailableException ex) {
        System.out.println("The TargetDataLine is Unavailable.");
    }

    Timer t = new Timer(); // I used a timer here, code is below
    while(t.seconds < 2){
    byte[] bytes = new byte[line.getBufferSize() / 5];
    line.read(bytes, 0, bytes.length);
    System.out.println("RMS Level: " + calculateRMSLevel(bytes));
    }
}
}

定时器代码:

public class Timer implements Runnable{
    int seconds;
    Thread t;

public Timer() {
    this.seconds = 0;
    t = new Thread(this, "Clap Timer");
    t.start(); // Start the thread
}

@Override
public void run() {
    // TODO Auto-generated method stub
    while(seconds < 2)
    {
        //Wait 1 second
        try {                                
            Thread.sleep(1000);
        }
        catch(Exception e) {
            System.out.println("Waiting interupted.");
        }

        seconds++;
    }
}
}

【问题讨论】:

    标签: java audio volume microphone rms


    【解决方案1】:

    当我在line.open() 之后添加line.start() 时,它起作用了。

    【讨论】:

    • @user2247192 可以回答您自己的问题,但您应该以正确的答案格式编写它,就像您回答其他人的问题一样。省略诸如“愚蠢的错误”之类的东西!并告诉应该在哪里添加该行(在line.open()? 之后),最好还说明为什么需要它(好吧,在这种情况下它可能很明显)。那么这是一个很好的答案,您可以稍后再回来接受它。
    • 会记住...我在 line.open() 下方添加了“line.start()”
    猜你喜欢
    • 2011-09-05
    • 2013-09-18
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2017-10-26
    • 2016-03-27
    相关资源
    最近更新 更多