From f930dd18744759f529a1655fe00aad8fc126eabf Mon Sep 17 00:00:00 2001 From: David Douglas Date: Tue, 21 Feb 2017 14:41:11 +0000 Subject: [PATCH 1/2] Force mic save as 16-bit .wav --- WavUtility.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/WavUtility.cs b/WavUtility.cs index 0d32f59..ef54694 100644 --- a/WavUtility.cs +++ b/WavUtility.cs @@ -5,7 +5,7 @@ /// /// WAV utility for recording and audio playback functions in Unity. -/// Version: 1.0 beta 1 +/// Version: 1.0 alpha 1 /// /// - Use "ToAudioClip" method for loading wav file / bytes. /// Loads .wav (PCM uncompressed) files at 8,16,24 and 32 bits and converts data to Unity's AudioClip. @@ -19,6 +19,9 @@ public class WavUtility { + // Force save as 16-bit .wav + const int BlockSize_16Bit = 2; + /// /// Load PCM format *.wav audio file (using Unity's Application data path) and convert to AudioClip. /// @@ -195,13 +198,13 @@ public static byte[] FromAudioClip (AudioClip audioClip, out string filepath, bo const int headerSize = 44; // get bit depth - UInt16 bitDepth = BitDepth (audioClip); + UInt16 bitDepth = 16; //BitDepth (audioClip); // NB: Only supports 16 bit - Debug.AssertFormat (bitDepth == 16, "Only converting 16 bit is currently supported. The audio clip data is {0} bit.", bitDepth); + //Debug.AssertFormat (bitDepth == 16, "Only converting 16 bit is currently supported. The audio clip data is {0} bit.", bitDepth); // total file size = 44 bytes for header format and audioClip.samples * factor due to float to Int16 / sbyte conversion - int fileSize = audioClip.samples * BlockSize (bitDepth) + headerSize; + int fileSize = audioClip.samples * BlockSize_16Bit + headerSize; // BlockSize (bitDepth) // chunk descriptor (riff) WriteFileHeader (ref stream, fileSize); @@ -301,7 +304,7 @@ private static int WriteFileData (ref MemoryStream stream, AudioClip audioClip, byte[] id = Encoding.ASCII.GetBytes ("data"); count += WriteBytesToMemoryStream (ref stream, id, "DATA_ID"); - int subchunk2Size = Convert.ToInt32 (audioClip.samples * BlockSize (bitDepth)); + int subchunk2Size = Convert.ToInt32 (audioClip.samples * BlockSize_16Bit); // BlockSize (bitDepth) count += WriteBytesToMemoryStream (ref stream, BitConverter.GetBytes (subchunk2Size), "SAMPLES"); // Validate header From de35be41d2715fc2be07d252073c50e909dc292c Mon Sep 17 00:00:00 2001 From: David Douglas Date: Tue, 7 Mar 2017 16:22:36 +0000 Subject: [PATCH 2/2] Save 16-bit .wav --- WavUtility.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WavUtility.cs b/WavUtility.cs index ef54694..4268e93 100644 --- a/WavUtility.cs +++ b/WavUtility.cs @@ -402,4 +402,4 @@ private static string FormatCode (UInt16 code) } } -} +} \ No newline at end of file