webapp/src/libs/semantic-kernel/SKSpeech.ts

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

// Copyright (c) Microsoft. All rights reserved.
import * as speechSdk from 'microsoft-cognitiveservices-speech-sdk';

interface TokenResponse {
    token: string;
    region: string;
    isSuccess: boolean;
}

interface SpeechServiceRequest {
    commandPath: string;
    method?: string;
}

export class SKSpeechService {
    constructor(private readonly serviceUrl: string) {}

    validSpeechKeyAsync = async () => {
        const response = await this.invokeTokenAsync();
        return response;
    };

    getSpeechRecognizerAsyncWithValidKey = async (response: TokenResponse) => {
        const { token, region, isSuccess } = response;

        if(!isSuccess)
        {
            return;
        }

        return this.generateSpeechRecognizer(token, region);
    };

    getSpeechRecognizerAsync = async () => {
        const response = await this.invokeTokenAsync();
        return await this.getSpeechRecognizerAsyncWithValidKey(response);
    };

    private generateSpeechRecognizer ( token: string, region: string) {
        const speechConfig = speechSdk.SpeechConfig.fromAuthorizationToken(token, region);
        speechConfig.speechRecognitionLanguage = 'en-US';
        const audioConfig = speechSdk.AudioConfig.fromDefaultMicrophoneInput();
        return new speechSdk.SpeechRecognizer(speechConfig, audioConfig);
    }

    private invokeTokenAsync = async (): Promise<TokenResponse> => {
        const result = await this.getAzureSpeechTokenAsync<TokenResponse>({
            commandPath: `speechToken`,
            method: 'GET',
        });
        return result;
    };

    private readonly getAzureSpeechTokenAsync = async <TokenResponse>(
        request: SpeechServiceRequest,
    ): Promise<TokenResponse> => {
        const { commandPath, method } = request;

        try {
            const requestUrl = new URL(commandPath, this.serviceUrl);
后面还有 21 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。