//UnitySynth private void ConvertBuffer(float[,] from, float[] to) { const int bytesPerSample = 2; //again we assume 16 bit audio int channels = from.GetLength(0); int bufferSize = from.GetLength(1); int sampleIndex = 0; //UnitySynth if (!(to.Length == bufferSize * channels * bytesPerSample)) { Debug.Log("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); to[sampleIndex++] = floatSample; } } }
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.Log("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); } } } }
private void ConvertBuffer(float[,] from, float[] to) { int channels = from.GetLength(0); int bufferSize = from.GetLength(1); // Make sure the buffer sizes are correct System.Diagnostics.Debug.Assert(to.Length == from.Length, "Buffer sizes are mismatched."); int k = 0; 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 to[k] = SynthHelper.Clamp(floatSample, -1.0f, 1.0f); k++; } } }