Stream audio from mic to IBM Watson SpeechToText Web service using Java SDK

前端 未结 2 1377
我寻月下人不归
我寻月下人不归 2020-12-16 06:04

Trying to send a continuous audio stream from microphone directly to IBM Watson SpeechToText Web service using the Java SDK. One of the examples provided with the distributi

2条回答
  •  生来不讨喜
    2020-12-16 06:39

    The Java SDK has an example and supports this.

    Update your pom.xml with:

     
       com.ibm.watson.developer_cloud
       java-sdk
       3.3.1
     
    

    Here is an example of how to listen to your microphone.

    SpeechToText service = new SpeechToText();
    service.setUsernameAndPassword("", "");
    
    // Signed PCM AudioFormat with 16kHz, 16 bit sample size, mono
    int sampleRate = 16000;
    AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    
    if (!AudioSystem.isLineSupported(info)) {
      System.out.println("Line not supported");
      System.exit(0);
    }
    
    TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);
    line.start();
    
    AudioInputStream audio = new AudioInputStream(line);
    
    RecognizeOptions options = new RecognizeOptions.Builder()
      .continuous(true)
      .interimResults(true)
      .timestamps(true)
      .wordConfidence(true)
      //.inactivityTimeout(5) // use this to stop listening when the speaker pauses, i.e. for 5s
      .contentType(HttpMediaType.AUDIO_RAW + "; rate=" + sampleRate)
      .build();
    
    service.recognizeUsingWebSocket(audio, options, new BaseRecognizeCallback() {
      @Override
      public void onTranscription(SpeechResults speechResults) {
        System.out.println(speechResults);
      }
    });
    
    System.out.println("Listening to your voice for the next 30s...");
    Thread.sleep(30 * 1000);
    
    // closing the WebSockets underlying InputStream will close the WebSocket itself.
    line.stop();
    line.close();
    
    System.out.println("Fin.");
    

提交回复
热议问题