Azure-Samples / cognitive-services-speech-sdk

Sample code for the Microsoft Cognitive Services Speech SDK
MIT License
2.8k stars 1.83k forks source link

[JAVA] please provide synchronous API for SpeechRecognizer #2380

Open RomanValov opened 3 months ago

RomanValov commented 3 months ago

Hey there!

Is your feature request related to a problem? Please describe.

I believe async APIs provided by Speech SDK were heavily influenced by .NET way of doing things. Where developer merely have to invoke a method and .NET will take care of all the threads and thread pool management. Moreover such async methods play nicely with .NET async/await constructs. Things are bit different in Java in two aspects:

1) Java has different constructs and frameworks to run asynchronous code and standard Future doesn't play well with alternatives 2) Java develops are encouraged to manipulate thread pools to run tasks (executors in terms of Java) in a more manually-managed way

As an example if I want to use promise-like functionality in Java I have to use CompletableFuture objects. Unfortunately there is no clean and easy way to bridge from Future to CompletableFuture. And here is a bare minimum bolierplate I have to write to "convert" futures:

var cf = CompletableFuture.supplyAsync(() -> {
    try {
        return reco.recognizeOnceAsync().get();
    } catch (InterruptedException ex) {
        throw new RuntimeException(ex);
    } catch (ExecutionException ex) {
        throw new RuntimeException(ex);
    }
});

Not only it's bad due to unnecessary visual clutter. Basically I have to synchronously wait for original future. And this synchronous wait occupies additional thread from pool. Hence this code hinders the scalability of application. Also resolving original Future inside supplyAsync forces to wrap exceptions (loss of information for catch clauses) and will result in additional visual clutter to handle them properly.

Describe the solution you'd like

Please provide synchronous APIs to SpeechRecognizer functionality and let the developer handle async code on their own. For example here is the code I would have to write with synchronous APIs to do the same:

var cf = CompletableFuture.supplyAsync(() -> reco.recognizeOnce());

Worth noting SpeechSynthesizer class provides both synchronous and asynchronous APIs.

Describe alternatives you've considered Return CompletableFuture s as a result from SpeechRecognizer APIs and provide a way to specify thread pool (executor). I.e. like Java HttpClient does. But CompletableFuture is not the only async construct in Java.

glharper commented 3 months ago

@RomanValov Thank you for using Java Speech SDK, and for writing this feature request up. I've added a work item to our backlog, and we will determine the appropriate priority.

Internal ref. 7356194

github-actions[bot] commented 3 months ago

This item has been open without activity for 19 days. Provide a comment on status and remove "update needed" label.