Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to convert WAV frequency from 44,1 to 8khz with linear interpolation?

843802Oct 16 2009 — edited Nov 1 2009
I am currently trying to convert data from a non compressed WAV file for academic purposes and I'm stuck with that linear interpolation theory.

According to captfoss in this post , he explains how linear interpolation can work from 8000 to 16000.
Say you have the following "sound" data at 8000, and you want to convert it to 16000.

1 3 1 3 1 3 1 3 (@ 8000)

becomes

1 ? 3 ? 1 ? 3 ? 1 ? 3 ? 1 ? 3 (@ 16000)
The first thing I think I understand is that from 44100 to 8000 I will need to sample

5 bytes when i % 2 == 1
6 bytes when i % 2 == 0
7 bytes when i % 80 == 0

where i is the byte from a byte array data.

I'm trying to interpolate these samples using an average and the outputed file has the correct sound within but with HUGE noise.

I have a file source and file sink where I pop bytes from the data and push the resampled one.

The WAV reader is already parsed in this code sample:
    int counter = 1;
        int counterPush = 0;
        while( counter <= subchunk2IDSize ) {
            int toPop = 5;
            
            if( 0 == counter % 2 ) {
                toPop++;
            }
            
            if( 0 == counter % 80 ) {
                toPop++;
            }
            
            byte[] toPush = new byte[]{ByteArray.getAverageByte(_source.pop(toPop))};
            
            counterPush++;
            _sink.push(toPush);
            
            counter += toPop;
        }
        
        _sink.close();
ByteArray.getAverageByte function:
    static public byte getAverageByte(byte[] bytes)
    {
        int sum = 0;
        
        for (byte b : bytes) {
            sum += b;
        }
        
        return (byte) (sum / bytes.length);
    }
I would be grateful if someone could help me or direct me in the good way :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 29 2009
Added on Oct 16 2009
5 comments
969 views