JOAL v2.6.0-rc-20250706
JOAL, OpenAL® API Binding for Java™ (public API).
Sound3DTest.java
Go to the documentation of this file.
1/**
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * -Redistribution of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * -Redistribution in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
16 * be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * This software is provided "AS IS," without a warranty of any kind.
20 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
22 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS
23 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
24 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
25 * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
26 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
27 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
28 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
29 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30 *
31 * You acknowledge that this software is not designed or intended for use in the
32 * design, construction, operation or maintenance of any nuclear facility.
33 */
34package com.jogamp.openal.test.manual;
35
36import java.io.IOException;
37
38import com.jogamp.openal.UnsupportedAudioFileException;
39import com.jogamp.openal.sound3d.AudioSystem3D;
40import com.jogamp.openal.sound3d.Context;
41import com.jogamp.openal.sound3d.Device;
42import com.jogamp.openal.sound3d.Listener;
43import com.jogamp.openal.sound3d.Source;
44import com.jogamp.openal.test.resources.ResourceLocation;
45
46/**
47 * @author Athomas Goldberg
48 *
49 */
50public class Sound3DTest {
51
52 public static float lerp(final float v1, final float v2, final float t) {
53 float result = 0;
54 result = v1 + ((v2 - v1) * t);
55 return result;
56 }
57
58 public static void main(final String[] args) throws IOException, InterruptedException, UnsupportedAudioFileException {
59
60 if( !AudioSystem3D.isAvailable() ) {
61 System.err.println("AudioSystem3D is not available, static initialization failed");
62 return;
63 }
64
65 // create the initial context - this can be collapsed into the init.
66 final Device device = AudioSystem3D.openDevice(null);
67 final Context context = new Context(device, null);
68 if( !context.makeCurrent(false) ) {
69 System.err.println("Context.makeCurrent() failed");
70 return;
71 }
72
73 // get the listener object
74 final Listener listener = AudioSystem3D.getListener();
75 listener.setPosition(0, 0, 0);
76
77 // load a source and play it
79 source1.setPosition(0, 0, 0);
80 source1.setLooping(true);
81 source1.play();
82
83 Thread.sleep(10000);
84
85 // move the source
86 source1.setPosition(1, 1, 1);
87
88 // move the listener
89 for (int i = 0; i < 1000; i++) {
90 final float t = (i) / 1000f;
91 final float lp = lerp(0f, 2f, t);
92 listener.setPosition(lp, lp, lp);
93 Thread.sleep(10);
94 }
95
96 // fade listener out.
97 for (int i = 0; i < 1000; i++) {
98 final float t = (i) / 1000f;
99 final float lp = lerp(1f, 0f, t);
100 listener.setGain(lp);
101 Thread.sleep(10);
102 }
103
104 source1.stop();
105 source1.delete();
106 context.destroy();
107 device.close();
108
109 }
110}
The AudioSystem3D class provides a set of methods for creating and manipulating a 3D audio environmen...
static Listener getListener()
Get the listener object associated with this Sound3D environment.
static Source loadSource(final String filename)
Loads a Sound3D Source with the specified audio file.
static Device openDevice(final String deviceName)
Opens the named audio device.
static boolean isAvailable()
Returns the available state of this instance.
This class provides a Sound3D Context associated with a specified device.
Definition: Context.java:49
boolean makeCurrent(final boolean throwException)
Makes the audio context current on the calling thread.
Definition: Context.java:231
void destroy()
destroys this context freeing its resources.
Definition: Context.java:171
This class provides a handle to a specific audio device.
Definition: Device.java:46
void close()
closes the device, freeing its resources.
Definition: Device.java:105
This class represents the human listener in the Sound3D environment.
Definition: Listener.java:46
void setGain(final float gain)
Sets the Gain, or volume of the audio in the environment relative to the listener.
Definition: Listener.java:56
void setPosition(final float x, final float y, final float z)
Sets the position in (x-y-z coordinates) of the Listener in the Sound3D environment.
Definition: Listener.java:84
This class is used to represent sound-producing objects in the Sound3D environment.
Definition: Source.java:48
void stop()
Stops the audio in this Source.
Definition: Source.java:130
void delete()
Delete this source, freeing its resources.
Definition: Source.java:95
void play()
Beginning playing the audio in this source.
Definition: Source.java:116
void setPosition(final Vec3f position)
Sets the x,y,z position of the source.
Definition: Source.java:334
void setLooping(final boolean isLooping)
turns looping on or off.
Definition: Source.java:478
static void main(final String[] args)
static float lerp(final float v1, final float v2, final float t)
static InputStream getTestStream0()
WAV 22050Hz, 1 channel, S8_LE.