2

我在 java 中有一个基本的波发生器,但是当波的幅度急剧变化时,我需要一些东西来消除我得到的点击。即当我开始/停止播放波浪时,尤其是当我有哔哔声时。

Phrogz 对 SO 的回答提供了一个非常好的和简单的功能,但我不确定我是否正确实施它。

当我第一次尝试使用它时,我无法让它工作,但后来我似乎记得它工作得很好......我已经摆弄了很多我的代码,现在它似乎没有工作再次很好。

所以这是我最接近SSCCE的地方:

如果你玩这个,你会注意到当过滤器打开时(filter = true),波浪更安静,咔哒声也略微减少,但这似乎主要是由于音量的减少。每次哔哔声仍然有明显的“撞击”,我不想要,我不记得以前在那里......

import javax.sound.sampled.*;


public class Oscillator{

    private static int SAMPLE_RATE = 22050;
    private static short MAX_AMPLITUDE = Short.MAX_VALUE;   
    private static AudioFormat af = null;
    private static SourceDataLine line = null;
    private int frequency = 440; //Hz
    private int numLoops = 1000;
    private int beep = 100;

    // set to true to apply low-pass filter
    private boolean  filter = true;
    // set the amount of "smoothing" here
    private int smoothing = 100;
    private double oldValue;

    public Oscillator(){

        prepareLine();

    }


    public static void main(String[] args) {
        System.out.println("Playing oscillator");
        Oscillator osc = new Oscillator();
        osc.play();
    }


    private void prepareLine(){


        af =  new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, 16, 2, 4, SAMPLE_RATE, false);

        try {

            DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);

            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line does not support: " + af);
                System.exit(0);
            }
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(af);
        }
        catch (Exception e) { 
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }

    private void play() {

        System.out.println("play");

         int maxSize = (int) Math.round( (SAMPLE_RATE * af.getFrameSize())/ frequency );  
         byte[] samples = new byte[maxSize];

         line.start();

         double volume = 1;

         int count = 0;
         for (int i = 0; i < numLoops; i ++){


             if (count == beep) {
                 if(volume==1) volume = 0;
                 else volume = 1;
                 count = 0;
             }

             count ++;

             playWave(frequency, volume, samples);


         }

         line.drain();
         line.stop();
         line.close();
         System.exit(0);
    }  

    private void playWave(int frequency, double volLevel, byte[] samples) {

         double amplitude = volLevel * MAX_AMPLITUDE;

         int numSamplesInWave = (int) Math.round( ((double) SAMPLE_RATE)/frequency );

         int index = 0;

         for (int i = 0; i < numSamplesInWave; i++) {

             double theta = (double)i/numSamplesInWave;

             double wave = getWave(theta);

             int sample = (int) (wave * amplitude);


             if (filter) sample = applyLowPassFilter(sample);



             // left sample
             samples[index + 0] = (byte) (sample & 0xFF);        
             samples[index + 1] = (byte) ((sample >> 8) & 0xFF); 
             // right sample
             samples[index + 2] = (byte) (sample & 0xFF);
             samples[index + 3] = (byte) ((sample >> 8) & 0xFF);
             index += 4;
         }

         int offset = 0;

         while (offset < index){
             double increment =line.write(samples, offset, index-offset);
             offset += increment;
         }
    }

    private double getWave(double theta){

        double value = 0;

        theta = theta * 2 * Math.PI;

        value = getSin(theta);
        //value = getSqr(theta);

        return value;

    }

    private double getSin(double theta){
        return Math.sin(theta);
    }

    private int getSqr(double theta){
        if (theta <= Math.PI) return 1;
        else return 0;
    }

    // implementation of basic low-pass filter
    private int applyLowPassFilter(int sample){

        int newValue = sample;
        double filteredValue = oldValue + (newValue - oldValue) / smoothing;

        oldValue = filteredValue;
        return (int) filteredValue;
    }
}

相关方法在最后。如果有人对此进行测试,如果您有耳机,请注意音量!

所以要么:

  1. 它正在工作,我只是期待太多这样一个简单的实现
  2. 我做错了什么,愚蠢而明显......

如果它只是 1。我应该/如何摆脱突然幅度变化带来的刺耳节拍/敲击/咔哒声?

如果它是 2. 好,应该是一个太长问题的简短回答。

4

1 回答 1

2

低通滤波器不会消除突然幅度变化的咔嗒声。相反,您需要避免突然的幅度变化。

您可以使用低通滤波器来过滤您的幅度级别。

**Pseudo code**

for i = 0 to numSamplesInWave-1 do
begin
  theta = i / numSamplesInWave;
  wave = getWave(theta);
  currentAmplitude = applyLowPassFilter(TargetAmplitude);   
  Sample[i] = wave * currentAmplitude;
end;

使用上述低通滤波器可以很好地平滑输入值。例如,当用户更改音量控制时。

在其他情况下,创建某种信封可能更合适。例如,合成器通常使用ADSR 包络来平滑新语音/声音开始和停止时的幅度变化。

于 2012-05-25T02:11:00.517 回答