播放采样声音javax.sound.sampled时出现大量延迟

时间:2013-06-05 05:42:45

标签: java audio signal-processing sound-synthesis javax.sound.sampled

我有一个工作的软合成器,可以正确输出和播放样本,但是我有一个巨大的延迟,大约一秒钟。我的代码基于以下文章:http://www.drdobbs.com/jvm/creating-music-components-in-java/229700113?pgno=2

我忽视了什么吗?样本的生成不是问题,这种情况发生得很快而且很简单。

我尝试将缓冲区大小更改为多个不同的值,但没有成功。我目前正在测试OSX机器,这可能是问题吗?

fyi,完成永远不会错。在沉默的时候,我只需将0的样本提供给缓冲区。

public class Player extends Thread {
    public static final int SAMPLE_RATE = 44100;
    public static final int BUFFER_SIZE = 2200;
    public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
    private static final int SAMPLE_SIZE = 16; // Don't change
    private static final int CHANNELS = 1;
    private static final boolean SIGNED = true;
    private static final boolean BIG_ENDIAN = true;
    private AudioFormat format;
    private DataLine.Info info;
    private SourceDataLine audioLine;
    private boolean done;
    private byte[] sampleData = new byte[BUFFER_SIZE];
    private Oscillator osc;

    public Player(Oscillator osc) {
        format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, CHANNELS, SIGNED, BIG_ENDIAN);
        info = new DataLine.Info(SourceDataLine.class, format);
        this.osc = osc;
    }

    public void run() {
        done = false;
        int bytesRead = 0;

        try {
            audioLine = (SourceDataLine) AudioSystem.getLine(info);
            audioLine.open(format);
            audioLine.start();

            while ((bytesRead != -1) && !done) {
                bytesRead = osc.getSamples(sampleData);

                if (bytesRead > 0) {
                    audioLine.write(sampleData, 0, bytesRead);
                }
            }
        } catch (LineUnavailableException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            audioLine.drain();
            audioLine.close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎没有设置音频系统使用的音频缓冲区大小 - 而是使用SAMPLES_PER_BUFFER来控制一次生成的样本数。

这些不是一回事 - 操作系统将请求呈现它使用的任何缓冲区大小(在MacOSX上,样本通过回调)。

此代码:

        while ((bytesRead != -1) && !done) {
            bytesRead = osc.getSamples(sampleData);

            if (bytesRead > 0) {
                audioLine.write(sampleData, 0, bytesRead);

只会填充audioline的缓冲区直到它阻塞。然后,渲染请求将到达,这将清空缓冲区,然后再次取消阻止。这将以音频缓冲区大小的速率发生。