/** Test we can create an Buffer, then modify the direct bytes in native code. */ @Test public void testCanDirectlyModifyNativeBytes() { byte buffer[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; Buffer buf = Buffer.make(null, buffer, 0, buffer.length); assertNotNull(buf); assertEquals(buf.getBufferSize(), buffer.length); // this give us the native bytes java.nio.ByteBuffer nativeBytes = buf.getByteBuffer(0, buffer.length); assertNotNull(nativeBytes); for (int i = 0; i < buffer.length; i++) { nativeBytes.put(i, (byte) (9 - buffer[i])); // reverse the order of the integers } // we can release it. no "update" method should be required. It should // have modified the underlying C++ bytes. nativeBytes = null; // now, get a copy of the bytes in the Buffer and make sure // the order of bytes was reversed. byte outBuffer[] = buf.getByteArray(0, buffer.length); assertNotNull(outBuffer); assertEquals(outBuffer.length, buffer.length); assertNotSame(buf, outBuffer); for (int i = 0; i < buffer.length; i++) { assertEquals(9 - buffer[i], outBuffer[i]); } }
// @Test public void playTest() throws Exception { logger.info("start playTest"); SourceDataLine audioLine = null; MediaAudio samples = beepSamples(); logger.info("sample is ready"); AudioFormat format = new AudioFormat( (float) samples.getSampleRate(), (int) samples.getBytesPerSample() * 8, samples.getChannels(), true, false); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); audioLine = (SourceDataLine) AudioSystem.getLine(info); audioLine.open(format); logger.info("beepstart"); audioLine.start(); Buffer buffer = samples.getData(0); audioLine.write( buffer.getByteArray(0, samples.getDataPlaneSize(0)), 0, samples.getDataPlaneSize(0)); audioLine.drain(); logger.info("beepend"); audioLine.close(); audioLine = null; }
/** * Test that we can create a Buffer from a Java byte[] array, and that we can copy the same data * out of an Buffer (via copy). */ @Test public void testCreateFromBytes() { byte buffer[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; Buffer buf = Buffer.make(null, buffer, 0, buffer.length); assertNotNull(buf); assertEquals(buf.getBufferSize(), buffer.length); byte outBuffer[] = buf.getByteArray(0, buffer.length); assertNotNull(outBuffer); assertEquals(outBuffer.length, buffer.length); assertNotSame(buf, outBuffer); for (int i = 0; i < buffer.length; i++) { assertEquals(buffer[i], outBuffer[i]); } }