Skip to content

Commit

Permalink
Merge pull request #1 from deadlyfingers/force16bit
Browse files Browse the repository at this point in the history
Force16bit
  • Loading branch information
deadlyfingers authored Mar 7, 2017
2 parents 2846545 + de35be4 commit 23e532a
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions WavUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/// <summary>
/// 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.
Expand All @@ -19,6 +19,9 @@

public class WavUtility
{
// Force save as 16-bit .wav
const int BlockSize_16Bit = 2;

/// <summary>
/// Load PCM format *.wav audio file (using Unity's Application data path) and convert to AudioClip.
/// </summary>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -399,4 +402,4 @@ private static string FormatCode (UInt16 code)
}
}

}
}

0 comments on commit 23e532a

Please sign in to comment.