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 * Lanczos interpolation resampler.
29 *
30 * @author Karl Helgason
31 */
32 public final class SoftLanczosResampler extends SoftAbstractResampler {
33
34 float[][] sinc_table;
35 int sinc_table_fsize = 2000;
36 int sinc_table_size = 5;
37 int sinc_table_center = sinc_table_size / 2;
38
39 public SoftLanczosResampler() {
40 super();
41 sinc_table = new float[sinc_table_fsize][];
42 for (int i = 0; i < sinc_table_fsize; i++) {
43 sinc_table[i] = sincTable(sinc_table_size, -i
44 / ((float) sinc_table_fsize));
52
53 // Generate sinc table
54 public static float[] sincTable(int size, float offset) {
55 int center = size / 2;
56 float[] w = new float[size];
57 for (int k = 0; k < size; k++) {
58 float x = (-center + k + offset);
59 if (x < -2 || x > 2)
60 w[k] = 0;
61 else if (x == 0)
62 w[k] = 1;
63 else {
64 w[k] = (float)(2.0 * Math.sin(Math.PI * x)
65 * Math.sin(Math.PI * x / 2.0)
66 / ((Math.PI * x) * (Math.PI * x)));
67 }
68 }
69 return w;
70 }
71
72 public int getPadding() // must be at least half of sinc_table_size
73 {
74 return sinc_table_size / 2 + 2;
75 }
76
77 public void interpolate(float[] in, float[] in_offset, float in_end,
78 float[] startpitch, float pitchstep, float[] out, int[] out_offset,
79 int out_end) {
80 float pitch = startpitch[0];
81 float ix = in_offset[0];
82 int ox = out_offset[0];
83 float ix_end = in_end;
84 int ox_end = out_end;
85
86 if (pitchstep == 0) {
87 while (ix < ix_end && ox < ox_end) {
88 int iix = (int) ix;
89 float[] sinc_table
90 = this.sinc_table[(int) ((ix - iix) * sinc_table_fsize)];
91 int xx = iix - sinc_table_center;
92 float y = 0;
93 for (int i = 0; i < sinc_table_size; i++, xx++)
94 y += in[xx] * sinc_table[i];
95 out[ox++] = y;
96 ix += pitch;
|
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 * Lanczos interpolation resampler.
30 *
31 * @author Karl Helgason
32 */
33 public final class SoftLanczosResampler extends SoftAbstractResampler {
34
35 float[][] sinc_table;
36 int sinc_table_fsize = 2000;
37 int sinc_table_size = 5;
38 int sinc_table_center = sinc_table_size / 2;
39
40 public SoftLanczosResampler() {
41 super();
42 sinc_table = new float[sinc_table_fsize][];
43 for (int i = 0; i < sinc_table_fsize; i++) {
44 sinc_table[i] = sincTable(sinc_table_size, -i
45 / ((float) sinc_table_fsize));
53
54 // Generate sinc table
55 public static float[] sincTable(int size, float offset) {
56 int center = size / 2;
57 float[] w = new float[size];
58 for (int k = 0; k < size; k++) {
59 float x = (-center + k + offset);
60 if (x < -2 || x > 2)
61 w[k] = 0;
62 else if (x == 0)
63 w[k] = 1;
64 else {
65 w[k] = (float)(2.0 * Math.sin(Math.PI * x)
66 * Math.sin(Math.PI * x / 2.0)
67 / ((Math.PI * x) * (Math.PI * x)));
68 }
69 }
70 return w;
71 }
72
73 @Override
74 public int getPadding() // must be at least half of sinc_table_size
75 {
76 return sinc_table_size / 2 + 2;
77 }
78
79 @Override
80 public void interpolate(float[] in, float[] in_offset, float in_end,
81 float[] startpitch, float pitchstep, float[] out, int[] out_offset,
82 int out_end) {
83 float pitch = startpitch[0];
84 float ix = in_offset[0];
85 int ox = out_offset[0];
86 float ix_end = in_end;
87 int ox_end = out_end;
88
89 if (pitchstep == 0) {
90 while (ix < ix_end && ox < ox_end) {
91 int iix = (int) ix;
92 float[] sinc_table
93 = this.sinc_table[(int) ((ix - iix) * sinc_table_fsize)];
94 int xx = iix - sinc_table_center;
95 float y = 0;
96 for (int i = 0; i < sinc_table_size; i++, xx++)
97 y += in[xx] * sinc_table[i];
98 out[ox++] = y;
99 ix += pitch;
|