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     public int getPadding() {
  37         return 2;
  38     }
  39 
  40     public void interpolate(float[] in, float[] in_offset, float in_end,
  41             float[] startpitch, float pitchstep, float[] out, int[] out_offset,
  42             int out_end) {
  43 
  44         float pitch = startpitch[0];
  45         float ix = in_offset[0];
  46         int ox = out_offset[0];
  47         float ix_end = in_end;
  48         int ox_end = out_end;
  49 
  50         // Check if we have do anything
  51         if (!(ix < ix_end && ox < ox_end))
  52             return;
  53 
  54         // 15 bit shift was choosed because
  55         // it resulted in no drift between p_ix and ix.
  56         int p_ix = (int) (ix * (1 << 15));
  57         int p_ix_end = (int) (ix_end * (1 << 15));
  58         int p_pitch = (int) (pitch * (1 << 15));
  59         // Pitch needs to recalculated
  60         // to ensure no drift between p_ix and ix.
  61         pitch = p_pitch * (1f / (1 << 15));
  62 
  63         if (pitchstep == 0f) {
  64 
  65             // To reduce
  66             //    while (p_ix < p_ix_end && ox < ox_end)
  67             // into
  68             //    while  (ox < ox_end)
  69             // We need to calculate new ox_end value.
  70             int p_ix_len = p_ix_end - p_ix;
  71             int p_mod = p_ix_len % p_pitch;
  72             if (p_mod != 0)
  73                 p_ix_len += p_pitch - p_mod;
  74             int ox_end2 = ox + p_ix_len / p_pitch;
  75             if (ox_end2 < ox_end)
  76                 ox_end = ox_end2;
  77 
  78             while (ox < ox_end) {
  79                 int iix = p_ix >> 15;
  80                 float fix = ix - iix;
  81                 float i = in[iix];
  82                 out[ox++] = i + (in[iix + 1] - i) * fix;
  83                 p_ix += p_pitch;
  84                 ix += pitch;
  85             }
  86 
  87         } else {
  88 
  89             int p_pitchstep = (int) (pitchstep * (1 << 15));
  90             pitchstep = p_pitchstep * (1f / (1 << 15));
  91 
  92             while (p_ix < p_ix_end && ox < ox_end) {
  93                 int iix = p_ix >> 15;
  94                 float fix = ix - iix;
  95                 float i = in[iix];
  96                 out[ox++] = i + (in[iix + 1] - i) * fix;
  97                 ix += pitch;
  98                 p_ix += p_pitch;
  99                 pitch += pitchstep;
 100                 p_pitch += p_pitchstep;
 101             }
 102         }
 103         in_offset[0] = ix;
 104         out_offset[0] = ox;
 105         startpitch[0] = pitch;
 106 
 107     }
 108 }