forked from microsoft/psi-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (53 loc) · 2.67 KB
/
Program.cs
File metadata and controls
61 lines (53 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace SimpleVoiceActivityDetector
{
using System;
using System.IO;
using System.Linq;
using Microsoft.Psi;
using Microsoft.Psi.Audio;
/// <summary>
/// Simple voice activity detector sample.
/// </summary>
public static class Program
{
/// <summary>
/// Main entry point.
/// </summary>
public static void Main()
{
using (var p = Pipeline.Create())
{
// Create the microphone, acoustic feature extractor, and connect them
var microphone = new AudioCapture(p, WaveFormat.Create16kHz1Channel16BitPcm());
// Comment out the line above, and uncomment the next two lines to run the application by
// replaying over existing data, rather than live.
////var inputStore = PsiStore.Open(p, "SimpleVAD", Path.Combine(Directory.GetCurrentDirectory(), "Stores", "SimpleVAD.0000"));
////var microphone = inputStore.OpenStream<AudioBuffer>("Audio");
var acousticFeaturesExtractor = new AcousticFeaturesExtractor(p);
microphone.PipeTo(acousticFeaturesExtractor);
// Display the log energy
acousticFeaturesExtractor.LogEnergy
.Sample(TimeSpan.FromSeconds(0.2))
.Do(logEnergy => Console.WriteLine($"LogEnergy = {logEnergy}"));
// Create a voice-activity stream by thresholding the log energy
var vad = acousticFeaturesExtractor.LogEnergy
.Select(l => l > 7);
// Create filtered signal by aggregating over historical buffers
var vadWithHistory = acousticFeaturesExtractor.LogEnergy
.Window(RelativeTimeInterval.Future(TimeSpan.FromMilliseconds(300)))
.Aggregate(false, (previous, buffer) => (!previous && buffer.All(v => v > 7)) || (previous && !buffer.All(v => v < 7)));
// Write the microphone output, VAD streams, and some acoustic features to the store
var store = PsiStore.Create(p, "SimpleVAD", Path.Combine(Directory.GetCurrentDirectory(), "Stores"));
microphone.Write("Audio", store);
vad.Write("VAD", store);
vadWithHistory.Write("VADFiltered", store);
acousticFeaturesExtractor.LogEnergy.Write("LogEnergy", store);
acousticFeaturesExtractor.ZeroCrossingRate.Write("ZeroCrossingRate", store);
p.RunAsync();
Console.ReadKey();
}
}
}
}