Link to home
Start Free TrialLog in
Avatar of florinsuciu
florinsuciu

asked on

sound sample rate resampling

Hi!
I have needed to do in Java an resampling from an sound (WAV or MP3) that have sample rate equal with 32KHz into a sound with 48KHz.
I have the array of sound samples, which will be filtered.
I have applied the bottom algorithm but I have obtained a sound with distortions (the resulted sound is played faster than the original sound.)
Algorithm: if we have a 32000hz rate in order to convert it to 48000hz we'll add between 2 sound samples a new sample equal to the first one. As input sound samples array will be X Y X Y X Y X Y and as output will be XXY XXY XXY XXY, so that the output array we'll have the size 1.5 of the input sound array.

Any suggestions, algorithms, sample code or URL links will be appreciated.
Thank you,
Florin
Avatar of Neutron
Neutron

Hi florinsuciu, :-)

You can try using Bresenham variation like this one:

-------8<-------cut-here-------Bresenham.java-------
public class Bresenham
{
    public static void main( String[] args )
    {
        int[] sourceSample = {1,2,3,4,5,6,7};
        // int[] sourceSample = {1,1,2,2,3,4,4,5,6,6,7,8};
       
        int sourceRate = 32000; // 48000
        int targetRate = 48000; // 32000
       
        int targetLength = (sourceSample.length * targetRate) / sourceRate;
        int[] targetSample = new int[targetLength];
       
        int measure = targetRate;
       
        int sourceIndex = 0;
        int targetIndex = 0;
       
        while (targetIndex<targetLength)
        {
            if (measure>=sourceRate)
            {
                targetSample[targetIndex++] = sourceSample[sourceIndex];
               
                measure -= sourceRate;
            }
            else
            {
                sourceIndex++;
                measure+= targetRate;
            }
        }
       
        System.out.print( "[" );
        for (int i=0; i<sourceSample.length; i++)
        {
            System.out.print( sourceSample[i]+"; " );
        }
        System.out.println( "]" );
       
        System.out.print( "[" );
        for (int i=0; i<targetSample.length; i++)
        {
            System.out.print( targetSample[i]+"; " );
        }
        System.out.println( "]" );
    }
   
} /* Bresenham */
-------8<-------cut-here-------Bresenham.java-------

This should work both ways from lower SR to higher and vice versa.

Example (input 32000 output 48000) :

[1; 2; 3; 4; 5; 6; 7; ]
[1; 2; 2; 3; 4; 4; 5; 6; 6; 7; ]

Example 2 (input 48000 output 32000) :

[1; 1; 2; 2; 3; 4; 4; 5; 6; 6; 7; 8; ]
[1; 2; 3; 4; 5; 6; 7; 8; ]

What I have done there is 'stretch' the sample data.

I hope that this solves your problem. :-)

Greetings,
    </ntr> :)
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
To be PAQ'ed and points refunded
Please leave any comments here within the
next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
guanwz
florinsuciu:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Avatar of girionis
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- To be PAQed and points NOT refunded.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
Hi,

the posted code performs exactly what was asked for.

Greetings,
    </ntr> :)
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Computer101,

since my comment is the only comment here, if you don't think that it solves the given problem - you should delete the question, not PAQ it.

</ntr>