1 /* 2 * Copyright (c) 2007, 2013, 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 package com.sun.media.sound; 26 27 /** 28 * A resampler that uses first-order (linear) interpolation. 29 * 30 * This one doesn't perform float to int casting inside the processing loop. 31 * 32 * @author Karl Helgason 33 */ 34 public final class SoftLinearResampler2 extends SoftAbstractResampler { 35 36 @Override 37 public int getPadding() { 38 return 2; 39 } 40 41 @Override 42 public void interpolate(float[] in, 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 52 // Check if we have do anything 53 if (!(ix < ix_end && ox < ox_end)) 54 return; 55 56 // 15 bit shift was choosed because 57 // it resulted in no drift between p_ix and ix. 58 int p_ix = (int) (ix * (1 << 15)); 59 int p_ix_end = (int) (ix_end * (1 << 15)); 60 int p_pitch = (int) (pitch * (1 << 15)); 61 // Pitch needs to recalculated 62 // to ensure no drift between p_ix and ix. 63 pitch = p_pitch * (1f / (1 << 15)); 64 65 if (pitchstep == 0f) { 66 67 // To reduce 68 // while (p_ix < p_ix_end && ox < ox_end) 69 // into 70 // while (ox < ox_end) 71 // We need to calculate new ox_end value. 72 int p_ix_len = p_ix_end - p_ix; 73 int p_mod = p_ix_len % p_pitch; 74 if (p_mod != 0) 75 p_ix_len += p_pitch - p_mod; 76 int ox_end2 = ox + p_ix_len / p_pitch; 77 if (ox_end2 < ox_end) 78 ox_end = ox_end2; 79 80 while (ox < ox_end) { 81 int iix = p_ix >> 15; 82 float fix = ix - iix; 83 float i = in[iix]; 84 out[ox++] = i + (in[iix + 1] - i) * fix; 85 p_ix += p_pitch; 86 ix += pitch; 87 } 88 89 } else { 90 91 int p_pitchstep = (int) (pitchstep * (1 << 15)); 92 pitchstep = p_pitchstep * (1f / (1 << 15)); 93 94 while (p_ix < p_ix_end && ox < ox_end) { 95 int iix = p_ix >> 15; 96 float fix = ix - iix; 97 float i = in[iix]; 98 out[ox++] = i + (in[iix + 1] - i) * fix; 99 ix += pitch; 100 p_ix += p_pitch; 101 pitch += pitchstep; 102 p_pitch += p_pitchstep; 103 } 104 } 105 in_offset[0] = ix; 106 out_offset[0] = ox; 107 startpitch[0] = pitch; 108 109 } 110 }