No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WavUtility.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using UnityEngine;
  2. using System.Text;
  3. using System.IO;
  4. using System;
  5. /// <summary>
  6. /// WAV utility for recording and audio playback functions in Unity.
  7. /// Version: 1.0 alpha 1
  8. ///
  9. /// - Use "ToAudioClip" method for loading wav file / bytes.
  10. /// Loads .wav (PCM uncompressed) files at 8,16,24 and 32 bits and converts data to Unity's AudioClip.
  11. ///
  12. /// - Use "FromAudioClip" method for saving wav file / bytes.
  13. /// Converts an AudioClip's float data into wav byte array at 16 bit.
  14. /// </summary>
  15. /// <remarks>
  16. /// For documentation and usage examples: https://github.com/deadlyfingers/UnityWav
  17. /// </remarks>
  18. public class WavUtility
  19. {
  20. // Force save as 16-bit .wav
  21. const int BlockSize_16Bit = 2;
  22. /// <summary>
  23. /// Load PCM format *.wav audio file (using Unity's Application data path) and convert to AudioClip.
  24. /// </summary>
  25. /// <returns>The AudioClip.</returns>
  26. /// <param name="filePath">Local file path to .wav file</param>
  27. public static AudioClip ToAudioClip (string filePath)
  28. {
  29. if (!filePath.StartsWith (Application.persistentDataPath) && !filePath.StartsWith (Application.dataPath)) {
  30. Debug.LogWarning ("This only supports files that are stored using Unity's Application data path. \nTo load bundled resources use 'Resources.Load(\"filename\") typeof(AudioClip)' method. \nhttps://docs.unity3d.com/ScriptReference/Resources.Load.html");
  31. return null;
  32. }
  33. byte[] fileBytes = File.ReadAllBytes (filePath);
  34. return ToAudioClip (fileBytes, 0);
  35. }
  36. public static AudioClip ToAudioClip (byte[] fileBytes, int offsetSamples = 0, string name = "wav")
  37. {
  38. //string riff = Encoding.ASCII.GetString (fileBytes, 0, 4);
  39. //string wave = Encoding.ASCII.GetString (fileBytes, 8, 4);
  40. int subchunk1 = BitConverter.ToInt32 (fileBytes, 16);
  41. UInt16 audioFormat = BitConverter.ToUInt16 (fileBytes, 20);
  42. // NB: Only uncompressed PCM wav files are supported.
  43. string formatCode = FormatCode (audioFormat);
  44. Debug.AssertFormat (audioFormat == 1 || audioFormat == 65534, "Detected format code '{0}' {1}, but only PCM and WaveFormatExtensable uncompressed formats are currently supported.", audioFormat, formatCode);
  45. UInt16 channels = BitConverter.ToUInt16 (fileBytes, 22);
  46. int sampleRate = BitConverter.ToInt32 (fileBytes, 24);
  47. //int byteRate = BitConverter.ToInt32 (fileBytes, 28);
  48. //UInt16 blockAlign = BitConverter.ToUInt16 (fileBytes, 32);
  49. UInt16 bitDepth = BitConverter.ToUInt16 (fileBytes, 34);
  50. int headerOffset = 16 + 4 + subchunk1 + 4;
  51. int subchunk2 = BitConverter.ToInt32 (fileBytes, headerOffset);
  52. //Debug.LogFormat ("riff={0} wave={1} subchunk1={2} format={3} channels={4} sampleRate={5} byteRate={6} blockAlign={7} bitDepth={8} headerOffset={9} subchunk2={10} filesize={11}", riff, wave, subchunk1, formatCode, channels, sampleRate, byteRate, blockAlign, bitDepth, headerOffset, subchunk2, fileBytes.Length);
  53. float[] data;
  54. switch (bitDepth) {
  55. case 8:
  56. data = Convert8BitByteArrayToAudioClipData (fileBytes, headerOffset, subchunk2);
  57. break;
  58. case 16:
  59. data = Convert16BitByteArrayToAudioClipData (fileBytes, headerOffset, subchunk2);
  60. break;
  61. case 24:
  62. data = Convert24BitByteArrayToAudioClipData (fileBytes, headerOffset, subchunk2);
  63. break;
  64. case 32:
  65. data = Convert32BitByteArrayToAudioClipData (fileBytes, headerOffset, subchunk2);
  66. break;
  67. default:
  68. throw new Exception (bitDepth + " bit depth is not supported.");
  69. }
  70. AudioClip audioClip = AudioClip.Create (name, data.Length, (int)channels, sampleRate, false);
  71. audioClip.SetData (data, 0);
  72. return audioClip;
  73. }
  74. #region wav file bytes to Unity AudioClip conversion methods
  75. private static float[] Convert8BitByteArrayToAudioClipData (byte[] source, int headerOffset, int dataSize)
  76. {
  77. int wavSize = BitConverter.ToInt32 (source, headerOffset);
  78. headerOffset += sizeof(int);
  79. Debug.AssertFormat (wavSize > 0 && wavSize == dataSize, "Failed to get valid 8-bit wav size: {0} from data bytes: {1} at offset: {2}", wavSize, dataSize, headerOffset);
  80. float[] data = new float[wavSize];
  81. sbyte maxValue = sbyte.MaxValue;
  82. int i = 0;
  83. while (i < wavSize) {
  84. data [i] = (float)source [i] / maxValue;
  85. ++i;
  86. }
  87. return data;
  88. }
  89. private static float[] Convert16BitByteArrayToAudioClipData (byte[] source, int headerOffset, int dataSize)
  90. {
  91. int wavSize = BitConverter.ToInt32 (source, headerOffset);
  92. headerOffset += sizeof(int);
  93. Debug.AssertFormat (wavSize > 0 && wavSize == dataSize, "Failed to get valid 16-bit wav size: {0} from data bytes: {1} at offset: {2}", wavSize, dataSize, headerOffset);
  94. int x = sizeof(Int16); // block size = 2
  95. int convertedSize = wavSize / x;
  96. float[] data = new float[convertedSize];
  97. Int16 maxValue = Int16.MaxValue;
  98. int offset = 0;
  99. int i = 0;
  100. while (i < convertedSize) {
  101. offset = i * x + headerOffset;
  102. data [i] = (float)BitConverter.ToInt16 (source, offset) / maxValue;
  103. ++i;
  104. }
  105. Debug.AssertFormat (data.Length == convertedSize, "AudioClip .wav data is wrong size: {0} == {1}", data.Length, convertedSize);
  106. return data;
  107. }
  108. private static float[] Convert24BitByteArrayToAudioClipData (byte[] source, int headerOffset, int dataSize)
  109. {
  110. int wavSize = BitConverter.ToInt32 (source, headerOffset);
  111. headerOffset += sizeof(int);
  112. Debug.AssertFormat (wavSize > 0 && wavSize == dataSize, "Failed to get valid 24-bit wav size: {0} from data bytes: {1} at offset: {2}", wavSize, dataSize, headerOffset);
  113. int x = 3; // block size = 3
  114. int convertedSize = wavSize / x;
  115. int maxValue = Int32.MaxValue;
  116. float[] data = new float[convertedSize];
  117. byte[] block = new byte[sizeof(int)]; // using a 4 byte block for copying 3 bytes, then copy bytes with 1 offset
  118. int offset = 0;
  119. int i = 0;
  120. while (i < convertedSize) {
  121. offset = i * x + headerOffset;
  122. Buffer.BlockCopy (source, offset, block, 1, x);
  123. data [i] = (float)BitConverter.ToInt32 (block, 0) / maxValue;
  124. ++i;
  125. }
  126. Debug.AssertFormat (data.Length == convertedSize, "AudioClip .wav data is wrong size: {0} == {1}", data.Length, convertedSize);
  127. return data;
  128. }
  129. private static float[] Convert32BitByteArrayToAudioClipData (byte[] source, int headerOffset, int dataSize)
  130. {
  131. int wavSize = BitConverter.ToInt32 (source, headerOffset);
  132. headerOffset += sizeof(int);
  133. Debug.AssertFormat (wavSize > 0 && wavSize == dataSize, "Failed to get valid 32-bit wav size: {0} from data bytes: {1} at offset: {2}", wavSize, dataSize, headerOffset);
  134. int x = sizeof(float); // block size = 4
  135. int convertedSize = wavSize / x;
  136. Int32 maxValue = Int32.MaxValue;
  137. float[] data = new float[convertedSize];
  138. int offset = 0;
  139. int i = 0;
  140. while (i < convertedSize) {
  141. offset = i * x + headerOffset;
  142. data [i] = (float)BitConverter.ToInt32 (source, offset) / maxValue;
  143. ++i;
  144. }
  145. Debug.AssertFormat (data.Length == convertedSize, "AudioClip .wav data is wrong size: {0} == {1}", data.Length, convertedSize);
  146. return data;
  147. }
  148. #endregion
  149. public static byte[] FromAudioClip (AudioClip audioClip)
  150. {
  151. string file;
  152. return FromAudioClip (audioClip, out file, false);
  153. }
  154. public static byte[] FromAudioClip (AudioClip audioClip, out string filepath, bool saveAsFile = true, string dirname = "recordings")
  155. {
  156. MemoryStream stream = new MemoryStream ();
  157. const int headerSize = 44;
  158. // get bit depth
  159. UInt16 bitDepth = 16; //BitDepth (audioClip);
  160. // NB: Only supports 16 bit
  161. //Debug.AssertFormat (bitDepth == 16, "Only converting 16 bit is currently supported. The audio clip data is {0} bit.", bitDepth);
  162. // total file size = 44 bytes for header format and audioClip.samples * factor due to float to Int16 / sbyte conversion
  163. int fileSize = audioClip.samples * BlockSize_16Bit + headerSize; // BlockSize (bitDepth)
  164. // chunk descriptor (riff)
  165. WriteFileHeader (ref stream, fileSize);
  166. // file header (fmt)
  167. WriteFileFormat (ref stream, audioClip.channels, audioClip.frequency, bitDepth);
  168. // data chunks (data)
  169. WriteFileData (ref stream, audioClip, bitDepth);
  170. byte[] bytes = stream.ToArray ();
  171. // Validate total bytes
  172. Debug.AssertFormat (bytes.Length == fileSize, "Unexpected AudioClip to wav format byte count: {0} == {1}", bytes.Length, fileSize);
  173. // Save file to persistant storage location
  174. if (saveAsFile) {
  175. filepath = string.Format ("{0}/{1}/{2}.{3}", Application.persistentDataPath, dirname, DateTime.UtcNow.ToString ("yyMMdd-HHmmss-fff"), "wav");
  176. Directory.CreateDirectory (Path.GetDirectoryName (filepath));
  177. File.WriteAllBytes (filepath, bytes);
  178. //Debug.Log ("Auto-saved .wav file: " + filepath);
  179. } else {
  180. filepath = null;
  181. }
  182. stream.Dispose ();
  183. return bytes;
  184. }
  185. #region write .wav file functions
  186. private static int WriteFileHeader (ref MemoryStream stream, int fileSize)
  187. {
  188. int count = 0;
  189. int total = 12;
  190. // riff chunk id
  191. byte[] riff = Encoding.ASCII.GetBytes ("RIFF");
  192. count += WriteBytesToMemoryStream (ref stream, riff, "ID");
  193. // riff chunk size
  194. int chunkSize = fileSize - 8; // total size - 8 for the other two fields in the header
  195. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (chunkSize), "CHUNK_SIZE");
  196. byte[] wave = Encoding.ASCII.GetBytes ("WAVE");
  197. count += WriteBytesToMemoryStream (ref stream, wave, "FORMAT");
  198. // Validate header
  199. Debug.AssertFormat (count == total, "Unexpected wav descriptor byte count: {0} == {1}", count, total);
  200. return count;
  201. }
  202. private static int WriteFileFormat (ref MemoryStream stream, int channels, int sampleRate, UInt16 bitDepth)
  203. {
  204. int count = 0;
  205. int total = 24;
  206. byte[] id = Encoding.ASCII.GetBytes ("fmt ");
  207. count += WriteBytesToMemoryStream (ref stream, id, "FMT_ID");
  208. int subchunk1Size = 16; // 24 - 8
  209. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (subchunk1Size), "SUBCHUNK_SIZE");
  210. UInt16 audioFormat = 1;
  211. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (audioFormat), "AUDIO_FORMAT");
  212. UInt16 numChannels = Convert.ToUInt16 (channels);
  213. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (numChannels), "CHANNELS");
  214. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (sampleRate), "SAMPLE_RATE");
  215. int byteRate = sampleRate * channels * BytesPerSample (bitDepth);
  216. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (byteRate), "BYTE_RATE");
  217. UInt16 blockAlign = Convert.ToUInt16 (channels * BytesPerSample (bitDepth));
  218. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (blockAlign), "BLOCK_ALIGN");
  219. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (bitDepth), "BITS_PER_SAMPLE");
  220. // Validate format
  221. Debug.AssertFormat (count == total, "Unexpected wav fmt byte count: {0} == {1}", count, total);
  222. return count;
  223. }
  224. private static int WriteFileData (ref MemoryStream stream, AudioClip audioClip, UInt16 bitDepth)
  225. {
  226. int count = 0;
  227. int total = 8;
  228. // Copy float[] data from AudioClip
  229. float[] data = new float[audioClip.samples * audioClip.channels];
  230. audioClip.GetData (data, 0);
  231. byte[] bytes = ConvertAudioClipDataToInt16ByteArray (data);
  232. byte[] id = Encoding.ASCII.GetBytes ("data");
  233. count += WriteBytesToMemoryStream (ref stream, id, "DATA_ID");
  234. int subchunk2Size = Convert.ToInt32 (audioClip.samples * BlockSize_16Bit); // BlockSize (bitDepth)
  235. count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (subchunk2Size), "SAMPLES");
  236. // Validate header
  237. Debug.AssertFormat (count == total, "Unexpected wav data id byte count: {0} == {1}", count, total);
  238. // Write bytes to stream
  239. count += WriteBytesToMemoryStream (ref stream, bytes, "DATA");
  240. // Validate audio data
  241. Debug.AssertFormat (bytes.Length == subchunk2Size, "Unexpected AudioClip to wav subchunk2 size: {0} == {1}", bytes.Length, subchunk2Size);
  242. return count;
  243. }
  244. private static byte[] ConvertAudioClipDataToInt16ByteArray (float[] data)
  245. {
  246. MemoryStream dataStream = new MemoryStream ();
  247. int x = sizeof(Int16);
  248. Int16 maxValue = Int16.MaxValue;
  249. int i = 0;
  250. while (i < data.Length) {
  251. dataStream.Write (BitConverter.GetBytes (Convert.ToInt16 (data [i] * maxValue)), 0, x);
  252. ++i;
  253. }
  254. byte[] bytes = dataStream.ToArray ();
  255. // Validate converted bytes
  256. Debug.AssertFormat (data.Length * x == bytes.Length, "Unexpected float[] to Int16 to byte[] size: {0} == {1}", data.Length * x, bytes.Length);
  257. dataStream.Dispose ();
  258. return bytes;
  259. }
  260. private static int WriteBytesToMemoryStream (ref MemoryStream stream, byte[] bytes, string tag = "")
  261. {
  262. int count = bytes.Length;
  263. stream.Write (bytes, 0, count);
  264. //Debug.LogFormat ("WAV:{0} wrote {1} bytes.", tag, count);
  265. return count;
  266. }
  267. #endregion
  268. /// <summary>
  269. /// Calculates the bit depth of an AudioClip
  270. /// </summary>
  271. /// <returns>The bit depth. Should be 8 or 16 or 32 bit.</returns>
  272. /// <param name="audioClip">Audio clip.</param>
  273. public static UInt16 BitDepth (AudioClip audioClip)
  274. {
  275. UInt16 bitDepth = Convert.ToUInt16 (audioClip.samples * audioClip.channels * audioClip.length / audioClip.frequency);
  276. Debug.AssertFormat (bitDepth == 8 || bitDepth == 16 || bitDepth == 32, "Unexpected AudioClip bit depth: {0}. Expected 8 or 16 or 32 bit.", bitDepth);
  277. return bitDepth;
  278. }
  279. private static int BytesPerSample (UInt16 bitDepth)
  280. {
  281. return bitDepth / 8;
  282. }
  283. private static int BlockSize (UInt16 bitDepth)
  284. {
  285. switch (bitDepth) {
  286. case 32:
  287. return sizeof(Int32); // 32-bit -> 4 bytes (Int32)
  288. case 16:
  289. return sizeof(Int16); // 16-bit -> 2 bytes (Int16)
  290. case 8:
  291. return sizeof(sbyte); // 8-bit -> 1 byte (sbyte)
  292. default:
  293. throw new Exception (bitDepth + " bit depth is not supported.");
  294. }
  295. }
  296. private static string FormatCode (UInt16 code)
  297. {
  298. switch (code) {
  299. case 1:
  300. return "PCM";
  301. case 2:
  302. return "ADPCM";
  303. case 3:
  304. return "IEEE";
  305. case 7:
  306. return "μ-law";
  307. case 65534:
  308. return "WaveFormatExtensable";
  309. default:
  310. Debug.LogWarning ("Unknown wav code format:" + code);
  311. return "";
  312. }
  313. }
  314. }