Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop Speaking #11

Open
3 tasks
esgraham opened this issue Jan 14, 2020 · 2 comments
Open
3 tasks

Stop Speaking #11

esgraham opened this issue Jan 14, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@esgraham
Copy link
Collaborator

Description

As an application user, I'd like to have stop speaking code on the iOS and Android platform, in order to have an event that stops the speaker from speaking.

Acceptance Criteria

  • Should have a function in the Cognitive Services plugin that implements the stop speaking functionality for Android.
  • Should have a function in the Cognitive Services plugin that implements the stop speaking functionality for iOS.
  • README documentation in the plugin should be updated to include changes to the stopSpeak function and example.
@esgraham esgraham added the enhancement New feature or request label Jan 14, 2020
@rozele
Copy link
Collaborator

rozele commented Jan 22, 2020

FYI - confirmed that the following does not work on Android:

SpeechSynthesizer synthesizer;
...
SpeechConfig config = SpeechConfig.fromSubscription(...);
synthesizer = new SpeechSynthesizer(config);
...
Future<SpeechSynthesisResult> future = synthesizer.SpeakTextAsync(...);
...
future.cancel(true);

@rozele
Copy link
Collaborator

rozele commented Jan 22, 2020

Here is an example where I was playing audio from a PullAudioInputStream for Direct Line Speech:

    private void playAudio(PullAudioOutputStream stream, Runnable callback) {
        final int SAMPLE_RATE = 16000;

        final int BUFFER_SIZE = AudioTrack.getMinBufferSize(
                SAMPLE_RATE,
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT);

        LinkedList<byte[]> buffer = new LinkedList<>();
        AtomicInteger doneFlag = new AtomicInteger(0);

        AsyncTask.execute(() -> {
            byte[] data = new byte[BUFFER_SIZE];
            while (stream.read(data) > 0) {
                synchronized (buffer) {
                    buffer.add(data);
                }
                data = new byte[BUFFER_SIZE];
            }
            doneFlag.getAndIncrement();
        });

        AsyncTask.execute(() -> {
            AudioTrack audioTrack = new AudioTrack(
                    AudioManager.STREAM_MUSIC,
                    SAMPLE_RATE,
                    AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    BUFFER_SIZE,
                    AudioTrack.MODE_STREAM);

            if (audioTrack.getState() == AudioTrack.STATE_INITIALIZED) {
                audioTrack.play();
                byte[] sound = new byte[BUFFER_SIZE];
                while (buffer.size() > 0 || doneFlag.get() == 0) {
                    if (buffer.size() > 0) {
                        audioTrack.write(buffer.peek(), 0, sound.length);
                        synchronized (buffer) {
                            buffer.pop();
                        }
                    }
                }
                audioTrack.stop();
                audioTrack.release();
            }

            if (callback != null) {
                callback.run();
            }
        });
    }

We could add something similar but we'll need to support cancellation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants