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 * A resampler that uses first-order (linear) interpolation.
829N/A *
829N/A * This one doesn't perform float to int casting inside the processing loop.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class SoftLinearResampler2 extends SoftAbstractResampler {
829N/A
829N/A public int getPadding() {
829N/A return 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
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 // Check if we have do anything
829N/A if (!(ix < ix_end && ox < ox_end))
829N/A return;
829N/A
829N/A // 15 bit shift was choosed because
829N/A // it resulted in no drift between p_ix and ix.
829N/A int p_ix = (int) (ix * (1 << 15));
829N/A int p_ix_end = (int) (ix_end * (1 << 15));
829N/A int p_pitch = (int) (pitch * (1 << 15));
829N/A // Pitch needs to recalculated
829N/A // to ensure no drift between p_ix and ix.
829N/A pitch = p_pitch * (1f / (1 << 15));
829N/A
829N/A if (pitchstep == 0f) {
829N/A
829N/A // To reduce
829N/A // while (p_ix < p_ix_end && ox < ox_end)
829N/A // into
829N/A // while (ox < ox_end)
829N/A // We need to calculate new ox_end value.
829N/A int p_ix_len = p_ix_end - p_ix;
829N/A int p_mod = p_ix_len % p_pitch;
829N/A if (p_mod != 0)
829N/A p_ix_len += p_pitch - p_mod;
829N/A int ox_end2 = ox + p_ix_len / p_pitch;
829N/A if (ox_end2 < ox_end)
829N/A ox_end = ox_end2;
829N/A
829N/A while (ox < ox_end) {
829N/A int iix = p_ix >> 15;
829N/A float fix = ix - iix;
829N/A float i = in[iix];
829N/A out[ox++] = i + (in[iix + 1] - i) * fix;
829N/A p_ix += p_pitch;
829N/A ix += pitch;
829N/A }
829N/A
829N/A } else {
829N/A
829N/A int p_pitchstep = (int) (pitchstep * (1 << 15));
829N/A pitchstep = p_pitchstep * (1f / (1 << 15));
829N/A
829N/A while (p_ix < p_ix_end && ox < ox_end) {
829N/A int iix = p_ix >> 15;
829N/A float fix = ix - iix;
829N/A float i = in[iix];
829N/A out[ox++] = i + (in[iix + 1] - i) * fix;
829N/A ix += pitch;
829N/A p_ix += p_pitch;
829N/A pitch += pitchstep;
829N/A p_pitch += p_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}