Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:27:13+00:00 2026-05-31T15:27:13+00:00

I have the basics made. However, the output file just repeats the WAV headerbytes

  • 0

I have the basics made. However, the output file just repeats the WAV headerbytes over and over. The resulting file is the right size, but it is filed with junk.

I am trying to use a class that extends AudioInputStream so that I can use it seamlessly with other code that will mix it with another AudioInputStream(which 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,0,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,0,15000);
    AudioSystem.write(music,AudioFileFormat.Type.WAVE,new File("out.wav"));
}
}

I simply cannot find anything wrong with it.
I did find a similar post on here that linked to https://forums.oracle.com/forums/thread.jspa?threadID=2136229&tstart=0. Which is what mine does except that you give it the start and end positions in milliseconds and it can be passed on to/wrapped in another AIS or output without the need to read the segment wanted into an array and then writing it. Been trying to figure this out for the past 3 hours and my mind is a little fried. So if something doesn’t make sense, feel free to ask for clarification. I would prefer to not parse the file but if I have to I have to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:27:14+00:00Added an answer on May 31, 2026 at 3:27 pm

    Heh, whoops! Just needed to remove tempBuffer and replace it with abData. Was a long day.
    Below is the corrected code. Thought about deleting this since it was a simple mistake, but the only reason I made this class is because it does not already exist.

    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,AudioInputStream audioInputStream,long startMilli,long endMilli){
        super(new ByteArrayInputStream(new byte[0]),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 nOffset,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,0,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, IOException{
        AudioInputStream music=null;
        music = AudioSystem.getAudioInputStream(new File("music/0.wav"));
        music=new TrimmerAIS(music.getFormat(),music,0,15000);
        AudioSystem.write(music,AudioFileFormat.Type.WAVE,new File("out.wav"));
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a very new programmer, I have made a couple basic applications, however
I have made a basic ascx control which is just a panel with a
The basics have already been answered here . But is there a pre-built PHP
I have been learning about the basics of C# but haven't come across a
I have made a simple timer but in trying to increase the timers speed
I have made a new windows service which works fine using barebone code (just
I have made this post over a year ago, and I think it makes
i have an editor that is basic microsoft actions: createlink bold etc. i made
The basics: I have a contact form that uses php to validate the forms.
I have been trying to grasp the basics of Support Vector Machines, and downloaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.