加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用Java声音API从WAV文件中修剪开始和结束

发布时间:2020-12-15 02:32:25 所属栏目:Java 来源:网络整理
导读:我有基础知识.但是,输出文件只是一遍又一遍地重复WAV标题.生成的文件大小合适,但它是垃圾邮件. 我正在尝试使用扩展AudioInputStream的类,以便我可以将它与其他代码无缝地结合使用,这些代码将它与另一个AudioInputStream(它工作得很漂亮)混合在一起. import j
我有基础知识.但是,输出文件只是一遍又一遍地重复WAV标题.生成的文件大小合适,但它是垃圾邮件.

我正在尝试使用扩展AudioInputStream的类,以便我可以将它与其他代码无缝地结合使用,这些代码将它与另一个AudioInputStream(它工作得很漂亮)混合在一起.

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

public class TrimmerAIS extends AudioInputStream{

private final AudioInputStream stream;
private final long startByte,endByte;
private long t_bytesRead=0;
public TrimmerAIS(AudioFormat audioFormat,AudioInputStream audioInputStream,long startMilli,long endMilli){
    super(new ByteArrayInputStream(new byte[0]),audioFormat,AudioSystem.NOT_SPECIFIED);
    stream=audioInputStream;
    //startByte=(long)((startMilli/1000f)*stream.getFormat().getSampleRate()*stream.getFormat().getSampleSizeInBits()*stream.getFormat().getChannels())/8;
    //endByte=(long)((endMilli/1000f)*stream.getFormat().getSampleRate()*stream.getFormat().getSampleSizeInBits()*stream.getFormat().getChannels())/8;
    startByte=(long)((startMilli/1000)*stream.getFormat().getFrameRate()*stream.getFormat().getFrameSize());
    endByte=(long)((endMilli/1000)*stream.getFormat().getFrameRate()*stream.getFormat().getFrameSize());
}
private byte[] tempBuffer;

@Override
public int available() throws IOException{
    return (int)(endByte-startByte-t_bytesRead);
}
public int read(byte[] abData,int nOffset,int nLength) throws IOException{
    // set up the temporary byte buffer
    if(tempBuffer==null||tempBuffer.length<nLength){
        tempBuffer=new byte[nLength];
    }
    int bytesRead=0;
    if(t_bytesRead<startByte){
        do{//skip to starting byte
            bytesRead=(int)skip(startByte-t_bytesRead);
            t_bytesRead+=bytesRead;
        }while(t_bytesRead<startByte);
    }
    if(t_bytesRead>=endByte){
        return -1;
    }

    bytesRead=stream.read(tempBuffer,nLength);
    if(bytesRead==-1){//premature EOF
        return -1;
    }else if(bytesRead==0){
        return 0;
    }
    t_bytesRead+=bytesRead;
    if(t_bytesRead>=endByte){//correct bytes read to exclude any bytes over the limit
        bytesRead=(int)(bytesRead-(t_bytesRead-endByte));
    }
    return bytesRead;
}
public static void main(String[] args) throws UnsupportedAudioFileException,IOException{
    AudioInputStream music=null;
    music = AudioSystem.getAudioInputStream(new File("music/0.wav"));
    music=new TrimmerAIS(music.getFormat(),music,15000);
    AudioSystem.write(music,AudioFileFormat.Type.WAVE,new File("out.wav"));
}
}

我根本找不到任何错误.
我确实在这里发现了一个链接到https://forums.oracle.com/forums/thread.jspa?threadID=2136229&tstart=0的类似帖子.除了你给它的开始和结束位置(以毫秒为单位)之外我做了什么,它可以传递到/包裹在另一个AIS或输出中,而无需阅读段想要成一个数组然后写它.在过去的3个小时里一直试图弄清楚这一点,我的想法有点油腻.因此,如果某些事情没有意义,请随意要求澄清.我宁愿不解析文件,但如果我必须这样做.

解决方法

嘿,哎呀!只需要删除tempBuffer并用abData替换它.是漫长的一天.
以下是更正后的代码.考虑删除这个,因为这是一个简单的错误,但我创建这个类的唯一原因是因为它还不存在.

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

public class TrimmerAIS extends AudioInputStream{

private final AudioInputStream stream;
private final long startByte,endByte;
private long t_bytesRead=0;

public TrimmerAIS(AudioFormat audioFormat,AudioSystem.NOT_SPECIFIED);
    stream=audioInputStream;
    //calculate where to start and where to end
    startByte=(long)((startMilli/1000)*stream.getFormat().getFrameRate()*stream.getFormat().getFrameSize());
    endByte=(long)((endMilli/1000)*stream.getFormat().getFrameRate()*stream.getFormat().getFrameSize());
}

@Override
public int available() throws IOException{
    return (int)(endByte-startByte-t_bytesRead);
}
public int read(byte[] abData,int nLength) throws IOException{
    int bytesRead=0;
    if(t_bytesRead<startByte){
        do{
            bytesRead=(int)skip(startByte-t_bytesRead);
            t_bytesRead+=bytesRead;
        }while(t_bytesRead<startByte);
    }
    if(t_bytesRead>=endByte)//end reached. signal EOF
        return -1;

    bytesRead=stream.read(abData,nLength);
    if(bytesRead==-1)
        return -1;
    else if(bytesRead==0)
        return 0;

    t_bytesRead+=bytesRead;
    if(t_bytesRead>=endByte)// "trim" the extra by altering the number of bytes read
        bytesRead=(int)(bytesRead-(t_bytesRead-endByte));

    return bytesRead;
}
public static void main(String[] args) throws UnsupportedAudioFileException,new File("out.wav"));
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读