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

Request: Make encoder options configurable at runtime #22

Open
Venryx opened this issue Sep 7, 2019 · 3 comments
Open

Request: Make encoder options configurable at runtime #22

Venryx opened this issue Sep 7, 2019 · 3 comments

Comments

@Venryx
Copy link

Venryx commented Sep 7, 2019

Is there any way to customize the bitrate for the Opus encoding?

I'd like to use this library to record speech to a file, however some recordings might be long, so the user may want to lower the bit-rate/quality to reduce the resulting file-size.

@Venryx
Copy link
Author

Venryx commented Sep 7, 2019

It looks like the file here contains the configuration for the Opus encoder: https://github.com/kbumsik/opus-media-recorder/blob/master/src/OpusEncoder.js

If so, I guess this issue is changing from asking:
Old: "How do you configure the Opus encoder?"
New: "Request: Make encoder options configurable at runtime"

@Venryx Venryx changed the title Configuring the bitrate for Opus encoding Request: Make encoder options configurable at runtime Sep 7, 2019
@Venryx
Copy link
Author

Venryx commented Sep 8, 2019

Hmm. I changed that constant, however now the assert at this location is failing: https://github.com/kbumsik/opus-media-recorder/blob/master/src/ContainerInterface.cpp#L20

Is updating ContainerInterface.cpp to support other sample rates an easy task, or does it take substantial rewriting?

I'd need it to support a sampling rate of 16000 to send the resulting Opus audio to the DialogFlow api: https://cloud.google.com/dialogflow/docs/reference/rpc/google.cloud.dialogflow.v2#google.cloud.dialogflow.v2.AudioEncoding

@Venryx
Copy link
Author

Venryx commented Sep 8, 2019

For others coming across this issue, I ended up solving the problem by using the opus-recorder library.

The API is maybe not quite as nice, but it has more configuration options, including the one I need (sample rate).

Here is the code I used, which works with uploading to DialogFlow:

function RecordFor10SecondsThenTranscribe() {
	var options = {
		monitorGain: 0,
		recordingGain: 1,
		numberOfChannels: 1,
		//encoderSampleRate: 48000,
		encoderSampleRate: 16000,
		originalSampleRateOverride: 16000, // necessary due to Google bug? (https://github.com/chris-rudmin/opus-recorder/issues/191#issuecomment-509426093)
		encoderPath: "/FromNodeModules/encoderWorker.min.js",
	};

	this.micRecorder = new Recorder(options);
	this.micRecorder.onstart = ()=>console.log("Recorder is started");
	this.micRecorder.onstop = ()=>console.log("Recorder is stopped");
	this.micRecorder.onpause = ()=>console.log("Recorder is paused");
	this.micRecorder.onresume = ()=>console.log("Recorder is resuming");
	this.micRecorder.ondataavailable = async typedArray=>{
		console.log("Data received");
		const audioData = new Blob([typedArray], {type: "audio/ogg"});
		const audioDataStr = await BlobToString(audioData, "readAsDataURL");
		const audioDataStr_trimmed = audioDataStr.replace(/^data:.+?base64,/, "");

		// this sends the audio-data to Google's servers for transcription
		const text = await ConvertSpeechToText(audioDataStr_trimmed);
		console.log(`Got speech!: ${text}`);
	};

	this.micRecorder.start(); // start recording (and conversion/encoding to ogg)
	await SleepAsync(10000); // wait 10 seconds (to let it pick up speech)
	this.micRecorder.stop(); // stop recorder, triggering "ondataavailable" listener
}

export function BlobToString(blob: Blob, readAsFuncName = "readAsText" as "readAsBinaryString" | "readAsDataURL" | "readAsText") {
	return new Promise((resolve, reject)=>{
		const reader = new FileReader();
		reader.addEventListener("loadend", e=>resolve(reader.result as string));
		reader[readAsFuncName](blob);
	}) as Promise<string>;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant