Understanding the c# Arguments

Asked 2 years ago, Updated 2 years ago, 151 views

Chapter 6-Create the MicrophoneManager class|Microsoft Docs

I am referring to the above document, but the following code
I have a question about private void dictationRecognizer_DictionResult (string text, ConfidenceLevel confidence).
Why is this function used without arguments in StartCapturingAudio() or StopCapturingAudio()?Thank you for your cooperation.

using UnityEngine;
using UnityEngine.Windows.Speech;

public class MicrophoneManager:MonoBehavior {

    // Help to access instance of this object 
    public static MicrophoneManager instance;

    // AudioSource component, providers access to mic 
    private AudioSource audioSource;

    // Flag indicating mic detection 
    private bool microphoneDetected;

    // Component converting speech to text 
    private DictationRecognizerDictationRecognizer;

    private void Awake()
    {
        // Set this class to have similar to singleton 
        instance=this;
    }

    void Start()
    {
        // Use Unity Microphone class to detect devices and setup AudioSource 
        if (Microphone.devices.Length>0)
        {
            Results.instance.SetMicrophoneStatus("Initializing...");
            audioSource=GetComponent<AudioSource>();
            microphoneDetected=true;
        }
        else
        {
            Results.instance.SetMicrophoneStatus("No Microphone detected");
        }
    }

    /// <summary> 
    /// Start microphone capture.Debugging message is delivered to the Results class. 
    /// </summary> 
    public void StartCapturingAudio()
    {
        if(microphoneDetected)
        {
            // Start dictation 
            dictationRecognizer = new dictationRecognizer();
            dictationRecognizer.DictionResult+=DictionRecognizer_DictionResult;
            dictationRecognizer.Start();

            // Update UI with mic status 
            Results.instance.SetMicrophoneStatus("Capturing...");
        }
    }

    /// <summary> 
    /// Stop microphone capture.Debugging message is delivered to the Results class. 
    /// </summary> 
    public void StopCapturingAudio()
    {
        Results.instance.SetMicrophoneStatus("Misleeping");
        Microphone.End(null);
        dictationRecognizer.DictionResult-=DictionRecognizer_DictionResult;
        dictationRecognizer.Dispose();
    }

    /// <summary>
    /// This handler is called every time the dictation detect a pause in the speech. 
    /// Debugging message is delivered to the Results class.
    /// </summary>
    private voidDictionRecognizer_DictionResult (string text, ConfidenceLevel confidence)
    {
        // Update UI with dictation captured
        Results.instance.SetDictionResult(text);

        // Start the coroutine that process the dictation through Azure 
        StartCoroutine (Translator.instance.TranslateWithUnityNetworking(text));
    }
}

c# unity3d azure

2022-09-30 19:29

2 Answers

The left-hand value dictionRecognizer.DictionResult is the event.I'm not using a function, I'm just registering to be called later.
And to subscribe to events in the program, the traditional format

as described in
dictionRecognizer.DictionResult+=newDictionRecognizer.DictionResultDelegate(DictionRecognizer_DictionResult);

Simplified formatting available in C#2.0 and later for

dictionRecognizer.DictionResult+=DictionRecognizer_DictionResult;

is used.When you read the source code, you should keep in mind that the code is the latter, but essentially the former.


2022-09-30 19:29

Why is this function available without arguments in StartCapturingAudio() or StopCapturingAudio()?

Rather than being available, I have registered functions called from the DictionRecognizer class in the DictionResult event handler.

Also, if the DictionRecognizer class notifies the recognized text as follows,

string msg="Recognized text.";
DictationResult?.Invoke(msg, ConfidenceLevel.High);

Multiple functions registered with the event handler are called.

For example,

m_DictionRecognizer=newDictionRecognizer();

m_DictionRecognizer.DictionResult+=(text, confidence)=>
{
      Debug.LogFormat("Diction result1:{0}", text);
};

m_DictionRecognizer.DictionResult+=(text, confidence)=>
{
      Debug.LogFormat("Diction result2:{0}", text);
};

If you register more than one function in an event handler as shown in , the log output is as follows:

Diction result1—Recognized text.
Diction result2—Recognized text.

Why are we doing this?
in the DictionRecognizer class. This is one of the common design patterns to minimize program coupling so that you don't know the calling function.Common Deligate Patterns


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.