2013-04-08 3 views
2

특정 사운드가 평균적으로 얼마나 큰지 찾기 위해 프로그램에서 볼륨을 인식 할 수 있기 때문에 마이크 입력의 최대 볼륨을 찾으려고합니다. 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)); 
    } 
} 
} 

타이머 코드 : 여기

는 내가 지금까지 무엇을 가지고 내가 line.open()line.start()에 추가 할 때 그것은 일

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++; 
    } 
} 
} 

답변

0

.

+0

@ user2247192 질문에 대한 답변은 괜찮지 만 다른 사람의 질문에 답하는 것처럼 적절한 응답 형식으로 작성해야합니다. "어리석은 실수!"와 같은 것을 남겨주세요. (line.open()?) 이후에 그 행을 추가해야하는 위치를 알려주며, 또한 왜 필요한지 (이 경우에는 명백 할 수도 있음)도 지정하는 것이 바람직합니다. 그렇다면 이것은 좋은 대답이며 나중에 다시 받아 들일 수 있습니다. – hyde

+0

명심하십시오 ... line.sten()을 line.open() 바로 아래에 추가했습니다. – Riptyde4