28package com.jogamp.openal.test.manual;
30import java.io.BufferedReader;
31import java.io.IOException;
32import java.io.InputStreamReader;
33import java.nio.ShortBuffer;
35import com.jogamp.openal.AL;
36import com.jogamp.openal.ALC;
37import com.jogamp.openal.ALCcontext;
38import com.jogamp.openal.ALCdevice;
39import com.jogamp.openal.ALConstants;
40import com.jogamp.openal.ALFactory;
41import com.jogamp.openal.ALVersion;
52 public static final float PI = 3.14159265358979323846f;
57 private static final float EPSILON = 1.1920929E-7f;
59 private static final float SHORT_MAX = 32767.0f;
62 final BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
63 System.err.println(
"> Press enter to "+message);
65 System.err.println(stdin.readLine());
66 }
catch (
final IOException e) { e.printStackTrace(); }
69 private ALC alc =
null;
74 private final int[] buffers = { 0 };
75 private final int[] sources = { 0 };
77 private void alCheckError(
final String given_label,
final boolean throwEx) {
80 final String msg = String.format(
"ERROR - 0x%X %s (%s)", error, al.
alGetString(error), given_label);
81 System.err.println(msg);
83 throw new RuntimeException(msg);
94 System.out.println(
"ALVersion: "+
new ALVersion(al).toString());
95 System.out.println(
"Output devices:");
98 if(
null != outDevices ) {
99 for (
final String name : outDevices) {
100 System.out.println(
" "+name);
104 System.out.println(
"Output all devices:");
107 if(
null != outDevices ) {
108 for (
final String name : outDevices) {
109 System.out.println(
" "+name);
113 alCheckError(
"setup",
true);
116 alCheckError(
"alGenBuffers",
true);
120 alCheckError(
"alGenSources",
true);
126 for (
int ii = 0; ii < 1; ++ii) {
129 alCheckError(
"sources: stop and disconnected",
true);
134 alCheckError(
"sources/buffers: deleted",
true);
136 if(
null != context ) {
141 if(
null != device ) {
147 public static int findBestWaveCount(
final float freq,
final int sampleRate,
final int minWaves,
final int maxWaves) {
148 final float period = 1.0f / freq;
149 final float sample_step = (
TWO_PI * freq ) / sampleRate;
151 float s_diff = Float.MAX_VALUE;
152 float sc_diff = Float.MAX_VALUE;
154 for(wave_count = minWaves; wave_count < maxWaves && s_diff >= EPSILON; ++wave_count) {
155 final float d = wave_count * period;
156 final float sc_f = d * sampleRate;
157 final int sc_i = (int)sc_f;
158 final float s1 = (float) Math.abs( Math.sin( sample_step * sc_i ) );
161 sc_diff = sc_f - sc_i;
162 wc_best = wave_count;
165 System.err.printf(
"%nBest: %d/[%d..%d], waves %d, sample_count diff %.12f, sample diff %.12f%n", wave_count, minWaves, maxWaves, wc_best, sc_diff, s_diff);
169 private static final int SAMPLE_RATE = 44100;
171 public void loop(
final float freq ) {
172 final float period = 1.0f / freq;
173 final float sample_step = (
TWO_PI * freq ) / SAMPLE_RATE;
176 final float duration = wave_count * period;
177 final int sample_count = (int)( duration * SAMPLE_RATE );
179 System.err.printf(
"%nFreq %f Hz, period %f [ms], waves %d, duration %f [ms], sample[rate %d, step %f]%n", freq, 1000.0*period, wave_count, 1000.0*duration, SAMPLE_RATE, sample_step);
182 final ShortBuffer samples = ShortBuffer.allocate(sample_count);
184 for(
int i=0; i<sample_count; ++i) {
185 final float s = (float) Math.sin( sample_step * i );
186 samples.put( (
short)( SHORT_MAX * s ) );
189 alCheckError(
"populating samples",
true);
193 alCheckError(
"alBufferData samples",
true);
199 alCheckError(
"alSourcei source <-> buffer",
true);
202 final int[] loopArray =
new int[1];
208 alCheckError(
"alSourcePlay",
true);
212 final int[] current_playing_state = { 0 };
214 alCheckError(
"alGetSourcei AL_SOURCE_STATE",
true);
221 public static float atof(
final String str,
final float def) {
223 return Float.parseFloat(str);
224 }
catch (
final Exception ex) {
225 ex.printStackTrace();
230 public static void main(
final String[] args) {
232 for(
int i=0; i<args.length; i++) {
233 if(args[i].equals(
"-f")) {
235 freq =
atof(args[i], freq);
This class provides factory methods for generating AL and ALC objects.
static AL getAL()
Get the default AL object.
static ALC getALC()
Get the default ALC object.
A continuous simple on-thread immutable sine wave synthesizer.
static final float PI
The value PI, i.e.
static void waitForKey(final String message)
static float atof(final String str, final float def)
static final float TWO_PI
The value 2PI, i.e.
static int findBestWaveCount(final float freq, final int sampleRate, final int minWaves, final int maxWaves)
void loop(final float freq)
static void main(final String[] args)
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...
java.lang.String[] alcGetAllDeviceSpecifiers()
Fetches the names of the available ALC all capture device specifiers.
java.lang.String[] alcGetDeviceSpecifiers()
Fetches the names of the available ALC device specifiers.
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_PLAYING
Define "AL_PLAYING" with expression '0x1012', 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_NO_ERROR
Define "AL_NO_ERROR" with expression '0', CType: int.
static final int AL_FORMAT_MONO16
Define "AL_FORMAT_MONO16" with expression '0x1101', CType: int.
static final int AL_SOURCE_STATE
Define "AL_SOURCE_STATE" with expression '0x1010', 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,...
int alGetError()
Entry point (through function pointer) to C language function: ALenum alGetError()
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 alSourceStopv(int n, IntBuffer sources)
Entry point (through function pointer) to C language function: void alSourceStopv(ALsizei n,...
void alDeleteBuffers(int n, IntBuffer buffers)
Entry point (through function pointer) to C language function: void alDeleteBuffers(ALsizei n,...
void alGenSources(int n, IntBuffer sources)
Entry point (through function pointer) to C language function: void alGenSources(ALsizei n,...
String alGetString(int param)
Entry point (through function pointer) to C language function: const ALchar * alGetString(ALenum pa...