有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当试图用tarsosdsp audioEventBuffer填充audioBuffer时,java干扰了噪音

我正在开发一个简单的Beatbox应用程序。首先,我用纯Java编写了所有内容,然后我发现了神奇的tarsosdsp框架。但现在我遇到了一个我无法解决的问题。你能帮我吗

我正在安装一个静音检测器,效果很好。然后我想用process方法中audioEvent的数据填充byte[]缓冲区。我失败了。。。变量audioBuffer的类型为ByteArrayOutputStream,可在运行时重用。请参阅相关代码片段:

    private void setNewMixer(Mixer mixer) throws LineUnavailableException,
UnsupportedAudioFileException {

    if(dispatcher!= null){
        dispatcher.stop();
    }
    currentMixer = mixer;

    //final AudioFormat format = new AudioFormat(sampleRate, frameRate, channel, true, true);
    final DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
    final TargetDataLine line;
    line = (TargetDataLine) mixer.getLine(dataLineInfo);
    final int numberOfSamples = bufferSize;
    line.open(audioFormat, numberOfSamples);
    line.start();
    final AudioInputStream stream = new AudioInputStream(line);

    JVMAudioInputStream audioStream = new JVMAudioInputStream(stream);
    // create a new dispatcher
    dispatcher = new AudioDispatcher(audioStream, bufferSize, overlap);

    // add a processor, handle percussion event.
    silenceDetector = new SilenceDetector(threshold,false);

    dispatcher.addAudioProcessor(bufferFiller);
    dispatcher.addAudioProcessor(silenceDetector);
    dispatcher.addAudioProcessor(this);

    // run the dispatcher (on a new thread).
    new Thread(dispatcher,"GunNoiseDetector Thread").start();

}

final AudioProcessor bufferFiller = new AudioProcessor() {

    @Override
    public boolean process(AudioEvent audioEvent) {

        if(isAdjusting){        

                byte[] bb = audioEvent.getByteBuffer().clone();

                try {
                    audioBuffer.write(bb);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                System.out.println("current buffer.size():: "+audioBuffer.size());

        } 
        else {          
            if (audioBuffer.size() > 0) {
                try {
                    byte[] ba = audioBuffer.toByteArray();
                    samples.add(ba);
                    System.out.println("stored: "+ba.length);
                    audioBuffer.flush();
                    audioBuffer.close();
                    audioBuffer = new ByteArrayOutputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }           
        }

        return true;
    }

    @Override
    public void processingFinished() {
        // TODO Auto-generated method stub
    }

}; 

@Override
public boolean process(AudioEvent audioEvent) {
    if(silenceDetector.currentSPL() > threshold){           
        isAdjusting = true;
        lastAction = System.currentTimeMillis();                        
    } 
    else {                  
        isAdjusting = false;            
    }

    return true;

}

有什么建议吗


共 (1) 个答案

  1. # 1 楼答案

    我找到了它不起作用的原因!就像这里提到的:What is the meaning of frame rate in AudioFormat?

    For PCM, A-law and μ-law data, a frame is all data that belongs to one sampling intervall. This means that the frame rate is the same as the sample rate.

    所以我的音频格式错了