v2 translation hypothesis, conditionally import audioWorkerUrl method using import.meta (#735)

This commit is contained in:
Glenn Harper 2023-09-25 10:42:19 -07:00 коммит произвёл GitHub
Родитель f468e86f49
Коммит 8cb9e719f9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
33 изменённых файлов: 54 добавлений и 140 удалений

Просмотреть файл

@ -2,7 +2,6 @@
// Licensed under the MIT license.
import { RiffPcmEncoder, Stream } from "../common/Exports";
import { getAudioWorkerUrl } from "./AudioWorkerUrl";
import { IRecorder } from "./IRecorder";
export class PcmRecorder implements IRecorder {
@ -91,7 +90,10 @@ export class PcmRecorder implements IRecorder {
const skipAudioWorklet = !!this.privSpeechProcessorScript && this.privSpeechProcessorScript.toLowerCase() === "ignore";
if (!!context.audioWorklet && !skipAudioWorklet) {
this.privSpeechProcessorScript = getAudioWorkerUrl();
/* eslint-disable-next-line */
const audioUrl = require("./AudioWorkerUrl");
/* eslint-disable-next-line */
this.privSpeechProcessorScript = audioUrl.getAudioWorkerUrl();
context.audioWorklet
.addModule(this.privSpeechProcessorScript)

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { Contracts } from "../../sdk/Contracts";
import { ITranslations } from "../Exports";
import { TranslationStatus } from "../TranslationStatus";
@ -15,13 +16,21 @@ export interface ITranslationHypothesis {
export class TranslationHypothesis implements ITranslationHypothesis {
private privTranslationHypothesis: ITranslationHypothesis;
private constructor(json: string) {
this.privTranslationHypothesis = JSON.parse(json) as ITranslationHypothesis;
private constructor(hypothesis: ITranslationHypothesis) {
this.privTranslationHypothesis = hypothesis;
this.privTranslationHypothesis.Translation.TranslationStatus = TranslationStatus[this.privTranslationHypothesis.Translation.TranslationStatus as unknown as keyof typeof TranslationStatus];
}
public static fromJSON(json: string): TranslationHypothesis {
return new TranslationHypothesis(json);
return new TranslationHypothesis(JSON.parse(json) as ITranslationHypothesis);
}
public static fromTranslationResponse(translationHypothesis: { SpeechHypothesis: ITranslationHypothesis }): TranslationHypothesis {
Contracts.throwIfNullOrUndefined(translationHypothesis, "translationHypothesis");
const hypothesis: ITranslationHypothesis = translationHypothesis.SpeechHypothesis;
translationHypothesis.SpeechHypothesis = undefined;
hypothesis.Translation = (translationHypothesis as unknown as ITranslations);
return new TranslationHypothesis(hypothesis);
}
public get Duration(): number {

Просмотреть файл

@ -11,7 +11,7 @@ export interface ITranslationPhrase {
Offset: number;
Duration: number;
Translation?: ITranslations;
Text: string;
Text?: string;
DisplayText?: string;
PrimaryLanguage?: IPrimaryLanguage;
}
@ -52,19 +52,19 @@ export class TranslationPhrase implements ITranslationPhrase {
return this.privTranslationPhrase.Duration;
}
public get Text(): string {
public get Text(): string | undefined {
return this.privTranslationPhrase.Text;
}
public get Language(): string {
public get Language(): string | undefined {
return this.privTranslationPhrase.PrimaryLanguage?.Language;
}
public get Confidence(): string {
public get Confidence(): string | undefined {
return this.privTranslationPhrase.PrimaryLanguage?.Confidence;
}
public get Translation(): ITranslations {
public get Translation(): ITranslations | undefined {
return this.privTranslationPhrase.Translation;
}
}

Просмотреть файл

@ -26,6 +26,7 @@ import {
CancellationErrorCodePropertyName,
ConversationServiceRecognizer,
EnumTranslation,
ITranslationHypothesis,
RecognitionStatus,
SynthesisStatus,
TranslationHypothesis,
@ -160,32 +161,40 @@ export class TranslationServiceRecognizer extends ConversationServiceRecognizer
};
const handleTranslationHypothesis = (hypothesis: TranslationHypothesis, resultProperties: PropertyCollection): void => {
const result: TranslationRecognitionEventArgs = this.fireEventForResult(hypothesis, resultProperties);
this.privRequestSession.onHypothesis(this.privRequestSession.currentTurnAudioOffset + result.offset);
if (!!this.privTranslationRecognizer.recognizing) {
try {
this.privTranslationRecognizer.recognizing(this.privTranslationRecognizer, result);
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
processed = true;
};
if (connectionMessage.messageType === MessageType.Text) {
resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, connectionMessage.textBody);
}
switch (connectionMessage.path.toLowerCase()) {
case "translation.hypothesis":
const result: TranslationRecognitionEventArgs = this.fireEventForResult(TranslationHypothesis.fromJSON(connectionMessage.textBody), resultProps);
this.privRequestSession.onHypothesis(this.privRequestSession.currentTurnAudioOffset + result.offset);
if (!!this.privTranslationRecognizer.recognizing) {
try {
this.privTranslationRecognizer.recognizing(this.privTranslationRecognizer, result);
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
processed = true;
handleTranslationHypothesis(TranslationHypothesis.fromJSON(connectionMessage.textBody), resultProps);
break;
case "translation.response":
const phrase: { SpeechPhrase: ITranslationPhrase } = JSON.parse(connectionMessage.textBody) as { SpeechPhrase: ITranslationPhrase };
if (!!phrase.SpeechPhrase) {
await handleTranslationPhrase(TranslationPhrase.fromTranslationResponse(phrase));
} else {
const hypothesis: { SpeechHypothesis: ITranslationHypothesis } = JSON.parse(connectionMessage.textBody) as { SpeechHypothesis: ITranslationHypothesis };
if (!!hypothesis.SpeechHypothesis) {
handleTranslationHypothesis(TranslationHypothesis.fromTranslationResponse(hypothesis), resultProps);
}
}
break;
case "translation.phrase":

Просмотреть файл

@ -7,9 +7,6 @@ import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary

Просмотреть файл

@ -27,10 +27,6 @@ import { Settings } from "./Settings";
import { closeAsyncObjects, WaitForCondition } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let objsToClose: any[];
const defaultTargetLanguage: string = "de-DE";
@ -488,6 +484,16 @@ describe.each([true, false])("Service based tests", (forceNodeWebSocket: boolean
}
};
r.recognizing = (o: sdk.Recognizer, e: sdk.TranslationRecognitionEventArgs) => {
expect(e.result).not.toBeUndefined();
expect(e.result.text).toContain("what's the");
expect(e.result.properties).not.toBeUndefined();
expect(e.result.properties.getProperty(sdk.PropertyId.SpeechServiceResponse_JsonResult)).not.toBeUndefined();
expect(e.result.translations).not.toBeUndefined();
expect(e.result.translations.languages[0]).toEqual(defaultTargetLanguage);
expect(e.result.translations.get(defaultTargetLanguage)).toContain("Wie ist das");
}
r.recognized = (o: sdk.Recognizer, e: sdk.TranslationRecognitionEventArgs) => {
try {
if (e.result.reason === sdk.ResultReason.TranslatedSpeech) {

Просмотреть файл

@ -22,10 +22,6 @@ import {
import * as fs from "fs";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let objsToClose: any[];
beforeAll(() => {

Просмотреть файл

@ -23,10 +23,6 @@ import { Settings } from "./Settings";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
import { closeAsyncObjects, RepeatingPullStream, WaitForCondition } from "./Utilities";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let objsToClose: any[];
beforeAll(() => {

Просмотреть файл

@ -26,10 +26,6 @@ import {
} from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
// eslint-disable-next-line no-console
const consoleInfo = console.info;

Просмотреть файл

@ -9,10 +9,6 @@ import { closeAsyncObjects, WaitForCondition } from "./Utilities";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll((): void => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -46,10 +46,6 @@ import {
} from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
// eslint-disable-next-line no-console
const consoleInfo = console.info;
const simpleMessageObj = { speak: "This is speech", text: "This is text", type: "message" };

Просмотреть файл

@ -9,10 +9,6 @@ import {
} from "../src/common.speech/Exports";
import { Settings } from "./Settings";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -5,10 +5,6 @@ import * as sdk from "../microsoft.cognitiveservices.speech.sdk";
import { Settings } from "./Settings";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let bufferSize: number;
beforeEach(() => {
// eslint-disable-next-line no-console

Просмотреть файл

@ -18,10 +18,6 @@ import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
import { AudioStreamFormatImpl } from "../src/sdk/Audio/AudioStreamFormat";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let bufferSize: number;
beforeAll(() => {

Просмотреть файл

@ -5,10 +5,6 @@ import * as sdk from "../microsoft.cognitiveservices.speech.sdk";
import { LanguageUnderstandingModelImpl } from "../src/sdk/LanguageUnderstandingModel";
import { Settings } from "./Settings";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -9,9 +9,6 @@ import { Settings } from "../Settings";
import { CreateRepeatingPullStream, WaitForCondition } from "../Utilities";
let objsToClose: any[];
jest.mock("../../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// override inputs, if necessary

Просмотреть файл

@ -11,10 +11,6 @@ import { CreateRepeatingPullStream, WaitForCondition } from "../Utilities";
let objsToClose: any[];
jest.mock("../../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -11,10 +11,6 @@ import { WaitForCondition } from "../Utilities";
let objsToClose: any[];
jest.mock("../../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll((): void => {
// override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -12,10 +12,6 @@ import { WaitForCondition } from "../Utilities";
let objsToClose: any[];
jest.mock("../../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll((): void => {
// override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -13,10 +13,6 @@ import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let objsToClose: any[];
function sleep(milliseconds: number): Promise<any> {

Просмотреть файл

@ -17,10 +17,6 @@ import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let objsToClose: any[];
beforeAll((): void => {

Просмотреть файл

@ -16,10 +16,6 @@ import { Settings } from "./Settings";
let bufferSize: number;
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -15,10 +15,6 @@ import {
} from "../src/sdk/Audio/AudioStreamFormat";
import { Settings } from "./Settings";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
let bufferSize: number;
beforeAll(() => {
// Override inputs, if necessary

Просмотреть файл

@ -14,10 +14,6 @@ let readCount: number;
const targetBytes: number = 4096;
const defaultAudioFormat: AudioStreamFormatImpl = sdk.AudioStreamFormat.getDefaultInputFormat() as AudioStreamFormatImpl;
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeEach(() => {
readCount = 0;
});

Просмотреть файл

@ -19,10 +19,6 @@ import { closeAsyncObjects } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll((): void => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -10,10 +10,6 @@ import {
} from "../src/common.speech/Exports";
import { Settings } from "./Settings";
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -28,11 +28,6 @@ const Canceled: string = "Canceled";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -59,10 +59,6 @@ const Canceled: string = "Canceled";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -22,10 +22,6 @@ import {
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -26,10 +26,6 @@ import { AudioStreamFormatImpl } from "../src/sdk/Audio/AudioStreamFormat";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -19,11 +19,6 @@ import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -20,11 +20,6 @@ import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();

Просмотреть файл

@ -13,9 +13,6 @@ import { closeAsyncObjects } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll((): void => {
// Override inputs, if necessary