829N/A/*
6321N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/A/**
829N/A * Lanczos interpolation resampler.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class SoftLanczosResampler extends SoftAbstractResampler {
829N/A
829N/A float[][] sinc_table;
829N/A int sinc_table_fsize = 2000;
829N/A int sinc_table_size = 5;
829N/A int sinc_table_center = sinc_table_size / 2;
829N/A
829N/A public SoftLanczosResampler() {
829N/A super();
829N/A sinc_table = new float[sinc_table_fsize][];
829N/A for (int i = 0; i < sinc_table_fsize; i++) {
829N/A sinc_table[i] = sincTable(sinc_table_size, -i
829N/A / ((float) sinc_table_fsize));
829N/A }
829N/A }
829N/A
829N/A // Normalized sinc function
829N/A public static double sinc(double x) {
829N/A return (x == 0.0) ? 1.0 : Math.sin(Math.PI * x) / (Math.PI * x);
829N/A }
829N/A
829N/A // Generate sinc table
829N/A public static float[] sincTable(int size, float offset) {
829N/A int center = size / 2;
829N/A float[] w = new float[size];
829N/A for (int k = 0; k < size; k++) {
829N/A float x = (-center + k + offset);
829N/A if (x < -2 || x > 2)
829N/A w[k] = 0;
829N/A else if (x == 0)
829N/A w[k] = 1;
829N/A else {
829N/A w[k] = (float)(2.0 * Math.sin(Math.PI * x)
829N/A * Math.sin(Math.PI * x / 2.0)
829N/A / ((Math.PI * x) * (Math.PI * x)));
829N/A }
829N/A }
829N/A return w;
829N/A }
829N/A
829N/A public int getPadding() // must be at least half of sinc_table_size
829N/A {
829N/A return sinc_table_size / 2 + 2;
829N/A }
829N/A
829N/A public void interpolate(float[] in, float[] in_offset, float in_end,
829N/A float[] startpitch, float pitchstep, float[] out, int[] out_offset,
829N/A int out_end) {
829N/A float pitch = startpitch[0];
829N/A float ix = in_offset[0];
829N/A int ox = out_offset[0];
829N/A float ix_end = in_end;
829N/A int ox_end = out_end;
829N/A
829N/A if (pitchstep == 0) {
829N/A while (ix < ix_end && ox < ox_end) {
829N/A int iix = (int) ix;
829N/A float[] sinc_table
829N/A = this.sinc_table[(int) ((ix - iix) * sinc_table_fsize)];
829N/A int xx = iix - sinc_table_center;
829N/A float y = 0;
829N/A for (int i = 0; i < sinc_table_size; i++, xx++)
829N/A y += in[xx] * sinc_table[i];
829N/A out[ox++] = y;
829N/A ix += pitch;
829N/A }
829N/A } else {
829N/A while (ix < ix_end && ox < ox_end) {
829N/A int iix = (int) ix;
829N/A float[] sinc_table
829N/A = this.sinc_table[(int) ((ix - iix) * sinc_table_fsize)];
829N/A int xx = iix - sinc_table_center;
829N/A float y = 0;
829N/A for (int i = 0; i < sinc_table_size; i++, xx++)
829N/A y += in[xx] * sinc_table[i];
829N/A out[ox++] = y;
829N/A
829N/A ix += pitch;
829N/A pitch += pitchstep;
829N/A }
829N/A }
829N/A in_offset[0] = ix;
829N/A out_offset[0] = ox;
829N/A startpitch[0] = pitch;
829N/A
829N/A }
829N/A}