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