/// <summary> wav- flac </summary> /// <returns> </returns> public static int Wav2Flac(String wavName, string flacName) { int sampleRate = 0; IAudioSource audioSource = new WAVReader(wavName, null); AudioBuffer buff = new AudioBuffer(audioSource, 0x10000); FlakeWriter flakewriter = new FlakeWriter(flacName, audioSource.PCM); sampleRate = audioSource.PCM.SampleRate; FlakeWriter audioDest = flakewriter; while (audioSource.Read(buff, -1) != 0) { audioDest.Write(buff); } audioDest.Close(); audioDest.Close(); return sampleRate; }
public static String GoogleSpeechRequest(String flacName, int sampleRate) { WebRequest request = WebRequest.Create("https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=ru-RU"); request.Method = "POST"; byte[] byteArray = File.ReadAllBytes(flacName); // Set the ContentType property of the WebRequest. request.ContentType = "audio/x-flac; rate=" + sampleRate; //"16000"; request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; }
{"status":0,"id":"4531050901df65542082eacfebf3bb1b-1","hypotheses":[{"utterance":" ","confidence":0.89697623}]}
[DataContract] public class RecognizedItem { [DataMember] public string utterance; [DataMember] public float confidence; } [DataContract] public class RecognitionResult { [DataMember] public string status; [DataMember] public string id; [DataMember] public RecognizedItem[] hypotheses; } public static RecognitionResult Parse(String toParse) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RecognitionResult)); MemoryStream stream1 = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(toParse)); RecognitionResult result= (RecognitionResult)ser.ReadObject(stream1); return result; }
Source: https://habr.com/ru/post/117234/
All Articles