35 * |
35 * |
36 * @author Karl Helgason |
36 * @author Karl Helgason |
37 */ |
37 */ |
38 public final class EmergencySoundbank { |
38 public final class EmergencySoundbank { |
39 |
39 |
40 private final static String[] general_midi_instruments = { |
40 private static final String[] general_midi_instruments = { |
41 "Acoustic Grand Piano", |
41 "Acoustic Grand Piano", |
42 "Bright Acoustic Piano", |
42 "Bright Acoustic Piano", |
43 "Electric Grand Piano", |
43 "Electric Grand Piano", |
44 "Honky-tonk Piano", |
44 "Honky-tonk Piano", |
45 "Electric Piano 1", |
45 "Electric Piano 1", |
2562 } |
2562 } |
2563 |
2563 |
2564 return ins; |
2564 return ins; |
2565 } |
2565 } |
2566 |
2566 |
2567 static public void ifft(double[] data) { |
2567 public static void ifft(double[] data) { |
2568 new FFT(data.length / 2, 1).transform(data); |
2568 new FFT(data.length / 2, 1).transform(data); |
2569 } |
2569 } |
2570 |
2570 |
2571 static public void fft(double[] data) { |
2571 public static void fft(double[] data) { |
2572 new FFT(data.length / 2, -1).transform(data); |
2572 new FFT(data.length / 2, -1).transform(data); |
2573 } |
2573 } |
2574 |
2574 |
2575 public static void complexGaussianDist(double[] cdata, double m, |
2575 public static void complexGaussianDist(double[] cdata, double m, |
2576 double s, double v) { |
2576 double s, double v) { |
2578 cdata[x * 2] += v * (1.0 / (s * Math.sqrt(2 * Math.PI)) |
2578 cdata[x * 2] += v * (1.0 / (s * Math.sqrt(2 * Math.PI)) |
2579 * Math.exp((-1.0 / 2.0) * Math.pow((x - m) / s, 2.0))); |
2579 * Math.exp((-1.0 / 2.0) * Math.pow((x - m) / s, 2.0))); |
2580 } |
2580 } |
2581 } |
2581 } |
2582 |
2582 |
2583 static public void randomPhase(double[] data) { |
2583 public static void randomPhase(double[] data) { |
2584 for (int i = 0; i < data.length; i += 2) { |
2584 for (int i = 0; i < data.length; i += 2) { |
2585 double phase = Math.random() * 2 * Math.PI; |
2585 double phase = Math.random() * 2 * Math.PI; |
2586 double d = data[i]; |
2586 double d = data[i]; |
2587 data[i] = Math.sin(phase) * d; |
2587 data[i] = Math.sin(phase) * d; |
2588 data[i + 1] = Math.cos(phase) * d; |
2588 data[i + 1] = Math.cos(phase) * d; |
2589 } |
2589 } |
2590 } |
2590 } |
2591 |
2591 |
2592 static public void randomPhase(double[] data, Random random) { |
2592 public static void randomPhase(double[] data, Random random) { |
2593 for (int i = 0; i < data.length; i += 2) { |
2593 for (int i = 0; i < data.length; i += 2) { |
2594 double phase = random.nextDouble() * 2 * Math.PI; |
2594 double phase = random.nextDouble() * 2 * Math.PI; |
2595 double d = data[i]; |
2595 double d = data[i]; |
2596 data[i] = Math.sin(phase) * d; |
2596 data[i] = Math.sin(phase) * d; |
2597 data[i + 1] = Math.cos(phase) * d; |
2597 data[i + 1] = Math.cos(phase) * d; |
2598 } |
2598 } |
2599 } |
2599 } |
2600 |
2600 |
2601 static public void normalize(double[] data, double target) { |
2601 public static void normalize(double[] data, double target) { |
2602 double maxvalue = 0; |
2602 double maxvalue = 0; |
2603 for (int i = 0; i < data.length; i++) { |
2603 for (int i = 0; i < data.length; i++) { |
2604 if (data[i] > maxvalue) |
2604 if (data[i] > maxvalue) |
2605 maxvalue = data[i]; |
2605 maxvalue = data[i]; |
2606 if (-data[i] > maxvalue) |
2606 if (-data[i] > maxvalue) |
2611 double gain = target / maxvalue; |
2611 double gain = target / maxvalue; |
2612 for (int i = 0; i < data.length; i++) |
2612 for (int i = 0; i < data.length; i++) |
2613 data[i] *= gain; |
2613 data[i] *= gain; |
2614 } |
2614 } |
2615 |
2615 |
2616 static public void normalize(float[] data, double target) { |
2616 public static void normalize(float[] data, double target) { |
2617 double maxvalue = 0.5; |
2617 double maxvalue = 0.5; |
2618 for (int i = 0; i < data.length; i++) { |
2618 for (int i = 0; i < data.length; i++) { |
2619 if (data[i * 2] > maxvalue) |
2619 if (data[i * 2] > maxvalue) |
2620 maxvalue = data[i * 2]; |
2620 maxvalue = data[i * 2]; |
2621 if (-data[i * 2] > maxvalue) |
2621 if (-data[i * 2] > maxvalue) |
2624 double gain = target / maxvalue; |
2624 double gain = target / maxvalue; |
2625 for (int i = 0; i < data.length; i++) |
2625 for (int i = 0; i < data.length; i++) |
2626 data[i * 2] *= gain; |
2626 data[i * 2] *= gain; |
2627 } |
2627 } |
2628 |
2628 |
2629 static public double[] realPart(double[] in) { |
2629 public static double[] realPart(double[] in) { |
2630 double[] out = new double[in.length / 2]; |
2630 double[] out = new double[in.length / 2]; |
2631 for (int i = 0; i < out.length; i++) { |
2631 for (int i = 0; i < out.length; i++) { |
2632 out[i] = in[i * 2]; |
2632 out[i] = in[i * 2]; |
2633 } |
2633 } |
2634 return out; |
2634 return out; |
2635 } |
2635 } |
2636 |
2636 |
2637 static public double[] imgPart(double[] in) { |
2637 public static double[] imgPart(double[] in) { |
2638 double[] out = new double[in.length / 2]; |
2638 double[] out = new double[in.length / 2]; |
2639 for (int i = 0; i < out.length; i++) { |
2639 for (int i = 0; i < out.length; i++) { |
2640 out[i] = in[i * 2]; |
2640 out[i] = in[i * 2]; |
2641 } |
2641 } |
2642 return out; |
2642 return out; |
2643 } |
2643 } |
2644 |
2644 |
2645 static public float[] toFloat(double[] in) { |
2645 public static float[] toFloat(double[] in) { |
2646 float[] out = new float[in.length]; |
2646 float[] out = new float[in.length]; |
2647 for (int i = 0; i < out.length; i++) { |
2647 for (int i = 0; i < out.length; i++) { |
2648 out[i] = (float) in[i]; |
2648 out[i] = (float) in[i]; |
2649 } |
2649 } |
2650 return out; |
2650 return out; |
2651 } |
2651 } |
2652 |
2652 |
2653 static public byte[] toBytes(float[] in, AudioFormat format) { |
2653 public static byte[] toBytes(float[] in, AudioFormat format) { |
2654 byte[] out = new byte[in.length * format.getFrameSize()]; |
2654 byte[] out = new byte[in.length * format.getFrameSize()]; |
2655 return AudioFloatConverter.getConverter(format).toByteArray(in, out); |
2655 return AudioFloatConverter.getConverter(format).toByteArray(in, out); |
2656 } |
2656 } |
2657 |
2657 |
2658 static public void fadeUp(double[] data, int samples) { |
2658 public static void fadeUp(double[] data, int samples) { |
2659 double dsamples = samples; |
2659 double dsamples = samples; |
2660 for (int i = 0; i < samples; i++) |
2660 for (int i = 0; i < samples; i++) |
2661 data[i] *= i / dsamples; |
2661 data[i] *= i / dsamples; |
2662 } |
2662 } |
2663 |
2663 |
2664 static public void fadeUp(float[] data, int samples) { |
2664 public static void fadeUp(float[] data, int samples) { |
2665 double dsamples = samples; |
2665 double dsamples = samples; |
2666 for (int i = 0; i < samples; i++) |
2666 for (int i = 0; i < samples; i++) |
2667 data[i] *= i / dsamples; |
2667 data[i] *= i / dsamples; |
2668 } |
2668 } |
2669 |
2669 |
2670 static public double[] loopExtend(double[] data, int newsize) { |
2670 public static double[] loopExtend(double[] data, int newsize) { |
2671 double[] outdata = new double[newsize]; |
2671 double[] outdata = new double[newsize]; |
2672 int p_len = data.length; |
2672 int p_len = data.length; |
2673 int p_ps = 0; |
2673 int p_ps = 0; |
2674 for (int i = 0; i < outdata.length; i++) { |
2674 for (int i = 0; i < outdata.length; i++) { |
2675 outdata[i] = data[p_ps]; |
2675 outdata[i] = data[p_ps]; |