JOAL v2.6.0-rc-20250712
JOAL, OpenAL® API Binding for Java™ (public API).
OpenALTest.java
Go to the documentation of this file.
1package com.jogamp.openal.test.manual;
2
3/**
4 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
5 * Copyright (c) 2011 JogAmp Community. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * -Redistribution of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * -Redistribution in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18 * be used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind.
22 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
24 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS
25 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
26 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
27 * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
28 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
31 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use in the
34 * design, construction, operation or maintenance of any nuclear facility.
35 */
36import java.io.IOException;
37import java.nio.ByteOrder;
38import java.nio.IntBuffer;
39
40import com.jogamp.common.nio.Buffers;
41import com.jogamp.openal.AL;
42import com.jogamp.openal.ALC;
43import com.jogamp.openal.ALCcontext;
44import com.jogamp.openal.ALCdevice;
45import com.jogamp.openal.ALConstants;
46import com.jogamp.openal.ALFactory;
47import com.jogamp.openal.ALVersion;
48import com.jogamp.openal.JoalVersion;
49import com.jogamp.openal.UnsupportedAudioFileException;
50import com.jogamp.openal.test.resources.ResourceLocation;
51import com.jogamp.openal.util.WAVData;
52
53/**
54 * @author Athomas Goldberg, Michael Bien, et.al.
55 */
56public class OpenALTest {
57 private static final AL al;
58 private static final ALC alc;
59 private ALCdevice device = null;
60 private ALCcontext context = null;
61 private int[] sources = null;
62 private boolean initialized = false;
63
64 static {
65 alc = ALFactory.getALC();
66 al = ALFactory.getAL();
67 }
68
69 public OpenALTest() {
70 }
71
72 public void init() throws UnsupportedAudioFileException, IOException {
73 if( initialized ) {
74 return;
75 }
76 System.err.println(JoalVersion.getInstance().toString(alc));
77
78 device = alc.alcOpenDevice(null);
79 context = alc.alcCreateContext(device, null);
80 alc.alcMakeContextCurrent(context);
81 System.out.println("ALVersion: "+new ALVersion(al).toString());
82
83 final int[] buffers = new int[1];
84 al.alGenBuffers(1, buffers, 0);
85
86 // WAVData wd = WAVData.loadFromStream(ResourceLocation.getTestStream0(), ResourceLocation.getTestStream0Size(), 1, 8, 22050, ByteOrder.LITTLE_ENDIAN, true);
87 // WAVData wd = WAVData.loadFromStream(ResourceLocation.getTestStream1(), ResourceLocation.getTestStream1Size(), 2, 16, 44100, ByteOrder.BIG_ENDIAN, true);
88 final WAVData wd = WAVData.loadFromStream(ResourceLocation.getTestStream2(), ResourceLocation.getTestStream2Size(), 2, 16, 44100, ByteOrder.LITTLE_ENDIAN, true);
89 // WAVData wd = WAVData.loadFromStream(ResourceLocation.getTestStream3(), ResourceLocation.getTestStream3Size(), 2, 16, 44100, ByteOrder.LITTLE_ENDIAN, true);
90 System.out.println("*** size "+wd.data.limit());
91
92 al.alBufferData(buffers[0], wd.format, wd.data, wd.size, wd.freq);
93
94 sources = new int[1];
95 al.alGenSources(1, sources, 0);
96 al.alSourcei(sources[0], ALConstants.AL_BUFFER, buffers[0]);
97
98 final int[] loopArray = new int[1];
99 al.alGetSourcei(sources[0], ALConstants.AL_LOOPING, loopArray, 0);
100 System.err.println("Looping 1: " + (loopArray[0] == ALConstants.AL_TRUE));
101
102 initialized = true;
103 }
104
105 public void play() {
106 if( !initialized ) {
107 return;
108 }
109 System.out.println("play direct");
110 al.alSourceRewind(sources[0]);
111 al.alSourcePlay(sources[0]);
112 }
113
114 public void play3f(final float x, final float y, final float z) {
115 if( !initialized ) {
116 return;
117 }
118 System.out.println("play3f "+x+", "+y+", "+z);
119 al.alSourceRewind(sources[0]);
120 al.alSourcePlay(sources[0]);
121 al.alSource3f(sources[0], ALConstants.AL_POSITION, x, y, z);
122 }
123
124 public void pause() {
125 if( !initialized ) {
126 return;
127 }
128 al.alSourcePause(sources[0]);
129 }
130
131 public void dispose() {
132 if( !initialized ) {
133 return;
134 }
135 if( null != sources ) {
136 al.alSourceStop(sources[0]);
137 al.alDeleteSources(1, sources, 0);
138 sources = null;
139 }
140 if( null != context ) {
141 alc.alcMakeContextCurrent(null);
142 alc.alcDestroyContext(context);
143 context = null;
144 }
145 if( null != device ) {
146 alc.alcCloseDevice(device);
147 device = null;
148 }
149 initialized = false;
150 }
151
152 public static void main(final String[] args) throws InterruptedException, UnsupportedAudioFileException, IOException {
153 final OpenALTest demo = new OpenALTest();
154 demo.init();
155
156 demo.play();
157 Thread.sleep(5000);
158
159 demo.play3f(2f, 2f, 2f);
160 Thread.sleep(5000);
161
162 demo.play3f(3f, 3f, 3f);
163 Thread.sleep(5000);
164
165 demo.play3f(0f, 0f, 0f);
166 Thread.sleep(5000);
167
168 demo.dispose();
169 }
170}
This class provides factory methods for generating AL and ALC objects.
Definition: ALFactory.java:62
static AL getAL()
Get the default AL object.
Definition: ALFactory.java:122
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.
void play3f(final float x, final float y, final float z)
static void main(final String[] args)
static InputStream getTestStream2()
CDR 44100Hz, 2 channels, S16_LE.
This class is a holder for WAV (.wav ) file Data returned from the WavLoader, or directly via loadFro...
Definition: WAVData.java:53
final int format
the format of the Data.
Definition: WAVData.java:66
final ByteBuffer data
The audio data.
Definition: WAVData.java:55
final int size
Size (in bytes) of the data.
Definition: WAVData.java:69
final int freq
The frequency of the data.
Definition: WAVData.java:72
static WAVData loadFromStream(InputStream aIn, final int byteCount, final int numChannels, final int bits, final int sampleRate, final ByteOrder byteOrder, final boolean loop)
This method loads a (.wav) file into a WAVData object.
Definition: WAVData.java:100
void alcDestroyContext(ALCcontext context)
Entry point (through function pointer) to C language function: void alcDestroyContext(ALCcontext * ...
boolean alcMakeContextCurrent(ALCcontext context)
Entry point (through function pointer) to C language function: ALCboolean alcMakeContextCurrent(ALC...
boolean alcCloseDevice(ALCdevice device)
Entry point (through function pointer) to C language function: ALCboolean alcCloseDevice(ALCdevice ...
ALCdevice alcOpenDevice(String devicename)
Entry point (through function pointer) to C language function: ALCdevice * alcOpenDevice(const ALCc...
ALCcontext alcCreateContext(ALCdevice device, IntBuffer attrlist)
Entry point (through function pointer) to C language function: ALCcontext * alcCreateContext(ALCdev...
static final int AL_BUFFER
Define "AL_BUFFER" with expression '0x1009', CType: int.
static final int AL_TRUE
Define "AL_TRUE" with expression '1', CType: int.
static final int AL_LOOPING
Define "AL_LOOPING" with expression '0x1007', CType: int.
static final int AL_POSITION
Define "AL_POSITION" with expression '0x1004', CType: int.
void alBufferData(int buffer, int format, Buffer data, int size, int samplerate)
Entry point (through function pointer) to C language function: void alBufferData(ALuint buffer,...
void alSourcePause(int source)
Entry point (through function pointer) to C language function: void alSourcePause(ALuint source)
void alSource3f(int source, int param, float value1, float value2, float value3)
Entry point (through function pointer) to C language function: void alSource3f(ALuint source,...
void alGenBuffers(int n, IntBuffer buffers)
Entry point (through function pointer) to C language function: void alGenBuffers(ALsizei n,...
void alDeleteSources(int n, IntBuffer sources)
Entry point (through function pointer) to C language function: void alDeleteSources(ALsizei n,...
void alSourcePlay(int source)
Entry point (through function pointer) to C language function: void alSourcePlay(ALuint source)
void alGetSourcei(int source, int param, IntBuffer value)
Entry point (through function pointer) to C language function: void alGetSourcei(ALuint source,...
void alSourcei(int source, int param, int value)
Entry point (through function pointer) to C language function: void alSourcei(ALuint source,...
void alSourceRewind(int source)
Entry point (through function pointer) to C language function: void alSourceRewind(ALuint source)
void alGenSources(int n, IntBuffer sources)
Entry point (through function pointer) to C language function: void alGenSources(ALsizei n,...
void alSourceStop(int source)
Entry point (through function pointer) to C language function: void alSourceStop(ALuint source)