public Decoder(String path, IntBuffer buffers){ this.buffers = buffers; vorbis = ResourceLoader.getBytes(path); IntBuffer error = BufferUtils.createIntBuffer(1); handle = stb_vorbis_open_memory(vorbis, error, null); if(handle == 0L){ throw new RuntimeException("Unable to open stb"); } STBVorbisInfo info = STBVorbisInfo.malloc(); stb_vorbis_get_info(handle, info); channels = info.channels(); sampleRate = info.sample_rate(); format = (channels == 1) ? AL10.AL_FORMAT_MONO16 : AL10.AL_FORMAT_STEREO16; lengthSamples = stb_vorbis_stream_length_in_samples(handle); lengthSeconds = stb_vorbis_stream_length_in_seconds(handle); pcm = BufferUtils.createShortBuffer(BUFFER_SIZE); samplesLeft = lengthSamples; }
private void allocate(){ ByteBuffer buffer = ResourceLoader.getBytes(path); IntBuffer error = BufferUtils.createIntBuffer(1); long decoder = stb_vorbis_open_memory(buffer, error, null); if(decoder == 0L){ Application.error("Unable to open STB Vorbis"); return; } STBVorbisInfo info = STBVorbisInfo.malloc(); stb_vorbis_get_info(decoder, info); int channels = info.channels(); int lengthSamples = stb_vorbis_stream_length_in_samples(decoder); ShortBuffer pcm = BufferUtils.createShortBuffer(lengthSamples); pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels); stb_vorbis_close(decoder); AL10.alBufferData(id, info.channels() == 1? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, info.sample_rate()); }
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception { try (MemoryStack stack = MemoryStack.stackPush()) { vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize); IntBuffer error = stack.mallocInt(1); long decoder = stb_vorbis_open_memory(vorbis, error, null); if (decoder == NULL) { throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0)); } stb_vorbis_get_info(decoder, info); int channels = info.channels(); int lengthSamples = stb_vorbis_stream_length_in_samples(decoder); pcm = MemoryUtil.memAllocShort(lengthSamples); pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels); stb_vorbis_close(decoder); return pcm; } }
public SoundBuffer(String file) throws Exception { this.bufferId = alGenBuffers(); try (STBVorbisInfo info = STBVorbisInfo.malloc()) { ShortBuffer pcm = readVorbis(file, 32 * 1024, info); // Copy to buffer alBufferData(bufferId, info.channels() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, info.sample_rate()); } }
/** * This method reads in the OGG data, and convert it into PCM samples ready to be fed into the mouth of OpenAL * ALBuffer class. */ private void decodeToPCM(DirectBuffer input) { // Create a memory representation to read from memory ByteBuffer memoryBuffer = (ByteBuffer) input.nativeBuffer(); // Open the vorbis file to get stb_vorbis* IntBuffer error = BufferUtils.createIntBuffer(1); long handle = stb_vorbis_open_memory(memoryBuffer, error, null); if (handle == NULL) throw new SilenceException("Error " + error.get(0) + ": decoding the OGG data"); // Get the information about the OGG header STBVorbisInfo info = STBVorbisInfo.malloc(); stb_vorbis_get_info(handle, info); int channels = info.channels(); sampleRate = info.sample_rate(); format = channels == 1 ? ALFormat.MONO_16 : ALFormat.STEREO_16; // Read all the samples once for all int numSamples = stb_vorbis_stream_length_in_samples(handle); ByteBuffer pcm = BufferUtils.createByteBuffer(numSamples * Short.BYTES); stb_vorbis_get_samples_short_interleaved(handle, channels, pcm.asShortBuffer()); // Convert the audio bytes and store the data buffer data = pcm; // Close the stb_vorbis* handle stb_vorbis_close(handle); info.free(); }
public static int loadSound(File file) { STBVorbisInfo info = STBVorbisInfo.malloc(); ByteBuffer vorbis; try { vorbis = loadSoundToByteBuffer(file, (int) file.length()); } catch (IOException e) { throw new RuntimeException(e); } IntBuffer error = BufferUtils.createIntBuffer(1); long decoder = stb_vorbis_open_memory(vorbis, error, null); if (decoder == NULL) throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0)); stb_vorbis_get_info(decoder, info); int lengthSamples = stb_vorbis_stream_length_in_samples(decoder); ShortBuffer pcm = BufferUtils.createShortBuffer(info.channels() * lengthSamples); stb_vorbis_get_samples_short_interleaved(decoder, info.channels(), pcm); stb_vorbis_close(decoder); int buffer = alGenBuffers(); alBufferData(buffer, info.channels() == 2 ? AL10.AL_FORMAT_STEREO16 : AL10.AL_FORMAT_MONO16, pcm, info.sample_rate()); return buffer; }
public static ALBufferData readVorbis(Supplier<InputStream> resource) { try (STBVorbisInfo info = STBVorbisInfo.malloc()) { ByteBuffer vorbis; try { vorbis = LUtils.inputStreamToDirectByteBuffer(resource); } catch (IOException e) { throw new RuntimeException(e); } IntBuffer error = BufferUtils.createIntBuffer(1); long decoder = stb_vorbis_open_memory(vorbis, error, null); if (decoder == MemoryUtil.NULL) throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0)); stb_vorbis_get_info(decoder, info); int channels = info.channels(); int lengthSamples = stb_vorbis_stream_length_in_samples(decoder); ShortBuffer pcm = BufferUtils.createShortBuffer(lengthSamples); pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels); stb_vorbis_close(decoder); return ALBufferData.create(channels == 1 ? AL10.AL_FORMAT_MONO16 : AL10.AL_FORMAT_STEREO16, pcm, info.sample_rate()); } }
public void loadOGGFormat(){ STBVorbisInfo info = STBVorbisInfo.malloc(); ByteBuffer buff = BufferUtils.createByteBuffer(0); //lecture du fichier //---------------------------------------------------------------------------------------------------------------- try { File file = new File(fileName); if ( file.isFile() ) { FileInputStream fis = new FileInputStream(file); FileChannel fc = fis.getChannel(); buff = BufferUtils.createByteBuffer((int)fc.size() + 1); while ( fc.read(buff) != -1 ) ; fis.close(); fc.close(); } else { System.err.println("File not found !"); return; } buff.flip(); } catch (IOException e) { throw new RuntimeException(e); } //---------------------------------------------------------------------------------------------------------------- IntBuffer error = BufferUtils.createIntBuffer(1); long decoder = stb_vorbis_open_memory(buff, error, null); if ( decoder == NULL ) throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0)); stb_vorbis_get_info(decoder, info); int channels = info.channels(); stb_vorbis_seek_start(decoder); int lengthSamples = stb_vorbis_stream_length_in_samples(decoder); ShortBuffer pcm = BufferUtils.createShortBuffer(lengthSamples * channels); stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm); stb_vorbis_close(decoder); buffer = alGenBuffers(); source = alGenSources(); if(channels == 1)alBufferData(buffer, AL_FORMAT_MONO16, pcm, info.sample_rate()); else alBufferData(buffer, AL_FORMAT_STEREO16, pcm, info.sample_rate()); }