Interpolate.java revision 2365:4c234c13f66a
119370Spst/*
246283Sdfr * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
319370Spst * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
419370Spst *
546283Sdfr * This code is free software; you can redistribute it and/or modify it
646283Sdfr * under the terms of the GNU General Public License version 2 only, as
719370Spst * published by the Free Software Foundation.  Oracle designates this
819370Spst * particular file as subject to the "Classpath" exception as provided
919370Spst * by Oracle in the LICENSE file that accompanied this code.
1019370Spst *
1119370Spst * This code is distributed in the hope that it will be useful, but WITHOUT
1219370Spst * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1319370Spst * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1419370Spst * version 2 for more details (a copy is included in the LICENSE file that
1519370Spst * accompanied this code).
1619370Spst *
1719370Spst * You should have received a copy of the GNU General Public License version
1819370Spst * 2 along with this work; if not, write to the Free Software Foundation,
1919370Spst * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2019370Spst *
2119370Spst * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2219370Spst * or visit www.oracle.com if you need additional information or have any
2319370Spst * questions.
2419370Spst */
2519370Spst
2619370Spst/* @test
2719370Spst   @summary Test SoftLinearResampler interpolate method */
2819370Spst
2946283Sdfrimport java.io.File;
3019370Spstimport java.io.FileOutputStream;
3119370Spstimport java.io.IOException;
3219370Spst
3319370Spstimport javax.sound.sampled.*;
3446283Sdfr
3546283Sdfrimport com.sun.media.sound.*;
3646283Sdfr
3746283Sdfrpublic class Interpolate {
3846283Sdfr
3946283Sdfr    private static float getResamplerTestValue(double i)
4046283Sdfr    {
4146283Sdfr        return (float)Math.sin(i / 10.0);
4246283Sdfr    }
4346283Sdfr
4446283Sdfr    private static void perfectInterpolation(float[] in_offset, float in_end,
4546283Sdfr            float[] startpitch, float pitchstep, float[] out, int[] out_offset,
4646283Sdfr            int out_end) {
4746283Sdfr
4846283Sdfr        float pitch = startpitch[0];
4946283Sdfr        float ix = in_offset[0];
5046283Sdfr        int ox = out_offset[0];
5146283Sdfr        float ix_end = in_end;
5246283Sdfr        int ox_end = out_end;
5346283Sdfr        if (pitchstep == 0f) {
5446283Sdfr            while (ix < ix_end && ox < ox_end) {
5546283Sdfr                out[ox++] = getResamplerTestValue(ix);
5619370Spst                ix += pitch;
5719370Spst            }
5819370Spst        } else {
5919370Spst            while (ix < ix_end && ox < ox_end) {
6019370Spst                out[ox++] = getResamplerTestValue(ix);
6119370Spst                ix += pitch;
6219370Spst                pitch += pitchstep;
6346283Sdfr            }
6446283Sdfr        }
6546283Sdfr        in_offset[0] = ix;
6646283Sdfr        out_offset[0] = ox;
6746283Sdfr        startpitch[0] = pitch;
6819370Spst
6919370Spst    }
7019370Spst
7119370Spst    private static float testResampler(SoftAbstractResampler resampler, float p_pitch, float p_pitch2)
7219370Spst    {
7319370Spst        float[] testbuffer = new float[4096];
7419370Spst        float[] testbuffer2 = new float[1024];
7519370Spst        float[] testbuffer3 = new float[1024];
7619370Spst        for (int i = 0; i < testbuffer.length; i++)
7746283Sdfr            testbuffer[i] = getResamplerTestValue(i);
7846283Sdfr        int pads = resampler.getPadding();
7946283Sdfr        float pitchstep = (p_pitch2 - p_pitch)/1024f;
8019370Spst        int[] out_offset2 = {0};
8119370Spst        int[] out_offset3 = {0};
8219370Spst        resampler.interpolate(testbuffer, new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer2, out_offset2, testbuffer2.length);
8346283Sdfr        perfectInterpolation(new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer3, out_offset3, testbuffer3.length);
8446283Sdfr        int out_off = out_offset2[0];
8546283Sdfr        if(out_offset3[0] < out_off)
8646283Sdfr            out_off = out_offset3[0];
8746283Sdfr        float ac_error = 0;
8846283Sdfr        int counter = 0;
8946283Sdfr        for (int i = pads; i < out_off; i++) {
9046283Sdfr            ac_error += Math.abs(testbuffer2[i] - testbuffer3[i]);
9146283Sdfr            counter++;
9246283Sdfr        }
9346283Sdfr        return ac_error / ((float)counter);
9446283Sdfr    }
9546283Sdfr
9619370Spst    private static void fail(String error) throws Exception
9719370Spst    {
9846283Sdfr        throw new RuntimeException(error);
9919370Spst    }
10019370Spst
10146283Sdfr    public static void main(String[] args) throws Exception {
10246283Sdfr        SoftLinearResampler resampler = new SoftLinearResampler();
10319370Spst        float max = testResampler(resampler, 0.3f, 0.3f);
10419370Spst        if(max > 0.001)
10519370Spst            fail("Interpolation failed, error="+max);
10619370Spst        max = testResampler(resampler, 0.3f, 0.01f);
10719370Spst        if(max > 0.001)
10819370Spst            fail("Interpolation failed, error="+max);
10919370Spst        max = testResampler(resampler, 1.0f, 0.00f);
11046283Sdfr        if(max > 0.001)
11146283Sdfr            fail("Interpolation failed, error="+max);
11219370Spst    }
11346283Sdfr}
11446283Sdfr
11546283Sdfr