Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3.  
  4. using UnityEngine;
  5.  
  6. namespace DialogFlow.Utilities
  7. {
  8. public static class Audio
  9. {
  10. public static AudioClip ToAudioClip(byte[] file)
  11. {
  12. if (IsCompatible(file))
  13. {
  14. int subChunk1Size = BitConverter.ToInt32(file, 16);
  15. int audioFormat = BitConverter.ToInt16(file, 20);
  16. int numberOfChannels = BitConverter.ToInt16(file, 22);
  17. int sampleRate = BitConverter.ToInt32(file, 24);
  18. int bitsPerSample = BitConverter.ToInt16(file, 34);
  19.  
  20. int dataIndex = 20 + subChunk1Size;
  21. for (int i = dataIndex; i < file.Length; i++)
  22. {
  23. if (file[i] == 'd' && file[i + 1] == 'a' && file[i + 2] == 't' && file[i + 3] == 'a')
  24. {
  25. dataIndex = i + 4;
  26. break;
  27. }
  28. }
  29.  
  30. int subChunk2Size = BitConverter.ToInt32(file, dataIndex);
  31. dataIndex += 4;
  32. int sampleSize = bitsPerSample / 8;
  33. int sampleCount = subChunk2Size / sampleSize;
  34.  
  35. if (audioFormat == 1)
  36. {
  37. float[] audioBuffer = new float[sampleCount];
  38. for (int i = 0; i < sampleCount; i++)
  39. {
  40. int sampleIndex = dataIndex + i * sampleSize;
  41. short intSample = BitConverter.ToInt16(file, sampleIndex);
  42. float sample = intSample / 32768.0f;
  43. audioBuffer[i] = sample;
  44. }
  45.  
  46. AudioClip audioClip = AudioClip.Create(Guid.NewGuid().ToString(), sampleCount, numberOfChannels, sampleRate, false);
  47. audioClip.SetData(audioBuffer, 0);
  48.  
  49. return audioClip;
  50. }
  51. return null;
  52. }
  53. return null;
  54. }
  55.  
  56. public static byte[] FromAudioClip(AudioClip clip)
  57. {
  58. float[] samples = new float[clip.samples];
  59. clip.GetData(samples, 0);
  60.  
  61. List<byte> file = new List<byte>();
  62. file.AddRange(new byte[] { (byte)'R', (byte)'I', (byte)'F', (byte)'F' });
  63. file.AddRange(BitConverter.GetBytes(samples.Length * 2 + 44 - 8));
  64. file.AddRange(new byte[] { (byte)'W', (byte)'A', (byte)'V', (byte)'E' });
  65. file.AddRange(new byte[] { (byte)'f', (byte)'m', (byte)'t', (byte)' ' });
  66. file.AddRange(BitConverter.GetBytes(16));
  67. file.AddRange(BitConverter.GetBytes((ushort)1));
  68. file.AddRange(BitConverter.GetBytes((ushort)clip.channels));
  69. file.AddRange(BitConverter.GetBytes(clip.frequency));
  70. file.AddRange(BitConverter.GetBytes(clip.frequency * clip.channels * 2));
  71. file.AddRange(BitConverter.GetBytes((ushort)(clip.channels * 2)));
  72. file.AddRange(BitConverter.GetBytes((ushort)16));
  73. file.AddRange(new byte[] { (byte)'d', (byte)'a', (byte)'t', (byte)'a' });
  74. file.AddRange(BitConverter.GetBytes(samples.Length * 2));
  75.  
  76. for (int i = 0; i < samples.Length; i++)
  77. {
  78. short sample = (short)(samples[i] * 32768.0f);
  79. file.AddRange(BitConverter.GetBytes(sample));
  80. }
  81.  
  82. return file.ToArray();
  83. }
  84.  
  85. public static AudioClip EmptyClip()
  86. {
  87. return null;
  88. }
  89.  
  90. private static bool IsCompatible(byte[] file)
  91. {
  92. byte[] data = new byte[4];
  93. Buffer.BlockCopy(file, 0, data, 0, data.Length);
  94. string chunkId = ByteArrayToString(data);
  95. Buffer.BlockCopy(file, 8, data, 0, data.Length);
  96. string format = ByteArrayToString(data);
  97.  
  98. return (chunkId == "RIFF" && format == "WAVE");
  99. }
  100.  
  101. private static string ByteArrayToString(byte[] content)
  102. {
  103. char[] chars = new char[content.Length];
  104. content.CopyTo(chars, 0);
  105. return new string(chars);
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement