Advertisement
Guest User

Untitled

a guest
May 24th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. /* From http://answers.unity3d.com/questions/737002/wav-byte-to-audioclip.html */
  2. /* From https://gamedev.stackexchange.com/a/114886 */
  3. public class WAV
  4. {
  5.  
  6. // convert two bytes to one float in the range -1 to 1
  7. static float BytesToFloat(byte firstByte, byte secondByte)
  8. {
  9. // convert two bytes to one short (little endian)
  10. short s = (short)((secondByte << 8) | firstByte);
  11. // convert to range from -1 to (just below) 1
  12. return s / 32768.0F;
  13. }
  14.  
  15. static int BytesToInt(byte[] bytes, int offset = 0)
  16. {
  17. int value = 0;
  18. for (int i = 0; i < 4; i++)
  19. {
  20. value |= ((int)bytes[offset + i]) << (i * 8);
  21. }
  22. return value;
  23. }
  24. // properties
  25. public float[] LeftChannel { get; internal set; }
  26. public float[] RightChannel { get; internal set; }
  27. public int ChannelCount { get; internal set; }
  28. public int SampleCount { get; internal set; }
  29. public int Frequency { get; internal set; }
  30.  
  31. public WAV(byte[] wav)
  32. {
  33.  
  34. // Determine if mono or stereo
  35. ChannelCount = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels
  36.  
  37. // Get the frequency
  38. Frequency = BytesToInt(wav, 24);
  39.  
  40. // Get past all the other sub chunks to get to the data subchunk:
  41. int pos = 12; // First Subchunk ID from 12 to 16
  42.  
  43. // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
  44. while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97))
  45. {
  46. pos += 4;
  47. int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
  48. pos += 4 + chunkSize;
  49. }
  50. pos += 8;
  51.  
  52. // Pos is now positioned to start of actual sound data.
  53. SampleCount = (wav.Length - pos) / 2; // 2 bytes per sample (16 bit sound mono)
  54. if (ChannelCount == 2) SampleCount /= 2; // 4 bytes per sample (16 bit stereo)
  55.  
  56. // Allocate memory (right will be null if only mono sound)
  57. LeftChannel = new float[SampleCount];
  58. if (ChannelCount == 2) RightChannel = new float[SampleCount];
  59. else RightChannel = null;
  60.  
  61. // Write to double array/s:
  62. int i = 0;
  63. while (pos < wav.Length)
  64. {
  65. LeftChannel[i] = BytesToFloat(wav[pos], wav[pos + 1]);
  66. pos += 2;
  67. if (ChannelCount == 2)
  68. {
  69. RightChannel[i] = BytesToFloat(wav[pos], wav[pos + 1]);
  70. pos += 2;
  71. }
  72. i++;
  73. }
  74. }
  75.  
  76. public override string ToString()
  77. {
  78. return string.Format("[WAV: LeftChannel={0}, RightChannel={1}, ChannelCount={2}, SampleCount={3}, Frequency={4}]", LeftChannel, RightChannel, ChannelCount, SampleCount, Frequency);
  79. }
  80. }
  81. class Mp3
  82. {
  83. private static MemoryStream AudioMemStream(WaveStream waveStream)
  84. {
  85. MemoryStream outputStream = new MemoryStream();
  86. using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
  87. {
  88. byte[] bytes = new byte[waveStream.Length];
  89. waveStream.Position = 0;
  90. waveStream.Read(bytes, 0, (int)waveStream.Length);
  91. waveFileWriter.Write(bytes, 0, bytes.Length);
  92. waveFileWriter.Flush();
  93. }
  94. return outputStream;
  95. }
  96. public static AudioClip GetMp3Audio(string name, byte[] data)
  97. {
  98. // Load the data into a stream
  99. MemoryStream mp3stream = new MemoryStream(data);
  100. // Convert the data in the stream to WAV format
  101. Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);
  102. // Convert to WAV data
  103. WAV wav = new WAV(AudioMemStream(mp3audio).ToArray());
  104. Debug.Log(wav);
  105. AudioClip audioClip = AudioClip.Create(name, wav.SampleCount, 1, wav.Frequency, false);
  106. audioClip.SetData(wav.LeftChannel, 0);
  107. // Return the clip
  108. return audioClip;
  109. }
  110. }
  111. public static AudioClip GetAudio(string path)
  112. {
  113. string extension = Path.GetExtension(path);
  114. byte[] data = File.ReadAllBytes(path);
  115. switch (extension)
  116. {
  117. case ".mp3":
  118. {
  119. return Mp3.GetMp3Audio(Path.GetFileName(path), data);
  120. }
  121. case ".wav":
  122. return WavUtility.ToAudioClip(data);
  123. default:
  124. {
  125. Debug.Log(extension + " file type is not yet supported");
  126. return null;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement