提高java中声音文件的播放速度

发布于 2024-11-02 19:28:40 字数 116 浏览 1 评论 0原文

我正在寻找有关如何使用 Java 及其声音 API 提高声音文件播放速度的信息。

我目前正在使用剪辑和 AudioInputStream 来播放文件,但如果这意味着我可以提高播放速度,我将很乐意更改它。

I'm looking for information on how I can increase the playback speed of a sound file using Java and it's sound API.

I'm currently using a clip and an AudioInputStream to play back the file, but I'll be glad to change that if it means I can increase the playback speed.

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

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

发布评论

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

评论(2

少女七分熟 2024-11-09 19:28:40

我通过使用线性插值来做到这一点。当您按一定增量逐步浏览样本时,使用分数距离创建要流式传输的值。

例如,如果您的值为 1.25(在值为 10 的样本和值为 30 的样本之间),则您将输出值为 15。

I do this by using linear interpolation. As you step through your samples by some increment, use the fractional distance to create a value to stream.

For example if you land at 1.25 (between a sample with value 10 and sample of value 30), you would output a value of 15.

<逆流佳人身旁 2024-11-09 19:28:40

以原始速度的整数倍 (2,3,4..) 倍播放的粗略方法是跳过原始输入流的每隔这么多样本。 EG 对于双倍速度,跳过二分之一,对于三倍速度,跳过三分之二。

AcceleratePlayback.java

import javax.swing.JOptionPane;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Date;

class AcceleratePlayback {

    public static void main(String[] args) throws Exception {
        int playBackSpeed = 1;
        if (args.length>0) {
            try {
                playBackSpeed = Integer.parseInt(args[0]);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
        int skip = playBackSpeed-1;
        System.out.println("Playback Rate: " + playBackSpeed);

        URL url = new URL("http://pscode.org/media/leftright.wav");
        System.out.println("URL: " + url);
        AudioInputStream ais = AudioSystem.getAudioInputStream(url);
        AudioFormat af = ais.getFormat();

        int frameSize = af.getFrameSize();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b = new byte[2^16];
        int read = 1;
        while( read>-1 ) {
            read = ais.read(b);
            if (read>0) {
                baos.write(b, 0, read);
            }
        }
        System.out.println("End entire: \t" + new Date());

        byte[] b1 = baos.toByteArray();
        byte[] b2 = new byte[b1.length/playBackSpeed];
        for (int ii=0; ii<b2.length/frameSize; ii++) {
            for (int jj=0; jj<frameSize; jj++) {
                b2[(ii*frameSize)+jj] = b1[(ii*frameSize*playBackSpeed)+jj];
            }
        }
        System.out.println("End sub-sample: \t" + new Date());

        ByteArrayInputStream bais = new ByteArrayInputStream(b2);
        AudioInputStream aisAccelerated =
            new AudioInputStream(bais, af, b2.length);
        Clip clip = AudioSystem.getClip();
        clip.open(aisAccelerated);
        clip.loop(2*playBackSpeed);
        clip.start();

        JOptionPane.showMessageDialog(null, "Exit?");
    }
}

示例输入/输出

prompt> java AcceleratePlayback
Playback Rate: 1
URL: http://pscode.org/media/leftright.wav
End entire:     Mon Apr 25 20:54:55 EST 2011
End sub-sample:         Mon Apr 25 20:54:55 EST 2011

prompt> java AcceleratePlayback 2
Playback Rate: 2
URL: http://pscode.org/media/leftright.wav
End entire:     Mon Apr 25 20:55:20 EST 2011
End sub-sample:         Mon Apr 25 20:55:20 EST 2011

prompt> java AcceleratePlayback 3
Playback Rate: 3
URL: http://pscode.org/media/leftright.wav
End entire:     Mon Apr 25 20:55:36 EST 2011
End sub-sample:         Mon Apr 25 20:55:36 EST 2011

prompt> 

A crude way to play back at an integral number (2,3,4..) of times the original speed, is to skip every so many samples of the original input stream. E.G. For double speed, skip one out of two, for triple speed, skip 2 out of 3.

AcceleratePlayback.java

import javax.swing.JOptionPane;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.Date;

class AcceleratePlayback {

    public static void main(String[] args) throws Exception {
        int playBackSpeed = 1;
        if (args.length>0) {
            try {
                playBackSpeed = Integer.parseInt(args[0]);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
        int skip = playBackSpeed-1;
        System.out.println("Playback Rate: " + playBackSpeed);

        URL url = new URL("http://pscode.org/media/leftright.wav");
        System.out.println("URL: " + url);
        AudioInputStream ais = AudioSystem.getAudioInputStream(url);
        AudioFormat af = ais.getFormat();

        int frameSize = af.getFrameSize();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b = new byte[2^16];
        int read = 1;
        while( read>-1 ) {
            read = ais.read(b);
            if (read>0) {
                baos.write(b, 0, read);
            }
        }
        System.out.println("End entire: \t" + new Date());

        byte[] b1 = baos.toByteArray();
        byte[] b2 = new byte[b1.length/playBackSpeed];
        for (int ii=0; ii<b2.length/frameSize; ii++) {
            for (int jj=0; jj<frameSize; jj++) {
                b2[(ii*frameSize)+jj] = b1[(ii*frameSize*playBackSpeed)+jj];
            }
        }
        System.out.println("End sub-sample: \t" + new Date());

        ByteArrayInputStream bais = new ByteArrayInputStream(b2);
        AudioInputStream aisAccelerated =
            new AudioInputStream(bais, af, b2.length);
        Clip clip = AudioSystem.getClip();
        clip.open(aisAccelerated);
        clip.loop(2*playBackSpeed);
        clip.start();

        JOptionPane.showMessageDialog(null, "Exit?");
    }
}

Example Input/Output

prompt> java AcceleratePlayback
Playback Rate: 1
URL: http://pscode.org/media/leftright.wav
End entire:     Mon Apr 25 20:54:55 EST 2011
End sub-sample:         Mon Apr 25 20:54:55 EST 2011

prompt> java AcceleratePlayback 2
Playback Rate: 2
URL: http://pscode.org/media/leftright.wav
End entire:     Mon Apr 25 20:55:20 EST 2011
End sub-sample:         Mon Apr 25 20:55:20 EST 2011

prompt> java AcceleratePlayback 3
Playback Rate: 3
URL: http://pscode.org/media/leftright.wav
End entire:     Mon Apr 25 20:55:36 EST 2011
End sub-sample:         Mon Apr 25 20:55:36 EST 2011

prompt> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文