I want to change the bit depth from 16 bits to 8 bits. Here is the conversion code from 32 bit to 16 bit:

var audioBuffer = frame.LockBuffer(AudioBufferAccessMode.Read); var buffer = Windows.Storage.Streams.Buffer.CreateCopyFromMemoryBuffer(audioBuffer); buffer.Length = audioBuffer.Length; using (var dataReader = DataReader.FromBuffer(buffer)) { dataReader.ByteOrder = ByteOrder.LittleEndian; byte[] byteData = new byte[buffer.Length / (4 * 3)]; int pos = 0; Int16 int16Tmp1; Int16 int16Tmp2; Int16 avg = 0; while (dataReader.UnconsumedBufferLength > 0) { for (int i = 0; i < 3; i++) { int16Tmp1 = (Int16)((dataReader.ReadSingle() * Int16.MaxValue) / 2); int16Tmp2 = (Int16)((dataReader.ReadSingle() * Int16.MaxValue) / 2); avg += (Int16)(int16Tmp1 + int16Tmp2); } avg = (Int16)(avg / 3); byte[] chunkBytes = BitConverter.GetBytes(avg); byteData[pos++] = chunkBytes[0]; byteData[pos++] = chunkBytes[1]; } _audioStream.Write(byteData, 0, byteData.Length); } 

Here is an attempt to convert from 32 bit to 8 bit:

  dataReader.ByteOrder = ByteOrder.LittleEndian; byte[] byteData = new byte[buffer.Length / (4 * 3 * 2)]; int pos = 0; sbyte int8Tmp1; sbyte int8Tmp2; sbyte avg = 0; while (dataReader.UnconsumedBufferLength > 0) { for (int i = 0; i < 3; i++) { int8Tmp1 = (sbyte)((dataReader.ReadSingle() * sbyte.MaxValue) / 2); int8Tmp2 = (sbyte)((dataReader.ReadSingle() * sbyte.MaxValue) / 2); avg += (sbyte)(int8Tmp1 + int8Tmp2); } avg = (sbyte)(avg / 3); byte[] chunkBytes = BitConverter.GetBytes(avg); byteData[pos++] = (byte)(avg); //byteData[pos++] = chunkBytes[0]; //byteData[pos++] = chunkBytes[1]; } 

DataReader reads audio frame:

  1. stereo.
  2. 32 bit
  3. 48 kHz

I will convert it to mono with 16 bit and 16 kHz. Well, I at least hope that is correct.

So, what do I need to change to get 8 bit sound?

  • Theoretically, if you need to leave one with two bytes, then having commented out the string byteData [pos ++] = chunkBytes [0]; - the result is relatively correct, but I am still confused by the dataReader.ReadSingle (). - nick_n_a
  • @nick_n_a There's an array of floats, that's why ReadSingle (). I did this: [ msdn.microsoft.com/ru-ru/windows/uwp/audio-video-camera/… And it says: Only 32-bit floating point formats are supported . - Aynur Sibagatullin
  • Plus, it seems to me, your option does not work. I commented out what you said. And the output is only noise. - Aynur Sibagatullin
  • one
    standard wave riff supports integer type (signed byte). If you want to change the bit, then obviously the byte should be 2 times less. And why do you need 8 bit sound? Which method will reproduce it? I processed the sound wave, but at the input I had int16 for 16-bit sound, and int8 for 8-bit. 8-bit float I did not find in the studio. - nick_n_a
  • @nick_n_a 8-bit is no longer a float, just like 16-bit. In 16 bit I have short. It is clear that the byte should be two times less, but when writing only one of the chunkBytes [] (of course, I reduced the length of the byteData), I end up with only interference. No normal sound. So far, I have not reproduced this sound in my application, but only accumulate and save it as .wav. - Aynur Sibagatullin

0