Exemplo n.º 1
0
 public void Dispose_InvalidatesHandle([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, byte[] audio)
 {
     Assume.That(() => vad.HasSpeech(audio), Throws.Nothing);
     vad.Dispose();
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <byte[]>(), It.IsAny <ulong>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio), Throws.InstanceOf <ObjectDisposedException>());
 }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        public void HasSpeech1_PassesAllParametersToProcess([Frozen] Mock <IWebRtcDll> libraryMock, [Frozen] IntPtr handle, WebRtcVad vad, short[] audio)
        {
            vad.HasSpeech(audio);
            var expectedLength = ExpectedFrameLength(vad.SampleRate, vad.FrameLength);

            libraryMock.Verify(l => l.Process(handle, (int)vad.SampleRate, audio, expectedLength));
        }
Exemplo n.º 4
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>());
        }
Exemplo n.º 5
0
        public void HasSpeech1_ThrowsOnInvalidHandle([Frozen] Mock <IWebRtcDll> libraryMock, short[] audio)
        {
            libraryMock.Setup(l => l.Create()).Returns(IntPtr.Zero);
            libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <short[]>(), It.IsAny <long>())).Returns(-1);
            var vad = new WebRtcVad(libraryMock.Object);

            Assert.That(() => vad.HasSpeech(audio), Throws.InstanceOf <ObjectDisposedException>());
        }
Exemplo n.º 6
0
        public void HasSpeech1_ReturnsResult([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, short[] audio, bool result)
        {
            var returnCode = Convert.ToInt32(result);

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

            Assert.That(hasSpeech, Is.EqualTo(result));
        }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
0
        IEnumerable <bool> DetectAllFrames(WebRtcVad vad, string filename)
        {
            var frameSize = (int)vad.SampleRate / 1000 * 2 * (int)vad.FrameLength;
            var buffer    = new byte[frameSize];

            using var audio = OpenTestFile(filename);
            for (int i = 0; i < audio.Length - frameSize; i += frameSize)
            {
                audio.Read(buffer, 0, buffer.Length);
                yield return(vad.HasSpeech(buffer));
            }
        }
Exemplo n.º 9
0
 void ReadAllFrames(byte[] allBytes, byte[] buffer)
 {
     using var vad = new WebRtcVad()
           {
               OperatingMode = OperatingMode.VeryAggressive
           };
     for (int i = 0; i < allBytes.Length - buffer.Length; i += buffer.Length)
     {
         Array.Copy(allBytes, i, buffer, 0, buffer.Length);
         vad.HasSpeech(buffer);
     }
 }
Exemplo n.º 10
0
        public void FrameTypeShort()
        {
            using var vad = new WebRtcVad()
                  {
                      FrameLength = FrameLength.Is30ms,
                  };

            var results = DetectAllFrames(vad, "8k-speech.raw", buf =>
            {
                short[] shortbuf = new short[buf.Length / 2];
                Buffer.BlockCopy(buf, 0, shortbuf, 0, buf.Length);
                return(vad.HasSpeech(shortbuf));
            });

            var resultMap = results.Aggregate("", (map, r) => map += (r ? "1" : "0"));

            Assert.That(resultMap.ToString(), Is.EqualTo("011110111111111111111111111100"));
        }
Exemplo n.º 11
0
        public void FrameTypeIntPtr()
        {
            using var vad = new WebRtcVad()
                  {
                      FrameLength = FrameLength.Is30ms,
                  };

            var results = DetectAllFrames(vad, "8k-speech.raw", buf =>
            {
                IntPtr ptrbuf = Marshal.AllocHGlobal(buf.Length);
                Marshal.Copy(buf, 0, ptrbuf, buf.Length);
                var result = vad.HasSpeech(ptrbuf);
                Marshal.FreeHGlobal(ptrbuf);
                return(result);
            });

            var resultMap = results.Aggregate("", (map, r) => map += (r ? "1" : "0"));

            Assert.That(resultMap.ToString(), Is.EqualTo("011110111111111111111111111100"));
        }
Exemplo n.º 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);
 }
Exemplo n.º 13
0
 public void HasSpeech2_ThrowsOnInvalidFrameLength([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, IntPtr audio, SampleRate rate)
 {
     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, (FrameLength)12), Throws.ArgumentException.With.Message.Contains(nameof(FrameLength)));
 }
Exemplo n.º 14
0
 public void HasSpeech1_ThrowsOnInvalidSampleRate([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, IntPtr audio)
 {
     vad.SampleRate = (SampleRate)42;
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <IntPtr>(), It.IsAny <ulong>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio), Throws.ArgumentException.With.Message.Contains(nameof(SampleRate)));
 }
Exemplo n.º 15
0
 public void HasSpeech2_ThrowsOnInvalidSampleRate([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, short[] audio, FrameLength length)
 {
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <short[]>(), It.IsAny <long>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio, (SampleRate)42, length), Throws.ArgumentException.With.Message.Contains(nameof(SampleRate)));
 }
Exemplo n.º 16
0
 IEnumerable <bool> DetectAllFrames(WebRtcVad vad, string filename)
 {
     return(DetectAllFrames(vad, filename, buf => vad.HasSpeech(buf)));
 }
Exemplo n.º 17
0
 public void HasSpeech1_ThrowsOnOtherError([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, short[] audio)
 {
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <short[]>(), It.IsAny <ulong>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio), Throws.InvalidOperationException);
 }
Exemplo n.º 18
0
 public void HasSpeech1_ThrowsOnInvalidFrameLength([Frozen] Mock <IWebRtcDll> libraryMock, WebRtcVad vad, short[] audio)
 {
     vad.FrameLength = (FrameLength)42;
     libraryMock.Setup(l => l.Process(It.IsAny <IntPtr>(), It.IsAny <int>(), It.IsAny <short[]>(), It.IsAny <ulong>())).Returns(-1);
     Assert.That(() => vad.HasSpeech(audio), Throws.ArgumentException.With.Message.Contains(nameof(FrameLength)));
 }