Interpolate.java revision 8729:0242fce0f717
190792Sgshapiro/*
2261363Sgshapiro * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
390792Sgshapiro * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490792Sgshapiro *
590792Sgshapiro * This code is free software; you can redistribute it and/or modify it
690792Sgshapiro * under the terms of the GNU General Public License version 2 only, as
790792Sgshapiro * published by the Free Software Foundation.
890792Sgshapiro *
990792Sgshapiro * This code is distributed in the hope that it will be useful, but WITHOUT
1090792Sgshapiro * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190792Sgshapiro * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1290792Sgshapiro * version 2 for more details (a copy is included in the LICENSE file that
13120256Sgshapiro * accompanied this code).
14120256Sgshapiro *
15120256Sgshapiro * You should have received a copy of the GNU General Public License version
1690792Sgshapiro * 2 along with this work; if not, write to the Free Software Foundation,
17120256Sgshapiro * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18120256Sgshapiro *
19120256Sgshapiro * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20120256Sgshapiro * or visit www.oracle.com if you need additional information or have any
21120256Sgshapiro * questions.
22120256Sgshapiro */
23120256Sgshapiro
2490792Sgshapiro/* @test
2590792Sgshapiro   @summary Test SoftLinearResampler interpolate method */
2690792Sgshapiro
27266692Sgshapiroimport java.io.File;
2890792Sgshapiroimport java.io.FileOutputStream;
2990792Sgshapiroimport java.io.IOException;
3090792Sgshapiro
3190792Sgshapiroimport javax.sound.sampled.*;
3290792Sgshapiro
3390792Sgshapiroimport com.sun.media.sound.*;
3490792Sgshapiro
3590792Sgshapiropublic class Interpolate {
3690792Sgshapiro
3790792Sgshapiro    private static float getResamplerTestValue(double i)
3890792Sgshapiro    {
3990792Sgshapiro        return (float)Math.sin(i / 10.0);
4090792Sgshapiro    }
4190792Sgshapiro
4290792Sgshapiro    private static void perfectInterpolation(float[] in_offset, float in_end,
4390792Sgshapiro            float[] startpitch, float pitchstep, float[] out, int[] out_offset,
4490792Sgshapiro            int out_end) {
4590792Sgshapiro
4690792Sgshapiro        float pitch = startpitch[0];
4790792Sgshapiro        float ix = in_offset[0];
4890792Sgshapiro        int ox = out_offset[0];
4990792Sgshapiro        float ix_end = in_end;
5090792Sgshapiro        int ox_end = out_end;
5190792Sgshapiro        if (pitchstep == 0f) {
5290792Sgshapiro            while (ix < ix_end && ox < ox_end) {
5390792Sgshapiro                out[ox++] = getResamplerTestValue(ix);
5490792Sgshapiro                ix += pitch;
5590792Sgshapiro            }
5690792Sgshapiro        } else {
5790792Sgshapiro            while (ix < ix_end && ox < ox_end) {
5890792Sgshapiro                out[ox++] = getResamplerTestValue(ix);
5990792Sgshapiro                ix += pitch;
6090792Sgshapiro                pitch += pitchstep;
6190792Sgshapiro            }
6290792Sgshapiro        }
6390792Sgshapiro        in_offset[0] = ix;
6490792Sgshapiro        out_offset[0] = ox;
6590792Sgshapiro        startpitch[0] = pitch;
6690792Sgshapiro
6790792Sgshapiro    }
6890792Sgshapiro
6990792Sgshapiro    private static float testResampler(SoftAbstractResampler resampler, float p_pitch, float p_pitch2)
7090792Sgshapiro    {
7190792Sgshapiro        float[] testbuffer = new float[4096];
7290792Sgshapiro        float[] testbuffer2 = new float[1024];
7390792Sgshapiro        float[] testbuffer3 = new float[1024];
7490792Sgshapiro        for (int i = 0; i < testbuffer.length; i++)
7590792Sgshapiro            testbuffer[i] = getResamplerTestValue(i);
7690792Sgshapiro        int pads = resampler.getPadding();
7790792Sgshapiro        float pitchstep = (p_pitch2 - p_pitch)/1024f;
7890792Sgshapiro        int[] out_offset2 = {0};
7990792Sgshapiro        int[] out_offset3 = {0};
8090792Sgshapiro        resampler.interpolate(testbuffer, new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer2, out_offset2, testbuffer2.length);
8190792Sgshapiro        perfectInterpolation(new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer3, out_offset3, testbuffer3.length);
8290792Sgshapiro        int out_off = out_offset2[0];
8390792Sgshapiro        if(out_offset3[0] < out_off)
8490792Sgshapiro            out_off = out_offset3[0];
8590792Sgshapiro        float ac_error = 0;
8690792Sgshapiro        int counter = 0;
8790792Sgshapiro        for (int i = pads; i < out_off; i++) {
8890792Sgshapiro            ac_error += Math.abs(testbuffer2[i] - testbuffer3[i]);
8990792Sgshapiro            counter++;
9090792Sgshapiro        }
9190792Sgshapiro        return ac_error / ((float)counter);
9290792Sgshapiro    }
9390792Sgshapiro
9490792Sgshapiro    private static void fail(String error) throws Exception
9590792Sgshapiro    {
9690792Sgshapiro        throw new RuntimeException(error);
9790792Sgshapiro    }
9890792Sgshapiro
9990792Sgshapiro    public static void main(String[] args) throws Exception {
10090792Sgshapiro        SoftLinearResampler resampler = new SoftLinearResampler();
10190792Sgshapiro        float max = testResampler(resampler, 0.3f, 0.3f);
10290792Sgshapiro        if(max > 0.001)
10390792Sgshapiro            fail("Interpolation failed, error="+max);
10490792Sgshapiro        max = testResampler(resampler, 0.3f, 0.01f);
105        if(max > 0.001)
106            fail("Interpolation failed, error="+max);
107        max = testResampler(resampler, 1.0f, 0.00f);
108        if(max > 0.001)
109            fail("Interpolation failed, error="+max);
110    }
111}
112
113