Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/common.browser/PCMRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions src/common.speech/ServiceMessages/TranslationHypothesis.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/common.speech/ServiceMessages/TranslationPhrase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ITranslationPhrase {
Offset: number;
Duration: number;
Translation?: ITranslations;
Text: string;
Text?: string;
DisplayText?: string;
PrimaryLanguage?: IPrimaryLanguage;
}
Expand Down Expand Up @@ -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;
}
}
37 changes: 23 additions & 14 deletions src/common.speech/TranslationServiceRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
CancellationErrorCodePropertyName,
ConversationServiceRecognizer,
EnumTranslation,
ITranslationHypothesis,
RecognitionStatus,
SynthesisStatus,
TranslationHypothesis,
Expand Down Expand Up @@ -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":
Expand Down
3 changes: 0 additions & 3 deletions tests/AudioOutputStreamTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions tests/AutoSourceLangDetectionTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions tests/ConnectionTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
4 changes: 0 additions & 4 deletions tests/ConversationTranscriberTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
4 changes: 0 additions & 4 deletions tests/ConversationTranslatorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 0 additions & 4 deletions tests/DiagnosticsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/DialogServiceConnectorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand Down
4 changes: 0 additions & 4 deletions tests/DynamicGrammarTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/GeneralRecognizerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions tests/IntentRecognizerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
4 changes: 0 additions & 4 deletions tests/LanguageModelTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions tests/LongRunning/SpeechRecoAuthTokenErrorMessageTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions tests/LongRunning/SpeechRecoAuthTokenRefreshTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/LongRunning/SpeechRecoReconnectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/LongRunning/TranslationRecoReconnectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/MeetingTranscriberTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
4 changes: 0 additions & 4 deletions tests/PronunciationAssessmentTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 0 additions & 4 deletions tests/PullInputStreamTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/PushInputStreamTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions tests/ReplayableAudioNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
4 changes: 0 additions & 4 deletions tests/SpeechConfigTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading