package com.supervision.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
import com.supervision.service.VoiceService;
import com.supervision.util.AsrUtil;
import com.supervision.util.TtsUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Service
@RequiredArgsConstructor
public class VoiceServiceImpl implements VoiceService {

    @Override
    public String voiceToText(MultipartFile file, String sessionId) throws IOException {
        log.info("语音转文字开始");
        TimeInterval timeInterval = DateUtil.timer();
        String transform = AsrUtil.asrTransformByBytes(file.getBytes());
        log.info("语音转文字结束，耗时：{}", timeInterval.interval());
        return transform;
    }

    @Override
    public String voiceToText(byte[] bytes, String sessionId){
        log.info("语音转文字开始");
        TimeInterval timeInterval = DateUtil.timer();
        String transform = AsrUtil.asrTransformByBytes(bytes);
        log.info("语音转文字结束，耗时：{}", timeInterval.interval());
        return transform;
    }


    @Override
    public TtsResultDTO textToVoice(String text) {
        log.info("文字转语音开始text:{}",text);
        TimeInterval timeInterval = DateUtil.timer();
        TtsResultDTO transform = TtsUtil.ttsTransform(text);
        log.info("文字转语音结束，耗时：{}", timeInterval.interval());
        return transform;
    }
}
