用java计算实时音频

发布于 2024-12-24 20:30:29 字数 5778 浏览 1 评论 0原文

我在根据麦克风的音频输入计算语音输入频率时遇到问题。谁能帮我解决这个问题吗?

我应该从麦克风获取音频输入并计算其频率。

这是我的代码,只是为了展示我是如何做到的;以及是否有人可以识别出错误的实现。

package STLMA;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author CATE GABRIELLE
 */

import java.io.*;
import javax.sound.sampled.*;

public class SpeechDetection {
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
TargetDataLine targetDataLine; // This is the object that acquires data from
                               // the microphone and delivers it to the program

// the declaration of three instance variables used to create a SourceDataLine
// object that feeds data to the speakers on playback
AudioFormat audioFormat;    
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;    

double voiceFreq = 0;    

FileOutputStream fout;
AudioFileFormat.Type fileType;
public static String closestSpeaker;

public SpeechDetection(){
    captureAudio();
}       

private void captureAudio(){
    try{
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new   
         DataLine.Info(TargetDataLine.class,audioFormat);
        // object that describes the data line that we need to handle the acquisition 
        // of the audio data from the microphone. The first parameter makes the audio 
        // data readable
        targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLineInfo);
         //  object to handle data acquisition 
        targetDataLine.open(audioFormat);                 
           //from the microphone that matches 
        targetDataLine.start();                            
         // the information encapsulated in the DataLine.Info object  
        Thread captureThread = new Thread(new CaptureThread());
        captureThread.start();
    } catch (Exception e) {
    System.out.println(e);
    System.exit(0);
    }
}    

private AudioFormat getAudioFormat(){
    float sampleRate = 8000.0F; // The number of samples that will be acquired 
    //8000,11025,16000,22050,44100  each second for each channel of audio data.
    int sampleSizeInBits = 16; //The number of bits that will be used to 
    //8,16                        describe the value of each audio sample.
    int channels = 1;           // Two channels for stereo, and one channel for mono.
    //1,2
    boolean signed = true;      // Whether the description of each audio sample 
    //true,false        
     //consists of both positive and negative values, or positive values only.          
    boolean bigEndian = false;
    //true,false
    return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);        
}

//Inner class to capture data from microphone
class CaptureThread extends Thread {
    byte tempBuffer[] = new byte[8000];  
    // byte buffer variable to contain the raw audio data
    int countzero;                      
     // counter variable to count the number of zero's               
    short convert[] = new short[tempBuffer.length]; 
    // short variable that is appropriate to 

    // collect the audio input for porcessing

    //        public void start(){
    //            Thread voices = new Thread(this);
    //            voices.start();
    //        }

    @Override
    public void run(){               
   // a continuous thread to process the continuous audio input
        byteArrayOutputStream = new ByteArrayOutputStream(); // the object to write the 

    // raw audio input to the byte buffer variable
        stopCapture = false;
        try{
            while(!stopCapture){                    
                int cnt = targetDataLine.read(tempBuffer,0,tempBuffer.length); 
            // reads the raw audio input 

             // and returns the number of bytes actually read
                byteArrayOutputStream.write(tempBuffer, 0, cnt); 
            // writing the number of bytes read to the 
                                                                 // container                 
                try{ 
                    countzero = 0; 

                    for(int i=0; i < tempBuffer.length; i++){  
                // the loop that stores the whole audio data                                        
                        convert[i] = tempBuffer[i];    
                // to the convert variable which is a short data type,
                        if(convert[i] == 0){countzero++;}     
                 // then counts the number of zero's 
                    }
                    voiceFreq = (countzero/2)+1;               
                // calculates the number of frequency and 
                                    // stores to the voiceFreq variable
                    if(voiceFreq>=80 && voiceFreq<=350)
                        System.out.println("Voice"+voiceFreq);
                    else
                       System.out.println("Unvoice"+voiceFreq);
                }catch(StringIndexOutOfBoundsException e)  
                {System.out.println(e.getMessage());}                                                                                    
                    Thread.sleep(0);                                        
            }
        byteArrayOutputStream.close();
        }catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
}           

public static void main(String [] args){
    SpeechDetection voiceDetector1 = new SpeechDetection();        
    //        voiceDetector1.setSize(300,100);
    //        voiceDetector1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    //        voiceDetector1.setVisible(true);
}



}

顺便说一句,“voiceFreq”代表语音频率。 我的目标是知道输入是语音还是噪音。 我希望有人能帮助我解决我的问题。谢谢你,新年快乐。

I have a problem counting voice input frequency from the audio input of my microphone. Can anyone help me with this?

I'm supposed to get an audio input from my microphone and count its frequency.

This is my code just to show how I did it; and if anyone can identify a faulty implementation.

package STLMA;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author CATE GABRIELLE
 */

import java.io.*;
import javax.sound.sampled.*;

public class SpeechDetection {
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
TargetDataLine targetDataLine; // This is the object that acquires data from
                               // the microphone and delivers it to the program

// the declaration of three instance variables used to create a SourceDataLine
// object that feeds data to the speakers on playback
AudioFormat audioFormat;    
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;    

double voiceFreq = 0;    

FileOutputStream fout;
AudioFileFormat.Type fileType;
public static String closestSpeaker;

public SpeechDetection(){
    captureAudio();
}       

private void captureAudio(){
    try{
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new   
         DataLine.Info(TargetDataLine.class,audioFormat);
        // object that describes the data line that we need to handle the acquisition 
        // of the audio data from the microphone. The first parameter makes the audio 
        // data readable
        targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLineInfo);
         //  object to handle data acquisition 
        targetDataLine.open(audioFormat);                 
           //from the microphone that matches 
        targetDataLine.start();                            
         // the information encapsulated in the DataLine.Info object  
        Thread captureThread = new Thread(new CaptureThread());
        captureThread.start();
    } catch (Exception e) {
    System.out.println(e);
    System.exit(0);
    }
}    

private AudioFormat getAudioFormat(){
    float sampleRate = 8000.0F; // The number of samples that will be acquired 
    //8000,11025,16000,22050,44100  each second for each channel of audio data.
    int sampleSizeInBits = 16; //The number of bits that will be used to 
    //8,16                        describe the value of each audio sample.
    int channels = 1;           // Two channels for stereo, and one channel for mono.
    //1,2
    boolean signed = true;      // Whether the description of each audio sample 
    //true,false        
     //consists of both positive and negative values, or positive values only.          
    boolean bigEndian = false;
    //true,false
    return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);        
}

//Inner class to capture data from microphone
class CaptureThread extends Thread {
    byte tempBuffer[] = new byte[8000];  
    // byte buffer variable to contain the raw audio data
    int countzero;                      
     // counter variable to count the number of zero's               
    short convert[] = new short[tempBuffer.length]; 
    // short variable that is appropriate to 

    // collect the audio input for porcessing

    //        public void start(){
    //            Thread voices = new Thread(this);
    //            voices.start();
    //        }

    @Override
    public void run(){               
   // a continuous thread to process the continuous audio input
        byteArrayOutputStream = new ByteArrayOutputStream(); // the object to write the 

    // raw audio input to the byte buffer variable
        stopCapture = false;
        try{
            while(!stopCapture){                    
                int cnt = targetDataLine.read(tempBuffer,0,tempBuffer.length); 
            // reads the raw audio input 

             // and returns the number of bytes actually read
                byteArrayOutputStream.write(tempBuffer, 0, cnt); 
            // writing the number of bytes read to the 
                                                                 // container                 
                try{ 
                    countzero = 0; 

                    for(int i=0; i < tempBuffer.length; i++){  
                // the loop that stores the whole audio data                                        
                        convert[i] = tempBuffer[i];    
                // to the convert variable which is a short data type,
                        if(convert[i] == 0){countzero++;}     
                 // then counts the number of zero's 
                    }
                    voiceFreq = (countzero/2)+1;               
                // calculates the number of frequency and 
                                    // stores to the voiceFreq variable
                    if(voiceFreq>=80 && voiceFreq<=350)
                        System.out.println("Voice"+voiceFreq);
                    else
                       System.out.println("Unvoice"+voiceFreq);
                }catch(StringIndexOutOfBoundsException e)  
                {System.out.println(e.getMessage());}                                                                                    
                    Thread.sleep(0);                                        
            }
        byteArrayOutputStream.close();
        }catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }
}           

public static void main(String [] args){
    SpeechDetection voiceDetector1 = new SpeechDetection();        
    //        voiceDetector1.setSize(300,100);
    //        voiceDetector1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    //        voiceDetector1.setVisible(true);
}



}

by the way, "voiceFreq" stands for voice frequency.
My goal here is to know if the input is a voice or a noise.
I hope someone could help me with my problem. Thank you and a happy New Year.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2024-12-31 20:30:29

我认为为了检测某些东西是否是潜在的语音或噪声,人们需要对一段数据进行 FFT 并查看频率分量是否在“正常语音”的某个范围内。

也许请参阅 Reliable and fast FFT in Java 了解一些 FFT 信息。

I would think for detecting whether something is a potential voice or a noise, one would want to do an FFT on a section of data and see whether the frequency components were within some range of "normal voice".

Maybe see Reliable and fast FFT in Java for some FFT information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文