Course 3 multi-source stream playback

Source: Internet
Author: User

Joal tutorial

Address: http://jogamp.org/joal-demos/www/devmaster/lesson3.html

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/40260861

Lesson 3 multiple sound sources

 

This article is the joal version of The openal tutorial for devmaster.net (http://devmaster.net. Originally translated in C language by jessemaurais

 

Hello everyone, it was a while before the last release of the tutorial, but it was always better to be late than not. I guess everyone is eager to read the new tutorial, so I started to write it.

This tutorial will teach you how to play multiple audios at the same time. In many intense games, there are various elements that are often accompanied by various sound effects when triggered, which is not difficult to implement, processing multiple channels of audio is very similar to processing a single channel of audio.

Import Java. NIO. bytebuffer; import Java. util. random; import COM. jogamp. openal. al; import COM. jogamp. openal. alfactory; import COM. jogamp. openal. util. ALUT; public class multiplesources {static Al; // maximum number of required buffers. static final int num_buffers = 3; // maximum number of required sound sources static final int num_sources = 3; // The following three variables mark different sound sources static final int battle = 0; static final int gun1 = 1; static final int gun2 = 2; // buffer for storing sound data static int [] buffers = new int [num_buffers]; // static int [] sources = new int [num_sources]; // location of each sound source static float [] [] sourcepos = new float [num_sources] [3]; // static float [] [] sourcevel = new float [num_sources] [3]; // static float [] listenerpos = {0.0f, 0.0f, 0.0f}; // audience speed static float [] listenervel = {0.0f, 0.0f, 0.0f}; // audience orientation static float [] listenerori = {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };

I believe that the above Code will be very familiar to those who have read the first two chapters. The only difference is that we need to load three different sound effects into the openal system.


Static int loadaldata () {// The value 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]; // read the wav file into the buffer zone Al. algenbuffers (num_buffers, buffers, 0); If (Al. algeterror ()! = Al. al_no_error) {return Al. al_false;} ALUT. alutloadwavfile ("wavdata/battle.wav", format, Data, size, freq, loop); Al. albufferdata (buffers [battle], format [0], data [0], size [0], freq [0]); ALUT. alutloadwavfile ("wavdata/gun1.wav", format, Data, size, freq, loop); Al. albufferdata (buffers [gun1], format [0], data [0], size [0], freq [0]); ALUT. alutloadwavfile ("wavdata/gun2.wav", format, Data, size, F Req, loop); Al. albufferdata (buffers [gun2], format [0], data [0], size [0], freq [0]); // bind the buffer zone to the sound source Al. algensources (num_sources, sources, 0); Al. alsourcei (sources [battle], Al. al_buffer, buffers [battle]); Al. alsourcef (sources [battle], Al. al_pitch, 1.0f); Al. alsourcef (sources [battle], Al. al_gain, 1.0f); Al. alsourcefv (sources [battle], Al. al_position, sourcepos [battle], 0); Al. alsourcefv (sources [battle], L. al_velocity, sourcevel [battle], 0); Al. alsourcei (sources [battle], Al. al_looping, Al. al_true); Al. alsourcei (sources [gun1], Al. al_buffer, buffers [gun1]); Al. alsourcef (sources [gun1], Al. al_pitch, 1.0f); Al. alsourcef (sources [gun1], Al. al_gain, 1.0f); Al. alsourcefv (sources [gun1], Al. al_position, sourcepos [gun1], 0); Al. alsourcefv (sources [gun1], Al. al_velocity, sourcevel [gun1], 0); Al. alsourcei (Source S [gun1], Al. al_looping, Al. al_false); Al. alsourcei (sources [gun2], Al. al_buffer, buffers [gun2]); Al. alsourcef (sources [gun2], Al. al_pitch, 1.0f); Al. alsourcef (sources [gun2], Al. al_gain, 1.0f); Al. alsourcefv (sources [gun2], Al. al_position, sourcepos [gun2], 0); Al. alsourcefv (sources [gun2], Al. al_velocity, sourcevel [gun2], 0); Al. alsourcei (sources [gun2], Al. al_looping, Al. al_false); // check the error and return the result if (Al. Algeterror ()! = Al. al_no_error) {return Al. al_false;} return Al. al_true ;}

The above Code seems a little different from the previous one. In fact, this is not the case. In general, we only load three audio files into the buffer zone and bind the three buffers to the corresponding sound source. The only difference is that battle.wav (corresponding to source [0]) is set to loop playback, while the other two audios do not.


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 killAllData() {        al.alDeleteBuffers(NUM_BUFFERS, buffers, 0);        al.alDeleteSources(NUM_SOURCES, sources, 0);        ALut.alutExit();    }

The above code is no different from the previous one.


Public static void main (string [] ARGs) {Al = alfactory. getal (); // initialize openal and reset the error mark ALUT. alutinit (); Al. algeterror (); // load audio data. if (loadaldata () = Al. al_false) {system. exit (1) ;}setlistenervalues (); // play the "battle" background music Al. alsourceplay (sources [battle]); long starttime = system. currenttimemillis (); long elapsed = 0; long totalelapsed = 0; random Rand = new random (); int [] State = new int [1]; while (total Elapsed <10000) {elapsed = system. currenttimemillis ()-starttime; If (elapsed> 50) {totalelapsed + = elapsed; starttime = system. currenttimemillis (); // randomly selects one of the two sound effects except the background music to determine whether it is playing int pick = math. ABS (Rand. nextint () % 2) + 1; Al. algetsourcei (sources [Pick], Al. al_source_state, state, 0); If (State [0]! = Al. al_playing) {// select a random position around the audience to play the audio. Double Theta = (Rand. nextint () % 360) * 3.14/180.0; sourcepos [Pick] [0] =-(float) math. cos (theta); sourcepos [Pick] [1] =-(float) (Rand. nextint () % 2); sourcepos [Pick] [2] =-(float) math. sin (theta); Al. alsourcefv (sources [Pick], Al. al_position, sourcepos [Pick], 0); Al. alsourceplay (sources [Pick]) ;}} killalldata ();}}

This is the most interesting part of this tutorial. We can check whether each sound source is playing and issue a Playback command for the sound source that has not been played, in addition, we set a random new location in the three-dimensional space for the play (this is just for fun ~).

 

Finally, we ran it. With mixed sound effects, we succeeded! Most readers may have realized that in order to let the sound source play together, we just Issued commands and didn't do anything special. openal has processed the mixing part, and make the actual playback sound consistent with the sound source, the audience location, and speed. Isn't it the charm of openal?

 

This is so simple that I myself doubt why this tutorial will take so long. If you want other special tutorials (not necessarily openal, but my knowledge is huge), you can contact me through [email protected, I plan to explain the buffer zone sharing and Doppler Effect in future tutorials. I wish you a pleasant coding!







Course 3 multi-source stream playback

Contact Us

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.