Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Gervill/SoftLanczosResampler/Interpolate.java
38862 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@summary Test SoftLanczosResampler interpolate method */2526import java.io.File;27import java.io.FileOutputStream;28import java.io.IOException;2930import javax.sound.sampled.*;3132import com.sun.media.sound.*;3334public class Interpolate {3536private static float getResamplerTestValue(double i)37{38return (float)Math.sin(i / 10.0);39}4041private static void perfectInterpolation(float[] in_offset, float in_end,42float[] startpitch, float pitchstep, float[] out, int[] out_offset,43int out_end) {4445float pitch = startpitch[0];46float ix = in_offset[0];47int ox = out_offset[0];48float ix_end = in_end;49int ox_end = out_end;50if (pitchstep == 0f) {51while (ix < ix_end && ox < ox_end) {52out[ox++] = getResamplerTestValue(ix);53ix += pitch;54}55} else {56while (ix < ix_end && ox < ox_end) {57out[ox++] = getResamplerTestValue(ix);58ix += pitch;59pitch += pitchstep;60}61}62in_offset[0] = ix;63out_offset[0] = ox;64startpitch[0] = pitch;6566}6768private static float testResampler(SoftAbstractResampler resampler, float p_pitch, float p_pitch2)69{70float[] testbuffer = new float[4096];71float[] testbuffer2 = new float[1024];72float[] testbuffer3 = new float[1024];73for (int i = 0; i < testbuffer.length; i++)74testbuffer[i] = getResamplerTestValue(i);75int pads = resampler.getPadding();76float pitchstep = (p_pitch2 - p_pitch)/1024f;77int[] out_offset2 = {0};78int[] out_offset3 = {0};79resampler.interpolate(testbuffer, new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer2, out_offset2, testbuffer2.length);80perfectInterpolation(new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer3, out_offset3, testbuffer3.length);81int out_off = out_offset2[0];82if(out_offset3[0] < out_off)83out_off = out_offset3[0];84float ac_error = 0;85int counter = 0;86for (int i = pads; i < out_off; i++) {87ac_error += Math.abs(testbuffer2[i] - testbuffer3[i]);88counter++;89}90return ac_error / ((float)counter);91}9293private static void fail(String error) throws Exception94{95throw new RuntimeException(error);96}9798public static void main(String[] args) throws Exception {99SoftLanczosResampler resampler = new SoftLanczosResampler();100float max = testResampler(resampler, 0.3f, 0.3f);101if(max > 0.01)102fail("Interpolation failed, error="+max);103max = testResampler(resampler, 0.3f, 0.01f);104if(max > 0.01)105fail("Interpolation failed, error="+max);106max = testResampler(resampler, 1.0f, 0.00f);107if(max > 0.01)108fail("Interpolation failed, error="+max);109}110}111112113114