Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uezo committed Mar 21, 2020
0 parents commit 530f45a
Show file tree
Hide file tree
Showing 36 changed files with 3,136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Mm]emoryCaptures/

# Ignore Asset meta data
*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# Explorer
desktop.ini

# Finder
.DS_Store
74 changes: 74 additions & 0 deletions ChatdollKit.Extension/AzureTableStorageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Newtonsoft.Json;


namespace ChatdollKit.Extension
{
public class AzureTableStorageHandler
{
public string StorageURI;
public LogType MinLevel;
private int serialNumber = 0;
private object locker = new object();

public AzureTableStorageHandler(string storageURI, LogType minLevel = LogType.Warning)
{
StorageURI = storageURI;
MinLevel = minLevel;
}

// Log handler
public void HandleLog(string message, string stackTrace, LogType logType)
{
if (logType <= MinLevel)
{
_ = SendLogAsync(message, stackTrace, logType);
}
}

// Send log to Azure Table Service
private async Task SendLogAsync(string message, string stackTrace, LogType logType)
{
// Increment the serial number to make the sequenceId unique
lock (locker)
{
if (serialNumber > 999)
{
serialNumber = 0;
}
else
{
serialNumber++;
}
}

// Build message
var now = DateTime.UtcNow;
var sequenceId = (now.Ticks).ToString() + "." + serialNumber.ToString("D4");
var localTimestamp = now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff");
var logJson = new Dictionary<string, string>()
{
{"PartitionKey", "user1234567890"},
{"RowKey", sequenceId.ToString()},
{"LocalTimestamp", localTimestamp},
{"Level", logType.ToString()},
{"Message", message},
{"StackTrace", stackTrace},
};

// Send request
var request = new HttpRequestMessage(HttpMethod.Post, StorageURI);
request.Headers.Add("Accept", "application/json");
request.Content = new StringContent(JsonConvert.SerializeObject(logJson), Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
await client.SendAsync(request);
}
}
}
}
124 changes: 124 additions & 0 deletions ChatdollKit.Extension/AzureVoiceRequestProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
//using Microsoft.CognitiveServices.Speech;
using ChatdollKit.Dialog;


namespace ChatdollKit.Extension
{
public class AzureVoiceRequestProvider : MonoBehaviour, IRequestProvider
{
// This provides voice request
public RequestType RequestType { get; } = RequestType.Voice;

// Azure configurations
public string ApiKey;
public string Region;
public string Language;

// Dummy for test
public bool UseDummy = false;
public string DummyText = string.Empty;

// Actions for each status
public Func<Request, Context, CancellationToken, Task> OnStartListeningAsync;
public Func<Request, Context, CancellationToken, Task> OnFinishListeningAsync;
public Func<Request, Context, CancellationToken, Task> OnErrorAsync;


private void Start()
{
// Don't remove this method to be able to inactivate this provider
}

// Create request using voice recognition
public async Task<Request> GetRequestAsync(User user, Context context, CancellationToken token)
{
var request = new Request(RequestType, user);

// Listen voice
try
{
// Invoke action before start recognition
await OnStartListeningAsync?.Invoke(request, context, token);

// Recognize speech
request.Text = await RecognizeOnceAsync();
if (request.IsSet())
{
Debug.Log(request.Text);
}
else
{
Debug.LogWarning("No speech recognized");
}
}
catch (TaskCanceledException)
{
Debug.Log("Canceled during recognizing speech");
}
catch (Exception ex)
{
Debug.LogError($"Error occured in recognizing speech: {ex.Message}\n{ex.StackTrace}");
await OnErrorAsync?.Invoke(request, context, token);
}
finally
{
// Invoke action after recognition
await OnFinishListeningAsync?.Invoke(request, context, token);
}

return request;
}

// Speech to Text by Azure
public async Task<string> RecognizeOnceAsync()
{
// For debugging and testing
if (UseDummy)
{
await Task.Delay(1000);
return DummyText;
}

// Declare return value
var recognizedText = string.Empty;

//// Configure Azure STT
//var config = SpeechConfig.FromSubscription(ApiKey, Region);
//config.SpeechRecognitionLanguage = Language;

//// Call speech recognizer
//using (var recognizer = new SpeechRecognizer(config))
//{
// var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

// // Checks result
// if (result.Reason == ResultReason.RecognizedSpeech)
// {
// // Successfully recognized
// recognizedText = result.Text;
// }
// else if (result.Reason == ResultReason.NoMatch)
// {
// // Nothing recognized
// Debug.Log("No speech recognized");
// }
// else if (result.Reason == ResultReason.Canceled)
// {
// // Canceled because error
// var cancellation = CancellationDetails.FromResult(result);
// Debug.LogError($"Speech recognition failed: Reason={cancellation.Reason} Details={cancellation.ErrorDetails}");
// }
// else
// {
// // Unknown error
// Debug.LogError($"Unknown error in speech recognition: {result.Reason.ToString()}");
// }
//}
return recognizedText;
}
}
}
Loading

0 comments on commit 530f45a

Please sign in to comment.