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 
26 package com.sun.media.sound;
27 
28 /**
29  * Hann windowed sinc interpolation resampler with anti-alias filtering.
30  *
31  * Using 30 points for the interpolation.
32  *
33  * @author Karl Helgason
34  */
35 public final class SoftSincResampler extends SoftAbstractResampler {
36 
37     float[][][] sinc_table;
38     int sinc_scale_size = 100;
39     int sinc_table_fsize = 800;
40     int sinc_table_size = 30;
41     int sinc_table_center = sinc_table_size / 2;
42 
SoftSincResampler()43     public SoftSincResampler() {
44         super();
45         sinc_table = new float[sinc_scale_size][sinc_table_fsize][];
46         for (int s = 0; s < sinc_scale_size; s++) {
47             float scale = (float) (1.0 / (1.0 + Math.pow(s, 1.1) / 10.0));
48             for (int i = 0; i < sinc_table_fsize; i++) {
49                 sinc_table[s][i] = sincTable(sinc_table_size,
50                         -i / ((float)sinc_table_fsize), scale);
51             }
52         }
53     }
54 
55     // Normalized sinc function
sinc(double x)56     public static double sinc(double x) {
57         return (x == 0.0) ? 1.0 : Math.sin(Math.PI * x) / (Math.PI * x);
58     }
59 
60     // Generate hann window suitable for windowing sinc
wHanning(int size, float offset)61     public static float[] wHanning(int size, float offset) {
62         float[] window_table = new float[size];
63         for (int k = 0; k < size; k++) {
64             window_table[k] = (float)(-0.5
65                     * Math.cos(2.0 * Math.PI * (double)(k + offset)
66                         / (double) size) + 0.5);
67         }
68         return window_table;
69     }
70 
71     // Generate sinc table
sincTable(int size, float offset, float scale)72     public static float[] sincTable(int size, float offset, float scale) {
73         int center = size / 2;
74         float[] w = wHanning(size, offset);
75         for (int k = 0; k < size; k++)
76             w[k] *= sinc((-center + k + offset) * scale) * scale;
77         return w;
78     }
79 
80     @Override
getPadding()81     public int getPadding() // must be at least half of sinc_table_size
82     {
83         return sinc_table_size / 2 + 2;
84     }
85 
86     @Override
interpolate(float[] in, float[] in_offset, float in_end, float[] startpitch, float pitchstep, float[] out, int[] out_offset, int out_end)87     public void interpolate(float[] in, float[] in_offset, float in_end,
88                             float[] startpitch, float pitchstep, float[] out, int[] out_offset,
89                             int out_end) {
90         float pitch = startpitch[0];
91         float ix = in_offset[0];
92         int ox = out_offset[0];
93         float ix_end = in_end;
94         int ox_end = out_end;
95         int max_p = sinc_scale_size - 1;
96         if (pitchstep == 0) {
97 
98             int p = (int) ((pitch - 1) * 10.0f);
99             if (p < 0)
100                 p = 0;
101             else if (p > max_p)
102                 p = max_p;
103             float[][] sinc_table_f = this.sinc_table[p];
104             while (ix < ix_end && ox < ox_end) {
105                 int iix = (int) ix;
106                 float[] sinc_table =
107                         sinc_table_f[(int)((ix - iix) * sinc_table_fsize)];
108                 int xx = iix - sinc_table_center;
109                 float y = 0;
110                 for (int i = 0; i < sinc_table_size; i++, xx++)
111                     y += in[xx] * sinc_table[i];
112                 out[ox++] = y;
113                 ix += pitch;
114             }
115         } else {
116             while (ix < ix_end && ox < ox_end) {
117                 int iix = (int) ix;
118                 int p = (int) ((pitch - 1) * 10.0f);
119                 if (p < 0)
120                     p = 0;
121                 else if (p > max_p)
122                     p = max_p;
123                 float[][] sinc_table_f = this.sinc_table[p];
124 
125                 float[] sinc_table =
126                         sinc_table_f[(int)((ix - iix) * sinc_table_fsize)];
127                 int xx = iix - sinc_table_center;
128                 float y = 0;
129                 for (int i = 0; i < sinc_table_size; i++, xx++)
130                     y += in[xx] * sinc_table[i];
131                 out[ox++] = y;
132 
133                 ix += pitch;
134                 pitch += pitchstep;
135             }
136         }
137         in_offset[0] = ix;
138         out_offset[0] = ox;
139         startpitch[0] = pitch;
140     }
141 }
142