Path: blob/master/test/jdk/javax/sound/midi/Gervill/SoftPointResampler/Interpolate.java
41155 views
/*1* Copyright (c) 2007, 2015, 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 SoftPointResampler interpolate method25@modules java.desktop/com.sun.media.sound26*/2728import java.io.File;29import java.io.FileOutputStream;30import java.io.IOException;3132import javax.sound.sampled.*;3334import com.sun.media.sound.*;3536public class Interpolate {3738private static float getResamplerTestValue(double i)39{40return (float)Math.sin(i / 10.0);41}4243private static void perfectInterpolation(float[] in_offset, float in_end,44float[] startpitch, float pitchstep, float[] out, int[] out_offset,45int out_end) {4647float pitch = startpitch[0];48float ix = in_offset[0];49int ox = out_offset[0];50float ix_end = in_end;51int ox_end = out_end;52if (pitchstep == 0f) {53while (ix < ix_end && ox < ox_end) {54out[ox++] = getResamplerTestValue(ix);55ix += pitch;56}57} else {58while (ix < ix_end && ox < ox_end) {59out[ox++] = getResamplerTestValue(ix);60ix += pitch;61pitch += pitchstep;62}63}64in_offset[0] = ix;65out_offset[0] = ox;66startpitch[0] = pitch;6768}6970private static float testResampler(SoftAbstractResampler resampler, float p_pitch, float p_pitch2)71{72float[] testbuffer = new float[4096];73float[] testbuffer2 = new float[1024];74float[] testbuffer3 = new float[1024];75for (int i = 0; i < testbuffer.length; i++)76testbuffer[i] = getResamplerTestValue(i);77int pads = resampler.getPadding();78float pitchstep = (p_pitch2 - p_pitch)/1024f;79int[] out_offset2 = {0};80int[] out_offset3 = {0};81resampler.interpolate(testbuffer, new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer2, out_offset2, testbuffer2.length);82perfectInterpolation(new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer3, out_offset3, testbuffer3.length);83int out_off = out_offset2[0];84if(out_offset3[0] < out_off)85out_off = out_offset3[0];86float ac_error = 0;87int counter = 0;88for (int i = pads; i < out_off; i++) {89ac_error += Math.abs(testbuffer2[i] - testbuffer3[i]);90counter++;91}92return ac_error / ((float)counter);93}9495private static void fail(String error) throws Exception96{97throw new RuntimeException(error);98}99100public static void main(String[] args) throws Exception {101SoftPointResampler resampler = new SoftPointResampler();102float max = testResampler(resampler, 0.3f, 0.3f);103if(max > 0.2)104fail("Interpolation failed, error="+max);105max = testResampler(resampler, 0.3f, 0.01f);106if(max > 0.2)107fail("Interpolation failed, error="+max);108max = testResampler(resampler, 1.0f, 0.00f);109if(max > 0.2)110fail("Interpolation failed, error="+max);111}112}113114115116