我不能改变音乐文件的速度而不回避,一个令人不安的噪声出现 [英] I can't change the speed of a music file without avoiding that a disturbing noise appear

查看:120
本文介绍了我不能改变音乐文件的速度而不回避,一个令人不安的噪声出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变一个音频文件的速度,如果我无符号数都这样做都是对的,但一旦我开始使用双值事情变得复杂,比如,我的code与所有的X.5的数字,但它不与任何小数其他号码,在我的情况,我想通过1.3个百分点,增加的速度。但我得到的是一个文件,你不能勉强听到什么,但恼人的噪音。

I am trying to change the speed of an audio file, if I do it with unsigned values all is all right, but once I start using double values things get messy, for instance, my code works with all the "x.5" numbers but it doesn't with any other number with decimals and in my case, I want to increase the speed by 1.3 points. But all I get is a file where you can't barely hear nothing but an annoying noise.

下面是code,我现在用:

Here is the code that I am using:

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

class AcceleratePlayback {

public static void main(String[] args) throws Exception {

    //double playBackSpeed = 1.5;  Works

    //double playBackSpeed = 1.3; Doesn't work

    File file1= new File("Sample2.wav");
    File file2= new File("DEF.wav");
    AudioInputStream ais = AudioSystem.getAudioInputStream(file1);
    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);
        }
    }

    byte[] b1 = baos.toByteArray();
    byte[] b2 = new byte[(int)(b1.length/playBackSpeed)];
    for (int i=0; i<(b2.length/frameSize); i++) {
        for (int j=0; j<frameSize; j++) {
            b2[(i*frameSize)+j] = b1[(int)((i*frameSize*playBackSpeed)+j)];
        }
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(b2);
    AudioInputStream aisAccelerated =
        new AudioInputStream(bais, af, b2.length/frameSize);
    AudioSystem.write(aisAccelerated, AudioFileFormat.Type.WAVE, file2);
}

}

推荐答案

您的阅读,从奇障碍瀑布:那是因为截断读从字节开始于一个奇怪的位置。使用以下从位置甚至启动:

Your reading-from falls on odd barriers: that is because of truncation the read-from byte starts at an odd location. Use the following to start from even location:

for (i=0; i<(b2.length/frameSize); i++) {
      int ind=(int)((i*frameSize*playBackSpeed));
      if((ind%2)==1) ind++;
    for (j=0; j<frameSize; j++) {
        b2[(i*frameSize)+j] = b1[ind+j];
    }
}

或者你也可以跳转更改为4:

Or you can change the jump to 4:

    if((ind%4)>0) ind+=(4-(ind%4));

这篇关于我不能改变音乐文件的速度而不回避,一个令人不安的噪声出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆