Пример #1
0
        public async Task RecognizeAsync(string textToRecognize, InputStream audio, Action onRecognized)
        {
            using (var stream = new InputStreamWrapper(audio, cancellationToken))
                using (var speech = new SpeechRecognitionEngine(culture)
                {
                    InitialSilenceTimeout = TimeSpan.Zero,
                    EndSilenceTimeout = TimeSpan.Zero,
                    EndSilenceTimeoutAmbiguous = TimeSpan.Zero
                })
                {
                    var gb = new GrammarBuilder(textToRecognize)
                    {
                        Culture = culture
                    };
                    speech.SetInputToAudioStream(stream, new SpeechAudioFormatInfo(48000, AudioBitsPerSample.Sixteen, AudioChannel.Stereo));
                    speech.LoadGrammar(new Grammar(gb));

                    bool finished = false;

                    void OnRecognized()
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            speech.RecognizeAsyncStop();
                            finished = true;
                        }
                        onRecognized();
                    }

                    speech.SpeechRecognitionRejected += (obj, args) =>
                    {
                        if (args.Result == null)
                        {
                            return;
                        }
                        if (!(args.Result.Text == textToRecognize && args.Result.Confidence > ConfidenceThreshold))
                        {
                            return;
                        }
                        OnRecognized();
                    };
                    speech.SpeechRecognized += (obj, args) =>
                    {
                        if (args.Result == null || args.Result.Confidence < ConfidenceThreshold)
                        {
                            return;
                        }
                        OnRecognized();
                    };

                    speech.RecognizeAsync(RecognizeMode.Multiple);
                    await Task.Run(() => { while (!finished)
                                           {
                                               Thread.Sleep(100);
                                           }
                                   }).ConfigureAwait(false);
                }
        }
Пример #2
0
    public static Stream OpenFileStream(string filename, int offset = 0)
    {
        var stream = new InputStreamWrapper(AssetManager.Call <AndroidJavaObject>("open", GetAssetFilePath(filename)));

        if (offset > 0)
        {
            stream.Seek(offset, SeekOrigin.Begin);
        }
        return(stream);
    }
Пример #3
0
        public static string ConvertText(string javaText, JavaConversionOptions options = null)
        {
            if (options == null)
            {
                options = new JavaConversionOptions();
            }

            options.ConversionStateChanged(ConversionState.Starting);

            var context = new ConversionContext(options);

            var textBytes = Encoding.UTF8.GetBytes(javaText ?? string.Empty);

            using (var stringreader = new MemoryStream(textBytes))
                using (var wrapper = new InputStreamWrapper(stringreader))
                {
                    options.ConversionStateChanged(ConversionState.ParsingJavaAST);

                    var parsed = JavaParser.parse(wrapper);

                    options.ConversionStateChanged(ConversionState.BuildingCSharpAST);

                    var types   = parsed.getTypes().ToList <TypeDeclaration>();
                    var imports = parsed.getImports().ToList <ImportDeclaration>();
                    var package = parsed.getPackage();

                    var usings = new List <UsingDirectiveSyntax>();

                    //foreach (var import in imports)
                    //{
                    //    var usingSyntax = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(import.getName().toString()));
                    //    usings.Add(usingSyntax);
                    //}

                    if (options.IncludeUsings)
                    {
                        foreach (var ns in options.Usings)
                        {
                            var usingSyntax = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(ns));
                            usings.Add(usingSyntax);
                        }
                    }

                    var rootMembers = new List <MemberDeclarationSyntax>();
                    NamespaceDeclarationSyntax namespaceSyntax = null;

                    if (options.IncludeNamespace && package != null)
                    {
                        string packageName = package.getName().toString();

                        foreach (var packageReplacement in options.PackageReplacements)
                        {
                            packageName = packageReplacement.Replace(packageName);
                        }

                        packageName = TypeHelper.Capitalize(packageName);

                        namespaceSyntax = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(packageName));
                    }

                    foreach (var type in types)
                    {
                        if (type is ClassOrInterfaceDeclaration)
                        {
                            var classOrIntType = type as ClassOrInterfaceDeclaration;

                            if (classOrIntType.isInterface())
                            {
                                var interfaceSyntax = VisitInterfaceDeclaration(context, classOrIntType, false);

                                if (options.IncludeNamespace)
                                {
                                    namespaceSyntax = namespaceSyntax.AddMembers(interfaceSyntax);
                                }
                                else
                                {
                                    rootMembers.Add(interfaceSyntax);
                                }
                            }
                            else
                            {
                                var classSyntax = VisitClassDeclaration(context, classOrIntType, false);

                                if (options.IncludeNamespace && namespaceSyntax != null)
                                {
                                    namespaceSyntax = namespaceSyntax.AddMembers(classSyntax);
                                }
                                else
                                {
                                    rootMembers.Add(classSyntax);
                                }
                            }
                        }
                    }

                    if (options.IncludeNamespace && namespaceSyntax != null)
                    {
                        rootMembers.Add(namespaceSyntax);
                    }

                    var root = SyntaxFactory.CompilationUnit(
                        externs: SyntaxFactory.List <ExternAliasDirectiveSyntax>(),
                        usings: SyntaxFactory.List(usings.ToArray()),
                        attributeLists: SyntaxFactory.List <AttributeListSyntax>(),
                        members: SyntaxFactory.List <MemberDeclarationSyntax>(rootMembers))
                               .NormalizeWhitespace();

                    var tree = CSharpSyntaxTree.Create(root);

                    options.ConversionStateChanged(ConversionState.Done);

                    return(tree.GetText().ToString());
                }
        }