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

revert initial changes to PCMRecorder and package.json #766

Merged
merged 2 commits into from
Nov 30, 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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"version": "1.31.0-alpha.0.1",
"license": "MIT",
"description": "Microsoft Cognitive Services Speech SDK for JavaScript",
"type": "module",
"keywords": [
"microsoft",
"cognitiveservices",
Expand Down
100 changes: 44 additions & 56 deletions src/common.browser/PCMRecorder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { RiffPcmEncoder, Stream } from "../common/Exports.js";
import { IRecorder } from "./IRecorder.js";
import { RiffPcmEncoder, Stream } from "../common/Exports";
import { IRecorder } from "./IRecorder";

export class PcmRecorder implements IRecorder {
private privMediaResources: IMediaResources;
Expand Down Expand Up @@ -60,71 +60,59 @@ export class PcmRecorder implements IRecorder {
};
};

const connectWorkletToMicInput = (context: AudioContext): void => {
const workletNode = new AudioWorkletNode(context, "speech-processor");
workletNode.port.onmessage = (ev: MessageEvent): void => {
const inputFrame: Float32Array = ev.data as Float32Array;

if (outputStream && !outputStream.isClosed) {
const waveFrame = waveStreamEncoder.encode(inputFrame);
if (!!waveFrame) {
outputStream.writeStreamChunk({
buffer: waveFrame,
isEnd: false,
timeReceived: Date.now(),
});
}
}
};
micInput.connect(workletNode);
workletNode.connect(context.destination);
this.privMediaResources = {
scriptProcessorNode: workletNode,
source: micInput,
stream: mediaStream,
};
};

// https://webaudio.github.io/web-audio-api/#audioworklet
// Using AudioWorklet to improve audio quality and avoid audio glitches due to blocking the UI thread
const skipAudioWorklet = !!this.privSpeechProcessorScript && this.privSpeechProcessorScript.toLowerCase() === "ignore";

if (!!context.audioWorklet && !skipAudioWorklet) {
/* eslint-disable-next-line */
this.privSpeechProcessorScript = new URL( /* webpackChunkName: 'script_processor_audioWorklet' */ "speech-processor.js", import.meta.url).toString();
if (!this.privSpeechProcessorScript) {
const workletScript = `class SP extends AudioWorkletProcessor {
constructor(options) {
super(options);
}
process(inputs, outputs) {
const input = inputs[0];
const output = [];
for (let channel = 0; channel < input.length; channel += 1) {
output[channel] = input[channel];
}
this.port.postMessage(output[0]);
return true;
}
}
registerProcessor('speech-processor', SP);`;
const blob = new Blob([workletScript], { type: "application/javascript; charset=utf-8" });
this.privSpeechProcessorScript = URL.createObjectURL(blob);
}

context.audioWorklet
.addModule(this.privSpeechProcessorScript)
.then((): void => {
connectWorkletToMicInput(context);
const workletNode = new AudioWorkletNode(context, "speech-processor");
workletNode.port.onmessage = (ev: MessageEvent): void => {
const inputFrame: Float32Array = ev.data as Float32Array;

if (outputStream && !outputStream.isClosed) {
const waveFrame = waveStreamEncoder.encode(inputFrame);
if (!!waveFrame) {
outputStream.writeStreamChunk({
buffer: waveFrame,
isEnd: false,
timeReceived: Date.now(),
});
}
}
};
micInput.connect(workletNode);
workletNode.connect(context.destination);
this.privMediaResources = {
scriptProcessorNode: workletNode,
source: micInput,
stream: mediaStream,
};
})
.catch((): void => {
const workletScript = `class SP extends AudioWorkletProcessor {
constructor(options) {
super(options);
}
process(inputs, outputs) {
const input = inputs[0];
const output = [];
for (let channel = 0; channel < input.length; channel += 1) {
output[channel] = input[channel];
}
this.port.postMessage(output[0]);
return true;
}
}
registerProcessor('speech-processor', SP);`;
const blob = new Blob([workletScript], { type: "application/javascript; charset=utf-8" });
this.privSpeechProcessorScript = URL.createObjectURL(blob);

context.audioWorklet
.addModule(this.privSpeechProcessorScript)
.then((): void => {
connectWorkletToMicInput(context);
})
.catch((): void => {
attachScriptProcessor();
});
attachScriptProcessor();
});
} else {
try {
Expand Down
22 changes: 0 additions & 22 deletions src/common.browser/__mocks__/PCMRecorder.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/AudioOutputStreamTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";

let objsToClose: any[];
jest.mock("../src/common.browser/PCMRecorder");


beforeAll(() => {
// Override inputs, if necessary
Expand Down
2 changes: 1 addition & 1 deletion tests/AutoSourceLangDetectionTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { Settings } from "./Settings";
import { closeAsyncObjects, WaitForCondition } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];
const defaultTargetLanguage: string = "de-DE";

Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {

import * as fs from "fs";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/ConversationTranscriberTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Settings } from "./Settings";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";
import { closeAsyncObjects, RepeatingPullStream, WaitForCondition } from "./Utilities";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/ConversationTranslatorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

// eslint-disable-next-line no-console
const consoleInfo = console.info;

Expand Down
2 changes: 1 addition & 1 deletion tests/DiagnosticsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as sdk from "../microsoft.cognitiveservices.speech.sdk";
import { Settings } from "./Settings";
import { closeAsyncObjects, WaitForCondition } from "./Utilities";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll((): void => {
Expand Down
2 changes: 1 addition & 1 deletion tests/DialogServiceConnectorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

type Callback = (result?: any) => void;
// eslint-disable-next-line no-console
const consoleInfo = console.info;
Expand Down
2 changes: 1 addition & 1 deletion tests/DynamicGrammarTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../src/common.speech/Exports";
import { Settings } from "./Settings";

jest.mock("../src/common.browser/PCMRecorder");

beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();
Expand Down
2 changes: 1 addition & 1 deletion tests/GeneralRecognizerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as sdk from "../microsoft.cognitiveservices.speech.sdk";
import { Settings } from "./Settings";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

let bufferSize: number;
beforeEach(() => {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion tests/IntentRecognizerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

import { AudioStreamFormatImpl } from "../src/sdk/Audio/AudioStreamFormat";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];
let bufferSize: number;

Expand Down
2 changes: 1 addition & 1 deletion tests/LanguageModelTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as sdk from "../microsoft.cognitiveservices.speech.sdk";
import { LanguageUnderstandingModelImpl } from "../src/sdk/LanguageUnderstandingModel";
import { Settings } from "./Settings";
jest.mock("../src/common.browser/PCMRecorder");


beforeAll(() => {
// Override inputs, if necessary
Expand Down
2 changes: 1 addition & 1 deletion tests/LongRunning/SpeechRecoAuthTokenErrorMessageTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Events } from "../../src/common/Exports";
import { Settings } from "../Settings";
import { CreateRepeatingPullStream, WaitForCondition } from "../Utilities";

jest.mock("../../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/LongRunning/SpeechRecoAuthTokenRefreshTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Events } from "../../src/common/Exports";
import { Settings } from "../Settings";
import { CreateRepeatingPullStream, WaitForCondition } from "../Utilities";

jest.mock("../../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/LongRunning/SpeechRecoReconnectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WaveFileAudioInput } from "../WaveFileAudioInputStream";

import { WaitForCondition } from "../Utilities";

jest.mock("../../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll((): void => {
Expand Down
2 changes: 1 addition & 1 deletion tests/LongRunning/TranslationRecoReconnectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { WaveFileAudioInput } from "../WaveFileAudioInputStream";

import { WaitForCondition } from "../Utilities";

jest.mock("../../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll((): void => {
Expand Down
2 changes: 1 addition & 1 deletion tests/MeetingTranscriberTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

function sleep(milliseconds: number): Promise<any> {
Expand Down
2 changes: 1 addition & 1 deletion tests/PronunciationAssessmentTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Settings } from "./Settings";
import { closeAsyncObjects, WaitForCondition } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll((): void => {
Expand Down
2 changes: 1 addition & 1 deletion tests/PullInputStreamTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "../src/sdk/Audio/AudioStreamFormat";
import { Settings } from "./Settings";

jest.mock("../src/common.browser/PCMRecorder");

let bufferSize: number;

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/PushInputStreamTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "../src/sdk/Audio/AudioStreamFormat";
import { Settings } from "./Settings";

jest.mock("../src/common.browser/PCMRecorder");

let bufferSize: number;
beforeAll(() => {
// Override inputs, if necessary
Expand Down
2 changes: 1 addition & 1 deletion tests/ReplayableAudioNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../src/common/Exports";
import { AudioStreamFormatImpl } from "../src/sdk/Audio/AudioStreamFormat";

jest.mock("../src/common.browser/PCMRecorder");

let readCount: number;
const targetBytes: number = 4096;
const defaultAudioFormat: AudioStreamFormatImpl = sdk.AudioStreamFormat.getDefaultInputFormat() as AudioStreamFormatImpl;
Expand Down
2 changes: 1 addition & 1 deletion tests/SpeechConfigTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];
beforeAll((): void => {
// Override inputs, if necessary
Expand Down
2 changes: 1 addition & 1 deletion tests/SpeechContextTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
SpeechContext,
} from "../src/common.speech/Exports";
import { Settings } from "./Settings";
jest.mock("../src/common.browser/PCMRecorder");


beforeAll(() => {
// Override inputs, if necessary
Expand Down
2 changes: 1 addition & 1 deletion tests/SpeechRecognizerSilenceTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { closeAsyncObjects, WaitForCondition } from "./Utilities";

import { AudioStreamFormatImpl } from "../src/sdk/Audio/AudioStreamFormat";

jest.mock("../src/common.browser/PCMRecorder");

const FIRST_EVENT_ID: number = 1;
const Recognizing: string = "Recognizing";
const Recognized: string = "Recognized";
Expand Down
2 changes: 1 addition & 1 deletion tests/SpeechRecognizerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const Recognized: string = "Recognized";
const Canceled: string = "Canceled";

let objsToClose: any[];
jest.mock("../src/common.browser/PCMRecorder");


beforeAll(() => {
// override inputs, if necessary
Expand Down
2 changes: 1 addition & 1 deletion tests/SpeechSynthesisTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
WaitForCondition
} from "./Utilities";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/TranslationRecognizerBasicsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

import { AudioStreamFormatImpl } from "../src/sdk/Audio/AudioStreamFormat";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/TranslationRecognizerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "./Utilities";
import { WaveFileAudioInput } from "./WaveFileAudioInputStream";

jest.mock("../src/common.browser/PCMRecorder");

let objsToClose: any[];

beforeAll(() => {
Expand Down
Loading