JOAL v2.6.0-rc-20250706
JOAL, OpenAL® API Binding for Java™ (public API).
Synth02bAL.java
Go to the documentation of this file.
1/**
2 * Copyright 2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.openal.test.manual;
29
30import java.io.BufferedReader;
31import java.io.IOException;
32import java.io.InputStreamReader;
33import java.util.concurrent.TimeUnit;
34
35import com.jogamp.common.os.Clock;
36import com.jogamp.openal.ALFactory;
37import com.jogamp.openal.JoalVersion;
38import com.jogamp.openal.util.SimpleSineSynth;
39
40/**
41 * Using two continuous simple off-thread mutable sine wave synthesizer.
42 * <p>
43 * Implementation utilizes an off-thread worker thread,
44 * allowing to change frequency and amplitude without disturbance.
45 * </p>
46 * <p>
47 * Latency is hardcoded as 1 - 3 times frameDuration, having a frameDuration of 12 ms.
48 * Averages around 24 ms.
49 * </p>
50 * <p>
51 * Latency needs improvement to have a highly responsive life-music synthesizer.
52 * </p>
53 */
54public final class Synth02bAL {
55 public static float atof(final String str, final float def) {
56 try {
57 return Float.parseFloat(str);
58 } catch (final Exception ex) {
59 ex.printStackTrace();
60 }
61 return def;
62 }
63
64 public static String enterValue(final String message) {
65 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
66 System.err.println(message);
67 try {
68 final String s = stdin.readLine();
69 System.err.println(s);
70 return s;
71 } catch (final IOException e) { e.printStackTrace(); }
72 return "";
73 }
74
75 public static void main(final String[] args) {
76 float freq = SimpleSineSynth.MIDDLE_C;
77 for(int i=0; i<args.length; i++) {
78 if(args[i].equals("-f")) {
79 i++;
80 freq = atof(args[i], freq);
81 }
82 }
83 System.err.println(JoalVersion.getInstance().toString(ALFactory.getALC()));
84
85 final SimpleSineSynth o1 = new SimpleSineSynth();
86 o1.setFreq(freq);
87 o1.setAmplitude(0.5f);
88 System.err.println("o1 0: "+o1);
89 o1.play();
90 System.err.println("o1 1: "+o1);
91
92 final SimpleSineSynth o2 = new SimpleSineSynth();
93 o2.setFreq(freq * 1.5f); // + half octave
94 o2.setAmplitude(0.5f / 2f);
95 System.err.println("o2 0: "+o2);
96 o2.play();
97 System.err.println("o2 1: "+o2);
98
99 enterValue("Press enter to start");
100 {
101 final float min = 100, max = 10000, step = 30;
102 final long t0 = Clock.currentNanos();
103 for(float f=min; f<max; f+=step) {
104 o1.setFreq(f);
105 o2.setFreq(f * 1.5f); // + half octave
106 try {
107 Thread.sleep( Math.max( o1.getLatency(), o2.getLatency() ) );
108 } catch (final InterruptedException e) { }
109 }
110 final long t1 = Clock.currentNanos();
111 final int exp = (int)( (max - min) / step ) * o1.getLatency();
112 final int has = (int)TimeUnit.NANOSECONDS.toMillis(t1-t0);
113 final int diff = has - exp;
114 System.err.println("Loop "+has+" / "+exp+" [ms], diff "+diff+" [ms], "+((float)diff/(float)exp) * 100f+"%");
115 }
116 System.err.println("o1: "+o1);
117 System.err.println("o2: "+o2);
118 o1.stop();
119 o2.stop();
120 }
121}
This class provides factory methods for generating AL and ALC objects.
Definition: ALFactory.java:62
static ALC getALC()
Get the default ALC object.
Definition: ALFactory.java:136
static JoalVersion getInstance()
StringBuilder toString(final ALC alc, StringBuilder sb)
Return JogampVersion package information and AL informal strings.
Using two continuous simple off-thread mutable sine wave synthesizer.
Definition: Synth02bAL.java:54
static String enterValue(final String message)
Definition: Synth02bAL.java:64
static void main(final String[] args)
Definition: Synth02bAL.java:75
static float atof(final String str, final float def)
Definition: Synth02bAL.java:55
A continuous simple off-thread mutable sine wave synthesizer.
int getLatency()
Returns latency or frame-duration in milliseconds.