/** * Adds a sound to the Sound Managers pool * * @param path Path to file to load * @return index into SoundManagers buffer list */ public int addSound(String path) { // Generate 1 buffer entry scratchBuffer.rewind().position(0).limit(1); AL10.alGenBuffers(scratchBuffer); buffers[bufferIndex] = scratchBuffer.get(0); // load wave data from buffer WaveData wavefile = WaveData.create("spaceinvaders/" + path); // copy to buffers AL10.alBufferData(buffers[bufferIndex], wavefile.format, wavefile.data, wavefile.samplerate); // unload file again wavefile.dispose(); // return index for this sound return bufferIndex++; }
private void executeMidStreamCreationTest() { try { AudioInputStream ais = AudioSystem.getAudioInputStream(WaveDataTest.class.getClassLoader().getResource(filePath)); int totalSize = ais.getFormat().getChannels() * (int) ais.getFrameLength() * ais.getFormat().getSampleSizeInBits() / 8; // skip 1/4 of the stream int skip = totalSize / 4; long skipped = ais.skip(skip); WaveData wd = WaveData.create(ais); if(wd == null) { System.out.println("executeMidStreamCreationTest::success"); } } catch (Exception e) { e.printStackTrace(); } }
private void loadSamples() throws Exception { AL10.alGetError(); WaveData data = WaveData.create("ding.wav"); for (int i = 1; i <= 10; i++) { AL10.alBufferData( buffers.get(i - 1), data.format, data.data, data.samplerate); if (AL10.alGetError() != AL10.AL_NO_ERROR) { System.out.println("Failed to load " + i + ".wav into buffer"); sources.position(0).limit(4); AL10.alDeleteSources(sources); buffers.position(0).limit(10); AL10.alDeleteBuffers(buffers); alExit(); } } data.dispose(); }
private IntBuffer initBuffer(String filename) { // WaveData waveFile = WaveData.create(getResource("Sounds/" + // filename)); WaveData waveFile = null; try { waveFile = WaveData.create( new BufferedInputStream(new FileInputStream("sound" + File.separatorChar + filename + ".wav"))); } catch (FileNotFoundException e) { System.err.println("Tried to load sound " + filename + ".wav , didn't work"); e.printStackTrace(); } IntBuffer buffer = BufferUtils.createIntBuffer(1); AL10.alGenBuffers(buffer); if (AL10.alGetError() != AL10.AL_NO_ERROR) { System.out.println("Error loading file: " + filename); return null; } AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate); return buffer; }
/** * Creates a new sound with the specified sound file. * * @param soundName the location of the sound file. */ public Sound(String soundName) { try { FileInputStream input = new FileInputStream("res/sounds/" + soundName + ".wav"); data = WaveData.create(new BufferedInputStream(input)); buffer = alGenBuffers(); alBufferData(buffer, data.format, data.data, data.samplerate); data.dispose(); source = alGenSources(); alSourcei(source, AL_BUFFER, buffer); GameData.soundManager.addSound(this); } catch (FileNotFoundException e) { e.printStackTrace(); } }
/** * Load in the data from the disk to OpenAL as a buffer, and create a source * to play our buffer. * @param filepath The path to the .wav file to load. * @return Returns AL10.AL_TRUE on success, and otherwise returns AL10.AL_FALSE. */ private int loadALData(String filepath) { /** Load the wav data into the buffer */ AL10.alGenBuffers(buffer); if(AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE; /** Load the wave file from the classpath */ WaveData waveFile = WaveData.create(filepath); /** Set up the buffer with the format (etc.) of the wav file.*/ AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate); waveFile.dispose(); /** Bind the buffer to the corresponding source */ AL10.alGenSources(source); if(AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE; /** Configure the AL data to do what we want. */ AL10.alSourcei (source.get(0), AL10.AL_BUFFER, buffer.get(0)); AL10.alSourcef (source.get(0), AL10.AL_PITCH, pitch); AL10.alSourcef (source.get(0), AL10.AL_GAIN, gain); AL10.alSourcefv(source.get(0), AL10.AL_POSITION, sourcePos); AL10.alSourcefv(source.get(0), AL10.AL_VELOCITY, sourceVel); /** Do a final error check, return AL10.AL_TRUE if everything worked. */ if(AL10.alGetError() == AL10.AL_NO_ERROR) return AL10.AL_TRUE; /** Otherwise, return AL10.AL_FALSE because something went wrong.*/ return AL10.AL_FALSE; }
public static void main(String[] args) throws FileNotFoundException { try { Display.setDisplayMode(new DisplayMode(640, 480)); Display.setTitle("OpenAL Demo"); Display.create(); AL.create(); } catch (LWJGLException e) { e.printStackTrace(); Display.destroy(); AL.destroy(); System.exit(1); } WaveData data = WaveData.create(new BufferedInputStream(new FileInputStream("res" + File.separatorChar + "sounds" + File.separatorChar + "thump.wav"))); int buffer = alGenBuffers(); alBufferData(buffer, data.format, data.data, data.samplerate); data.dispose(); int source = alGenSources(); alSourcei(source, AL_BUFFER, buffer); while (!Display.isCloseRequested()) { while (Keyboard.next()) { if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { alSourcePlay(source); } } Display.update(); Display.sync(60); } alDeleteBuffers(buffer); AL.destroy(); Display.destroy(); System.exit(0); }
private void executeStreamCreationTest() { try { AudioInputStream ais = AudioSystem.getAudioInputStream(new File(filePath)); WaveData wd = WaveData.create(ais); if(wd == null) { System.out.println("executeMidStreamCreationTest::success"); } } catch (Exception e) { e.printStackTrace(); } }
/** * Reads the file into a ByteBuffer * * @param filename Name of file to load * @return ByteBuffer containing file data */ protected ByteBuffer getData(String filename) { ByteBuffer buffer = null; System.out.println("Attempting to load: " + filename); try { BufferedInputStream bis = new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filename)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bufferLength = 4096; byte[] readBuffer = new byte[bufferLength]; int read = -1; while((read = bis.read(readBuffer, 0, bufferLength)) != -1) { baos.write(readBuffer, 0, read); } //done reading, close bis.close(); // if ogg vorbis data, we need to pass it unmodified to alBufferData buffer = ByteBuffer.allocateDirect(baos.size()); buffer.order(ByteOrder.nativeOrder()); buffer.put(baos.toByteArray()); buffer.rewind(); } catch (Exception ioe) { ioe.printStackTrace(); } return buffer; }
/** * Reads the file into a ByteBuffer * * @param filename Name of file to load * @return ByteBuffer containing file data */ protected ByteBuffer getData(String filename) { ByteBuffer buffer = null; System.out.println("Attempting to load: " + filename); try { BufferedInputStream bis = new BufferedInputStream(WaveData.class.getClassLoader().getResourceAsStream(filename)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bufferLength = 4096; byte[] readBuffer = new byte[bufferLength]; int read = -1; while((read = bis.read(readBuffer, 0, bufferLength)) != -1) { baos.write(readBuffer, 0, read); } //done reading, close bis.close(); // if ogg vorbis data, we need to pass it unmodified to alBufferData if(usingVorbis) { buffer = ByteBuffer.allocateDirect(baos.size()); } else { buffer = ByteBuffer.allocate(baos.size()); } buffer.order(ByteOrder.nativeOrder()); buffer.put(baos.toByteArray()); buffer.rewind(); } catch (Exception ioe) { ioe.printStackTrace(); } return buffer; }
private static void loadWavIntoBuffer(int buffer, String path) { try { WaveData wd = WaveData.create(new BufferedInputStream(new FileInputStream(path))); alBufferData(buffer,wd.format,wd.data,wd.samplerate); wd.dispose(); } catch (FileNotFoundException e) { System.out.println(e.toString()); } }
public boolean CacheSound(String File) { if (!LoadedFiles.contains(File)) { try { // Load the sound, create a buffer and save the filename. WaveData Data = WaveData.create(new BufferedInputStream(new FileInputStream("res/sounds/" + File))); int Buffer = alGenBuffers(); alBufferData(Buffer, Data.format, Data.data, Data.samplerate); Data.dispose(); if (!LoadedFiles.contains(File)) { LoadedFiles.add(File); SoundBuffers.add(Buffer); } //IntSources.add(alGenSources()); } catch (Exception ex) { return false; } } return true; }