diff --git a/build/artyom.js b/build/artyom.js index 15227e2..07c946c 100644 --- a/build/artyom.js +++ b/build/artyom.js @@ -13,7 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); /// // Remove "export default " keywords if willing to build with `npm run artyom-build-window` -var Artyom = (function () { +var Artyom = /** @class */ (function () { // Triggered at the declaration of function Artyom() { this.ArtyomCommands = []; @@ -25,7 +25,7 @@ var Artyom = (function () { // Italian "it-IT": ["Google italiano", "it-IT", "it_IT"], // Japanese - "jp-JP": ["Google 日本人", "ja-JP", "ja_JP"], + "ja-JP": ["Google 日本人", "ja-JP", "ja_JP"], // English USA "en-US": ["Google US English", "en-US", "en_US"], // English UK @@ -89,7 +89,8 @@ var Artyom = (function () { speaking: false, obeying: true, soundex: false, - name: null + name: null, + voice: null, }; this.ArtyomGarbageCollection = []; this.ArtyomFlags = { @@ -901,6 +902,9 @@ var Artyom = (function () { _this.hey(resolve, reject); }); } + if (config.hasOwnProperty("voice")) { + _this.setVoice(config.voice); + } return Promise.resolve(true); }; /** @@ -1061,6 +1065,23 @@ var Artyom = (function () { Artyom.prototype.getGarbageCollection = function () { return this.ArtyomGarbageCollection; }; + /** + * Set a voice of the browser by it's voice name + * You can find a list of available voices using .getVoices() + * + * @param voiceName + */ + Artyom.prototype.setVoice = function (voiceName) { + this.debug('Setting voice to ' + voiceName); + var availableVoices = this.getVoices(); + var newVoice = availableVoices.filter(function (v) { return v.name === voiceName; })[0]; + if (!newVoice) { + console.warn("The provided voice " + voiceName + " isn't available, ignoring setVoice command"); + return; + } + this.ArtyomProperties.voice = voiceName; + this.ArtyomVoice = newVoice; + }; /** * Retrieve a single voice of the browser by it's language code. * It will return the first voice available for the language on every device. @@ -1357,6 +1378,7 @@ var Artyom = (function () { * @returns {undefined} */ Artyom.prototype.talk = function (text, actualChunk, totalChunks, callbacks) { + var _this_1 = this; var _this = this; var msg = new SpeechSynthesisUtterance(); msg.text = text; @@ -1370,6 +1392,15 @@ var Artyom = (function () { availableVoice = _this.getVoice(callbacks.lang); } } + if (this.ArtyomProperties.voice) { + var availableVoices = this.getVoices(); + var newVoice = availableVoices.filter(function (v) { return v.name === _this_1.ArtyomProperties.voice; })[0]; + if (!newVoice) { + console.warn("The provided voice " + this.ArtyomProperties.voice + " isn't available, using default for " + _this.ArtyomProperties.lang); + return; + } + availableVoice = newVoice; + } // If is a mobile device, provide only the language code in the lang property i.e "es_ES" if (this.Device.isMobile) { // Try to set the voice only if exists, otherwise don't use anything to use the native voice diff --git a/build/artyom.window.js b/build/artyom.window.js index e0ccccc..e0bfd4f 100644 --- a/build/artyom.window.js +++ b/build/artyom.window.js @@ -11,7 +11,7 @@ */ /// // Remove "export default " keywords if willing to build with `npm run artyom-build-window` -var Artyom = (function () { +var Artyom = /** @class */ (function () { // Triggered at the declaration of function Artyom() { this.ArtyomCommands = []; @@ -23,7 +23,7 @@ var Artyom = (function () { // Italian "it-IT": ["Google italiano", "it-IT", "it_IT"], // Japanese - "jp-JP": ["Google 日本人", "ja-JP", "ja_JP"], + "ja-JP": ["Google 日本人", "ja-JP", "ja_JP"], // English USA "en-US": ["Google US English", "en-US", "en_US"], // English UK @@ -87,7 +87,8 @@ var Artyom = (function () { speaking: false, obeying: true, soundex: false, - name: null + name: null, + voice: null, }; this.ArtyomGarbageCollection = []; this.ArtyomFlags = { @@ -899,6 +900,9 @@ var Artyom = (function () { _this.hey(resolve, reject); }); } + if (config.hasOwnProperty("voice")) { + _this.setVoice(config.voice); + } return Promise.resolve(true); }; /** @@ -1059,6 +1063,23 @@ var Artyom = (function () { Artyom.prototype.getGarbageCollection = function () { return this.ArtyomGarbageCollection; }; + /** + * Set a voice of the browser by it's voice name + * You can find a list of available voices using .getVoices() + * + * @param voiceName + */ + Artyom.prototype.setVoice = function (voiceName) { + this.debug('Setting voice to ' + voiceName); + var availableVoices = this.getVoices(); + var newVoice = availableVoices.filter(function (v) { return v.name === voiceName; })[0]; + if (!newVoice) { + console.warn("The provided voice " + voiceName + " isn't available, ignoring setVoice command"); + return; + } + this.ArtyomProperties.voice = voiceName; + this.ArtyomVoice = newVoice; + }; /** * Retrieve a single voice of the browser by it's language code. * It will return the first voice available for the language on every device. @@ -1355,6 +1376,7 @@ var Artyom = (function () { * @returns {undefined} */ Artyom.prototype.talk = function (text, actualChunk, totalChunks, callbacks) { + var _this_1 = this; var _this = this; var msg = new SpeechSynthesisUtterance(); msg.text = text; @@ -1368,6 +1390,15 @@ var Artyom = (function () { availableVoice = _this.getVoice(callbacks.lang); } } + if (this.ArtyomProperties.voice) { + var availableVoices = this.getVoices(); + var newVoice = availableVoices.filter(function (v) { return v.name === _this_1.ArtyomProperties.voice; })[0]; + if (!newVoice) { + console.warn("The provided voice " + this.ArtyomProperties.voice + " isn't available, using default for " + _this.ArtyomProperties.lang); + return; + } + availableVoice = newVoice; + } // If is a mobile device, provide only the language code in the lang property i.e "es_ES" if (this.Device.isMobile) { // Try to set the voice only if exists, otherwise don't use anything to use the native voice diff --git a/source/artyom.d.ts b/source/artyom.d.ts index 68a55fa..38c87e4 100644 --- a/source/artyom.d.ts +++ b/source/artyom.d.ts @@ -55,6 +55,7 @@ interface ArtyomProperties { obeying?: boolean; soundex?: boolean; name?: string; + voice?: string; } interface PromptOptions { diff --git a/source/artyom.ts b/source/artyom.ts index 2688d83..121ff9d 100644 --- a/source/artyom.ts +++ b/source/artyom.ts @@ -140,7 +140,8 @@ export default class Artyom { speaking: false, obeying: true, soundex: false, - name: null + name: null, + voice: null, }; this.ArtyomGarbageCollection = []; @@ -1095,6 +1096,10 @@ export default class Artyom { }); } + if (config.hasOwnProperty("voice")) { + _this.setVoice(config.voice) + } + return Promise.resolve(true); } @@ -1275,6 +1280,26 @@ export default class Artyom { return this.ArtyomGarbageCollection; } + + + /** + * Set a voice of the browser by it's voice name + * You can find a list of available voices using .getVoices() + * + * @param voiceName + */ + setVoice(voiceName: string) { + this.debug('Setting voice to ' + voiceName) + const availableVoices = this.getVoices() + const newVoice = availableVoices.filter(v => v.name === voiceName)[0] + if (!newVoice) { + console.warn(`The provided voice ${voiceName} isn't available, ignoring setVoice command` ); + return + } + this.ArtyomProperties.voice = voiceName + this.ArtyomVoice = newVoice; + } + /** * Retrieve a single voice of the browser by it's language code. * It will return the first voice available for the language on every device. @@ -1620,7 +1645,7 @@ export default class Artyom { msg.text = text; msg.volume = this.ArtyomProperties.volume; msg.rate = this.ArtyomProperties.speed; - + // Select the voice according to the selected let availableVoice = _this.getVoice(_this.ArtyomProperties.lang); @@ -1630,6 +1655,16 @@ export default class Artyom { availableVoice = _this.getVoice(callbacks.lang); } } + + if (this.ArtyomProperties.voice) { + const availableVoices = this.getVoices() + const newVoice = availableVoices.filter(v => v.name === this.ArtyomProperties.voice)[0] + if (!newVoice) { + console.warn(`The provided voice ${this.ArtyomProperties.voice} isn't available, using default for ${_this.ArtyomProperties.lang}` ); + return + } + availableVoice = newVoice + } // If is a mobile device, provide only the language code in the lang property i.e "es_ES" if(this.Device.isMobile){