Reputation: 20782
I am receiving some raw data from audio input via System.Runtime.InteropServices.Marshal.Copy()
and this is stored in a byte[]. The actual data in that byte[] I know to be of type Int16. I would like to treat this byte[] as if it was an array of type Int16 and loop over its elements, process it and at the end of this I would like to pass this altered array as byte[] into another function.
For those who wonder what I'm trying to do: I would like to try adding an echo effect to the incoming sound (from mic) by simply adding up the wave data from previous buffer with the wave data from the current buffer, mixing it in at a lower volume.
EDIT: the byte[] array may be storing a int16 number 258 as a pair of bytes [1][2]
0000 0001 0000 0010
Upvotes: 0
Views: 642
Reputation: 2640
You can use BitConverter
to treat the bytes as Int16 elements using BitConverter.ToInt16
. You can also use Buffer.BlockCopy
to copy the array of bytes to an array of Int16s. The first is more memory efficient and the second is more speed efficient.
Upvotes: 1
Reputation: 1500923
You can't, as far as I'm aware.
I suggest you use Buffer.BlockCopy
to copy the data into a short[]
, work on it, and then copy it back. Of course, that assumes that the bytes are arranged in an appropriate endianness... but it's likely that they will be.
Alternatively... use the overload of Marshal.Copy
which accepts a short[]
to copy the data into to start with.
Upvotes: 4