I am collecting data from a USB device and this data has to go to an audio output component. At the moment I am not delivering the data fast enough to avoid clicks in the output signal. So every millisecond counts.
At the moment I am collecting the data which is delivered in a byte array of 65536 bytes.The first two bytes represent 16 bits of data in little endian format. These two bytes must be placed in the first element of a double array. The second two bytes, must be placed in the first element of a different double array. This is then repeated for all the bytes in the 65536 buffer so that you end up with 2 double[] arrays of size 16384.
I am currently using BitConverter.ToInt16 as shown in the code. It takes around 0.3ms to run this but it has to be done 10 times to get a packet to send off to the audio output. So the overhead is 3ms which is just enough for some packets to not be delivered on time eventually.
Code
byte[] buffer = new byte[65536];
double[] bufferA = new double[16384];
double[] bufferB = new double[16384]
for(int i= 0; i < 65536; i +=4)
{
bufferA[i/4] = BitConverter.ToInt16(buffer, i);
bufferB[i/4] = BitConverter.ToInt16(buffer, i+2);
}
How can I improve this? Is it possible to copy the values with unsafe code? I have no experience in that. Thanks