JOAL v2.6.0-rc-20250712
JOAL, OpenAL® API Binding for Java™ (public API).
ALut.java
Go to the documentation of this file.
1/**
2* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are met:
6*
7* -Redistribution of source code must retain the above copyright notice,
8* this list of conditions and the following disclaimer.
9*
10* -Redistribution in binary form must reproduce the above copyright notice,
11* this list of conditions and the following disclaimer in the documentation
12* and/or other materials provided with the distribution.
13*
14* Neither the name of Sun Microsystems, Inc. or the names of contributors may
15* be used to endorse or promote products derived from this software without
16* specific prior written permission.
17*
18* This software is provided "AS IS," without a warranty of any kind.
19* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
20* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
21* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS
22* LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
23* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
24* IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
25* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
26* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
27* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
28* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
29*
30* You acknowledge that this software is not designed or intended for use in the
31* design, construction, operation or maintenance of any nuclear facility.
32*
33* Created on Jun 27, 2003
34*/
35
36package com.jogamp.openal.util;
37
38import java.io.*;
39import java.nio.ByteBuffer;
40
41import com.jogamp.openal.*;
42
43/**
44 * @author Athomas Goldberg
45 *
46 */
47public final class ALut {
48
49 private static ALC alc;
50 private static ALCdevice device;
51 private static ALCcontext context;
52 private static Thread initializingThread;
53
54 private ALut() { }
55
56 /** Initializes the OpenAL Utility Toolkit, creates an OpenAL
57 context and makes it current on the current thread. The ALut may
58 only be initialized on one thread at any given time. */
59 public static synchronized void alutInit() throws ALException {
60 if (context != null) {
61 throw new ALException("Already initialized on thread " + initializingThread.getName());
62 }
63 if (alc == null) {
64 alc = ALFactory.getALC();
65 }
66 final String deviceName = null;
67 final ALCdevice d = alc.alcOpenDevice(deviceName);
68 if (d == null) {
69 throw new ALException("Error opening default OpenAL device");
70 }
71 final ALCcontext c = alc.alcCreateContext(d, null);
72 if (c == null) {
73 alc.alcCloseDevice(d);
74 throw new ALException("Error creating OpenAL context");
75 }
77 if (alc.alcGetError(d) != 0) {
78 alc.alcDestroyContext(c);
79 alc.alcCloseDevice(d);
80 throw new ALException("Error making OpenAL context current");
81 }
82 // Fully initialized; finish setup
83 device = d;
84 context = c;
85 initializingThread = Thread.currentThread();
86 }
87
88 /** Shuts down the OpenAL Utility Toolkit; releases and destroys the
89 internal OpenAL context and closes the output device. Must be
90 called from the same thread as alutInit(). Most applications
91 should not need to call this; only those which wish to toggle
92 sound on / off at run time by initializing and un-initializing
93 OpenAL need to call it. */
94 public static synchronized void alutExit() throws ALException {
95 if (context == null) {
96 throw new ALException("Not initialized");
97 }
98 alc.alcMakeContextCurrent(null);
99 alc.alcDestroyContext(context);
100 alc.alcCloseDevice(device);
101 context = null;
102 device = null;
103 initializingThread = null;
104 }
105
106 public static void alutLoadWAVFile(final String fileName,
107 final int[] format,
108 final ByteBuffer[] data,
109 final int[] size,
110 final int[] freq,
111 final int[] loop) throws ALException {
112 try {
113 final WAVData wd = WAVLoader.loadFromFile(fileName);
114 format[0] = wd.format;
115 data[0] = wd.data;
116 size[0] = wd.size;
117 freq[0] = wd.freq;
118 loop[0] = wd.loop ? ALConstants.AL_TRUE : ALConstants.AL_FALSE;
119 } catch (final Exception e) {
120 throw new ALException(e);
121 }
122 }
123
124 public static void alutLoadWAVFile(InputStream stream,
125 final int[] format,
126 final ByteBuffer[] data,
127 final int[] size,
128 final int[] freq,
129 final int[] loop) throws ALException {
130 try {
131 if (!(stream instanceof BufferedInputStream)) {
132 stream = new BufferedInputStream(stream);
133 }
134 final WAVData wd = WAVLoader.loadFromStream(stream);
135 format[0] = wd.format;
136 data[0] = wd.data;
137 size[0] = wd.size;
138 freq[0] = wd.freq;
139 loop[0] = wd.loop ? ALConstants.AL_TRUE : ALConstants.AL_FALSE;
140 } catch (final Exception e) {
141 throw new ALException(e);
142 }
143 }
144}
A generic exception for OpenAL errors used throughout the binding as a substitute for RuntimeExceptio...
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 void alutLoadWAVFile(InputStream stream, final int[] format, final ByteBuffer[] data, final int[] size, final int[] freq, final int[] loop)
Definition: ALut.java:124
static synchronized void alutInit()
Initializes the OpenAL Utility Toolkit, creates an OpenAL context and makes it current on the current...
Definition: ALut.java:59
static synchronized void alutExit()
Shuts down the OpenAL Utility Toolkit; releases and destroys the internal OpenAL context and closes t...
Definition: ALut.java:94
static void alutLoadWAVFile(final String fileName, final int[] format, final ByteBuffer[] data, final int[] size, final int[] freq, final int[] loop)
Definition: ALut.java:106
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
A Loader utility for (.wav) files.
Definition: WAVLoader.java:51
static WAVData loadFromStream(final InputStream stream)
This method loads a (.wav) file into a WAVData object.
Definition: WAVLoader.java:81
static WAVData loadFromFile(final String filename)
This method loads a (.wav) file into a WAVData object.
Definition: WAVLoader.java:64
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...
int alcGetError(ALCdevice device)
Entry point (through function pointer) to C language function: ALCenum alcGetError(ALCdevice * dev...
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_FALSE
Define "AL_FALSE" with expression '0', CType: int.