Last active
October 24, 2021 15:31
-
-
Save anilkay/0f0dce5cd9b7237ab8983bed21b87be5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CognitiveServices.Speech; | |
using Microsoft.CognitiveServices.Speech.Audio; | |
using NAudio.CoreAudioApi; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
async static Task Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
var speechConfig = SpeechConfig.FromSubscription("key", "cokgizliyer"); | |
//await RecognizeFromWav(speechConfig); | |
await RecognizeContinious(speechConfig); | |
} | |
async static Task RecognizeFromWav(SpeechConfig speechConfig) | |
{ | |
using var audioConfig = AudioConfig.FromMicrophoneInput("deviceid"); | |
using var recognizer = new SpeechRecognizer(speechConfig, "tr-TR", audioConfig); | |
int emptyCounter = 0; | |
List<string> utterences = new List<string>(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
try | |
{ | |
var result = await recognizer.RecognizeOnceAsync(); | |
utterences.Add(result.Text); | |
Random random = new Random(); | |
int values = random.Next(1, 10); | |
string resultTrimmed = result.Text.Trim(); | |
if (resultTrimmed.Length < 1) | |
{ | |
emptyCounter++; | |
} | |
if (emptyCounter >= 150) break; | |
//Console.WriteLine($"Offset={result.OffsetInTicks} Text={result.Text}"); | |
Console.Out.WriteLineAsync($"Offset={result.OffsetInTicks} Text={result.Text}"); | |
if (result.Text.ToLower().Contains("dikteyi kapat")) break; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.ToString()); | |
Random random = new Random(); | |
int values = random.Next(1, 199999); | |
await File.WriteAllLinesAsync("transcriptkisa" + values + ".txt", utterences.ToArray()); | |
} | |
} | |
await File.WriteAllLinesAsync("transcriptkisa.txt", utterences.ToArray()); | |
} | |
async static Task RecognizeContinious(SpeechConfig speechConfig) | |
{ | |
using var audioConfig = AudioConfig.FromMicrophoneInput("deviceid"); | |
using var recognizer = new SpeechRecognizer(speechConfig, "tr-TR", audioConfig); | |
SynchronizedCollection<string> utterences = new SynchronizedCollection<string>(); | |
//Asks user for mic input and prints transcription result on screen | |
for (int i = 0; i < 10000; i++) | |
{ | |
bool isRecognitionCancelled = false; | |
try | |
{ | |
await recognizer.StartContinuousRecognitionAsync(); | |
recognizer.Recognized += (s, e) => | |
{ | |
string resultAsText = e.Result.Text; | |
if (!isRecognitionCancelled) | |
{ | |
utterences.Add(resultAsText); | |
} | |
Console.Out.WriteLineAsync($"Offset={e.Result.OffsetInTicks} Text={e.Result.Text}"); | |
if(resultAsText.ToLower().Contains("dikteyi kapat")) | |
{ | |
recognizer.StopContinuousRecognitionAsync(); | |
} | |
}; | |
recognizer.Canceled += (s, e) => | |
{ | |
isRecognitionCancelled = true; | |
Console.Out.WriteLineAsync("Dinleme Sonlandı"); | |
}; | |
recognizer.SessionStopped += (s, e) => | |
{ | |
Console.WriteLine("Dinleme Sonlandı."); | |
Random random = new Random(); | |
if (utterences.Count > 0) | |
{ | |
File.WriteAllLinesAsync("transcript" + random.Next(0, 1000) + ".txt", utterences); | |
utterences.Clear(); | |
} | |
}; | |
} | |
catch (Exception e) | |
{ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment