JOAL v2.6.0-rc-20250706
JOAL, OpenAL® API Binding for Java™ (public API).
Synth02AL.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 * A 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 Synth02AL {
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 o = new SimpleSineSynth();
86 o.setFreq(freq);
87 o.setAmplitude(0.5f);
88 System.err.println("0: "+o);
89 o.play();
90 System.err.println("1: "+o);
91 enterValue("Press enter to start");
92 {
93 final float min = 100, max = 10000, step = 30;
94 final long t0 = Clock.currentNanos();
95 for(float f=min; f<max; f+=step) {
96 o.setFreq(f);
97 try {
98 Thread.sleep( o.getLatency() );
99 } catch (final InterruptedException e) { }
100 }
101 final long t1 = Clock.currentNanos();
102 final int exp = (int)( (max - min) / step ) * o.getLatency();
103 final int has = (int)TimeUnit.NANOSECONDS.toMillis(t1-t0);
104 final int diff = has - exp;
105 System.err.println("Loop "+has+" / "+exp+" [ms], diff "+diff+" [ms], "+((float)diff/(float)exp) * 100f+"%");
106 }
108 System.err.println("c: "+o);
109 boolean quit = false;
110 while ( !quit ){
111 final String in = enterValue("Enter new frequency or just enter to exit");
112 if( 0 == in.length() ) {
113 quit = true;
114 } else {
115 freq = atof(in, freq);
116 o.setFreq(freq);
117 System.err.println("n: "+o);
118 }
119 }
120 o.stop();
121 }
122}
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.
A continuous simple off-thread mutable sine wave synthesizer.
Definition: Synth02AL.java:54
static float atof(final String str, final float def)
Definition: Synth02AL.java:55
static void main(final String[] args)
Definition: Synth02AL.java:75
static String enterValue(final String message)
Definition: Synth02AL.java:64
A continuous simple off-thread mutable sine wave synthesizer.
int getLatency()
Returns latency or frame-duration in milliseconds.