// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
// Copyright (c) (2018-2022) Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2
// Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein.
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
namespace UnityEngine.XR.MagicLeap
{
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Native;
///
/// APIs for the voice service system.
///
public partial class MLVoice
{
///
/// See ml_voice.h for additional comments.
///
public class NativeBindings : MagicLeapNativeBindings
{
///
/// This callback will be invoked whenever a voice intent event is detected.
///
/// Voice intent event.
/// User data as passed to MLVoiceIntentSetCallbacks().
public delegate void OnVoiceEventDelegate(in IntentEventInternal intentEvent, IntPtr data);
///
/// Native callback that is invoked when a data channel receives a message.
///
/// Voice intent event.
/// User data as passed to MLVoiceIntentSetCallbacks().
[AOT.MonoPInvokeCallback(typeof(OnVoiceEventDelegate))]
private static void OnEvent(in IntentEventInternal intentEvent, IntPtr data)
{
IntentEvent newEvent = new IntentEvent();
newEvent.State = intentEvent.State;
newEvent.NoIntentReason = intentEvent.NoIntentReason;
newEvent.EventName = intentEvent.Text;
newEvent.EventID = intentEvent.IntentID;
IntentSlotInternal[] internalSlots = ConvertArray(intentEvent.AppIntentSlots, (int)intentEvent.AppIntentSlotCount);
newEvent.EventSlotsUsed = new System.Collections.Generic.List();
newEvent.EventSlotsUsed.AddRange(internalSlots.Select(slot => new MLVoice.EventSlot(slot.SlotName, slot.SlotValue)));
bool eventSuccessful = intentEvent.IsSuccess;
MLThreadDispatch.ScheduleMain(() =>
{
OnVoiceEventInternal?.Invoke(eventSuccessful, newEvent);
});
}
///
/// A structure containing voice app-intent slot in voice event.
///
[StructLayout(LayoutKind.Sequential)]
public struct IntentSlotInternal
{
///
/// The C string carrying slot name which is UTF-8 and null terminated.
/// And the memory of this variable is managed by system.
///
[MarshalAs(UnmanagedType.LPStr)]
public string SlotName;
///
/// The C string carrying concept value which is UTF-8 and null terminated.
/// And the memory of this variable is managed by system.
///
[MarshalAs(UnmanagedType.LPStr)]
public string SlotValue;
}
///
/// A structure containing voice intent event data.
///
[StructLayout(LayoutKind.Sequential)]
public struct IntentEventInternal
{
///
/// Voice event handling result in the voice intent event.
/// In case of false, other member variables should be ignored. (text will be NULL)
///
[MarshalAs(UnmanagedType.I1)]
public bool IsSuccess;
///
/// Voice state when generating the voice intent event.
///
public State State;
///
/// If intent is not detected, it contains the reason,
/// otherwise the value is MLVoiceIntentNoIntentReason.NoReason.
///
public NoIntentReason NoIntentReason;
///
/// The string carrying identifier data which is UTF-8 and null terminated.
/// And the memory of this variable is managed by system.
///
[MarshalAs(UnmanagedType.LPStr)]
public string Text;
///
/// User defined intent index which is detected.
///
public uint IntentID;
///
/// Array of app-intent slot, array count is set in app_intent_slot_count.
/// Count of an array app_intent_slots.
///
public IntPtr AppIntentSlots;
///
/// Count of an array AppIntentSlots.
///
public uint AppIntentSlotCount;
};
///
/// A structure containing callbacks for voice intent events.
/// The final parameter to all the callbacks is a IntPtr, which will point to
/// whatever payload data the user provides in MLVoiceIntentSetCallbacks().
/// Individual callbacks which are not required by the client can be NULL.
///
/// This structure must be initialized by calling IntentCallbacks.Create()
/// before use.
///
[StructLayout(LayoutKind.Sequential)]
public struct IntentCallbacks
{
///
/// Version of the struct.
///
public uint Version;
///
/// Native OnVoiceEvent.
///
public OnVoiceEventDelegate OnEvent;
///
/// Creates an initialized IntentCallbacks object.
///
/// An initialized IntentCallbacks object.
public static IntentCallbacks Create()
{
IntentCallbacks callbacks = new IntentCallbacks();
callbacks.Version = 2u;
callbacks.OnEvent = MLVoice.NativeBindings.OnEvent;
return callbacks;
}
}
///
/// A structure containing settings for voice app intent.
/// This structure must be initialized by calling IntentSettings.Create() before use.
///
[StructLayout(LayoutKind.Sequential)]
public struct IntentSettings
{
///
/// Version of the struct.
///
public uint Version;
///
/// The C string carrying app intent made by JSON format, which is UTF-8 and null terminated.
/// The app intent should contain pre-defined fields, names, and values.
/// Memory of this variable is managed by user.
///
[MarshalAs(UnmanagedType.LPStr)]
public string AppIntent;
///
/// Creates an initialized IntentSettings object.
///
/// An initialized IntentSettings object.
public static IntentSettings Create()
{
IntentSettings settings = new IntentSettings();
settings.Version = 1;
settings.AppIntent = null;
return settings;
}
}
///
/// Converts an unmanged array to a managed array of type T.
///
private static T[] ConvertArray(IntPtr arrayPtr, int count)
{
T[] convertedArray = new T[count];
int tSize = Marshal.SizeOf();
for (int i = 0; i < count; ++i)
{
convertedArray[i] = Marshal.PtrToStructure((arrayPtr + (tSize * i)));
}
return convertedArray;
}
///
/// Creates a voice intent client.
///
/// A handle to the created voice intent client. Only valid if the return value is MLResult.Code.Ok.
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to create the voice intent client due to an invalid input parameter
/// MLResult.Result will be MLResult.Code.Ok if successfully created the voice intent client.
/// MLResult.Result will be MLResult.Code.PermissionDenied if failed to create client due to permission (permission).
///
/// Permissions: VOICE_INPUT
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentCreate(out ulong handle);
///
/// Destroy a previously created voice intent client.
///
/// Handle to the voice intent client.
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to destroy the voice intent client due to an invalid input parameter
/// MLResult.Result will be MLResult.Code.Ok if successfully destroyed the voice intent client.
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentDestroy(ulong handle);
///
/// Sets the settings for voice intent events.
///
/// App needs to call MLVoiceIntentConfigureSettings at least once before it calls MLVoiceIntentStartProcessing.
/// App can call MLVoiceIntentConfigureSettings anytime it needs to update the voice intents.
/// But such call will trigger updating intent grammar inside speech engine, which may result in a short delay (could be a few seconds depending on the device's workload).
/// So it is not recommended to call the function frequently.
///
/// Handle to the voice intent client.
/// Pointer to MLVoiceIntentSettings structure.
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to set settings for voice intent events due to an invalid param.
/// MLResult.Result will be MLResult.Code.Ok if Successfully set settings for voice intent events.
/// MLResult.Result will be MLResult.Code.MLVoiceResult_IntentDisabled if failed to set the settings because required voice intent feature is disabled in system settings.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if the operation failed with an unspecified error.
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentConfigureSettings(ulong handle, in IntentSettings voiceSettings);
///
/// Sets the callbacks for voice intent events.
///
/// Handle to the voice intent client.
/// Pointer to MLVoiceIntentCallbacks structure. Set this to NULL to unregister callbacks.
/// Pointer to user payload data (can be NULL).
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to set callbacks for voice intent events due to an invalid param.
/// MLResult.Result will be MLResult.Code.Ok if Successfully set callbacks for voice intent events.
/// MLResult.Result will be MLResult.Code.MLVoiceResult_IntentDisabled if failed to set the callbacks because required voice intent feature is disabled in system settings.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if the operation failed with an unspecified error.
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentSetCallbacks(ulong handle, in IntentCallbacks callbacks, in IntPtr data);
///
/// Starts voice intent processing. This affects system voice service directly and permanently while system is running.
/// Application should use this function carefully.
/// It is strongly encourage to call MLVoiceIntentStopProcessing when the application is paused to avoid unexpected system behavior.
///
/// Handle to the voice intent client.
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to start processing due to an invalid parameter.
/// MLResult.Result will be MLResult.Code.Ok if Successfully started intent processing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if the operation failed with an unspecified error.
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentStartProcessing(ulong handle);
///
/// Stops voice intent processing. This affects system voice service directly and permanently while system is running.
/// Application should use this function carefully.
/// It is strongly encourage to call this when the application is paused to avoid unexpected system behavior.
///
/// Handle to the voice intent client.
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to stop processing due to an invalid parameter.
/// MLResult.Result will be MLResult.Code.Ok if Successfully stopped intent processing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if the operation failed with an unspecified error.
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentStopProcessing(ulong handle);
///
/// Checks voice intent feature is enabled in the system.
///
/// Handle to the voice intent client.
/// Result whether voice intent feature is enabled.
///
/// MLResult.Result will be MLResult.Code.InvalidParam if failed to check due to an invalid parameter.
/// MLResult.Result will be MLResult.Code.Ok if Successfully checked voice intent feature is enabled.
///
[DllImport(MLVoiceDll, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLVoiceIntentIsEnabled(ulong handle, [MarshalAs(UnmanagedType.I1)] out bool isEnabled);
}
}
}