audio - OpenAL Sound don't play normally in Java -
i write program use openal , run. however, sound don't play normally.
the sound played in amazing speed. also, sound become noisy if change pitch of sound. what's that?
code. i consulted source cord exists in lower url.
http://wiki.lwjgl.org/wiki/openal_tutorial_1_-_single_static_source
package other; import java.io.bufferedinputstream; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.nio.floatbuffer; import java.nio.intbuffer; import org.lwjgl.bufferutils; import org.lwjgl.lwjglexception; import org.lwjgl.openal.al; import org.lwjgl.openal.al10; import org.lwjgl.openal.al11; import org.lwjgl.util.wavedata; public class music { /** position of source sound. */ floatbuffer sourcepos; /** velocity of source sound. */ floatbuffer sourcevel; /** position of listener. */ floatbuffer listenerpos; /** velocity of listener. */ floatbuffer listenervel; /** orientation of listener. (first 3 elements "at", second 3 "up") */ floatbuffer listenerori; /** buffers hold sound data. */ intbuffer buffer; /** sources points emitting sound. */ intbuffer source; public music() { /** buffers hold sound data. */ buffer = bufferutils.createintbuffer(1); /** sources points emitting sound. */ source = bufferutils.createintbuffer(1); /** position of source sound. */ sourcepos = bufferutils.createfloatbuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f}); /** velocity of source sound. */ sourcevel = bufferutils.createfloatbuffer(3).put(new float[] {0.0f, 0.0f, 0.0f }); /** position of listener. */ listenerpos = bufferutils.createfloatbuffer(3).put(new float[] {0.0f, 0.0f, 0.0f }); /** velocity of listener. */ listenervel = bufferutils.createfloatbuffer(3).put(new float[] {0.0f, 0.0f, 0.0f }); /** orientation of listener. (first 3 elements "at", second 3 "up") */ listenerori = bufferutils.createfloatbuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }); } int loadaldata() { sourcepos.rewind(); sourcevel.rewind(); // load wav data buffer. al10.algenbuffers(buffer); if(al10.algeterror() != al10.al_no_error) { return al10.al_false; } wavedata wavefile; try { //fileinputstream fin=new fileinputstream("18am01.wav"); fileinputstream fin=new fileinputstream("awaken.wav"); bufferedinputstream bin = new bufferedinputstream(fin); wavefile = wavedata.create(bin); al10.albufferdata(buffer.get(0), wavefile.format, wavefile.data, wavefile.samplerate); wavefile.dispose(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } // bind buffer source. al10.algensources(source); if (al10.algeterror() != al10.al_no_error) { return al10.al_false; } al11.alsource(source.get(0), al10.al_buffer, buffer); al10.alsourcef(source.get(0), al10.al_pitch, 1.0f ); al10.alsourcef(source.get(0), al10.al_gain, 1.0f ); al10.alsource (source.get(0), al10.al_position, sourcepos ); al10.alsource (source.get(0), al10.al_velocity, sourcevel ); //al10.alsourcef(source.get(0), al11.al_speed_of_sound,100.5f); // error check , return. if (al10.algeterror() == al10.al_no_error) { return al10.al_true; } return al10.al_false; } /** * void setlistenervalues() * * defined values listener, need * tell openal use data. function that. */ void setlistenervalues() { listenerpos.rewind(); listenervel.rewind(); listenerori.rewind(); al10.allistener(al10.al_position, listenerpos); al10.allistener(al10.al_velocity, listenervel); al10.allistener(al10.al_orientation, listenerori); } /** * void killaldata() * * have allocated memory our buffers , sources needs * returned system. function frees memory. */ void killaldata() { al10.aldeletesources(source); al10.aldeletebuffers(buffer); } public void execute() { // initialize openal , clear error bit. try{ al.create(); } catch (lwjglexception le) { le.printstacktrace(); return; } al10.algeterror(); // load wav data. if(loadaldata() == al10.al_false) { system.out.println("error loading data."); return; } setlistenervalues(); // loop. char c = ' '; while(c != 'q') { try { c = (char) system.in.read(); } catch (ioexception ioe) { c = 'q'; } switch(c) { // pressing 'p' begin playing sample. case 'p': al10.alsourceplay(source.get(0)); break; // pressing 's' stop sample playing. case's': al10.alsourcestop(source.get(0)); break; // pressing 'h' pause sample. case 'h': al10.alsourcepause(source.get(0)); break; }; } killaldata(); } public static void main(string[] args) { music music= new music(); music.execute(); }
}
Comments
Post a Comment