JOAL学习笔记 第五课 多声源共享缓冲区

本文是关于JOAL库的学习笔记,主要讨论如何在OpenAL中优化多声源管理,通过实现一个声源池来提高效率。作者提出了一种多例模式的改进,将声源作为资源进行管理,避免了每次播放请求都创建新声源的浪费,类比于粒子特效的实现。文中提供了管理声源集合的工具类、声源接口以及用于播放的包装类的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JOAL学习笔记

 

先是例行的连续代码页

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jogamp.openal.AL;
import com.jogamp.openal.ALC;
import com.jogamp.openal.ALCcontext;
import com.jogamp.openal.ALCdevice;
import com.jogamp.openal.ALException;
import com.jogamp.openal.ALFactory;
import com.jogamp.openal.util.ALut;

public class SourceSharingBuffers {

	static ALC alc;
	static AL al;

	// These index the buffers.
	public static final int THUNDER = 0;
	public static final int WATERDROP = 1;
	public static final int STREAM = 2;
	public static final int RAIN = 3;

	public static final int CHIMES = 4;
	public static final int OCEAN = 5;
	public static final int NUM_BUFFERS = 6;

	// Buffers hold sound data.
	static int[] buffers = new int[NUM_BUFFERS];

	// A list of sources for multiple emissions.
	static List<Integer> sources = new ArrayList<>();

	// Position of the source sounds.
	static float[] sourcePos = { 0.0f, 0.0f, 0.0f };

	// Velocity of the source sounds.
	static float[] sourceVel = { 0.0f, 0.0f, 0.0f };

	// Position of the listener.
	static float[] listenerPos = { 0.0f, 0.0f, 0.0f };

	// Velocity of the listener.
	static float[] listenerVel = { 0.0f, 0.0f, 0.0f };

	// Orientation of the listener. (first 3 elements are "at", second 3 are
	// "up")
	static float[] listenerOri = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };

	static int initOpenAL() {
		al = ALFactory.getAL();
		alc = ALFactory.getALC();
		ALCdevice device;
		ALCcontext context;
		String deviceSpecifier;
		String deviceName = "DirectSound3D"; // You may choose to open a
												// specific OpenAL device if you
												// know its name.
		deviceName = null; // Passing a null String to alcOpenDevice will open
							// the default device on your system!

		// Get handle to device.
		device = alc.alcOpenDevice(deviceName);

		// Get the device specifier.
		deviceSpecifier = alc.alcGetString(device, ALC.ALC_DEVICE_SPECIFIER);

		System.out.println("Using device " + deviceSpecifier);

		// Create audio context.
		context = alc.alcCreateContext(device, null);

		// Set active context.
		alc.alcMakeContextCurrent(context);

		// Check for an error.
		if (alc.alcGetError(device) != ALC.ALC_NO_ERROR)
			return AL.AL_FALSE;

		return AL.AL_TRUE;
	}

	static void exitOpenAL() {
		ALCcontext curContext;
		ALCdevice curDevice;

		// Get the current context.
		curContext = alc.alcGetCurrentContext();

		// Get the device used by that context.
		curDevice = alc.alcGetContextsDevice(curContext);

		// Reset the current context to NULL.
		alc.alcMakeContextCurrent(null);

		// Release the context and the device.
		alc.alcDestroyContext(curContext);
		alc.alcCloseDevice(curDevice);
	}

	static int loadALData() {
		// Variables to load into.
		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 buffers.
		al.alGenBuffers(NUM_BUFFERS, buffers, 0);

		if (al.alGetError() != AL.AL_NO_ERROR)
			return AL.AL_FALSE;

		ALut.alutLoadWAVFile("wavdata/thunder.wav", format, data, size, freq,
				loop);
		al.alBufferData(buffers[THUNDER], format[0], data[0], size[0], freq[0]);

		ALut.alutLoadWAVFile("wavdata/waterdrop.wav", format, data, size, freq,
				loop);
		al.alBufferData(buffers[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值