-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathOperatorExtensions.cs
More file actions
64 lines (58 loc) · 2.87 KB
/
OperatorExtensions.cs
File metadata and controls
64 lines (58 loc) · 2.87 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
62
63
64
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace MultiModalSpeechDetection
{
using System;
using System.Linq;
using Microsoft.Psi;
using Microsoft.Psi.Audio;
using Microsoft.Psi.CognitiveServices.Speech;
/// <summary>
/// Operator extension methods.
/// </summary>
public static class OperatorExtensions
{
// This field is required to run the sample and must be a valid key which may
// be obtained by signing up at https://azure.microsoft.com/en-us/try/cognitive-services/?api=speech-api.
private static string azureSubscriptionKey = string.Empty;
private static string azureRegion = string.Empty; // the region to which the subscription is associated (e.g. "westus")
/// <summary>
/// Define an operator that uses the Azure Speech Recognizer to translate from audio to text.
/// </summary>
/// <param name="audio">Our audio stream.</param>
/// <param name="speechDetector">A stream that indicates whether the user is speaking.</param>
/// <returns>A new producer with the translated speech and time stamp.</returns>
public static IProducer<(string, TimeSpan)> SpeechToText(this IProducer<AudioBuffer> audio, IProducer<bool> speechDetector)
{
var speechRecognizer = new AzureSpeechRecognizer(audio.Out.Pipeline, new AzureSpeechRecognizerConfiguration() { SubscriptionKey = azureSubscriptionKey, Region = azureRegion });
audio.Join(speechDetector).PipeTo(speechRecognizer);
return speechRecognizer.Where(r => r.IsFinal).Select(r => (r.Text, r.Duration.Value));
}
/// <summary>
/// Defines an operator that will hold a signal (i.e. smooth out the signal).
/// </summary>
/// <param name="source">Source of the signal.</param>
/// <param name="threshold">Threshold below which the signal is considered off.</param>
/// <param name="decay">Speed at which signal decays.</param>
/// <param name="deliveryPolicy">An optional delivery policy.</param>
/// <returns>Returns true if signal is on, false otherwise.</returns>
public static IProducer<bool> Hold(this IProducer<double> source, double threshold, double decay = 0.2, DeliveryPolicy<double> deliveryPolicy = null)
{
double maxValue = 0;
return source.Select(
newValue =>
{
if (newValue > maxValue && newValue > threshold)
{
maxValue = newValue;
}
else
{
maxValue = (maxValue * (1 - decay)) + (newValue * decay);
}
return maxValue >= threshold;
},
deliveryPolicy);
}
}
}