Java Code Examples for org.lwjgl.openal.AL10#alSourcePlay()

The following examples show how to use org.lwjgl.openal.AL10#alSourcePlay() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoundStore.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Play the specified buffer as music (i.e. use the music channel)
 * 
 * @param buffer The buffer to be played
 * @param pitch The pitch to play the music at
 * @param gain The gaing to play the music at
 * @param loop True if we should loop the music
 */
void playAsMusic(int buffer,float pitch,float gain, boolean loop) {
	paused = false;
	
	setMOD(null);
	
	if (soundWorks) {
		if (currentMusic != -1) {
			AL10.alSourceStop(sources.get(0));
		}
		
		getMusicSource();
		
		AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffer);
		AL10.alSourcef(sources.get(0), AL10.AL_PITCH, pitch);
	    AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
		
		currentMusic = sources.get(0);
		
		if (!music) {
			pauseLoop();
		} else {
			AL10.alSourcePlay(sources.get(0)); 
		}
	}
}
 
Example 2
Source File: SoundStore.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Play the specified buffer as music (i.e. use the music channel)
 * 
 * @param buffer The buffer to be played
 * @param pitch The pitch to play the music at
 * @param gain The gaing to play the music at
 * @param loop True if we should loop the music
 */
void playAsMusic(int buffer,float pitch,float gain, boolean loop) {
	paused = false;
	
	setMOD(null);
	
	if (soundWorks) {
		if (currentMusic != -1) {
			AL10.alSourceStop(sources.get(0));
		}
		
		getMusicSource();
		
		AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffer);
		AL10.alSourcef(sources.get(0), AL10.AL_PITCH, pitch);
	    AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
		
		currentMusic = sources.get(0);
		
		if (!music) {
			pauseLoop();
		} else {
			AL10.alSourcePlay(sources.get(0)); 
		}
	}
}
 
Example 3
Source File: SoundStore.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Play the specified buffer as music (i.e. use the music channel)
 * 
 * @param buffer The buffer to be played
 * @param pitch The pitch to play the music at
 * @param gain The gaing to play the music at
 * @param loop True if we should loop the music
 */
void playAsMusic(int buffer,float pitch,float gain, boolean loop) {
	paused = false;
	
	setMOD(null);
	
	if (soundWorks) {
		if (currentMusic != -1) {
			AL10.alSourceStop(sources.get(0));
		}
		
		getMusicSource();
		
		AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffer);
		AL10.alSourcef(sources.get(0), AL10.AL_PITCH, pitch);
	    AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
		
		currentMusic = sources.get(0);
		
		if (!music) {
			pauseLoop();
		} else {
			AL10.alSourcePlay(sources.get(0)); 
		}
	}
}
 
Example 4
Source File: OpenALMODPlayer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Poll the bufferNames - check if we need to fill the bufferNames with another
 * section. 
 * 
 * Most of the time this should be reasonably quick
 */
public void update() {
	if (done) {
		return;
	}
	
	int processed = AL10.alGetSourcei(source, AL10.AL_BUFFERS_PROCESSED);
	
	while (processed > 0) {
		unqueued.clear();
		
		AL10.alSourceUnqueueBuffers(source, unqueued);
        if (stream(unqueued.get(0))) {
        	AL10.alSourceQueueBuffers(source, unqueued);
        } else {
        	remainingBufferCount--;
        	if (remainingBufferCount == 0) {
        		done = true;
        	}
        }
        processed--;
	}
	
	int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
    
    if (state != AL10.AL_PLAYING) {
    	AL10.alSourcePlay(source);
    }
}
 
Example 5
Source File: SoundStore.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Play the specified buffer as a sound effect with the specified
 * pitch and gain.
 * 
 * @param buffer The ID of the buffer to play
 * @param pitch The pitch to play at
 * @param gain The gain to play at
 * @param loop True if the sound should loop
 * @param x The x position to play the sound from
 * @param y The y position to play the sound from
 * @param z The z position to play the sound from
 * @return source The source that will be used
 */
int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z) {
	gain *= soundVolume;
	if (gain == 0) {
		gain = 0.001f;
	}
	if (soundWorks) {
		if (sounds) {
			int nextSource = findFreeSource();
			if (nextSource == -1) {
				return -1;
			}
			
			AL10.alSourceStop(sources.get(nextSource));
			
			AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer);
			AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch);
			AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain); 
		    AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
		    
		    sourcePos.clear();
		    sourceVel.clear();
			sourceVel.put(new float[] { 0, 0, 0 });
			sourcePos.put(new float[] { x, y, z });
		    sourcePos.flip();
		    sourceVel.flip();
		    AL10.alSource(sources.get(nextSource), AL10.AL_POSITION, sourcePos);
   			AL10.alSource(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel);
		    
			AL10.alSourcePlay(sources.get(nextSource)); 
			
			return nextSource;
		}
	}
	
	return -1;
}
 
Example 6
Source File: OpenALStreamPlayer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Starts the streaming.
 */
private void startPlayback() {
	AL10.alSourcei(source, AL10.AL_LOOPING, AL10.AL_FALSE);
	AL10.alSourcef(source, AL10.AL_PITCH, pitch);

	remainingBufferCount = BUFFER_COUNT;

	for (int i = 0; i < BUFFER_COUNT; i++) {
		stream(bufferNames.get(i));
	}

	AL10.alSourceQueueBuffers(source, bufferNames);
	AL10.alSourcePlay(source);
}
 
Example 7
Source File: SoundStore.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Restart the music loop that is currently paused
 */
public void restartLoop() {
	if ((music) && (soundWorks) && (currentMusic != -1)){
		paused = false;
		AL10.alSourcePlay(currentMusic);
		if (stream != null)
			stream.resuming();
	}
}
 
Example 8
Source File: SoundStore.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Play the specified buffer as a sound effect with the specified
 * pitch and gain.
 * 
 * @param buffer The ID of the buffer to play
 * @param pitch The pitch to play at
 * @param gain The gain to play at
 * @param loop True if the sound should loop
 * @param x The x position to play the sound from
 * @param y The y position to play the sound from
 * @param z The z position to play the sound from
 * @return source The source that will be used
 */
int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z) {
	gain *= soundVolume;
	if (gain == 0) {
		gain = 0.001f;
	}
	if (soundWorks) {
		if (sounds) {
			int nextSource = findFreeSource();
			if (nextSource == -1) {
				return -1;
			}
			
			AL10.alSourceStop(sources.get(nextSource));
			
			AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer);
			AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch);
			AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain); 
		    AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
		    
		    sourcePos.clear();
		    sourceVel.clear();
			sourceVel.put(new float[] { 0, 0, 0 });
			sourcePos.put(new float[] { x, y, z });
		    sourcePos.flip();
		    sourceVel.flip();
		    AL10.alSource(sources.get(nextSource), AL10.AL_POSITION, sourcePos);
   			AL10.alSource(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel);
		    
			AL10.alSourcePlay(sources.get(nextSource)); 
			
			return nextSource;
		}
	}
	
	return -1;
}
 
Example 9
Source File: OpenALMODPlayer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
    * Play a mod or xm track streamed from the specified location
    * 
    * @param module The moudle to play back
    * @param source The OpenAL source to play the music on
    * @param start True if the music should be started
    * @param loop True if the track should be looped
    */
public void play(Module module, int source, boolean loop, boolean start) {
	this.source = source;
	this.loop = loop;
	this.module = module;
	done = false;
	
	ibxm = new IBXM(48000);
	ibxm.set_module(module);
	songDuration = ibxm.calculate_song_duration();

	if (bufferNames != null) {
		AL10.alSourceStop(source);
		bufferNames.flip();
		AL10.alDeleteBuffers(bufferNames);
	}
	
	bufferNames = BufferUtils.createIntBuffer(2);
	AL10.alGenBuffers(bufferNames);
	remainingBufferCount = 2;
	
	for (int i=0;i<2;i++) {
        stream(bufferNames.get(i));
	}
       AL10.alSourceQueueBuffers(source, bufferNames);
	AL10.alSourcef(source, AL10.AL_PITCH, 1.0f);
	AL10.alSourcef(source, AL10.AL_GAIN, 1.0f); 
	
	if (start) {
		AL10.alSourcePlay(source);
	}
}
 
Example 10
Source File: OpenALStreamPlayer.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Poll the bufferNames - check if we need to fill the bufferNames with another
 * section. 
 * 
 * Most of the time this should be reasonably quick
 */
public synchronized void update() {
	if (done) {
		return;
	}

	int processed = AL10.alGetSourcei(source, AL10.AL_BUFFERS_PROCESSED);
	while (processed > 0) {
		unqueued.clear();
		AL10.alSourceUnqueueBuffers(source, unqueued);
		
		int bufferIndex = unqueued.get(0);

		int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);

		playedPos += bufferLength;

		if (musicLength > 0 && playedPos > musicLength)
			playedPos -= musicLength;

		if (stream(bufferIndex)) {
			AL10.alSourceQueueBuffers(source, unqueued);
		} else {
			remainingBufferCount--;
			if (remainingBufferCount == 0) {
				done = true;
			}
		}
		processed--;
	}
	
	int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
	
	if (state != AL10.AL_PLAYING) {
		AL10.alSourcePlay(source);
	}
}
 
Example 11
Source File: QueuedAudioPlayer.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public final void refill() { // Run by the Refiller thread
		int processed = AL10.alGetSourcei(source.getSource(), AL10.AL_BUFFERS_PROCESSED);
//System.out.println("this = " + this + " | processed = " + processed);
		while (processed > 0) {
//			assert processed <= al_buffers.capacity();
//			al_buffers.position(oldest_buffer);
//			al_buffers.limit(oldest_buffer + 1);
//			assert AL10.alIsBuffer(al_buffers.get(al_buffers.position())): al_buffers.get(al_buffers.position()) + " is not a buffer";
			AL10.alSourceUnqueueBuffers(source.getSource(), al_return_buffers);
//			assert al_return_buffers.get(0) == al_buffers.get(al_buffers.position()): "Unexpected buffer removed: " + al_return_buffers.get(0) + " should be " + al_buffers.get(al_buffers.position());
			int bytes = fillBuffer(al_return_buffers.get(0));
			if (bytes == 0) {
				stop();
				return;
			}
//			assert AL10.alIsBuffer(al_buffers.get(al_buffers.position())): al_buffers.get(al_buffers.position()) + " is not a buffer";
			AL10.alSourceQueueBuffers(source.getSource(), al_return_buffers);
//System.out.println("oldest_buffer = " + oldest_buffer + " | processed = " + processed + " | capacity = " + buffer_streams[oldest_buffer].buffer().capacity() + " | position " + buffer_streams[oldest_buffer].buffer().position() + " | limit " + buffer_streams[oldest_buffer].buffer().limit() + " al_size = " + AL10.alGetBufferi(al_buffers.get(oldest_buffer), AL10.AL_SIZE));
			oldest_buffer = (oldest_buffer + 1)%NUM_BUFFERS;
			processed--;
/*			int test_processed = AL10.alGetSourcei(source.getSource(), AL10.AL_BUFFERS_PROCESSED);
			assert test_processed >= processed: test_processed + " " + processed;*/
		}
		if (AL10.alGetSourcei(source.getSource(), AL10.AL_SOURCE_STATE) == AL10.AL_STOPPED)
			AL10.alSourcePlay(source.getSource());
//System.out.println("		AL10.alGetSourcei(source_index,AL10.AL_SOURCE_STATE) = " + 		AL10.alGetSourcei(source.getSource(),AL10.AL_SOURCE_STATE) + " | AL10.AL_STOPPED = " + AL10.AL_STOPPED + " | AL10.AL_PLAYING = " + AL10.AL_PLAYING);
	}
 
Example 12
Source File: QueuedAudioPlayer.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
QueuedAudioPlayer(AudioSource source, AudioParameters params) {
	super(source, params);
	this.url = Utils.makeURL((String)params.sound);
	this.buffer_stream = new ByteBufferOutputStream(true);
	if ((!params.music && !Settings.getSettings().play_sfx) || this.source == null) {
		this.ogg_stream = null;
		this.channels = 0;
		this.audio = null;
		return;
	}

	if (params.relative)
		AL10.alSourcei(source.getSource(), AL10.AL_SOURCE_RELATIVE, AL10.AL_TRUE);
	else
		AL10.alSourcei(source.getSource(), AL10.AL_SOURCE_RELATIVE, AL10.AL_FALSE);
	
	setGain(params.gain);
	setPos(params.x, params.y, params.z);

	audio = new Audio(NUM_BUFFERS);
	IntBuffer al_buffers = audio.getBuffers();
	this.ogg_stream = new OGGStream(url);
	this.channels = ogg_stream.getChannels();
	for (int i = 0; i < al_buffers.capacity(); i++) {
		fillBuffer(al_buffers.get(i));
	}

	AL10.alSourcei(source.getSource(), AL10.AL_LOOPING, AL10.AL_FALSE);
	AL10.alSourcef(source.getSource(), AL10.AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR);
	AL10.alSourcef(source.getSource(), AL10.AL_REFERENCE_DISTANCE, params.radius);
	AL10.alSourcef(source.getSource(), AL10.AL_MIN_GAIN, 0f);
	AL10.alSourcef(source.getSource(), AL10.AL_MAX_GAIN, 1f);
	AL10.alSourcef(source.getSource(), AL10.AL_PITCH, params.pitch);
	AL10.alSourceQueueBuffers(source.getSource(), al_buffers);
	if (params.music || AudioManager.getManager().startPlaying())
		AL10.alSourcePlay(source.getSource());

	AudioManager.getManager().registerQueuedPlayer(this);
}
 
Example 13
Source File: AudioManager.java    From tribaltrouble with GNU General Public License v2.0 5 votes vote down vote up
public final void startSources() {
	if (sound_play_counter == 0) {
		for (int i = 0; i < ambients.size(); i++) {
			AL10.alSourcePlay(((AudioSource)ambients.get(i)).getSource());
		}
	}
	sound_play_counter++;
}
 
Example 14
Source File: SoundStore.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Restart the music loop that is currently paused
 */
public void restartLoop() {
	if ((music) && (soundWorks) && (currentMusic != -1)){
		paused = false;
		AL10.alSourcePlay(currentMusic);
	}
}
 
Example 15
Source File: SoundStore.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Restart the music loop that is currently paused
 */
public void restartLoop() {
	if ((music) && (soundWorks) && (currentMusic != -1)){
		paused = false;
		AL10.alSourcePlay(currentMusic);
		if (stream != null)
			stream.resuming();
	}
}
 
Example 16
Source File: SoundStore.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Play the specified buffer as a sound effect with the specified
 * pitch and gain.
 * 
 * @param buffer The ID of the buffer to play
 * @param pitch The pitch to play at
 * @param gain The gain to play at
 * @param loop True if the sound should loop
 * @param x The x position to play the sound from
 * @param y The y position to play the sound from
 * @param z The z position to play the sound from
 * @return source The source that will be used
 */
int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z) {
	gain *= soundVolume;
	if (gain == 0) {
		gain = 0.001f;
	}
	if (soundWorks) {
		if (sounds) {
			int nextSource = findFreeSource();
			if (nextSource == -1) {
				return -1;
			}
			
			AL10.alSourceStop(sources.get(nextSource));
			
			AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer);
			AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch);
			AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain); 
		    AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
		    
		    sourcePos.clear();
		    sourceVel.clear();
			sourceVel.put(new float[] { 0, 0, 0 });
			sourcePos.put(new float[] { x, y, z });
		    sourcePos.flip();
		    sourceVel.flip();
		    AL10.alSource(sources.get(nextSource), AL10.AL_POSITION, sourcePos);
   			AL10.alSource(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel);
		    
			AL10.alSourcePlay(sources.get(nextSource)); 
			
			return nextSource;
		}
	}
	
	return -1;
}
 
Example 17
Source File: OpenALStreamPlayer.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Poll the bufferNames - check if we need to fill the bufferNames with another
 * section. 
 * 
 * Most of the time this should be reasonably quick
 */
public synchronized void update() {
	if (done) {
		return;
	}

	int processed = AL10.alGetSourcei(source, AL10.AL_BUFFERS_PROCESSED);
	while (processed > 0) {
		unqueued.clear();
		AL10.alSourceUnqueueBuffers(source, unqueued);
		
		int bufferIndex = unqueued.get(0);

		int bufferLength = AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE);

		playedPos += bufferLength;

		if (musicLength > 0 && playedPos > musicLength)
			playedPos -= musicLength;

		if (stream(bufferIndex)) {
			AL10.alSourceQueueBuffers(source, unqueued);
		} else {
			remainingBufferCount--;
			if (remainingBufferCount == 0) {
				done = true;
			}
		}
		processed--;
	}
	
	int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
	
	if (state != AL10.AL_PLAYING) {
		AL10.alSourcePlay(source);
	}
}
 
Example 18
Source File: LwjglAL.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void alSourcePlay(final int source) {
    AL10.alSourcePlay(source);
}
 
Example 19
Source File: LwjglAL.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void alSourcePlay(int source) {
    AL10.alSourcePlay(source);
}
 
Example 20
Source File: OpenALStreamPlayer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Poll the bufferNames - check if we need to fill the bufferNames with another
 * section. 
 * 
 * Most of the time this should be reasonably quick
 */
public void update() {
	if (done) {
		return;
	}

	float sampleRate = audio.getRate();
	float sampleSize;
	if (audio.getChannels() > 1) {
		sampleSize = 4; // AL10.AL_FORMAT_STEREO16
	} else {
		sampleSize = 2; // AL10.AL_FORMAT_MONO16
	}
	
	int processed = AL10.alGetSourcei(source, AL10.AL_BUFFERS_PROCESSED);
	while (processed > 0) {
		unqueued.clear();
		AL10.alSourceUnqueueBuffers(source, unqueued);
		
		int bufferIndex = unqueued.get(0);

		float bufferLength = (AL10.alGetBufferi(bufferIndex, AL10.AL_SIZE) / sampleSize) / sampleRate;
		positionOffset += bufferLength;
		
        if (stream(bufferIndex)) {			
        	AL10.alSourceQueueBuffers(source, unqueued);
        } else {
        	remainingBufferCount--;
        	if (remainingBufferCount == 0) {
        		done = true;
        	}
        }
        processed--;
	}
	
	int state = AL10.alGetSourcei(source, AL10.AL_SOURCE_STATE);
    
    if (state != AL10.AL_PLAYING) {
    	AL10.alSourcePlay(source);
    }
}