package org.example;

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.*;
import org.vosk.LogLevel;
import org.vosk.Recognizer;
import org.vosk.LibVosk;
import org.vosk.Model;

// 识别传入的音频
public class Main {
    public static void main(String[] args) throws IOException, UnsupportedAudioFileException {
        LibVosk.setLogLevel(LogLevel.DEBUG);

        long startTime = System.currentTimeMillis(); // 记录开始时间

        try (Model model = new Model("src\\main\\resources\\model\\vosk-model-cn-0.22");
//        try (Model model = new Model("D:\\Chrome Downloads\\voiceToText\\model\\vosk-model-small-cn-0.22");
             InputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream("src\\main\\resources\\voice\\dhxy.wav")));
             Recognizer recognizer = new Recognizer(model, 16000)) {

            int bytes;
            byte[] b = new byte[4096];
            while ((bytes = ais.read(b)) >= 0) {
                recognizer.acceptWaveForm(b, bytes);
            }

            System.out.println(recognizer.getFinalResult() + System.lineSeparator());
        }catch (Exception e){
            System.out.println("文件读取错误"+e);
        }

        long endTime = System.currentTimeMillis(); // 记录结束时间
        long durationMillis = endTime - startTime; // 计算耗时（毫秒）

        System.out.println("处理总耗时: " + durationMillis + " 毫秒");
    }
}
