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

Wip/25 add gain node #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 6 additions & 0 deletions ts/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export interface IStreamClientOptions {
lang?: Constants.AVAILABLE_LANGUAGES;
contentType?: string;
readingInterval?: string;
/**
* Set the applied gain for the processed audio stream.
*
* @type {number}
*/
gain?: number;
onOpen?: () => void;
onClose?: () => void;
onInit?: () => void;
Expand Down
16 changes: 15 additions & 1 deletion ts/Stream/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StreamClient {

private audioContext: AudioContext;
private mediaStreamSource: MediaStreamAudioSourceNode;
private gainNode: GainNode;
private userSpeechAnalyser: AnalyserNode;
private recorder: Recorder;
private resampleProcessor;
Expand All @@ -29,7 +30,7 @@ class StreamClient {
private onOpen; private onClose; private onInit; private onStartListening;
private onStopListening; private onResults; private onEvent; private onError;

constructor(config: IStreamClientOptions = {}) {
constructor(private config: IStreamClientOptions = {}) {

Processors.bindProcessors();

Expand Down Expand Up @@ -175,7 +176,14 @@ class StreamClient {

this.mediaStreamSource = this.audioContext.createMediaStreamSource(stream);
this.onEvent(IStreamClient.EVENT.MSG_MEDIA_STREAM_CREATED, "Media stream created");

// create audio nodes
this.gainNode = this.createGainNode();
this.userSpeechAnalyser = this.audioContext.createAnalyser();

// connect: input ~> gain ~> userSpeechAnalyser ~> output
this.mediaStreamSource.connect(this.gainNode);
this.gainNode.connect(this.userSpeechAnalyser);
this.mediaStreamSource.connect(this.userSpeechAnalyser);
this.recorder = new Recorder(this.mediaStreamSource);
this.onEvent(IStreamClient.EVENT.MSG_INIT_RECORDER, "Recorder initialized");
Expand All @@ -185,6 +193,12 @@ class StreamClient {
}
}

private createGainNode(): GainNode {
const gainNode = this.audioContext.createGain();
gainNode.gain.value = this.config.gain || gainNode.gain.defaultValue;
return gainNode;
}

private openWebSocket() {
if (!this.recorder) {
this.onError(IStreamClient.ERROR.ERR_AUDIO, "Recorder undefined");
Expand Down