Trim start and end from wav file using java sound API

I have basic knowledge However, the output file just repeats the wav title over and over again The generated file size is appropriate, but it is spam

I'm trying to use a class that extends audioinputstream so that I can use it seamlessly with other code that mixes it with another audioinputstream that works beautifully

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"));
}
}

I can't find any mistakes at all I did find a link here to https://forums.oracle.com/forums/thread.jspa?threadID=2136229&tstart=0 Similar posts In addition to the start and end positions (in milliseconds) you give it, what do I do? It can be passed / wrapped in another AIS or output without reading the segment. I want to form an array and write it I've been trying to figure this out for the past three hours. My thoughts are a little greasy Therefore, if something is meaningless, please feel free to ask for clarification I'd rather not parse the file, but if I have to

Solution

Hey, Gee! Just delete tempbuffer and replace it with abdata It was a long day

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"));
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>