forked from Corvych/VKSpeechRecognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechRecognition.cs
100 lines (94 loc) · 4.14 KB
/
SpeechRecognition.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Text.Json;
using System.Net;
namespace SR
{
public class SpeechRecognition
{
private static readonly HttpClient httpClient = new HttpClient();
private static string? token = "YOUR TOKEN HERE";
private static string? servUrl = "";
public static string? taskid = "";
public static string? status = "";
public static string? text = "";
public static bool? start;
public static string? req;
private static void parseResponseServUrl(string response)
{
using JsonDocument doc = JsonDocument.Parse(response);
JsonElement root = doc.RootElement;
var resp = root.GetProperty("response");
servUrl = Convert.ToString(resp.GetProperty("upload_url"));
start = true;
}
private static void parseResponseProcess(string response)
{
using JsonDocument doc = JsonDocument.Parse(response);
JsonElement root = doc.RootElement;
var resp = root.GetProperty("response");
taskid = Convert.ToString(resp.GetProperty("task_id"));
}
private static void parseResponseStatus(string response)
{
using JsonDocument doc = JsonDocument.Parse(response);
JsonElement root = doc.RootElement;
var resp = root.GetProperty("response");
status = Convert.ToString(resp.GetProperty("status"));
}
private static void parseResponseText(string response)
{
using JsonDocument doc = JsonDocument.Parse(response);
JsonElement root = doc.RootElement;
var resp = root.GetProperty("response");
text = Convert.ToString(resp.GetProperty("text"));
}
public static async Task getServUrl()
{
string responseString = httpClient.GetStringAsync($"https://api.vk.com/method/asr.getUploadUrl?access_token={token}&v=5.131").Result;
parseResponseServUrl(responseString);
}
public static async Task startSpeechRecog(string request)
{
string responseString = httpClient.GetStringAsync($"https://api.vk.com/method/asr.process?access_token={token}&audio={request}&model=spontaneous&v=5.131").Result;
parseResponseProcess(responseString);
}
public static async Task uplToVKServ(string fp)
{
start = false;
if (File.Exists(fp))
{
FileStream fs = new FileStream(fp, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
// Generate post objects
Dictionary<string, object> postParameters = new Dictionary<string, object>();
postParameters.Add("file", new FormUpload.FileParameter(data, "file", "multipart/form-data"));
// Create request and receive response
string postURL = servUrl;
string userAgent = "Remember Me";
HttpWebResponse webResponse = FormUpload.MultipartFormDataPost(postURL, userAgent, postParameters);
// Process response
StreamReader responseReader = new StreamReader(webResponse.GetResponseStream());
req = responseReader.ReadToEnd();
webResponse.Close();
}
}
public static async Task getTaskStat()
{
status = "";
while (status != "finished" && taskid != "")
{
string responseString = httpClient.GetStringAsync($"https://api.vk.com/method/asr.checkStatus?access_token={token}&task_id={taskid}&v=5.131").Result;
parseResponseStatus(responseString);
Thread.Sleep(500);
}
if (status == "finished" && taskid != "")
{
string respString = httpClient.GetStringAsync($"https://api.vk.com/method/asr.checkStatus?access_token={token}&task_id={taskid}&v=5.131").Result;
parseResponseText(respString);
Thread.Sleep(500);
}
}
}
}