Java 长度无限的音频输入流

发布于 2024-12-22 09:33:50 字数 236 浏览 2 评论 0原文

我有一堆代码,运行时会产生程序声音。不幸的是,它只持续几秒钟。理想情况下,它会一直运行,直到我告诉它停止为止。我不是在谈论循环,生成它的算法目前提供 2^64 个样本,因此在可预见的将来它不会用完。 AudioInputStream 的构造函数采用第三个输入,理想情况下我可以将其删除。我可以提供一个巨大的数字,但这似乎是错误的方法。

我考虑过使用 SourceDataLine,但理想情况下该算法将按需调用,而不是提前运行并编写路径。想法?

I have a bunch of code that will, when run, produce procedural sound. Unfortunately, it only lasts a few seconds. Ideally it would run until I tell it to stop. I'm not talking about looping, the algorithm to generate it provides 2^64 samples, at the moment, so it isn't going to run out within the foreseeable future. The constructor for AudioInputStream takes a third input, which I could ideally just remove. I could just provide a huge number, but that seems like the wrong way to go about it.

I thought about using SourceDataLine, but the algorithm ideally would be called on-demand, not running ahead and writing the path. Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-12-29 09:33:50

看来我已经回答了我自己的问题。

经过进一步研究,使用 SourceDataLine 是可行的方法,因为当您给它足够的空间来使用时,它会阻塞。

对于缺乏适当的 Javadoc 表示歉意。

class SoundPlayer
{
    // plays an InputStream for a given number of samples, length
    public static void play(InputStream stream, float sampleRate, int sampleSize, int length) throws LineUnavailableException
    {
        // you can specify whatever format you want...I just don't need much flexibility here
        AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true);
        AudioInputStream audioStream = new AudioInputStream(stream, format, length);
        Clip clip = AudioSystem.getClip();
        clip.open(audioStream);
        clip.start();
    }

    public static void play(InputStream stream, float sampleRate, int sampleSize) throws LineUnavailableException
    {
        AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true);
        SourceDataLine line = AudioSystem.getSourceDataLine(format);
        line.open(format);
        line.start();
        // if you wanted to block, you could just run the loop in here
        SoundThread soundThread = new SoundThread(stream, line);
        soundThread.start();
    }

    private static class SoundThread extends Thread
    {
        private static final int buffersize = 1024;

        private InputStream stream;
        private SourceDataLine line;

        SoundThread(InputStream stream, SourceDataLine line)
        {
            this.stream = stream;
            this.line = line;
        }

        public void run()
        {
            byte[] b = new byte[buffersize];
            // you could, of course, have a way of stopping this...
            for (;;)
            {
                stream.read(b);
                line.write(b, 0, buffersize);
            }
        }
    }
}

It seems I've answered my own question.

Upon further research, using the SourceDataLine is the way to go, as it'll block when you've given it enough to work with.

Apologies for the lack of proper Javadoc.

class SoundPlayer
{
    // plays an InputStream for a given number of samples, length
    public static void play(InputStream stream, float sampleRate, int sampleSize, int length) throws LineUnavailableException
    {
        // you can specify whatever format you want...I just don't need much flexibility here
        AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true);
        AudioInputStream audioStream = new AudioInputStream(stream, format, length);
        Clip clip = AudioSystem.getClip();
        clip.open(audioStream);
        clip.start();
    }

    public static void play(InputStream stream, float sampleRate, int sampleSize) throws LineUnavailableException
    {
        AudioFormat format = new AudioFormat(sampleRate, sampleSize, 1, false, true);
        SourceDataLine line = AudioSystem.getSourceDataLine(format);
        line.open(format);
        line.start();
        // if you wanted to block, you could just run the loop in here
        SoundThread soundThread = new SoundThread(stream, line);
        soundThread.start();
    }

    private static class SoundThread extends Thread
    {
        private static final int buffersize = 1024;

        private InputStream stream;
        private SourceDataLine line;

        SoundThread(InputStream stream, SourceDataLine line)
        {
            this.stream = stream;
            this.line = line;
        }

        public void run()
        {
            byte[] b = new byte[buffersize];
            // you could, of course, have a way of stopping this...
            for (;;)
            {
                stream.read(b);
                line.write(b, 0, buffersize);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文