@speechly/browser-client
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -26,3 +26,2 @@ | ||
unmute(): void; | ||
private initializeAudioContext; | ||
} | ||
@@ -55,2 +54,9 @@ | ||
* Initializes the client, by initializing the microphone and establishing connection to the API. | ||
* | ||
* This function HAS to be invoked by a user by e.g. binding it to a button press, | ||
* or some other user-performed action. | ||
* | ||
* If this function is invoked without a user interaction, | ||
* the microphone functionality will not work due to security restrictions by the browser. | ||
* | ||
* @param cb - the callback which is invoked when the initialization is complete. | ||
@@ -57,0 +63,0 @@ */ |
@@ -20,3 +20,2 @@ import { AudioCallback, Microphone, ErrorCallback } from '../types'; | ||
unmute(): void; | ||
private initializeAudioContext; | ||
} |
@@ -32,2 +32,33 @@ "use strict"; | ||
this.audioTrack.enabled = false; | ||
if (window.AudioContext !== undefined) { | ||
this.audioContext = new window.AudioContext(); | ||
} | ||
else if (window.webkitAudioContext !== undefined) { | ||
// eslint-disable-next-line new-cap | ||
this.audioContext = new window.webkitAudioContext(); | ||
} | ||
else { | ||
throw Error('Microphone functionality is not supported in your browser'); | ||
} | ||
this.downsampler = downsampler_1.generateDownsampler(this.audioContext.sampleRate, this.sampleRate); | ||
this.audioProcessor = this.audioContext.createScriptProcessor(4096, 1, 1); | ||
// Connect microphone to processor. | ||
this.audioContext.createMediaStreamSource(this.mediaStream).connect(this.audioProcessor); | ||
// Connect processor to destination. | ||
this.audioProcessor.connect(this.audioContext.destination); | ||
// Bind audio handler to receive audio data. | ||
this.audioProcessor.onaudioprocess = audioProcessingEvent => { | ||
if (this.audioTrack === undefined) { | ||
throw Error('Microphone audio track is not initialized'); | ||
} | ||
if (this.downsampler === undefined) { | ||
throw Error('Microphone downsampler is not initialized'); | ||
} | ||
// Skip audio if the mic is muted. | ||
if (!this.audioTrack.enabled) { | ||
return; | ||
} | ||
const downsampled = downsampler_1.float32ToInt16(this.downsampler(audioProcessingEvent.inputBuffer.getChannelData(0))); | ||
this.onAudioCb(downsampled); | ||
}; | ||
cb(); | ||
@@ -82,47 +113,5 @@ }) | ||
this.audioTrack.enabled = true; | ||
if (this.audioContext === undefined) { | ||
this.initializeAudioContext(); | ||
} | ||
} | ||
initializeAudioContext() { | ||
if (this.mediaStream === undefined) { | ||
throw Error('Microphone media stream is not initialized'); | ||
} | ||
if (this.audioContext !== undefined) { | ||
throw Error('Microphone audio context is already initialized'); | ||
} | ||
if (window.AudioContext !== undefined) { | ||
this.audioContext = new window.AudioContext(); | ||
} | ||
else if (window.webkitAudioContext !== undefined) { | ||
// eslint-disable-next-line new-cap | ||
this.audioContext = new window.webkitAudioContext(); | ||
} | ||
else { | ||
throw Error('Microphone functionality is not supported in your browser'); | ||
} | ||
this.downsampler = downsampler_1.generateDownsampler(this.audioContext.sampleRate, this.sampleRate); | ||
this.audioProcessor = this.audioContext.createScriptProcessor(4096, 1, 1); | ||
// Connect microphone to processor. | ||
this.audioContext.createMediaStreamSource(this.mediaStream).connect(this.audioProcessor); | ||
// Connect processor to destination. | ||
this.audioProcessor.connect(this.audioContext.destination); | ||
// Bind audio handler to receive audio data. | ||
this.audioProcessor.onaudioprocess = audioProcessingEvent => { | ||
if (this.audioTrack === undefined) { | ||
throw Error('Microphone audio track is not initialized'); | ||
} | ||
if (this.downsampler === undefined) { | ||
throw Error('Microphone downsampler is not initialized'); | ||
} | ||
// Skip audio if the mic is muted. | ||
if (!this.audioTrack.enabled) { | ||
return; | ||
} | ||
const downsampled = downsampler_1.float32ToInt16(this.downsampler(audioProcessingEvent.inputBuffer.getChannelData(0))); | ||
this.onAudioCb(downsampled); | ||
}; | ||
} | ||
} | ||
exports.BrowserMicrophone = BrowserMicrophone; | ||
//# sourceMappingURL=browser_microphone.js.map |
{ | ||
"name": "@speechly/browser-client", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Browser client for Speechly API", | ||
@@ -53,2 +53,3 @@ "private": false, | ||
"jest": "^25.1.0", | ||
"minimist": "0.2.1", | ||
"prettier": "^1.19.1", | ||
@@ -55,0 +56,0 @@ "rimraf": "^3.0.2", |
@@ -34,2 +34,3 @@ # <a href="https://speechly.com/"><img src="https://www.speechly.com/images/logo.png" height="100" alt="Speechly"></a> | ||
// Initialize the client - this will ask the user for microphone permissions and establish the connection to Speechly API. | ||
// Make sure you call `initlialize` from a user action handler (e.g. from a button press handler). | ||
client.initialize((err?: Error) => { | ||
@@ -36,0 +37,0 @@ if (err !== undefined) { |
@@ -28,2 +28,9 @@ import { ErrorCallback, ContextCallback } from '../types'; | ||
* Initializes the client, by initializing the microphone and establishing connection to the API. | ||
* | ||
* This function HAS to be invoked by a user by e.g. binding it to a button press, | ||
* or some other user-performed action. | ||
* | ||
* If this function is invoked without a user interaction, | ||
* the microphone functionality will not work due to security restrictions by the browser. | ||
* | ||
* @param cb - the callback which is invoked when the initialization is complete. | ||
@@ -30,0 +37,0 @@ */ |
@@ -125,2 +125,9 @@ "use strict"; | ||
* Initializes the client, by initializing the microphone and establishing connection to the API. | ||
* | ||
* This function HAS to be invoked by a user by e.g. binding it to a button press, | ||
* or some other user-performed action. | ||
* | ||
* If this function is invoked without a user interaction, | ||
* the microphone functionality will not work due to security restrictions by the browser. | ||
* | ||
* @param cb - the callback which is invoked when the initialization is complete. | ||
@@ -175,2 +182,3 @@ */ | ||
this.activeContexts.clear(); | ||
this.setState(types_1.ClientState.Disconnected); | ||
return errs.length > 0 ? cb(Error(errs.join(','))) : cb(); | ||
@@ -177,0 +185,0 @@ }); |
@@ -8,5 +8,5 @@ // This file is read by tools that parse documentation comments conforming to the TSDoc standard. | ||
"packageName": "@microsoft/api-extractor", | ||
"packageVersion": "7.7.8" | ||
"packageVersion": "7.7.10" | ||
} | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
97192
1755
63
0
22