package org.example;

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

public class demo2 {

    private static volatile boolean recording = false; // 控制录音的标志
    private static final String START_KEYWORD = "开始"; // 启动录音的关键词
    private static final String STOP_KEYWORD = "关闭"; // 停止录音的关键词

    public static void main(String[] args) throws IOException {
        LibVosk.setLogLevel(LogLevel.DEBUG);

        // 设置音频格式
        AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

        // 检查是否支持该音频格式
        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("不支持的音频格式");
            System.exit(0);
        }

        // 获取并打开音频输入线
        TargetDataLine microphone;
        try {
            microphone = (TargetDataLine) AudioSystem.getLine(info);
            microphone.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        }

        // 创建Vosk识别器用于检测关键词
        try (Model model = new Model("src\\main\\resources\\model\\vosk-model-cn-0.22");
             Recognizer recognizer = new Recognizer(model, 16000)) {

            microphone.start();
            System.out.println("语言识别模型载入完成...");
            System.out.println("开始监听...");

            byte[] buffer = new byte[4096];
            int bytesRead;

            // 初步监听关键词“开始”
            while (true) {
                bytesRead = microphone.read(buffer, 0, buffer.length);
                if (bytesRead > 0) {
                    recognizer.acceptWaveForm(buffer, bytesRead);
                    String result = recognizer.getPartialResult(); // 获取部分识别结果

                    if (result.contains(START_KEYWORD)) {
                        System.out.println("检测到开始字样，启动录音...");
                        recording = true;
                        startRecording(microphone, model);
                        break; // 退出循环，开始录音
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭麦克风
            if (microphone != null && microphone.isOpen()) {
                microphone.stop();
                microphone.close();
            }
        }
    }

    private static void startRecording(TargetDataLine microphone, Model model) {
        try (Recognizer recognizer = new Recognizer(model, 16000)) {
            System.out.println("开始录音...");

            byte[] buffer = new byte[4096];
            int bytesRead;

            while (recording) { // 进行实际的语音识别
                bytesRead = microphone.read(buffer, 0, buffer.length);
                if (bytesRead > 0) {
                    recognizer.acceptWaveForm(buffer, bytesRead);
                    String result = recognizer.getPartialResult(); // 获取部分识别结果
                    System.out.println(result);

                    if (result.contains(STOP_KEYWORD)) {
                        System.out.println("检测到关闭字样，停止录音...");
                        recording = false; // 设置标志，停止录音
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
