ByteUtils.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using UnityEngine;
  3. public class ByteUtils {
  4. public enum Endian {
  5. LITTLE_ENDIAN,
  6. BIG_ENDIAN
  7. }
  8. /**
  9. * Writes a short (Int16) to a byte array.
  10. * This is an aux function used when creating the WAV data.
  11. */
  12. public static void writeShortToBytes(byte[] __bytes, ref int __position, short __newShort, Endian __endian) {
  13. writeBytes(__bytes, ref __position, new byte[2] { (byte)((__newShort >> 8) & 0xff), (byte)(__newShort & 0xff) }, __endian);
  14. }
  15. /**
  16. * Writes a uint (UInt32) to a byte array.
  17. * This is an aux function used when creating the WAV data.
  18. */
  19. public static void writeUintToBytes(byte[] __bytes, ref int __position, uint __newUint, Endian __endian) {
  20. writeBytes(__bytes, ref __position, new byte[4] { (byte)((__newUint >> 24) & 0xff), (byte)((__newUint >> 16) & 0xff), (byte)((__newUint >> 8) & 0xff), (byte)(__newUint & 0xff) }, __endian);
  21. }
  22. /**
  23. * Writes any number of bytes into a byte array, at a given position.
  24. * This is an aux function used when creating the WAV data.
  25. */
  26. public static void writeBytes(byte[] __bytes, ref int __position, byte[] __newBytes, Endian __endian) {
  27. // Writes __newBytes to __bytes at position __position, increasing the position depending on the length of __newBytes
  28. for (int i = 0; i < __newBytes.Length; i++) {
  29. __bytes[__position] = __newBytes[__endian == Endian.BIG_ENDIAN ? i : __newBytes.Length - i - 1];
  30. __position++;
  31. }
  32. }
  33. }