Created
February 17, 2018 10:57
-
-
Save peted70/aeb9f26e8b52da357369139f5dbf9100 to your computer and use it in GitHub Desktop.
Convert floating point audio data in the range -1.0 to 1.0 to signed 16-bit audio data and populate the provided stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Convert floating point audio data in the range -1.0 to 1.0 to signed 16-bit audio data | |
/// and populate the provided stream | |
/// </summary> | |
/// <param name="audioData"></param> | |
/// <param name="stream"></param> | |
/// <returns></returns> | |
int BufferConvertedData(float[] audioData, Stream stream) | |
{ | |
// Can't just do a block copy here as we need to convert from float[-1.0f, 1.0f] to 16bit PCM | |
int i = 0; | |
while (i < audioData.Length) | |
{ | |
stream.Write(BitConverter.GetBytes(Convert.ToInt16(audioData[i] * Int16.MaxValue)), 0, sizeof(Int16)); | |
++i; | |
} | |
return audioData.Length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment