I have found converting a short to byte array, and byte array to short array, but not short array to byte array.
Here is the code leading up to the conversion
while(!stopped)
{
Log.i("Map", "Writing new data to buffer");
short[] buffer = buffers[ix++ % buffers.length];
N = recorder.read(buffer,0,buffer.length);
track.write(buffer, 0, buffer.length);
byte[] bytes2 = new byte[N];
I have tried
int i = 0;
ByteBuffer byteBuf = ByteBuffer.allocate(N);
while (buffer.length >= i) {
byteBuf.putShort(buffer[i]);
i++;
}
bytes2 = byteBuf.array();
and
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);
However I receive this error on both (the error if not exactly the same but very similar for both):
05-29 13:41:12.021: W/AudioTrack(9758): obtainBuffer() track 0x30efa0 disabled, restarting
05-29 13:41:12.857: W/AudioWorker(9758): Error reading voice AudioWorker
05-29 13:41:12.857: W/AudioWorker(9758): java.nio.BufferOverflowException
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:422)
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:210)
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:391)
05-29 13:41:12.857: W/AudioWorker(9758): at com.avispl.nicu.audio.AudioWorker.run(AudioWorker.java:126)
And just to be give as much info as possible here is the code after that uses the byte array
Log.i("Map", "test");
//convert to ulaw
read(bytes2, 0, N);
//send to server
os.write(bytes2,0,bytes2.length);
System.out.println("bytesRead "+buffer.length);
System.out.println("data "+Arrays.toString(buffer));
}
I found ByteBuffer to be the slowest conversion method out of three that I've profiled. See below...
Platform: Nexus S, Android 4.1.1, No SIM card
Method #1: Use a ByteBuffer
byte [] ShortToByte_ByteBuffer_Method(short [] input)
{
int index;
int iterations = input.length;
ByteBuffer bb = ByteBuffer.allocate(input.length * 2);
for(index = 0; index != iterations; ++index)
{
bb.putShort(input[index]);
}
return bb.array();
}
Method #2: Twiddle bits directly
byte [] ShortToByte_Twiddle_Method(short [] input)
{
int short_index, byte_index;
int iterations = input.length;
byte [] buffer = new byte[input.length * 2];
short_index = byte_index = 0;
for(/*NOP*/; short_index != iterations; /*NOP*/)
{
buffer[byte_index] = (byte) (input[short_index] & 0x00FF);
buffer[byte_index + 1] = (byte) ((input[short_index] & 0xFF00) >> 8);
++short_index; byte_index += 2;
}
return buffer;
}
Method #3: Use C via JNI
TypeCast.java
package mynamespace.util;
public class TypeCast
{
public static native byte [] shortToByte(short [] input);
static
{
System.loadLibrary("type_conversion");
}
}
native.c
#include <jni.h>
#include <string.h>
jbyteArray Java_mynamespace_util_TypeCast_shortToByte(JNIEnv *env, jobject obj, jshortArray input)
{
jshort *input_array_elements;
int input_length;
jbyte *output_array_elements;
jbyteArray output;
input_array_elements = (*env)->GetShortArrayElements(env, input, 0);
input_length = (*env)->GetArrayLength(env, input);
output = (jbyteArray) ((*env)->NewByteArray(env, input_length * 2));
output_array_elements = (*env)->GetByteArrayElements(env, output, 0);
memcpy(output_array_elements, input_array_elements, input_length * 2);
(*env)->ReleaseShortArrayElements(env, input, input_array_elements, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, output, output_array_elements, 0);
return output;
}
Results:
For a one million element input array, the time of execution is as follows:
Method #1 ByteBuffer: 865 ms
Method #2 Twiddle: 299 ms
Method #3 C: 39 ms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With