# Course 2 cycle and fade out of joal

Source: Internet
Author: User

Joal tutorial

Original article athomas Goldberg

3-way slate brick

Retained the above information.

The continuous code page and Study Notes for this section: http://blog.csdn.net/shuzhe66/article/details/40260465

Course 2 cycle and fade out

I hope the last lesson will be useful to you. This will be a simple and quick tutorial, and of course it will be hard to find it.

`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;`

The only difference here is the speed of the sound source. We give the Z axis a speed of 0.1.

`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]; // load WAV data into the buffer zone 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, sourcepo S, 0); Al. alsourcefv (source [0], Al. al_velocity, sourcevel, 0); // [the velocity here is mistakenly written as position in the original text. Correction here -- Translator's note] 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 ;}`

The preceding two changes are involved. First, we replace the used audio file, and then set the al_looping parameter to al_true, which indicates that the sound source will be played cyclically until the command is stopped. [If you want to use your own WAV file, make sure it is in the single-channel format. Otherwise, openal will ignore the sound impact caused by spatial location change.

`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();    }`

The above part has not changed.

`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();    }}`

Compared with the previous one, we changed its loop structure. The sound will not suddenly stop, but will slowly fade as the distance between the sound source and the audience increases. We will continue to accumulate the corresponding speed to the location to achieve this effect, the time used is calculated by the system's time function. You do not need to modify the above time, but if the audio fades out too fast, you can change the above 100 to a higher number.

Course 2 cycle and fade out of joal

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.