com.alibaba.dashscope.audio.asr.recognition.Recognition Maven / Gradle / Ivy
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.audio.asr.recognition;
import com.alibaba.dashscope.api.SynchronizeFullDuplexApi;
import com.alibaba.dashscope.audio.asr.recognition.timestamp.Sentence;
import com.alibaba.dashscope.common.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.ApiServiceOption;
import com.alibaba.dashscope.protocol.Protocol;
import com.alibaba.dashscope.protocol.StreamingMode;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Emitter;
import io.reactivex.Flowable;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import lombok.Builder;
import lombok.NonNull;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
@Slf4j
public final class Recognition {
SynchronizeFullDuplexApi duplexApi;
private ApiServiceOption serviceOption;
private Emitter audioEmitter;
@SuperBuilder
private static class AsyncCmdBuffer {
@Builder.Default private boolean isStop = false;
private ByteBuffer audioFrame;
}
private final Queue cmdBuffer = new LinkedList<>();
private RecognitionState state = RecognitionState.IDLE;
private AtomicReference stopLatch = new AtomicReference<>(null);
private long startStreamTimeStamp = -1;
private long firstPackageTimeStamp = -1;
@SuperBuilder
private static class RecognitionParamWithStream extends RecognitionParam {
@NonNull private Flowable audioStream;
@Override
public Flowable