java - 使用 SouceDataLine 播放时生成的声音模糊

标签 java audio javax.sound.sampled

我正在尝试实时生成一组同步音调。但程序产生的所有声音都是“模糊的”,或者是“静态的”,甚至听起来像是背景中的“尖叫声”。这在低音调的声音中尤其明显。 这是代码:

static final long bufferLength = 44100;
static final AudioFormat af = new AudioFormat(bufferLength, 8, 1, true, false);
static boolean go = true; //to be changed somewhere else

static void startSound(double[] hertz) {
    if (hertz.length == 0) {return;}
    try {
        SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
        sdl.open();
        sdl.start();
        int i = 0;
        //iterate as long as the sound must play
        do {
            //create a new buffer
            double[] buf = new double[128]; //arbitrary number
            final int startI = i;
            //iterate through each of the tones
            for (int k = 0; k < hertz.length; k++) {
                i = startI;
                //iterate through each of the samples for this buffer
                for (int j = 0; j < buf.length; j++) {
                    double x = (double)i/bufferLength*hertz[k]*2*Math.PI;
                    double wave1 = Math.sin(x);
                    //decrease volume with increasing pitch
                    double volume = Math.min(Math.max(300 - hertz[k], 50d), 126d);
                    buf[j] += wave1*volume;
                    i++;
                    if (i == 9999999) { //prevent i from getting too big
                        i = 0;
                    }
                }
            }

            final byte[] finalBuffer = new byte[buf.length];
            //copy the double buffer into byte buffer
            for (int j = 0; j < buf.length; j++) {
                //divide by hertz.length to prevent simultaneous sounds
                //    from becoming too loud
                finalBuffer[j] = (byte)(buf[j]/hertz.length);
            }

            //play the sound
            sdl.write(finalBuffer, 0, finalBuffer.length);
        } while (go);
        sdl.flush();
        sdl.stop();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }
}

//play some deep example tones
startSound(new double[]{65.4064, 58.2705, 48.9995});

我尝试录制该程序输出的声音,波形确实看起来有点锯齿状。但是当我直接从程序打印生成的波形时,它们看起来非常平滑。我发出的声音似乎与扬声器发出的声音不匹配。谁能发现我做错了什么吗?

最佳答案

根据我的评论,我认为您正在听到quantization error由于是 8 位音频,您应该切换到 16 位。量化误差有时称为噪声,但它是一种方波谐波失真,是您听到的微妙泛音的来源。

对于语音等听起来更像噪音的东西,8 位有时是可以接受的。纯音的失真更加明显。

我把你的代码变成了一个粗略的 MCVE来证明差异。

class SoundTest {
    static final int bufferLength = 44100;
    static final AudioFormat af8 = new AudioFormat(bufferLength, 8, 1, true, false);
    static final AudioFormat af16 = new AudioFormat(bufferLength, 16, 1, true, false);
    static volatile boolean go = true; //to be changed somewhere else

    static void startSound8(double[] hertz) {
        if (hertz.length == 0) {return;}
        try {
            SourceDataLine sdl = AudioSystem.getSourceDataLine(af8);
            sdl.open();
            sdl.start();
            int i = 0;
            //iterate as long as the sound must play
            do {
                //create a new buffer
                double[] buf = new double[128]; //arbitrary number
                final int startI = i;
                //iterate through each of the tones
                for (int k = 0; k < hertz.length; k++) {
                    i = startI;
                    //iterate through each of the samples for this buffer
                    for (int j = 0; j < buf.length; j++) {
                        double x = (double)i/bufferLength*hertz[k]*2*Math.PI;
                        double wave1 = Math.sin(x);
                        //decrease volume with increasing pitch
//                        double volume = Math.min(Math.max(300 - hertz[k], 50d), 126d);
                        double volume = 64;
                        buf[j] += wave1*volume;
                        i++;
                        if (i == 9999999) { //prevent i from getting too big
                            i = 0;
                        }
                    }
                }

                final byte[] finalBuffer = new byte[buf.length];
                //copy the double buffer into byte buffer
                for (int j = 0; j < buf.length; j++) {
                    //divide by hertz.length to prevent simultaneous sounds
                    //    from becoming too loud
                    finalBuffer[j] = (byte)(buf[j]/hertz.length);
                }

                //play the sound
                sdl.write(finalBuffer, 0, finalBuffer.length);
            } while (go);
            sdl.flush();
            sdl.stop();
            synchronized (SoundTest.class) {
                SoundTest.class.notifyAll();
            }
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    static void startSound16(double[] hertz) {
        if (hertz.length == 0) {return;}
        try {
            SourceDataLine sdl = AudioSystem.getSourceDataLine(af16);
            sdl.open();
            sdl.start();
            int i = 0;
            //iterate as long as the sound must play
            do {
                //create a new buffer
                double[] buf = new double[128]; //arbitrary number
                final int startI = i;
                //iterate through each of the tones
                for (int k = 0; k < hertz.length; k++) {
                    i = startI;
                    //iterate through each of the samples for this buffer
                    for (int j = 0; j < buf.length; j++) {
                        double x = (double)i/bufferLength*hertz[k]*2*Math.PI;
                        double wave1 = Math.sin(x);
                        //decrease volume with increasing pitch
                        // double volume = Math.min(Math.max(300 - hertz[k], 50d), 126d);
                        double volume = 16384;
                        buf[j] += wave1*volume;
                        i++;
                        if (i == 9999999) { //prevent i from getting too big
                            i = 0;
                        }
                    }
                }

                final byte[] finalBuffer = new byte[buf.length * 2];

                //copy the double buffer into byte buffer
                for (int j = 0; j < buf.length; j++) {
                    //divide by hertz.length to prevent simultaneous sounds
                    //    from becoming too loud

                    int a = (int) (buf[j] / hertz.length);
                    finalBuffer[j * 2] = (byte) a;
                    finalBuffer[(j * 2) + 1] = (byte) (a >>> 8);
                }

                //play the sound
                sdl.write(finalBuffer, 0, finalBuffer.length);
            } while (go);
            sdl.flush();
            sdl.stop();
            synchronized (SoundTest.class) {
                SoundTest.class.notifyAll();
            }
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    static void playTone(final double hz, final boolean fewBits) {
        go = true;
        new Thread() {
            @Override
            public void run() {
                if (fewBits) {
                    startSound8(new double[] {hz});
                } else {
                    startSound16(new double[] {hz});
                }
            }
        }.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException x) {
            x.printStackTrace();
        } finally {
            go = false;
            synchronized (SoundTest.class) {
                try {
                    SoundTest.class.wait();
                } catch (InterruptedException x) {
                    x.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        playTone(220, true);
        playTone(220, false);
    }
}

我讨论了用于打包 16 位字节数组的位操作的概念 here还有示例代码。

还值得一提的是,如果专业应用程序出于某种原因需要使用 8 位,它可能会添加 dither在量化之前,这听起来比纯粹的量化误差更好。 (对于 16 位来说也是如此,但 16 位的量化误差是听不见的,除非它被累积。)

关于java - 使用 SouceDataLine 播放时生成的声音模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021140/

相关文章:

ruby - 如何在 Ruby 中播放 wav 文件?

java - 网 bean : how to resize the UI Window to screen Size

java - Eclipse:动态Web项目是否必须在WEB-INF中包含OJDBC.jar文件?

javascript - 如何使用JavaScript在自定义html5音频播放器中计算剩余时间?

audio - 如何通过api网关将音频文件上传到s3?

java - 剪辑有时无法播放

java SourceDataLine 正弦波点击

java - 音频输入流不工作

java - 如何在不替换 JTextArea 中的文本的情况下设置文本?

java - 对于本地化的从右到左的语言字符串,JavaFX 中显示的括号不一致