Skip to content

Instantly share code, notes, and snippets.

@peted70
Created February 17, 2018 10:57
Show Gist options
  • Save peted70/aeb9f26e8b52da357369139f5dbf9100 to your computer and use it in GitHub Desktop.
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
/// <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