@voice-ping/cognitive-services-speech
Version:
VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft
284 lines (282 loc) • 10.8 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Exports_1 = require("../../common/Exports");
var Contracts_1 = require("../Contracts");
var AudioOutputFormat_1 = require("./AudioOutputFormat");
/**
* Represents audio output stream used for custom audio output configurations.
* @class AudioOutputStream
*/
var AudioOutputStream = /** @class */ (function () {
/**
* Creates and initializes an instance.
* @constructor
*/
function AudioOutputStream() {
}
/**
* Creates a memory backed PullAudioOutputStream with the specified audio format.
* @member AudioOutputStream.createPullStream
* @function
* @public
* @returns {PullAudioOutputStream} The audio output stream being created.
*/
AudioOutputStream.createPullStream = function () {
return PullAudioOutputStream.create();
};
return AudioOutputStream;
}());
exports.AudioOutputStream = AudioOutputStream;
/**
* Represents memory backed push audio output stream used for custom audio output configurations.
* @class PullAudioOutputStream
*/
// tslint:disable-next-line:max-classes-per-file
var PullAudioOutputStream = /** @class */ (function (_super) {
__extends(PullAudioOutputStream, _super);
function PullAudioOutputStream() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Creates a memory backed PullAudioOutputStream with the specified audio format.
* @member PullAudioOutputStream.create
* @function
* @public
* @returns {PullAudioOutputStream} The push audio output stream being created.
*/
PullAudioOutputStream.create = function () {
return new PullAudioOutputStreamImpl();
};
return PullAudioOutputStream;
}(AudioOutputStream));
exports.PullAudioOutputStream = PullAudioOutputStream;
/**
* Represents memory backed push audio output stream used for custom audio output configurations.
* @private
* @class PullAudioOutputStreamImpl
*/
// tslint:disable-next-line:max-classes-per-file
var PullAudioOutputStreamImpl = /** @class */ (function (_super) {
__extends(PullAudioOutputStreamImpl, _super);
/**
* Creates and initializes an instance with the given values.
* @constructor
*/
function PullAudioOutputStreamImpl() {
var _this = _super.call(this) || this;
_this.privId = Exports_1.createNoDashGuid();
_this.privStream = new Exports_1.Stream();
return _this;
}
Object.defineProperty(PullAudioOutputStreamImpl.prototype, "format", {
/**
* Format information for the audio
*/
get: function () {
return this.privFormat;
},
/**
* Sets the format information to the stream. For internal use only.
* @param {AudioStreamFormat} format - the format to be set.
*/
set: function (format) {
if (format === undefined || format === null) {
this.privFormat = AudioOutputFormat_1.AudioOutputFormatImpl.getDefaultOutputFormat();
}
this.privFormat = format;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PullAudioOutputStreamImpl.prototype, "isClosed", {
/**
* Checks if the stream is closed
* @member PullAudioOutputStreamImpl.prototype.isClosed
* @property
* @public
*/
get: function () {
return this.privStream.isClosed;
},
enumerable: true,
configurable: true
});
/**
* Gets the id of the stream
* @member PullAudioOutputStreamImpl.prototype.id
* @property
* @public
*/
PullAudioOutputStreamImpl.prototype.id = function () {
return this.privId;
};
/**
* Reads audio data from the internal buffer.
* @member PullAudioOutputStreamImpl.prototype.read
* @function
* @public
* @param {ArrayBuffer} dataBuffer - An ArrayBuffer to store the read data.
* @returns {Promise<number>} - Audio buffer length has been read.
*/
PullAudioOutputStreamImpl.prototype.read = function (dataBuffer) {
var _this = this;
var intView = new Int8Array(dataBuffer);
var totalBytes = 0;
if (this.privLastChunkView !== undefined) {
if (this.privLastChunkView.length > dataBuffer.byteLength) {
intView.set(this.privLastChunkView.slice(0, dataBuffer.byteLength));
this.privLastChunkView = this.privLastChunkView.slice(dataBuffer.byteLength);
return Exports_1.PromiseHelper.fromResult(dataBuffer.byteLength);
}
intView.set(this.privLastChunkView);
totalBytes = this.privLastChunkView.length;
this.privLastChunkView = undefined;
}
var deffer = new Exports_1.Deferred();
// Until we have the minimum number of bytes to send in a transmission, keep asking for more.
var readUntilFilled = function () {
if (totalBytes < dataBuffer.byteLength && !_this.privStream.isReadEnded) {
_this.privStream.read()
.onSuccessContinueWith(function (chunk) {
if (chunk !== undefined && !chunk.isEnd) {
var tmpBuffer = void 0;
if (chunk.buffer.byteLength > dataBuffer.byteLength - totalBytes) {
tmpBuffer = chunk.buffer.slice(0, dataBuffer.byteLength - totalBytes);
_this.privLastChunkView = new Int8Array(chunk.buffer.slice(dataBuffer.byteLength - totalBytes));
}
else {
tmpBuffer = chunk.buffer;
}
intView.set(new Int8Array(tmpBuffer), totalBytes);
totalBytes += tmpBuffer.byteLength;
readUntilFilled();
}
else {
_this.privStream.readEnded();
deffer.resolve(totalBytes);
}
});
}
else {
deffer.resolve(totalBytes);
}
};
readUntilFilled();
return deffer.promise();
};
/**
* Writes the audio data specified by making an internal copy of the data.
* @member PullAudioOutputStreamImpl.prototype.write
* @function
* @public
* @param {ArrayBuffer} dataBuffer - The audio buffer of which this function will make a copy.
*/
PullAudioOutputStreamImpl.prototype.write = function (dataBuffer) {
Contracts_1.Contracts.throwIfNullOrUndefined(this.privStream, "must set format before writing");
this.privStream.writeStreamChunk({
buffer: dataBuffer,
isEnd: false,
timeReceived: Date.now()
});
};
/**
* Closes the stream.
* @member PullAudioOutputStreamImpl.prototype.close
* @function
* @public
*/
PullAudioOutputStreamImpl.prototype.close = function () {
this.privStream.close();
};
return PullAudioOutputStreamImpl;
}(PullAudioOutputStream));
exports.PullAudioOutputStreamImpl = PullAudioOutputStreamImpl;
/*
* Represents audio output stream used for custom audio output configurations.
* @class PushAudioOutputStream
*/
// tslint:disable-next-line:max-classes-per-file
var PushAudioOutputStream = /** @class */ (function (_super) {
__extends(PushAudioOutputStream, _super);
/**
* Creates and initializes and instance.
* @constructor
*/
function PushAudioOutputStream() {
return _super.call(this) || this;
}
/**
* Creates a PushAudioOutputStream that delegates to the specified callback interface for
* write() and close() methods.
* @member PushAudioOutputStream.create
* @function
* @public
* @param {PushAudioOutputStreamCallback} callback - The custom audio output object,
* derived from PushAudioOutputStreamCallback
* @returns {PushAudioOutputStream} The push audio output stream being created.
*/
PushAudioOutputStream.create = function (callback) {
return new PushAudioOutputStreamImpl(callback);
};
return PushAudioOutputStream;
}(AudioOutputStream));
exports.PushAudioOutputStream = PushAudioOutputStream;
/**
* Represents audio output stream used for custom audio output configurations.
* @private
* @class PushAudioOutputStreamImpl
*/
// tslint:disable-next-line:max-classes-per-file
var PushAudioOutputStreamImpl = /** @class */ (function (_super) {
__extends(PushAudioOutputStreamImpl, _super);
/**
* Creates a PushAudioOutputStream that delegates to the specified callback interface for
* read() and close() methods.
* @constructor
* @param {PushAudioOutputStreamCallback} callback - The custom audio output object,
* derived from PushAudioOutputStreamCallback
*/
function PushAudioOutputStreamImpl(callback) {
var _this = _super.call(this) || this;
_this.privId = Exports_1.createNoDashGuid();
_this.privCallback = callback;
return _this;
}
Object.defineProperty(PushAudioOutputStreamImpl.prototype, "format", {
// tslint:disable-next-line:no-empty
set: function (format) { },
enumerable: true,
configurable: true
});
PushAudioOutputStreamImpl.prototype.write = function (buffer) {
if (!!this.privCallback.write) {
this.privCallback.write(buffer);
}
};
PushAudioOutputStreamImpl.prototype.close = function () {
if (!!this.privCallback.close) {
this.privCallback.close();
}
};
PushAudioOutputStreamImpl.prototype.id = function () {
return this.privId;
};
return PushAudioOutputStreamImpl;
}(PushAudioOutputStream));
exports.PushAudioOutputStreamImpl = PushAudioOutputStreamImpl;
//# sourceMappingURL=AudioOutputStream.js.map