From c68205500e6f350fbeed116f80e866fccee1151e Mon Sep 17 00:00:00 2001 From: Ian Lavery Date: Fri, 20 Oct 2023 11:14:10 -0700 Subject: [PATCH] v3.0 dotnet (#674) --- .github/workflows/dotnet-demos.yml | 61 +++++++++++++++++++ .github/workflows/dotnet.yml | 52 ++++++++++++++++ .../dotnet/PicovoiceDemo/PicovoiceDemo.csproj | 2 +- sdk/dotnet/Picovoice/Picovoice.cs | 55 +++++++++++++---- sdk/dotnet/Picovoice/Picovoice.csproj | 6 +- sdk/dotnet/PicovoiceTest/MainTest.cs | 23 ++++++- 6 files changed, 181 insertions(+), 18 deletions(-) diff --git a/.github/workflows/dotnet-demos.yml b/.github/workflows/dotnet-demos.yml index 47662081f..b0b661164 100644 --- a/.github/workflows/dotnet-demos.yml +++ b/.github/workflows/dotnet-demos.yml @@ -29,10 +29,13 @@ jobs: include: - os: ubuntu-latest platform: linux + nuget-path: ~/.nuget/NuGet/local-nuget - os: windows-latest platform: windows + nuget-path: ~/AppData/Roaming/NuGet/local-nuget - os: macos-latest platform: mac + nuget-path: ~/.nuget/NuGet/local-nuget steps: - uses: actions/checkout@v3 @@ -44,6 +47,34 @@ jobs: with: dotnet-version: 6.0.x + # *********** REMOVE AFTER RELEASE ********************** + - name: mkdir NuGet directory + run: mkdir -p ${{ matrix.nuget-path }} + + - name: Add local NuGet source + run: dotnet nuget add source local-nuget + + - name: Pack Porcupine for local ref + run: dotnet pack -c Release + working-directory: resources/porcupine/binding/dotnet + + - name: Copy Porcupine Nuget + run: cp ../../../resources/porcupine/binding/dotnet/Porcupine/bin/Release/*.nupkg ${{ matrix.nuget-path }} + + - name: Pack Rhino for local ref + run: dotnet pack -c Release + working-directory: resources/rhino/binding/dotnet + + - name: Copy Rhino Nuget + run: cp ../../../resources/rhino/binding/dotnet/Rhino/bin/Release/*.nupkg ${{ matrix.nuget-path }} + + - name: Pack Picovoice for local ref + run: dotnet pack ../../../sdk/dotnet/Picovoice/Picovoice.csproj -c Release + + - name: Copy Picovoice Nuget + run: cp ../../../sdk/dotnet/Picovoice/bin/Release/*.nupkg ${{ matrix.nuget-path }} + # ****************************************************** + - name: Dotnet build micdemo run: dotnet build -c MicDemo.Release -v n @@ -84,6 +115,36 @@ jobs: with: submodules: recursive + # *********** REMOVE AFTER RELEASE ********************** + - name: mkdir NuGet directory + run: rm -rf ~/.nuget/NuGet/local-nuget && mkdir -p ~/.nuget/NuGet/local-nuget + continue-on-error: true + + - name: Add local NuGet source + run: dotnet nuget add source local-nuget + continue-on-error: true + + - name: Pack Porcupine for local ref + run: dotnet pack -c Release + working-directory: resources/porcupine/binding/dotnet + + - name: Copy Porcupine Nuget + run: cp ../../../resources/porcupine/binding/dotnet/Porcupine/bin/Release/*.nupkg ~/.nuget/NuGet/local-nuget + + - name: Pack Rhino for local ref + run: dotnet pack -c Release + working-directory: resources/rhino/binding/dotnet + + - name: Copy Rhino Nuget + run: cp ../../../resources/rhino/binding/dotnet/Rhino/bin/Release/*.nupkg ~/.nuget/NuGet/local-nuget + + - name: Pack Picovoice for local ref + run: dotnet pack ../../../sdk/dotnet/Picovoice/Picovoice.csproj -c Release + + - name: Copy Picovoice Nuget + run: cp ../../../sdk/dotnet/Picovoice/bin/Release/*.nupkg ~/.nuget/NuGet/local-nuget + # ****************************************************** + - name: Dotnet build micdemo run: dotnet build -c MicDemo.Release -v n diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 9e831788a..8861c1312 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -53,6 +53,12 @@ jobs: - dotnet-version: 6.0.x binding-framework: net6.0 test-framework: net6.0 + - os: ubuntu-latest + nuget-path: ~/.nuget/NuGet/local-nuget + - os: macos-latest + nuget-path: ~/.nuget/NuGet/local-nuget + - os: windows-latest + nuget-path: ~/AppData/Roaming/NuGet/local-nuget steps: - name: Checkout repository and submodules @@ -65,6 +71,28 @@ jobs: with: dotnet-version: ${{ matrix.dotnet-version }} + # *********** REMOVE AFTER RELEASE ********************** + - name: mkdir NuGet directory + run: mkdir -p ${{ matrix.nuget-path }} + + - name: Add local NuGet source + run: dotnet nuget add source local-nuget + + - name: Pack Porcupine for local ref + run: dotnet pack -c Release + working-directory: resources/porcupine/binding/dotnet + + - name: Copy Porcupine Nuget + run: cp ../../resources/porcupine/binding/dotnet/Porcupine/bin/Release/*.nupkg ${{ matrix.nuget-path }} + + - name: Pack Rhino for local ref + run: dotnet pack -c Release + working-directory: resources/rhino/binding/dotnet + + - name: Copy Rhino Nuget + run: cp ../../resources/rhino/binding/dotnet/Rhino/bin/Release/*.nupkg ${{ matrix.nuget-path }} + # ****************************************************** + - name: Build binding run: dotnet build Picovoice/Picovoice.csproj --framework ${{ matrix.binding-framework }} -v n @@ -86,6 +114,30 @@ jobs: with: submodules: recursive + # *********** REMOVE AFTER RELEASE ********************** + - name: mkdir NuGet directory + run: rm -rf ~/.nuget/NuGet/local-nuget && mkdir -p ~/.nuget/NuGet/local-nuget + continue-on-error: true + + - name: Add local NuGet source + run: dotnet nuget add source local-nuget + continue-on-error: true + + - name: Pack Porcupine for local ref + run: dotnet pack -c Release + working-directory: resources/porcupine/binding/dotnet + + - name: Copy Porcupine Nuget + run: cp ../../resources/porcupine/binding/dotnet/Porcupine/bin/Release/*.nupkg ~/.nuget/NuGet/local-nuget + + - name: Pack Rhino for local ref + run: dotnet pack -c Release + working-directory: resources/rhino/binding/dotnet + + - name: Copy Rhino Nuget + run: cp ../../resources/rhino/binding/dotnet/Rhino/bin/Release/*.nupkg ~/.nuget/NuGet/local-nuget + # ****************************************************** + - name: Build binding run: dotnet build Picovoice/Picovoice.csproj --framework net6.0 -v n diff --git a/demo/dotnet/PicovoiceDemo/PicovoiceDemo.csproj b/demo/dotnet/PicovoiceDemo/PicovoiceDemo.csproj index fe41cbdaa..b0b135ef0 100644 --- a/demo/dotnet/PicovoiceDemo/PicovoiceDemo.csproj +++ b/demo/dotnet/PicovoiceDemo/PicovoiceDemo.csproj @@ -19,6 +19,6 @@ - + diff --git a/sdk/dotnet/Picovoice/Picovoice.cs b/sdk/dotnet/Picovoice/Picovoice.cs index 353386c75..df8567442 100644 --- a/sdk/dotnet/Picovoice/Picovoice.cs +++ b/sdk/dotnet/Picovoice/Picovoice.cs @@ -203,21 +203,50 @@ public void Process(short[] pcm) throw new PicovoiceInvalidStateException("Cannot process frame - resources have been released."); } - if (!_isWakeWordDetected) - { - _isWakeWordDetected = _porcupine.Process(pcm) == 0; - if (_isWakeWordDetected) - _wakeWordCallback.Invoke(); - } - else + try { - bool isFinalized = _rhino.Process(pcm); - if (isFinalized) + if (!_isWakeWordDetected) { - _isWakeWordDetected = false; - Inference inference = _rhino.GetInference(); - _inferenceCallback.Invoke(inference); + _isWakeWordDetected = _porcupine.Process(pcm) == 0; + if (_isWakeWordDetected) + _wakeWordCallback.Invoke(); } + else + { + bool isFinalized = _rhino.Process(pcm); + if (isFinalized) + { + _isWakeWordDetected = false; + Inference inference = _rhino.GetInference(); + _inferenceCallback.Invoke(inference); + } + } + } + catch (Exception ex) + { + throw MapToPicovoiceException(ex); + } + } + + /// + /// Resets the internal state of Picovoice. It should be called before processing a new stream of audio + /// or when Picovoice was stopped while processing a stream of audio. + /// + public void Reset() + { + if (_porcupine == null || _rhino == null) + { + throw new PicovoiceInvalidStateException("Cannot process frame - resources have been released."); + } + + try + { + _isWakeWordDetected = false; + _rhino.Reset(); + } + catch (Exception ex) + { + throw MapToPicovoiceException(ex); } } @@ -237,7 +266,7 @@ public void Process(short[] pcm) /// Gets the version number of the Picovoice platform /// /// Version of Picovoice - public string Version => "2.1.0"; + public string Version => "3.0.0"; /// /// Get the version of the Porcupine library diff --git a/sdk/dotnet/Picovoice/Picovoice.csproj b/sdk/dotnet/Picovoice/Picovoice.csproj index f811d2aa0..3db73cdb8 100644 --- a/sdk/dotnet/Picovoice/Picovoice.csproj +++ b/sdk/dotnet/Picovoice/Picovoice.csproj @@ -1,7 +1,7 @@  net6.0;netcoreapp3.0;netstandard2.0 - 2.2.2 + 3.0.0 Picovoice Picovoice End-to-End Platform @@ -35,10 +35,10 @@ - + analyzers - + analyzers diff --git a/sdk/dotnet/PicovoiceTest/MainTest.cs b/sdk/dotnet/PicovoiceTest/MainTest.cs index 22593c8e5..7065cae7d 100644 --- a/sdk/dotnet/PicovoiceTest/MainTest.cs +++ b/sdk/dotnet/PicovoiceTest/MainTest.cs @@ -150,7 +150,7 @@ public void TestClean() _picovoice?.Dispose(); } - public void RunTestCase(string audioFileName, string expectedIntent, Dictionary expectedSlots) + public void ProcessFileHelper(string audioFileName) { string testAudioPath = Path.Combine(ROOT_DIR, "resources/audio_samples/", audioFileName); @@ -166,6 +166,11 @@ public void RunTestCase(string audioFileName, string expectedIntent, Dictionary< _picovoice.Process(frame.ToArray()); } + } + + public void RunTestCase(string audioFileName, string expectedIntent, Dictionary expectedSlots) + { + ProcessFileHelper(audioFileName); Assert.IsTrue(_isWakeWordDetected); Assert.AreEqual(_inference.Intent, expectedIntent); @@ -175,6 +180,22 @@ public void RunTestCase(string audioFileName, string expectedIntent, Dictionary< expectedSlots[keyValuePair.Key] == keyValuePair.Value)); } + [TestMethod] + public void TestReset() + { + _picovoice = Picovoice.Create( + _accessKey, + GetKeywordPath("en", "picovoice"), + () => _picovoice.Reset(), + GetContextPath("en", "coffee_maker"), + _inferenceCallback); + + _inference = null; + ProcessFileHelper("picovoice-coffee.wav"); + + Assert.IsNull(_inference); + } + [TestMethod] [DynamicData(nameof(ParametersTestData))] public void TestTwice(