convert method

Stream<Map> convert (
  1. Stream<List<int>> audioStream,
  2. {int sampleRate: 16000,
  3. String langCode: 'zh-CN',
  4. MODEL_TYPE modelType: MODEL_TYPE.DEFAULT,
  5. dynamic useEnhanced: false}
)

Implementation

Stream<Map> convert(Stream<List<int>> audioStream,
    {int sampleRate = 16000,
    String langCode = 'zh-CN',
    MODEL_TYPE modelType = MODEL_TYPE.DEFAULT,
    useEnhanced = false}) {
  var controller = StreamController<Map>();
  channel =
      IOWebSocketChannel.connect(wsURL, headers: {"X-NLS-Token": token});

  channel.stream.listen((_resp) {
    print(_resp);
    var resp = jsonDecode(_resp);
    Map header = resp["header"];
    Map payload = resp["payload"];
    if (header["status"] == 20000000 &&
        payload != null &&
        payload["result"] != null) {
      controller.add({
        "transcript": payload["result"],
        "isFinal": header["name"] == "SentenceEnd"
      });
    }
  }, onDone: () {
    controller.close();
  });
  channel.sink.add(getStartPayload(sampleRate: sampleRate));
  // patchStream(audioStream, CHUNK_SIZE).listen((audio) {
  audioStream.listen((audio) {
    channel.sink.add(audio);
  });
  return controller.stream;
}