JOAL學習筆記
先是例行的連續代碼頁
import java.nio.ByteBuffer;
import com.jogamp.openal.AL;
import com.jogamp.openal.ALC;
import com.jogamp.openal.ALFactory;
import com.jogamp.openal.util.ALut;
public class LoopingAndFadeaway {
static int[] buffer = new int[1];
static int[] source = new int[1];
static float[] sourcePos = { 0.0f, 0.0f, 0.0f };
static float[] sourceVel = { 0.0f, 0.0f, 0.1f };
static float[] listenerPos = { 0.0f, 0.0f, 0.0f };
static float[] listenerVel = { 0.0f, 0.0f, 0.0f };
static float[] listenerOri = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
static AL al;
static ALC alc;
static int loadALData() {
if (al.alGetError() != AL.AL_NO_ERROR) {
return AL.AL_FALSE;
}
int[] format = new int[1];
int[] size = new int[1];
ByteBuffer[] data = new ByteBuffer[1];
int[] freq = new int[1];
int[] loop = new int[1];
al.alGenBuffers(1, buffer, 0);
if (al.alGetError() != AL.AL_NO_ERROR)
return AL.AL_FALSE;
ALut.alutLoadWAVFile("wavdata/Footsteps.wav",format,data,size,freq,loop);
al.alBufferData(buffer[0], format[0], data[0], size[0], freq[0]);
al.alGenSources(1, source, 0);
al.alSourcei(source[0], AL.AL_BUFFER, buffer[0]);
al.alSourcef(source[0], AL.AL_PITCH, 1.0f);
al.alSourcef(source[0], AL.AL_GAIN, 1.0f);
al.alSourcefv(source[0], AL.AL_POSITION, sourcePos, 0);
al.alSourcefv(source[0], AL.AL_VELOCITY, sourceVel, 0);
al.alSourcei(source[0], AL.AL_LOOPING, AL.AL_TRUE);
if (al.alGetError() != AL.AL_NO_ERROR) {
return AL.AL_FALSE;
}
return AL.AL_TRUE;
}
static void setListenerValues() {
al.alListenerfv(AL.AL_POSITION, listenerPos, 0);
al.alListenerfv(AL.AL_VELOCITY, listenerVel, 0);
al.alListenerfv(AL.AL_ORIENTATION, listenerOri, 0);
}
static void killALData() {
al.alDeleteBuffers(1, buffer, 0);
al.alDeleteSources(1, source, 0);
ALut.alutExit();
}
public static void main(String[] args) {
ALut.alutInit();
al = ALFactory.getAL();
if(loadALData() == AL.AL_FALSE) {
System.exit(1);
};
setListenerValues();
al.alSourcePlay(source[0]);
long startTime = System.currentTimeMillis();
long elapsed = 0;
long ticker = 0;
long lastTime = 0;
while (elapsed < 10000) {
elapsed = System.currentTimeMillis() - startTime;
if (ticker > 100) {
ticker = 0;
sourcePos[0] += sourceVel[0];
sourcePos[1] += sourceVel[1];
sourcePos[2] += sourceVel[2];
al.alSourcefv(
source[0],
AL.AL_POSITION,
sourcePos, 0);
}
ticker += System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
}
ALut.alutExit();
}
}
之後是一些值得注意的問題
首先,如果你想通過修改聲源或聽衆的位置來達到不同的聲音效果,請確定你用於測試的音頻是單聲道音頻,如果使用了雙聲道音頻,OpenAL依然會播放,但會忽略其位置量造成的聲音改變,筆者在這裏走了很多彎路,最終在一個國外論壇的一角發現了這個原因。
之後一點,這個程序是由C語言翻譯過來的,因此帶了一點C語言的特徵,例如最後循環使用了忙等待,對於一個高性能CPU忙等待不是一個好的選擇,可以使用sleep修改爲閒等待,雖然這樣精確性有所下降,但聲音的淡出過程的確對精確性要求不高,以此節約大量CPU資源是值得的,比如說這樣寫:
while (true) {
sourcePos[0] += sourceVel[0];
sourcePos[1] += sourceVel[1];
sourcePos[2] += sourceVel[2];
al.alSourcefv(source[0], AL.AL_POSITION, sourcePos, 0);
try {
Thread.sleep(100);
} catch (InterruptedException e) {System.out.println("Intp!");}
}