1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* @test
  27    @summary Test SoftLinearResampler interpolate method */
  28 
  29 import java.io.File;
  30 import java.io.FileOutputStream;
  31 import java.io.IOException;
  32 
  33 import javax.sound.sampled.*;
  34 
  35 import com.sun.media.sound.*;
  36 
  37 public class Interpolate {
  38 
  39     private static float getResamplerTestValue(double i)
  40     {
  41         return (float)Math.sin(i / 10.0);
  42     }
  43 
  44     private static void perfectInterpolation(float[] in_offset, float in_end,
  45             float[] startpitch, float pitchstep, float[] out, int[] out_offset,
  46             int out_end) {
  47 
  48         float pitch = startpitch[0];
  49         float ix = in_offset[0];
  50         int ox = out_offset[0];
  51         float ix_end = in_end;
  52         int ox_end = out_end;
  53         if (pitchstep == 0f) {
  54             while (ix < ix_end && ox < ox_end) {
  55                 out[ox++] = getResamplerTestValue(ix);
  56                 ix += pitch;
  57             }
  58         } else {
  59             while (ix < ix_end && ox < ox_end) {
  60                 out[ox++] = getResamplerTestValue(ix);
  61                 ix += pitch;
  62                 pitch += pitchstep;
  63             }
  64         }
  65         in_offset[0] = ix;
  66         out_offset[0] = ox;
  67         startpitch[0] = pitch;
  68 
  69     }
  70 
  71     private static float testResampler(SoftAbstractResampler resampler, float p_pitch, float p_pitch2)
  72     {
  73         float[] testbuffer = new float[4096];
  74         float[] testbuffer2 = new float[1024];
  75         float[] testbuffer3 = new float[1024];
  76         for (int i = 0; i < testbuffer.length; i++)
  77             testbuffer[i] = getResamplerTestValue(i);
  78         int pads = resampler.getPadding();
  79         float pitchstep = (p_pitch2 - p_pitch)/1024f;
  80         int[] out_offset2 = {0};
  81         int[] out_offset3 = {0};
  82         resampler.interpolate(testbuffer, new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer2, out_offset2, testbuffer2.length);
  83         perfectInterpolation(new float[] {pads}, testbuffer.length - pads, new float[] {p_pitch}, pitchstep, testbuffer3, out_offset3, testbuffer3.length);
  84         int out_off = out_offset2[0];
  85         if(out_offset3[0] < out_off)
  86             out_off = out_offset3[0];
  87         float ac_error = 0;
  88         int counter = 0;
  89         for (int i = pads; i < out_off; i++) {
  90             ac_error += Math.abs(testbuffer2[i] - testbuffer3[i]);
  91             counter++;
  92         }
  93         return ac_error / ((float)counter);
  94     }
  95 
  96     private static void fail(String error) throws Exception
  97     {
  98         throw new RuntimeException(error);
  99     }
 100 
 101     public static void main(String[] args) throws Exception {
 102         SoftLinearResampler resampler = new SoftLinearResampler();
 103         float max = testResampler(resampler, 0.3f, 0.3f);
 104         if(max > 0.001)
 105             fail("Interpolation failed, error="+max);
 106         max = testResampler(resampler, 0.3f, 0.01f);
 107         if(max > 0.001)
 108             fail("Interpolation failed, error="+max);
 109         max = testResampler(resampler, 1.0f, 0.00f);
 110         if(max > 0.001)
 111             fail("Interpolation failed, error="+max);
 112     }
 113 }
 114