使用Java源数据线进行音频时出现爆裂声/爆裂声

使用Java源数据线进行音频时出现爆裂声/爆裂声,java,debugging,audio,javasound,Java,Debugging,Audio,Javasound,我的Java源数据行有问题。我需要播放一个音调,所以我创建了一个音调类,它只表示一个音调。一切都很好,只是当我播放声音时,扬声器会在声音开始时弹出。有办法解决这个问题吗?这是一个研究项目,需要在没有裂纹的情况下运行,因为这可能会影响结果。下面是源代码。谢谢 package edu.jhu.halberda.audiopanamath; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem;

我的Java源数据行有问题。我需要播放一个音调,所以我创建了一个音调类,它只表示一个音调。一切都很好,只是当我播放声音时,扬声器会在声音开始时弹出。有办法解决这个问题吗?这是一个研究项目,需要在没有裂纹的情况下运行,因为这可能会影响结果。下面是源代码。谢谢

package edu.jhu.halberda.audiopanamath;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JOptionPane;

public class Tone {
public enum Channel {
    LEFT, RIGHT, STEREO
};

public static final float SAMPLE_RATE = 44104; // Should be a multiple of 8
protected byte[] buf;
protected int hz, msecs;
protected double vol;
protected Channel channel;

Tone() {
} // necessary so that subclasses don't complain

public Tone(int hz, int msecs, double vol, Tone.Channel channel) {
    if (hz <= 0)
        throw new IllegalArgumentException("Frequency <= 0 hz");
    if (msecs <= 0)
        throw new IllegalArgumentException("Duration <= 0 msecs");
    if (vol > 1.0 || vol < 0.0)
        throw new IllegalArgumentException("Volume out of range 0.0 - 1.0");
    this.channel = channel;
    this.hz = hz;
    this.vol = vol;
    this.msecs = msecs;
    generateTone();

}

private void generateTone() {
    int len = (int)Math.ceil((2 * SAMPLE_RATE * msecs / 1000.0d));
    if (len % 2 == 1)
        len = len + 1;
    buf = new byte[len];
    for (int i = 0; i < buf.length /2; i++) {
        double angle = (i * hz / SAMPLE_RATE) * 2.0 * Math.PI;
        buf[2*i + 1] = buf[2*i] = (byte) Math.round(Math.sin(angle) * 127.0 * vol);
    }
}

public void play(SourceDataLine sdl) { // takes an opened SourceDataLine
    FloatControl panControl = (FloatControl) sdl
            .getControl(FloatControl.Type.PAN);
    if (panControl != null) { // Preferred method using built in sound
                                // control, but not guaranteed to be
                                // available
        if (channel == Channel.LEFT) {
            panControl.setValue(-1);
        } else if (channel == Channel.RIGHT) {
            panControl.setValue(1);
        } else {
            panControl.setValue(0);
        }
    } else { // fallback method is directly manipulates the buffer
        if (channel != Channel.STEREO) {
            int nSilenceOffset;
            byte nSilenceValue = 0;
            if (channel == Channel.LEFT) {
                nSilenceOffset = 1;
            } else {
                nSilenceOffset = 0;
            }
            for (int i = 0; i < buf.length; i += 2) {
                buf[i + nSilenceOffset] = nSilenceValue;
            }
        }

    }
    sdl.write(buf, 0, buf.length);
    sdl.drain();
}

public static void main(String[] args) {
    AudioFormat af = new AudioFormat(Tone.SAMPLE_RATE, 8, 2, true, false);
    SourceDataLine sdl;
    try {
        sdl = AudioSystem.getSourceDataLine(af);
    } catch (LineUnavailableException e) {
        JOptionPane.showMessageDialog(null, "Couldn't get sound line");
        return;
    }
    try {
        sdl.open(af);
    } catch (LineUnavailableException e) {
        JOptionPane.showMessageDialog(null, "Couldn't open sound line");
        return;
    }
    sdl.start();
    Tone left = new Tone(400, 2000, .5, Tone.Channel.LEFT);
    System.out.println("Playing left");
    long t = System.currentTimeMillis();
    left.play(sdl);
    System.out.println(System.currentTimeMillis()-t);
    System.out.println("Finished left");
    Tone right = new Tone(400, 2000, .5, Tone.Channel.RIGHT);
    System.out.println("Playing right");
    right.play(sdl);
    System.out.println("Finished right");
    sdl.stop();
    sdl.close();
}

}
包edu.jhu.halberda.audiopanamath;
导入javax.sound.sampled.AudioFormat;
导入javax.sound.sampled.AudioSystem;
导入javax.sound.sampled.FloatControl;
导入javax.sound.sampled.LineUnavailableException;
导入javax.sound.sampled.SourceDataLine;
导入javax.swing.JOptionPane;
公开课声调{
公共枚举通道{
左,右,立体声
};
public static final float SAMPLE_RATE=44104;//应为8的倍数
受保护字节[]buf;
受保护的整数赫兹,毫秒;
保护双卷;
受保护信道;
语气(){
}//必须这样子类才不会抱怨
公共音调(整数赫兹,整数毫秒,双音量,音调通道){

如果(hz尝试此变体,则使用粗略的淡入/淡出效果

private void generateTone() {
    int len = (int)Math.ceil((2 * SAMPLE_RATE * msecs / 1000.0d));
    if (len % 2 == 1)
        len = len + 1;
    buf = new byte[len];
    int fadeCount = 1600;
    for (int i = 0; i < buf.length /2; i++) {
        double fadeRate = 1.0;
        double angle = (i * hz / SAMPLE_RATE) * 2.0 * Math.PI;
        if (i<fadeCount) {
            fadeRate = (double)i/(double)fadeCount;
        } else if (i>(buf.length/2)-fadeCount) {
            int bufLength = buf.length;
            int buf = bufLength/2;
            int countDown = buf-i;
            fadeRate = (double)countDown/(double)(fadeCount);
        }
        buf[2*i + 1] = buf[2*i] = (byte) Math.round(
            Math.cos(angle) * 127.0 * vol * fadeRate);
    }
}
private void generateTone(){
int len=(int)Math.ceil((2*采样率*毫秒/1000.0d));
如果(长度%2==1)
len=len+1;
buf=新字节[len];
int fadeCount=1600;
对于(int i=0;i