Skip to content

Commit 2d2ac0b

Browse files
authored
.NET SDK (#232)
1 parent 3736ace commit 2d2ac0b

26 files changed

+2740
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ lib/** linguist-detectable=false
33
res/** linguist-detectable=false
44

55
binding/android/** linguist-detectable=false
6+
binding/dotnet/CobraTest/** linguist-detectable=false
67
binding/ios/CobraAppTest/** linguist-detectable=false
78
binding/nodejs/** linguist-detectable=false
89
binding/nodejs/src/cobra.ts linguist-detectable=true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: .NET Codestyle
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ main ]
7+
paths:
8+
- 'binding/dotnet/**/*.cs'
9+
- 'demo/dotnet/**/*.cs'
10+
- '.github/workflows/dotnet-codestyle.yml'
11+
pull_request:
12+
branches: [ main, 'v[0-9]+.[0-9]+' ]
13+
paths:
14+
- 'binding/dotnet/**/*.cs'
15+
- 'demo/dotnet/**/*.cs'
16+
- '.github/workflows/dotnet-codestyle.yml'
17+
18+
jobs:
19+
check-dotnet-codestyle:
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
25+
- name: Set up .NET 8.0
26+
uses: actions/setup-dotnet@v3
27+
with:
28+
dotnet-version: 8.0.x
29+
30+
- name: Run Binding Codestyle
31+
run: dotnet format --verify-no-changes
32+
working-directory: binding/dotnet
33+
34+
- name: Run Demo Codestyle
35+
run: dotnet format --verify-no-changes
36+
working-directory: demo/dotnet

.github/workflows/dotnet-demos.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: .NET Demos
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ main ]
7+
paths:
8+
- 'demo/dotnet/**'
9+
- '!demo/dotnet/README.md'
10+
- 'res/audio/**'
11+
- '.github/workflows/dotnet-demos.yml'
12+
pull_request:
13+
branches: [ main, 'v[0-9]+.[0-9]+' ]
14+
paths:
15+
- 'demo/dotnet/**'
16+
- '!demo/dotnet/README.md'
17+
- 'res/audio/**'
18+
- '.github/workflows/dotnet-demos.yml'
19+
20+
defaults:
21+
run:
22+
working-directory: demo/dotnet/CobraDemo
23+
24+
jobs:
25+
build-github-hosted:
26+
runs-on: ${{ matrix.os }}
27+
28+
strategy:
29+
matrix:
30+
os: [ubuntu-latest, windows-latest, macos-latest]
31+
32+
steps:
33+
- uses: actions/checkout@v3
34+
35+
- name: Set up .NET 8.0
36+
uses: actions/setup-dotnet@v3
37+
with:
38+
dotnet-version: 8.0.x
39+
40+
- name: Package restore
41+
run: dotnet restore
42+
43+
- name: Dotnet build mic demo
44+
run: dotnet build -c MicDemo.Release
45+
46+
- name: Dotnet build file demo
47+
run: dotnet build -c FileDemo.Release
48+
49+
- name: Run Dotnet file demo
50+
run: dotnet run -c FileDemo.Release -- --input_audio_path ../../../res/audio/sample.wav --access_key ${{secrets.PV_VALID_ACCESS_KEY}}
51+
52+
build-self-hosted:
53+
runs-on: ${{ matrix.machine }}
54+
55+
strategy:
56+
matrix:
57+
machine: [rpi3-32, rpi3-64, rpi4-32, rpi4-64, rpi5-64, pv-windows-arm64]
58+
59+
steps:
60+
- uses: actions/checkout@v3
61+
62+
- name: Package restore
63+
run: dotnet restore
64+
65+
# ************** REMOVE AFTER RELEASE ********************
66+
- name: Remove local package
67+
if: ${{ matrix.machine != 'pv-windows-arm64' }}
68+
run: rm -rf ~/.nuget/packages/cobra
69+
70+
- name: Remove local package
71+
if: ${{ matrix.machine == 'pv-windows-arm64' }}
72+
run: rm ~/.nuget/packages/cobra -r -force -ErrorAction SilentlyContinue
73+
74+
- name: Build Local Packages
75+
run: dotnet build
76+
working-directory: binding/dotnet/Cobra
77+
78+
- name: Pack Local Packages
79+
run: dotnet pack -c Release
80+
working-directory: binding/dotnet/Cobra
81+
82+
- name: Install Local Packages
83+
run: dotnet add package -s ../../../binding/dotnet/Cobra/bin/Release Cobra
84+
# ********************************************************
85+
86+
- name: Dotnet build mic demo
87+
run: dotnet build -c MicDemo.Release
88+
89+
- name: Dotnet build file demo
90+
run: dotnet build -c FileDemo.Release
91+
92+
- name: Run Dotnet file demo
93+
run: dotnet run -c FileDemo.Release -- --input_audio_path ../../../res/audio/sample.wav --access_key ${{secrets.PV_VALID_ACCESS_KEY}}

.github/workflows/dotnet.yml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
name: .NET
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ main ]
7+
paths:
8+
- 'binding/dotnet/**'
9+
- '!binding/dotnet/README.md'
10+
- 'lib/linux/**'
11+
- 'lib/mac/**'
12+
- 'lib/raspberry-pi/**'
13+
- 'lib/windows/**'
14+
- 'res/audio/**'
15+
- '.github/workflows/dotnet.yml'
16+
pull_request:
17+
branches: [ main, 'v[0-9]+.[0-9]+' ]
18+
paths:
19+
- 'binding/dotnet/**'
20+
- '!binding/dotnet/README.md'
21+
- 'lib/linux/**'
22+
- 'lib/mac/**'
23+
- 'lib/raspberry-pi/**'
24+
- 'lib/windows/**'
25+
- 'res/audio/**'
26+
- '.github/workflows/dotnet.yml'
27+
28+
defaults:
29+
run:
30+
working-directory: binding/dotnet
31+
32+
jobs:
33+
build-github-hosted:
34+
runs-on: ${{ matrix.os }}
35+
env:
36+
ACCESS_KEY: ${{secrets.PV_VALID_ACCESS_KEY}}
37+
38+
strategy:
39+
matrix:
40+
os: [ubuntu-latest, macos-latest, macos-13, windows-latest]
41+
dotnet-version: [2.1.x, 3.0.x, 3.1.x, 5.0.x, 6.0.x, 8.0.x]
42+
include:
43+
- dotnet-version: 2.1.x
44+
binding-framework: netstandard2.0
45+
test-framework: netcoreapp2.1
46+
- dotnet-version: 3.0.x
47+
binding-framework: netcoreapp3.0
48+
test-framework: netcoreapp3.0
49+
- dotnet-version: 3.1.x
50+
binding-framework: netcoreapp3.0
51+
test-framework: netcoreapp3.1
52+
- dotnet-version: 5.0.x
53+
binding-framework: netcoreapp3.0
54+
test-framework: net5.0
55+
- dotnet-version: 6.0.x
56+
binding-framework: net6.0
57+
test-framework: net6.0
58+
- dotnet-version: 8.0.x
59+
binding-framework: net8.0
60+
test-framework: net8.0
61+
exclude:
62+
- os: ubuntu-latest
63+
dotnet-version: 2.1.x
64+
- os: ubuntu-latest
65+
dotnet-version: 3.0.x
66+
- os: ubuntu-latest
67+
dotnet-version: 3.1.x
68+
- os: ubuntu-latest
69+
dotnet-version: 5.0.x
70+
- os: macos-latest
71+
dotnet-version: 2.1.x
72+
- os: macos-latest
73+
dotnet-version: 3.0.x
74+
- os: macos-latest
75+
dotnet-version: 3.1.x
76+
- os: macos-latest
77+
dotnet-version: 5.0.x
78+
- os: macos-latest
79+
dotnet-version: 6.0.x
80+
- os: macos-13
81+
dotnet-version: 8.0.x
82+
83+
steps:
84+
- uses: actions/checkout@v3
85+
86+
- name: Set up .NET
87+
uses: actions/setup-dotnet@v3
88+
with:
89+
dotnet-version: ${{ matrix.dotnet-version }}
90+
91+
- name: Set up .NET (8)
92+
if: ${{ matrix.os == 'ubuntu-latest' && matrix.dotnet-version == '6.0.x' }}
93+
uses: actions/setup-dotnet@v3
94+
with:
95+
dotnet-version: 8.0.x
96+
97+
- name: Build binding
98+
run: dotnet build Cobra/Cobra.csproj --framework ${{ matrix.binding-framework }}
99+
100+
- name: Test
101+
run: dotnet test --framework ${{ matrix.test-framework }} -v n
102+
103+
build-self-hosted:
104+
runs-on: ${{ matrix.machine }}
105+
env:
106+
ACCESS_KEY: ${{secrets.PV_VALID_ACCESS_KEY}}
107+
108+
strategy:
109+
matrix:
110+
machine: [rpi3-32, rpi3-64, rpi4-32, rpi4-64, rpi5-64, pv-windows-arm64]
111+
112+
steps:
113+
- uses: actions/checkout@v3
114+
115+
- name: Build binding
116+
run: dotnet build Cobra/Cobra.csproj --framework net8.0
117+
118+
- name: Test
119+
run: dotnet test --framework net8.0 -v n

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
[![npm](https://img.shields.io/npm/v/@picovoice/cobra-web?label=npm%20%5Bweb%5D)](https://www.npmjs.com/package/@picovoice/cobra-web)
1010
[![CocoaPods](https://img.shields.io/cocoapods/v/Cobra-iOS)](https://cocoapods.org/pods/Cobra-iOS)
1111
[![PyPI](https://img.shields.io/pypi/v/pvcobra)](https://pypi.org/project/pvcobra/)
12+
[![Nuget](https://img.shields.io/nuget/v/Cobra)](https://www.nuget.org/packages/Cobra/)
1213

1314
Made in Vancouver, Canada by [Picovoice](https://picovoice.ai)
1415

@@ -23,6 +24,7 @@ Cobra is a highly-accurate and lightweight voice activity detection (VAD) engine
2324
- [Table of Contents](#table-of-contents)
2425
- [Demos](#demos)
2526
- [Python](#python-demos)
27+
- [.NET](#net-demos)
2628
- [C](#c-demos)
2729
- [Android](#android-demos)
2830
- [iOS](#ios-demos)
@@ -31,6 +33,7 @@ Cobra is a highly-accurate and lightweight voice activity detection (VAD) engine
3133
- [Rust](#rust-demos)
3234
- [SDKs](#sdks)
3335
- [Python](#python)
36+
- [.NET](#net)
3437
- [C](#c)
3538
- [Android](#android)
3639
- [iOS](#ios)
@@ -60,6 +63,17 @@ will start processing the audio input from the microphone in realtime and output
6063

6164
For more information about the Python demos go to [demo/python](demo/python).
6265

66+
### .NET Demos
67+
68+
From [demo/dotnet/CobraDemo](demo/dotnet/CobraDemo) build and run the demo:
69+
70+
```console
71+
dotnet build -c MicDemo.Release
72+
dotnet run -c MicDemo.Release -- --access_key ${ACCESS_KEY}
73+
```
74+
75+
For more information about .NET demos go to [demo/dotnet](demo/dotnet).
76+
6377
### C Demos
6478

6579
Build the demo:
@@ -191,6 +205,50 @@ while True:
191205

192206
Finally, when done be sure to explicitly release the resources using `handle.delete()`.
193207

208+
### .NET
209+
210+
Install the Cobra .NET SDK using NuGet or the dotnet CLI:
211+
212+
```console
213+
dotnet add package Cobra
214+
```
215+
216+
Create instances of the Cobra class:
217+
218+
```csharp
219+
using Pv;
220+
221+
const string accessKey = "${ACCESS_KEY}"; // Obtained from the Picovoice Console (https://console.picovoice.ai/)
222+
223+
Cobra cobra = new Cobra(accessKey);
224+
```
225+
226+
Once instantiated, `cobra` can process audio via its `.Process` method:
227+
228+
```csharp
229+
short[] GetNextAudioFrame()
230+
{
231+
// .. get audioFrame
232+
return audioFrame;
233+
}
234+
235+
while(true)
236+
{
237+
float voiceProbability = cobra.Process(frame.ToArray());
238+
// .. use probability to trigger other functionality
239+
}
240+
```
241+
242+
Cobra will have its resources freed by the garbage collector, but to have resources freed immediately after use,
243+
wrap it in a using statement:
244+
245+
```csharp
246+
using(Cobra cobra = new Cobra(accessKey))
247+
{
248+
// .. Cobra usage here
249+
}
250+
```
251+
194252
### C
195253

196254
[include/pv_cobra.h](include/pv_cobra.h) header file contains relevant information. Build an instance of the object:

0 commit comments

Comments
 (0)