CSharpSynth.Synthesis.StreamSynthesizer.ConvertBuffer C# (CSharp) Method

ConvertBuffer() private method

private ConvertBuffer ( float from, byte to ) : void
from float
to byte
return void
        private void ConvertBuffer(float[,] from, byte[] to)
        {
            const int bytesPerSample = 2; //again we assume 16 bit audio
            int channels = from.GetLength(0);
            int bufferSize = from.GetLength(1);

            // Make sure the buffer sizes are correct
               //UnitySynth
            if (!(to.Length == (bufferSize * channels * bytesPerSample)))
               Debug.LogError("Buffer sizes are mismatched.");

            for (int i = 0; i < bufferSize; i++)
            {
                for (int c = 0; c < channels; c++)
                {
                    // Apply master volume
                    float floatSample = from[c, i] * MainVolume;

                    // Clamp the value to the [-1.0..1.0] range
                    floatSample = SynthHelper.Clamp(floatSample, -1.0f, 1.0f);

                    // Convert it to the 16 bit [short.MinValue..short.MaxValue] range
                    short shortSample = (short)(floatSample >= 0.0f ? floatSample * short.MaxValue : floatSample * short.MinValue * -1);

                    // Calculate the right index based on the PCM format of interleaved samples per channel [L-R-L-R]
                    int index = i * channels * bytesPerSample + c * bytesPerSample;

                    // Store the 16 bit sample as two consecutive 8 bit values in the buffer with regard to endian-ness
                    if (!BitConverter.IsLittleEndian)
                    {
                        to[index] = (byte)(shortSample >> 8);
                        to[index + 1] = (byte)shortSample;
                    }
                    else
                    {
                        to[index] = (byte)shortSample;
                        to[index + 1] = (byte)(shortSample >> 8);
                    }
                }
            }
        }

Same methods

StreamSynthesizer::ConvertBuffer ( float from, float to ) : void