录制时检测静音

在Java中开始录制操作时,如何检测静音?什么是PCM数据?如何用Java计算PCM数据?


我找到了解决方案:


package bemukan.voiceRecognition.speechToText;


import javax.sound.sampled.*;

import java.io.*;


public class RecordAudio {

    private File audioFile;

    protected boolean running;

    private ByteArrayOutputStream out;

    private AudioInputStream inputStream;

    final static float MAX_8_BITS_SIGNED = Byte.MAX_VALUE;

    final static float MAX_8_BITS_UNSIGNED = 0xff;

    final static float MAX_16_BITS_SIGNED = Short.MAX_VALUE;

    final static float MAX_16_BITS_UNSIGNED = 0xffff;

    private AudioFormat format;

    private float level;

    private int frameSize;


    public RecordAudio(){

         getFormat();

    }


    private AudioFormat getFormat() {

        File file = new File("src/Facebook/1.wav");

        AudioInputStream stream;

        try {

            stream = AudioSystem.getAudioInputStream(file);

            format=stream.getFormat();

            frameSize=stream.getFormat().getFrameSize();

            return stream.getFormat();

        } catch (UnsupportedAudioFileException e) {


        } catch (IOException e) {


        }

        return null;

    }


    public void stopAudio() {


        running = false;

    }


    public void recordAudio() {


        try {

            final AudioFormat format = getFormat();

            DataLine.Info info = new DataLine.Info(

                    TargetDataLine.class, format);

            final TargetDataLine line = (TargetDataLine)

                    AudioSystem.getLine(info);

            line.open(format);

            line.start();

            Runnable runner = new Runnable() {

                int bufferSize = (int) format.getSampleRate()

                        * format.getFrameSize();

                byte buffer[] = new byte[bufferSize];


                public void run() {

                     int readPoint = 0;


                        }

                    }

                }

            };



忽然笑
浏览 776回答 2
2回答

慕码人2483693

在Java中开始录制操作时,如何检测静音?计算一组声音帧的dB或RMS值,并确定将其视为“静音”的级别。什么是PCM数据?数据是在脉冲编码调制格式。如何用Java计算PCM数据?我不明白这个问题。但是,猜测它与speech-recognition标记有关,我有一些坏消息。理论上,这可以使用Java Speech API来完成。但是,显然没有适用于API的“语音到文本”实现(只有“文字到语音”)。我必须计算语音识别项目的均方根值。但是我不知道如何用Java计算。对于信号double范围在-1到1之间的单个通道,可以使用此方法。/** Computes the RMS volume of a group of signal sizes ranging from -1 to 1. */public double volumeRMS(double[] raw) {&nbsp; &nbsp; double sum = 0d;&nbsp; &nbsp; if (raw.length==0) {&nbsp; &nbsp; &nbsp; &nbsp; return sum;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; for (int ii=0; ii<raw.length; ii++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += raw[ii];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; double average = sum/raw.length;&nbsp; &nbsp; double sumMeanSquare = 0d;&nbsp; &nbsp; for (int ii=0; ii<raw.length; ii++) {&nbsp; &nbsp; &nbsp; &nbsp; sumMeanSquare += Math.pow(raw[ii]-average,2d);&nbsp; &nbsp; }&nbsp; &nbsp; double averageMeanSquare = sumMeanSquare/raw.length;&nbsp; &nbsp; double rootMeanSquare = Math.sqrt(averageMeanSquare);&nbsp; &nbsp; return rootMeanSquare;}有一个字节缓冲区来保存行中的输入值,我应该使用该缓冲区做什么?如果使用该volumeRMS(double[])方法,则将byte值转换double为-1到1范围内的值的数组。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java