Beispiel #1
0
 /// <summary>
 /// Appends header specific attributes to <paramref name="attributes"/> dictionary.
 /// </summary>
 /// <param name="attributes">Dictionary to append header specific attributes to.</param>
 internal void AppendHeaderAttributes(Dictionary <string, string> attributes)
 {
     attributes.Add("Frame Type", (ushort)TypeID + ": " + TypeID);
     attributes.Add("Using Phasor File Format", UsePhasorDataFileFormat.ToString());
     attributes.Add("Frame Length", FrameLength.ToString());
     attributes.Add("Packet Number", PacketNumber.ToString());
     attributes.Add("Word Count", WordCount.ToString());
 }
 public void Save(XmlWriter writer)
 {
     writer.WriteElementString("AnimType", AnimType.ToString());
     writer.WriteElementString("AnimIndex", AnimIndex.ToString());
     writer.WriteElementString("FrameLength", FrameLength.ToString());
     writer.WriteElementString("Anim1", Anim1.ToString());
     writer.WriteElementString("Anim2", Anim2.ToString());
     writer.WriteElementString("Anim3", Anim3.ToString());
 }
Beispiel #3
0
        private byte[][] CreateFrames(Bitmap image, FrameLength len)
        {
            //| SFD = 42 | Len  | LastFrame | FrameNum |         Data          |    CS    |
            //     6        2         1         15      50, 100, 500, 1000 байт  4 байта

            byte[][] result = new byte[(int)Math.Ceiling(((image.Width * image.Height + 1) * 2 / (float)len.Value))][];

            var pointer = 0;

            for (var i = 0; i < result.Length; i++)
            {
                result[i]     = new byte[7 + len.Value];
                result[i][0]  = (42 << 2);
                result[i][0] |= (byte)len.Code;
                result[i][1]  = (byte)((i == result.Length - 1 ? 1 : 0) << 7);
                result[i][1] |= (byte)(i >> 8);
                result[i][2]  = (byte)(i & 0xFF);

                var j = 0;
                if (i == 0)
                {
                    result[i][3] = (byte)image.Width;
                    result[i][4] = (byte)image.Height;
                    j            = 1;
                }

                for (; j < len.Value / 2; j++, pointer++)
                {
                    if (pointer >= image.Width * image.Height)
                    {
                        result[i][j * 2 + 3] = 0;
                        result[i][j * 2 + 4] = 0;
                    }
                    else
                    {
                        int y        = pointer / image.Width;
                        int x        = pointer % image.Width;
                        var color    = image.GetPixel(x, y);
                        int lpcColor = ((color.R / 8) << 11) | ((color.G / 4) << 5) | (color.B / 8);

                        result[i][j * 2 + 3] = (byte)(lpcColor >> 8);
                        result[i][j * 2 + 4] = (byte)(lpcColor & 0xFF);
                    }
                }

                var crc = CRC32(result[i], 3 + len.Value);
                result[i][len.Value + 3] = (byte)(crc >> 24);
                result[i][len.Value + 4] = (byte)((crc >> 16) & 0xFF);
                result[i][len.Value + 5] = (byte)((crc >> 8) & 0xFF);
                result[i][len.Value + 6] = (byte)(crc & 0xFF);
            }

            return(result);
        }
        public void FrameLengths(FrameLength length, string expectedMap)
        {
            using var vad = new WebRtcVad()
                  {
                      OperatingMode = OperatingMode.Aggressive,
                      FrameLength   = length
                  };

            var results   = DetectAllFrames(vad, "8k-speech.raw");
            var resultMap = results.Aggregate("", (map, r) => map += (r ? "1" : "0"));

            Console.WriteLine(resultMap);

            Assert.That(resultMap.ToString(), Is.EqualTo(expectedMap));
        }
Beispiel #5
0
        /// <summary>
        /// Appends header specific attributes to <paramref name="attributes"/> dictionary.
        /// </summary>
        /// <param name="attributes">Dictionary to append header specific attributes to.</param>
        internal void AppendHeaderAttributes(Dictionary <string, string> attributes)
        {
            attributes.Add("Frame Type", (ushort)TypeID + ": " + TypeID);

            if (FrameImages != null)
            {
                attributes.Add("Frame Length", FrameImages.BinaryLength.ToString());
                attributes.Add("Frame Images", FrameImages.Count.ToString());
            }
            else
            {
                attributes.Add("Frame Length", FrameLength.ToString());
                attributes.Add("Frame Images", "0");
            }

            attributes.Add("Frame Count", FrameCount.ToString());
            attributes.Add("Sample Count", m_sampleCount.ToString());
            attributes.Add("Status Flags", m_statusFlags.ToString());
            attributes.Add("Is First Frame", IsFirstFrame.ToString());
            attributes.Add("Is Last Frame", IsLastFrame.ToString());
        }
Beispiel #6
0
        /// <summary>
        /// Appends header specific attributes to <paramref name="attributes"/> dictionary.
        /// </summary>
        /// <param name="attributes">Dictionary to append header specific attributes to.</param>
        internal void AppendHeaderAttributes(Dictionary <string, string> attributes)
        {
            attributes.Add("Frame Type", (ushort)TypeID + ": " + TypeID);
            attributes.Add("Frame Length", FrameLength.ToString());
            attributes.Add("Version", Version.ToString());
            attributes.Add("Second of Century", SecondOfCentury.ToString());
            attributes.Add("Fraction of Second", FractionOfSecond.ToString());

            uint timeQualityFlags = (uint)TimeQualityFlags;

            attributes.Add("Time Quality Flags", timeQualityFlags.ToString());

            if (timeQualityFlags > 0)
            {
                attributes.Add("Leap Second State", TimeQualityFlags.ToString());
            }
            else
            {
                attributes.Add("Leap Second State", "No leap second is currently pending");
            }

            attributes.Add("Time Quality Indicator Code", (uint)TimeQualityIndicatorCode + ": " + TimeQualityIndicatorCode);
            attributes.Add("Time Base", Timebase.ToString());
        }
Beispiel #7
0
        public bool LoadSprite(XElement spriteValues, string graphicDirectory, out string reason)
        {
            string filename = null;

            Name = null;
            foreach (var attribute in spriteValues.Attributes())
            {
                //Get the graphic file
                switch (attribute.Name.ToString().ToLower())
                {
                case "file":
                {
                    filename = attribute.Value;
                    break;
                }

                case "name":
                {
                    Name = attribute.Value;
                    break;
                }

                case "stretchmode":
                {
                    if (attribute.Value == "repeat")
                    {
                        stretchMode = StretchMode.REPEAT;
                    }
                    break;
                }
                }
            }
            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(filename))
            {
                reason = "No sprite name or file location.";
                return(false);
            }
            Sprite graphicFile;

            if (ImageCache.Images.Contains(Name))
            {
                graphicFile = new Sprite(Name, ImageCache.Images[Name]);
            }
            else
            {
                string path = Path.Combine(graphicDirectory, filename);
                if (!File.Exists(path))
                {
                    reason = "Graphic File doesn't exist: " + path;
                    return(false);
                }
                graphicFile = new Sprite(Name, Image.FromFile(Path.Combine(graphicDirectory, filename)));
            }

            int frameCount = 0;

            foreach (var element in spriteValues.Elements())
            {
                int    x           = 0;
                int    y           = 0;
                int    width       = 0;
                int    height      = 0;
                int    axisX       = 0;
                int    axisY       = 0;
                string frameLength = null;
                //Process each frame
                foreach (var attribute in element.Attributes())
                {
                    switch (attribute.Name.ToString().ToLower())
                    {
                    case "x":
                    {
                        x = int.Parse(attribute.Value);
                        break;
                    }

                    case "y":
                    {
                        y = int.Parse(attribute.Value);
                        break;
                    }

                    case "width":
                    {
                        width = int.Parse(attribute.Value);
                        break;
                    }

                    case "height":
                    {
                        height = int.Parse(attribute.Value);
                        break;
                    }

                    case "framelength":
                    {
                        frameLength = attribute.Value;
                        break;
                    }

                    case "axisx":
                    {
                        axisX = int.Parse(attribute.Value);
                        break;
                    }

                    case "axisy":
                    {
                        axisY = int.Parse(attribute.Value);
                        break;
                    }
                    }
                }
                var frame = new Sprite(Name + frameCount, graphicFile.Image, x, y, width, height, axisX,
                                       axisY);
                if (stretchMode == StretchMode.REPEAT)
                {
                    frame.WrapMode = ImageAddressing.Wrapping;
                }
                Frames.Add(frame);
                FrameLength.Add(frameLength);
                frameCount++;
            }
            reason = null;
            return(true);
        }
Beispiel #8
0
        public void FrameLength_ValidatesNewValue([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, FrameLength length)
        {
            vad.FrameLength = length;
            var expectedLength = ExpectedFrameLength(vad.SampleRate, length);

            libraryMock.Verify(l => l.ValidRateAndFrameLength((int)vad.SampleRate, expectedLength));
        }
Beispiel #9
0
 public void FrameLength_CanSetNewValue(FrameLength length, WebRtcVad vad)
 {
     vad.FrameLength = length;
     Assert.That(vad.FrameLength, Is.EqualTo(length));
 }
Beispiel #10
0
        public void HasSpeech2_PassesAllParametersToProcess([Frozen] Mock <IWebRtcDll> libraryMock, [Frozen] IntPtr handle, WebRtcVad vad, IntPtr audio, SampleRate rate, FrameLength length)
        {
            vad.HasSpeech(audio, rate, length);
            var expectedLength = ExpectedFrameLength(rate, length);

            libraryMock.Verify(l => l.Process(handle, (int)rate, audio, expectedLength));
        }
Beispiel #11
0
        public void HasSpeech2_ReturnsResult([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, IntPtr audio, SampleRate rate, FrameLength length, bool result)
        {
            var returnCode = Convert.ToInt32(result);

            libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <IntPtr>(), It.IsAny <ulong>())).Returns(returnCode);
            var hasSpeech = vad.HasSpeech(audio, rate, length);

            Assert.That(hasSpeech, Is.EqualTo(result));
        }
Beispiel #12
0
 public void HasSpeech2_ThrowsOnOtherError([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, IntPtr audio, SampleRate rate, FrameLength length)
 {
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <IntPtr>(), It.IsAny <ulong>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio, rate, length), Throws.InvalidOperationException);
 }
Beispiel #13
0
 public void HasSpeech2_ThrowsOnInvalidSampleRate([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, IntPtr audio, FrameLength length)
 {
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <IntPtr>(), It.IsAny <ulong>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio, (SampleRate)42, length), Throws.ArgumentException.With.Message.Contains(nameof(SampleRate)));
 }
Beispiel #14
0
        public void HasSpeech2_ThrowsOnInvalidHandle([Frozen] Mock <IWebRtcDll> libraryMock, IntPtr audio, SampleRate rate, FrameLength length)
        {
            libraryMock.Setup(l => l.Create()).Returns(IntPtr.Zero);
            libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <IntPtr>(), It.IsAny <ulong>())).Returns(-1);
            var vad = new WebRtcVad(libraryMock.Object);

            Assert.That(() => vad.HasSpeech(audio, rate, length), Throws.InstanceOf <ObjectDisposedException>());
        }
Beispiel #15
0
 private ulong ExpectedFrameLength(SampleRate rate, FrameLength length)
 {
     // calculate a number of 16-bit samples
     return((ulong)rate / 1000 * (ulong)length);
 }
Beispiel #16
0
        /// <summary>
        /// Appends header specific attributes to <paramref name="attributes"/> dictionary.
        /// </summary>
        /// <param name="attributes">Dictionary to append header specific attributes to.</param>
        internal void AppendHeaderAttributes(Dictionary <string, string> attributes)
        {
            attributes.Add("Frame Type", (ushort)TypeID + ": " + TypeID);
            attributes.Add("Frame Length", FrameLength.ToString());
            attributes.Add("Header Length", Length.ToString());
            attributes.Add("Payload Length", DataLength.ToString());
            attributes.Add("Second of Century", SecondOfCentury.ToString());
            attributes.Add("Fraction of Second", FractionOfSecond.ToString());

            uint timeQualityFlags = (uint)TimeQualityFlags;

            attributes.Add("Time Quality Flags", timeQualityFlags.ToString());

            if (timeQualityFlags > 0)
            {
                attributes.Add("Leap Second State", TimeQualityFlags.ToString());
            }
            else
            {
                attributes.Add("Leap Second State", "No leap second is currently pending");
            }

            attributes.Add("Time Quality Indicator Code", (uint)TimeQualityIndicatorCode + ": " + TimeQualityIndicatorCode);
            attributes.Add("Time Base", Timebase + (Timebase != Common.Timebase ? " - NON STANDARD" : ""));

            if (m_frameType != IEC61850_90_5.FrameType.DataFrame)
            {
                attributes.Add("Version", Version.ToString());
            }
            else
            {
                attributes.Add("SPDU Length", m_spduLength.ToString());
                attributes.Add("ASDU Payload Length", m_payloadSize.ToString());
                attributes.Add("Packet Number", PacketNumber.ToString());
                attributes.Add("Key ID", m_keyID.ToString("X").PadLeft(8, '0'));
                attributes.Add("Security Algorithm", (byte)m_securityAlgorithm + ": " + m_securityAlgorithm);
                attributes.Add("Signature Algorithm", (byte)m_signatureAlgorithm + ": " + m_signatureAlgorithm);

                if ((object)m_sourceHash != null && m_sourceHash.Length > 0)
                {
                    attributes.Add("Parsed Signature Hash", ByteEncoding.Hexadecimal.GetString(m_sourceHash, ' '));
                }
                else
                {
                    attributes.Add("Parsed Signature Hash", "null");
                }

                if ((object)m_calculatedHash != null && m_calculatedHash.Length > 0)
                {
                    attributes.Add("Calculated Signature Hash", ByteEncoding.Hexadecimal.GetString(m_calculatedHash, ' '));
                }
                else
                {
                    attributes.Add("Calculated Signature Hash", "null");
                }

                attributes.Add("Ignoring Checksum Validation", IgnoreSignatureValidationFailures.ToString());
                attributes.Add("Number of ASDUs", m_asduCount.ToString());
                attributes.Add("ConfigurationRevision", m_configurationRevision.ToString());
                attributes.Add("Simulated Data", m_simulatedData.ToString());
                attributes.Add("Application ID", m_applicationID.ToString());
                attributes.Add("Using ETR Configuration", UseETRConfiguration.ToString());
                attributes.Add("Configuration Guessing Allowed", GuessConfiguration.ToString());
                attributes.Add("Parsing Redundant ASDUs", ParseRedundantASDUs.ToString());
                attributes.Add("Ignoring Signature Validation Errors", IgnoreSignatureValidationFailures.ToString());
                attributes.Add("Ignoring Sample Size Validation Errors", IgnoreSampleSizeValidationFailures.ToString());
                attributes.Add("Selected Angle Format", m_angleFormat.ToString());
            }
        }
Beispiel #17
0
 public static double GetCapacity(EthernetType ethernetType, FrameLength byteFrameLength, bool useMiliSeconds = true)
 {
     return(GetCapacity(ethernetType, (int)byteFrameLength, useMiliSeconds));
 }