Skip to content

Commit

Permalink
.NET 2 support testing (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
laves authored Aug 26, 2023
1 parent 3c90578 commit 04166d3
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 183 deletions.
14 changes: 4 additions & 10 deletions .github/workflows/dotnet-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ jobs:
with:
dotnet-version: 6.0.x

- name: Package restore
run: dotnet restore

- name: Dotnet build micdemo
run: dotnet build -c MicDemo.Release
run: dotnet build -c MicDemo.Release -v n

- name: Dotnet build filedemo
run: dotnet build -c FileDemo.Release
run: dotnet build -c FileDemo.Release -v n

- name: Run Dotnet filedemo
run: dotnet run -c FileDemo.Release --
Expand Down Expand Up @@ -87,14 +84,11 @@ jobs:
with:
submodules: recursive

- name: Package restore
run: dotnet restore

- name: Dotnet build micdemo
run: dotnet build -c MicDemo.Release
run: dotnet build -c MicDemo.Release -v n

- name: Dotnet build filedemo
run: dotnet build -c FileDemo.Release
run: dotnet build -c FileDemo.Release -v n

- name: Run Dotnet filedemo
run: dotnet run -c FileDemo.Release --
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
dotnet-version: [2.0.x, 3.0.x, 3.1.x, 5.0.x, 6.0.x]
dotnet-version: [2.1.x, 3.0.x, 3.1.x, 5.0.x, 6.0.x]
include:
- dotnet-version: 2.0.x
- dotnet-version: 2.1.x
binding-framework: netstandard2.0
test-framework: netcoreapp2.0
test-framework: netcoreapp2.1
- dotnet-version: 3.0.x
binding-framework: netcoreapp3.0
test-framework: netcoreapp3.0
Expand All @@ -66,10 +66,10 @@ jobs:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Build binding
run: dotnet build Picovoice/Picovoice.csproj --framework ${{ matrix.binding-framework }}
run: dotnet build Picovoice/Picovoice.csproj --framework ${{ matrix.binding-framework }} -v n

- name: Test
run: dotnet test --framework ${{ matrix.test-framework }} -v d
run: dotnet test --framework ${{ matrix.test-framework }} -v n

build-self-hosted:
runs-on: ${{ matrix.machine }}
Expand All @@ -87,7 +87,7 @@ jobs:
submodules: recursive

- name: Build binding
run: dotnet build Picovoice/Picovoice.csproj --framework net6.0
run: dotnet build Picovoice/Picovoice.csproj --framework net6.0 -v n

- name: Test
run: dotnet test --framework net6.0 -v d
run: dotnet test --framework net6.0 -v n
87 changes: 45 additions & 42 deletions demo/dotnet/PicovoiceDemo/FileDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ namespace PicovoiceDemo
/// </summary>
public class FileDemo
{
static void WakeWordCallback() => Console.WriteLine("[wake word]");

static void InferenceCallback(Inference inference)
{
if (inference.IsUnderstood)
{
Console.WriteLine("{");
Console.WriteLine($" intent : '{inference.Intent}'");
Console.WriteLine(" slots : {");
foreach (KeyValuePair<string, string> slot in inference.Slots)
Console.WriteLine($" {slot.Key} : '{slot.Value}'");
Console.WriteLine(" }");
Console.WriteLine("}\n");
}
else
{
Console.WriteLine("Didn't understand the command\n");
}
}

/// <summary>
/// Reads through input audio file and prints to the console when it encounters the specified keyword or makes an
Expand Down Expand Up @@ -60,61 +79,45 @@ public static void RunDemo(
float endpointDurationSec,
bool requireEndpoint)
{
static void wakeWordCallback() => Console.WriteLine("[wake word]");

static void inferenceCallback(Inference inference)
{
if (inference.IsUnderstood)
{
Console.WriteLine("{");
Console.WriteLine($" intent : '{inference.Intent}'");
Console.WriteLine(" slots : {");
foreach (KeyValuePair<string, string> slot in inference.Slots)
Console.WriteLine($" {slot.Key} : '{slot.Value}'");
Console.WriteLine(" }");
Console.WriteLine("}\n");
}
else
{
Console.WriteLine("Didn't understand the command\n");
}
}

// init picovoice platform
using Picovoice picovoice = Picovoice.Create(
using (Picovoice picovoice = Picovoice.Create(
accessKey,
keywordPath,
wakeWordCallback,
WakeWordCallback,
contextPath,
inferenceCallback,
InferenceCallback,
porcupineModelPath,
porcupineSensitivity,
rhinoModelPath,
rhinoSensitivity,
endpointDurationSec,
requireEndpoint);

// open and validate wav
using BinaryReader reader = new BinaryReader(File.Open(inputAudioPath, FileMode.Open));
ValidateWavFile(reader, picovoice.SampleRate, 16, out short numChannels);

// read audio and send frames to picovoice
short[] picovoiceFrame = new short[picovoice.FrameLength];
int frameIndex = 0;
while (reader.BaseStream.Position != reader.BaseStream.Length)
requireEndpoint))
{
picovoiceFrame[frameIndex++] = reader.ReadInt16();

if (frameIndex == picovoiceFrame.Length)
// open and validate wav
using (BinaryReader reader = new BinaryReader(File.Open(inputAudioPath, FileMode.Open)))
{
picovoice.Process(picovoiceFrame);
frameIndex = 0;
}
ValidateWavFile(reader, picovoice.SampleRate, 16, out short numChannels);

// skip right channel
if (numChannels == 2)
{
reader.ReadInt16();
// read audio and send frames to picovoice
short[] picovoiceFrame = new short[picovoice.FrameLength];
int frameIndex = 0;
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
picovoiceFrame[frameIndex++] = reader.ReadInt16();

if (frameIndex == picovoiceFrame.Length)
{
picovoice.Process(picovoiceFrame);
frameIndex = 0;
}

// skip right channel
if (numChannels == 2)
{
reader.ReadInt16();
}
}
}
}
}
Expand Down
129 changes: 66 additions & 63 deletions demo/dotnet/PicovoiceDemo/MicDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ namespace PicovoiceDemo
/// </summary>
public class MicDemo
{
static void WakeWordCallback() => Console.WriteLine("[wake word]");

static void InferenceCallback(Inference inference)
{
if (inference.IsUnderstood)
{
Console.WriteLine("{");
Console.WriteLine($" intent : '{inference.Intent}'");
Console.WriteLine(" slots : {");
foreach (KeyValuePair<string, string> slot in inference.Slots)
Console.WriteLine($" {slot.Key} : '{slot.Value}'");
Console.WriteLine(" }");
Console.WriteLine("}\n");
}
else
{
Console.WriteLine("Didn't understand the command\n");
}
}

/// <summary>
/// Reads through input audio file and prints to the console when it encounters the specified keyword or makes an
Expand Down Expand Up @@ -63,87 +82,71 @@ public static void RunDemo(
int audioDeviceIndex,
string outputPath = null)
{
static void wakeWordCallback() => Console.WriteLine("[wake word]");

static void inferenceCallback(Inference inference)
{
if (inference.IsUnderstood)
{
Console.WriteLine("{");
Console.WriteLine($" intent : '{inference.Intent}'");
Console.WriteLine(" slots : {");
foreach (KeyValuePair<string, string> slot in inference.Slots)
Console.WriteLine($" {slot.Key} : '{slot.Value}'");
Console.WriteLine(" }");
Console.WriteLine("}\n");
}
else
{
Console.WriteLine("Didn't understand the command\n");
}
}

// init picovoice platform
using Picovoice picovoice = Picovoice.Create(
using (Picovoice picovoice = Picovoice.Create(
accessKey,
keywordPath,
wakeWordCallback,
WakeWordCallback,
contextPath,
inferenceCallback,
InferenceCallback,
porcupineModelPath,
porcupineSensitivity,
rhinoModelPath,
rhinoSensitivity,
endpointDurationSec,
requireEndpoint);

// create recorder
using PvRecorder recorder = PvRecorder.Create(audioDeviceIndex, picovoice.FrameLength);
Console.WriteLine($"Using device: {recorder.SelectedDevice}");
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
requireEndpoint))
{
e.Cancel = true;
recorder.Stop();
Console.WriteLine("Stopping...");
};

// open stream to output file
BinaryWriter outputFileWriter = null;
int totalSamplesWritten = 0;
if (!string.IsNullOrWhiteSpace(outputPath))
{
outputFileWriter = new BinaryWriter(new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write));
WriteWavHeader(outputFileWriter, 1, 16, recorder.SampleRate, 0);
}
// create recorder
using (PvRecorder recorder = PvRecorder.Create(frameLength: picovoice.FrameLength, deviceIndex: audioDeviceIndex))
{
Console.WriteLine($"Using device: {recorder.SelectedDevice}");
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
recorder.Stop();
Console.WriteLine("Stopping...");
};

recorder.Start();
Console.WriteLine($"Using device: {recorder.SelectedDevice}");
Console.WriteLine("Listening...\n");
// open stream to output file
BinaryWriter outputFileWriter = null;
int totalSamplesWritten = 0;
if (!string.IsNullOrWhiteSpace(outputPath))
{
outputFileWriter = new BinaryWriter(new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write));
WriteWavHeader(outputFileWriter, 1, 16, recorder.SampleRate, 0);
}

while (recorder.IsRecording)
{
short[] frame = recorder.Read();
recorder.Start();
Console.WriteLine($"Using device: {recorder.SelectedDevice}");
Console.WriteLine("Listening...\n");

picovoice.Process(frame);
while (recorder.IsRecording)
{
short[] frame = recorder.Read();

if (outputFileWriter != null)
{
foreach (short sample in frame)
picovoice.Process(frame);

if (outputFileWriter != null)
{
foreach (short sample in frame)
{
outputFileWriter.Write(sample);
}
totalSamplesWritten += frame.Length;
}
Thread.Yield();
}

if (outputFileWriter != null)
{
outputFileWriter.Write(sample);
// write size to header and clean up
WriteWavHeader(outputFileWriter, 1, 16, recorder.SampleRate, totalSamplesWritten);
outputFileWriter.Flush();
outputFileWriter.Dispose();
Console.Write($"Wrote audio to '{outputPath}'");
}
totalSamplesWritten += frame.Length;
}
Thread.Yield();
}

if (outputFileWriter != null)
{
// write size to header and clean up
WriteWavHeader(outputFileWriter, 1, 16, recorder.SampleRate, totalSamplesWritten);
outputFileWriter.Flush();
outputFileWriter.Dispose();
Console.Write($"Wrote audio to '{outputPath}'");
}
}

Expand Down
5 changes: 2 additions & 3 deletions demo/dotnet/PicovoiceDemo/PicovoiceDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -19,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PvRecorder" Version="1.2.1" />
<PackageReference Include="Picovoice" Version="2.2.1" />
<PackageReference Include="PvRecorder" Version="1.2.3" />
<PackageReference Include="Picovoice" Version="2.2.2" />
</ItemGroup>
</Project>
Loading

0 comments on commit 04166d3

Please sign in to comment.