From 417688fc9fc67e465bc9dbcae7f63f35cc2c9496 Mon Sep 17 00:00:00 2001 From: Waken Vincent <111260358+VotreWaken@users.noreply.github.com> Date: Mon, 6 May 2024 16:53:39 +0300 Subject: [PATCH 1/2] Fix ProtocolsTypes Values --- AudioLibrary/AudioWaveOut/Protocols.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AudioLibrary/AudioWaveOut/Protocols.cs b/AudioLibrary/AudioWaveOut/Protocols.cs index 57509f9..2855727 100644 --- a/AudioLibrary/AudioWaveOut/Protocols.cs +++ b/AudioLibrary/AudioWaveOut/Protocols.cs @@ -77,7 +77,7 @@ public Byte[] ToBytes(Byte[] data) } // Receive_TCP_STX_ETX - public void Receive_LH(Object sender, Byte[] data) + public void Receive_TCP(Object sender, Byte[] data) { lock (m_LockerReceive) { From 7e5c725b8e3368a2c9bb2ebc18d2e7da03388ba4 Mon Sep 17 00:00:00 2001 From: Waken Vincent <111260358+VotreWaken@users.noreply.github.com> Date: Mon, 6 May 2024 16:58:53 +0300 Subject: [PATCH 2/2] Mixer Responsible for mixing bytes Depending on BitsPerSample --- AudioLibrary/AudioWaveOut/Mixer.cs | 269 +++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 AudioLibrary/AudioWaveOut/Mixer.cs diff --git a/AudioLibrary/AudioWaveOut/Mixer.cs b/AudioLibrary/AudioWaveOut/Mixer.cs new file mode 100644 index 0000000..55b4a3e --- /dev/null +++ b/AudioLibrary/AudioWaveOut/Mixer.cs @@ -0,0 +1,269 @@ +//---------------------------------------------------------------------------- +// File Name: Mixer.cs +// +// Description: +// Recorder is responsible for creating WaveIn Headers, their allocation, +// initialization, and releasing WaveIn headers, creating a stream for recording, +// opening WaveIn, defining the format, WaveIn device. +// +// Initializing and opening OpenWaveIn and its starting, stopping and closing +// Responsible for creating and behaving a stream for WaveIn devices, creating a +// playback buffer and copying and playing data from this buffer +// and monitors changes in WaveIn Devices. +// +// +// Author(s): +// Egor Waken +// +// History: +// 06 May 2024 Egor Waken Created. +// +// License: +// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file. +//---------------------------------------------------------------------------- + +namespace AudioWaveOut +{ + // Mixer + public class Mixer + { + // MixBytes + public static List MixBytes(List> listList, int BitsPerSample) + { + // Result + List list16 = new List(); + List list16Abs = new List(); + int maximum = 0; + + // Ready + return MixBytes_Intern(listList, BitsPerSample, out list16, out list16Abs, out maximum); + } + + // MixBytes + public static List MixBytes(List> listList, int BitsPerSample, out List listLinear, out List listLinearAbs, out int maximum) + { + // Ready + return MixBytes_Intern(listList, BitsPerSample, out listLinear, out listLinearAbs, out maximum); + } + + // MixBytes_Intern + private static List MixBytes_Intern(List> listList, int BitsPerSample, out List listLinear, out List listLinearAbs, out int maximum) + { + + // Set default value + listLinear = new List(); + listLinearAbs = new List(); + maximum = 0; + + // Determine maximum number of bytes for mixing + int maxBytesCount = 0; + foreach (List l in listList) + { + if (l.Count > maxBytesCount) + { + maxBytesCount = l.Count; + } + } + + // If data exists + if (listList.Count > 0 && maxBytesCount > 0) + { + + // Depending on BitsPerSample + switch (BitsPerSample) + { + // 8 Bits + case 8: + return MixBytes_8Bit(listList, maxBytesCount, out listLinear, out listLinearAbs, out maximum); + + // 16 Bits + case 16: + return MixBytes_16Bit(listList, maxBytesCount, out listLinear, out listLinearAbs, out maximum); + } + } + + // Mistake + return new List(); + } + + // MixBytes_16Bit + private static List MixBytes_16Bit(List> listList, int maxBytesCount, out List listLinear, out List listLinearAbs, out int maximum) + { + // Result + maximum = 0; + + // Create array with linear and byte values + int linearCount = maxBytesCount / 2; + Int32[] bytesLinear = new Int32[linearCount]; + Int32[] bytesLinearAbs = new Int32[linearCount]; + Byte[] bytesRaw = new Byte[maxBytesCount]; + + // For each byte list + for (int v = 0; v < listList.Count; v++) + { + // Convert to array + Byte[] bytes = listList[v].ToArray(); + + // For every 16bit value + for (int i = 0, a = 0; i < linearCount; i++, a += 2) + { + // If there are values to mix + if (i < bytes.Length && a < bytes.Length - 1) + { + // Determine value + Int16 value16 = BitConverter.ToInt16(bytes, a); + int value32 = bytesLinear[i] + value16; + + // Add value (catch overflows) + if (value32 < Int16.MinValue) + { + value32 = Int16.MinValue; + } + else if (value32 > Int16.MaxValue) + { + value32 = Int16.MaxValue; + } + + // Set values + bytesLinear[i] = value32; + bytesLinearAbs[i] = Math.Abs(value32); + Int16 mixed16 = Convert.ToInt16(value32); + Array.Copy(BitConverter.GetBytes(mixed16), 0, bytesRaw, a, 2); + + // Calculate maximum + if (value32 > maximum) + { + maximum = value32; + } + } + else + { + // Leave silent + } + } + } + + // Out result + listLinear = new List(bytesLinear); + listLinearAbs = new List(bytesLinearAbs); + + // Ready + return new List(bytesRaw); + } + + // MixBytes_8Bit + private static List MixBytes_8Bit(List> listList, int maxBytesCount, out List listLinear, out List listLinearAbs, out int maximum) + { + // Result + maximum = 0; + + // Create array with linear and byte values + int linearCount = maxBytesCount; + Int32[] bytesLinear = new Int32[linearCount]; + Byte[] bytesRaw = new Byte[maxBytesCount]; + + // For each byte list + for (int v = 0; v < listList.Count; v++) + { + // Convert to array + Byte[] bytes = listList[v].ToArray(); + + // For every 8 bit value + for (int i = 0; i < linearCount; i++) + { + // If there are values to mix + if (i < bytes.Length) + { + // Determine value + Byte value8 = bytes[i]; + int value32 = bytesLinear[i] + value8; + + // Add value (catch overflows) + if (value32 < Byte.MinValue) + { + value32 = Byte.MinValue; + } + else if (value32 > Byte.MaxValue) + { + value32 = Byte.MaxValue; + } + + // Set values + bytesLinear[i] = value32; + bytesRaw[i] = BitConverter.GetBytes(value32)[0]; + + // Calculate maximum + if (value32 > maximum) + { + maximum = value32; + } + } + else + { + // Leave silent + } + } + } + + // Out results + listLinear = new List(bytesLinear); + listLinearAbs = new List(bytesLinear); + + // Ready + return new List(bytesRaw); + } + + // SubsctractBytes_16Bit + public static List SubsctractBytes_16Bit(List listSource, List listToSubstract) + { + // Result + List list = new List(listSource.Count); + + // Create array with linear values (16bit) + int value16Count = listSource.Count / 2; + List list16Mixed = new List(new Int16[value16Count]); + + // Convert to array + Byte[] bytesSource = listSource.ToArray(); + Byte[] bytesSubstract = listToSubstract.ToArray(); + + // For every 16bit value + for (int i = 0, a = 0; i < value16Count; i++, a += 2) + { + // If values exist + if (i < bytesSource.Length && a < bytesSource.Length - 1) + { + // Determine values + Int16 value16Source = BitConverter.ToInt16(bytesSource, a); + Int16 value16Substract = BitConverter.ToInt16(bytesSubstract, a); + int value32 = value16Source - value16Substract; + + // Add value (catch overflows) + if (value32 < Int16.MinValue) + { + value32 = Int16.MinValue; + } + else if (value32 > Int16.MaxValue) + { + value32 = Int16.MaxValue; + } + + // Set value + list16Mixed[i] = Convert.ToInt16(value32); + } + } + + // For every value + foreach (Int16 v16 in list16Mixed) + { + // Convert integers to bytes + Byte[] bytes = BitConverter.GetBytes(v16); + list.AddRange(bytes); + + } + + // Ready + return list; + } + } +}