scratch-vm/src/extensions/scratch3_text2speech/index.js

437 lines
18 KiB
JavaScript
Raw Normal View History

const formatMessage = require('format-message');
const nets = require('nets');
const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');
const Cast = require('../../util/cast');
const Clone = require('../../util/clone');
const log = require('../../util/log');
2018-10-10 14:09:34 -04:00
/**
* Icon svg to be displayed in the blocks category menu, encoded as a data URI.
* @type {string}
*/
// eslint-disable-next-line max-len
const menuIconURI = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMjBweCIgaGVpZ2h0PSIyMHB4IiB2aWV3Qm94PSIwIDAgMjAgMjAiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDUyLjIgKDY3MTQ1KSAtIGh0dHA6Ly93d3cuYm9oZW1pYW5jb2RpbmcuY29tL3NrZXRjaCAtLT4KICAgIDx0aXRsZT5FeHRlbnNpb25zL1NvZnR3YXJlL1RleHQtdG8tU3BlZWNoLU1lbnU8L3RpdGxlPgogICAgPGRlc2M+Q3JlYXRlZCB3aXRoIFNrZXRjaC48L2Rlc2M+CiAgICA8ZyBpZD0iRXh0ZW5zaW9ucy9Tb2Z0d2FyZS9UZXh0LXRvLVNwZWVjaC1NZW51IiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj4KICAgICAgICA8ZyBpZD0idGV4dDJzcGVlY2giIHRyYW5zZm9ybT0idHJhbnNsYXRlKDIuMDAwMDAwLCAyLjAwMDAwMCkiIGZpbGwtcnVsZT0ibm9uemVybyI+CiAgICAgICAgICAgIDxwYXRoIGQ9Ik01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ODk5Njk5MSw2LjkwNjc5MDA1IDYsNS41NTczMjY4MyA2LDQgQzYsMS43OTA4NjEgNy43OTA4NjEsNC4wNTgxMjI1MWUtMTYgMTAsMCBMMTIsMCBDMTQuMjA5MTM5LC00LjA1ODEyMjUxZS0xNiAxNiwxLjc5MDg2MSAxNiw0IEMxNiw2LjIwOTEzOSAxNC4yMDkxMzksOCAxMiw4IEwxMC43MDQ4MzEzLDggWiIgaWQ9InNwZWVjaCIgZmlsbD0iIzBFQkQ4QyI+PC9wYXRoPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+';
/**
* Icon svg to be displayed at the left edge of each extension block, encoded as a data URI.
* @type {string}
*/
// eslint-disable-next-line max-len
const blockIconURI = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iNDBweCIgaGVpZ2h0PSI0MHB4IiB2aWV3Qm94PSIwIDAgNDAgNDAiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDUyLjIgKDY3MTQ1KSAtIGh0dHA6Ly93d3cuYm9oZW1pYW5jb2RpbmcuY29tL3NrZXRjaCAtLT4KICAgIDx0aXRsZT5FeHRlbnNpb25zL1NvZnR3YXJlL1RleHQtdG8tU3BlZWNoLUJsb2NrPC90aXRsZT4KICAgIDxkZXNjPkNyZWF0ZWQgd2l0aCBTa2V0Y2guPC9kZXNjPgogICAgPGcgaWQ9IkV4dGVuc2lvbnMvU29mdHdhcmUvVGV4dC10by1TcGVlY2gtQmxvY2siIHN0cm9rZT0ibm9uZSIgc3Ryb2tlLXdpZHRoPSIxIiBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHN0cm9rZS1vcGFj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zIDE1LjEwOTcyNjUsMTkuNzkyNDUxNyBDMTQuNzM3NjAzOSwxOS42MDYzOTA0IDE0LjUsMTkuMjQ5OTg0NiAxNC41LDE4Ljc2MTkwNDggQzE0LjUsMTguNjU2ODA0MSAxNC41MTcwNTU1LDE4LjU1NDUwNzYgMTQuNTQ5NDQ2NywxOC40NTQwODQ0IEMxNC42MjU3NTQ1LDE4LjIxNzUwNjMgMTUuMTczNTcyMSwxNy40Njc1MzEgMTUuMjc3MjA3MSwxNy4yODA5ODgxIEMxNS41NDYzNTI2LDE2Ljc5NjUyNjEgMTUuNzM5MDI1LDE2LjIwNjM1NjEgMTUuODQzMjg5MSwxNS40MTYwMDM0IEMxMy4xODk3MDA1LDEzLjkyNjgzNjkgMTEuNSwxMS4xMTM5NjY4IDExLjUsOCBDMTEuNSwzLjMwNTU3OTYzIDE1LjMwNTU3OTYsLTAuNSAyMCwtMC41IEwyNCwtMC41IEMyOC42OTQ0MjA0LC0wLjUgMzIuNSwzLjMwNTU3OTYzIDMyLjUsOCBDMzIuNSwxMi42OTQ0MjA0IDI4LjY5NDQyMDQsMTYuNSAyNCwxNi41IEwyMS42NDM2MDY2LDE2LjUgWiIgaWQ9InNwZWVjaCIgZmlsbD0iI0ZGRkZGRiI+PC9wYXRoPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+';
/**
* The url of the synthesis server.
* @type {string}
*/
const SERVER_HOST = 'https://synthesis-service.scratch.mit.edu';
/**
* How long to wait in ms before timing out requests to synthesis server.
* @type {int}
*/
const SERVER_TIMEOUT = 10000; // 10 seconds
2018-09-05 18:36:01 -04:00
/**
* Volume for playback of speech sounds, as a percentage.
* @type {number}
*/
const SPEECH_VOLUME = 250;
/**
* An id for one of the voices.
*/
const QUINN_ID = 'QUINN';
/**
* An id for one of the voices.
*/
const MAX_ID = 'MAX';
/**
* An id for one of the voices.
*/
const SQUEAK_ID = 'SQUEAK';
/**
* An id for one of the voices.
*/
const GIANT_ID = 'GIANT';
/**
* An id for one of the voices.
*/
const KITTEN_ID = 'KITTEN';
/**
* Class for the text2speech blocks.
* @constructor
*/
class Scratch3Text2SpeechBlocks {
constructor (runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
2018-09-24 17:16:10 -04:00
/**
* The current language code to use for speech synthesis.
* @type {string}
*/
this.currentLanguage = 'en-US';
2018-09-04 21:12:45 -04:00
/**
* Map of soundPlayers by sound id.
* @type {Map<string, SoundPlayer>}
*/
this._soundPlayers = new Map();
this._stopAllSpeech = this._stopAllSpeech.bind(this);
if (this.runtime) {
this.runtime.on('PROJECT_STOP_ALL', this._stopAllSpeech);
}
this._onTargetCreated = this._onTargetCreated.bind(this);
if (this.runtime) {
runtime.on('targetWasCreated', this._onTargetCreated);
}
}
/**
* An object with info for each voice.
*/
get VOICE_INFO () {
return {
[QUINN_ID]: {
name: formatMessage({
id: 'text2speech.quinn',
default: 'quinn',
description: 'Name for a voice with ambiguous gender.'
}),
gender: 'female',
playbackRate: 1
},
[MAX_ID]: {
name: formatMessage({
id: 'text2speech.max',
default: 'max',
description: 'Name for a voice with ambiguous gender.'
}),
gender: 'male',
playbackRate: 1
},
[SQUEAK_ID]: {
name: formatMessage({
id: 'text2speech.squeak',
default: 'squeak',
description: 'Name for a funny voice with a high pitch.'
}),
gender: 'female',
playbackRate: 1.4
},
[GIANT_ID]: {
name: formatMessage({
id: 'text2speech.giant',
default: 'giant',
description: 'Name for a funny voice with a low pitch.'
}),
gender: 'male',
playbackRate: 0.84
},
[KITTEN_ID]: {
name: formatMessage({
id: 'text2speech.kitten',
default: 'kitten',
description: 'A baby cat.'
}),
gender: 'female',
playbackRate: 1.4
}
};
}
2018-09-24 17:16:10 -04:00
/**
* An object with language names mapped to their language codes.
*/
get LANGUAGE_INFO () {
return {
'Danish': 'da-DK',
'Dutch': 'nl-NL',
'English': 'en-US',
'French': 'fr-FR',
'German': 'de-DE',
'Icelandic': 'is-IS',
'Italian': 'it-IT',
'Japanese': 'ja-JP',
'Polish': 'pl-PL',
'Portuguese (Brazilian)': 'pt-BR',
'Portuguese (European)': 'pt-PT',
'Russian': 'ru-RU',
'Spanish (European)': 'es-ES',
'Spanish (Latin American)': 'es-US'
};
}
/**
* The key to load & store a target's text2speech state.
* @return {string} The key.
*/
static get STATE_KEY () {
return 'Scratch.text2speech';
}
/**
* The default state, to be used when a target has no existing state.
* @type {Text2SpeechState}
*/
static get DEFAULT_TEXT2SPEECH_STATE () {
return {
voiceId: QUINN_ID
};
}
/**
* @param {Target} target - collect state for this target.
* @returns {Text2SpeechState} the mutable state associated with that target. This will be created if necessary.
* @private
*/
_getState (target) {
let state = target.getCustomState(Scratch3Text2SpeechBlocks.STATE_KEY);
if (!state) {
state = Clone.simple(Scratch3Text2SpeechBlocks.DEFAULT_TEXT2SPEECH_STATE);
target.setCustomState(Scratch3Text2SpeechBlocks.STATE_KEY, state);
}
return state;
}
/**
* When a Target is cloned, clone the state.
* @param {Target} newTarget - the newly created target.
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
* @listens Runtime#event:targetWasCreated
* @private
*/
_onTargetCreated (newTarget, sourceTarget) {
if (sourceTarget) {
const state = sourceTarget.getCustomState(Scratch3Text2SpeechBlocks.STATE_KEY);
if (state) {
newTarget.setCustomState(Scratch3Text2SpeechBlocks.STATE_KEY, Clone.simple(state));
}
}
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo () {
return {
id: 'text2speech',
name: 'Text to Speech',
2018-10-10 14:09:34 -04:00
blockIconURI: blockIconURI,
menuIconURI: menuIconURI,
blocks: [
{
opcode: 'speakAndWait',
text: formatMessage({
id: 'text2speech.speakAndWaitBlock',
default: 'speak [WORDS]',
description: 'Speak some words.'
}),
blockType: BlockType.COMMAND,
arguments: {
WORDS: {
type: ArgumentType.STRING,
defaultValue: formatMessage({
id: 'text2speech.defaultTextToSpeak',
default: 'hello',
description: 'hello: the default text to speak'
})
}
}
},
{
opcode: 'setVoice',
text: formatMessage({
id: 'text2speech.setVoiceBlock',
default: 'set voice to [VOICE]',
description: 'Set the voice for speech synthesis.'
}),
blockType: BlockType.COMMAND,
arguments: {
VOICE: {
type: ArgumentType.STRING,
menu: 'voices',
defaultValue: QUINN_ID
}
}
2018-09-24 17:16:10 -04:00
},
{
opcode: 'setLanguage',
text: formatMessage({
id: 'text2speech.setLanguageBlock',
default: 'set language to [LANGUAGE]',
description: 'Set the language for speech synthesis.'
}),
blockType: BlockType.COMMAND,
arguments: {
LANGUAGE: {
type: ArgumentType.STRING,
menu: 'languages',
defaultValue: this.currentLanguage
}
}
}
],
menus: {
2018-09-24 17:16:10 -04:00
voices: this.getVoiceMenu(),
languages: this.getLanguageMenu()
}
};
}
/**
* Get the viewer's language code.
* @return {string} the language code.
*/
getViewerLanguageCode () {
// @todo This should be the language code of the project *creator*
// rather than the project viewer.
// @todo Amazon Polly needs the locale in a two part form (e.g. ja-JP),
// so we probably need to create a lookup table. It will convert from these codes:
// https://github.com/LLK/scratch-l10n/blob/master/src/supported-locales.js
// to these codes:
// https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html
// but note also that only a subset of these languages have both male and female voices:
// https://docs.aws.amazon.com/polly/latest/dg/voicelist.html
return formatMessage.setup().locale || navigator.language || navigator.userLanguage || 'en-US';
}
/**
* Get the menu of voices for the "set voice" block.
* @return {array} the text and value for each menu item.
*/
getVoiceMenu () {
return Object.keys(this.VOICE_INFO).map(voiceId => ({
text: this.VOICE_INFO[voiceId].name,
value: voiceId
}));
}
2018-09-24 17:16:10 -04:00
/**
* Get the menu of languages for the "set language" block.
* @return {array} the text and value for each menu item.
*/
getLanguageMenu () {
return Object.keys(this.LANGUAGE_INFO).map(languageName => ({
text: languageName,
value: this.LANGUAGE_INFO[languageName]
}));
}
/**
* Set the voice for speech synthesis for this sprite.
* @param {object} args Block arguments
* @param {object} util Utility object provided by the runtime.
*/
setVoice (args, util) {
const state = this._getState(util.target);
// Only set the voice if the arg is a valid voice id.
if (Object.keys(this.VOICE_INFO).includes(args.VOICE)) {
state.voiceId = args.VOICE;
}
}
2018-09-24 17:16:10 -04:00
/**
* Set the language for speech synthesis.
* @param {object} args Block arguments
*/
setLanguage (args) {
// Only set the language if the arg is a valid language code.
if (Object.values(this.LANGUAGE_INFO).includes(args.LANGUAGE)) {
this.currentLanguage = args.LANGUAGE;
}
}
2018-09-04 21:12:45 -04:00
/**
* Stop all currently playing speech sounds.
*/
_stopAllSpeech () {
this._soundPlayers.forEach(player => {
player.stop();
});
}
/**
* Convert the provided text into a sound file and then play the file.
* @param {object} args Block arguments
* @param {object} util Utility object provided by the runtime.
* @return {Promise} A promise that resolves after playing the sound
*/
speakAndWait (args, util) {
// Cast input to string
let words = Cast.toString(args.WORDS);
const state = this._getState(util.target);
const gender = this.VOICE_INFO[state.voiceId].gender;
const playbackRate = this.VOICE_INFO[state.voiceId].playbackRate;
// @todo localize this?
if (state.voiceId === KITTEN_ID) {
words = words.replace(/\w+/g, 'meow');
}
// Build up URL
let path = `${SERVER_HOST}/synth`;
2018-09-24 17:16:10 -04:00
path += `?locale=${this.currentLanguage}`;
path += `&gender=${gender}`;
2018-09-12 14:06:26 -04:00
path += `&text=${encodeURI(words.substring(0, 128))}`;
// Perform HTTP request to get audio file
return new Promise(resolve => {
nets({
url: path,
timeout: SERVER_TIMEOUT
}, (err, res, body) => {
if (err) {
log.warn(err);
return resolve();
}
if (res.statusCode !== 200) {
log.warn(res.statusCode);
return resolve();
}
// Play the sound
const sound = {
data: {
buffer: body.buffer
}
};
this.runtime.audioEngine.decodeSoundPlayer(sound).then(soundPlayer => {
2018-09-04 21:12:45 -04:00
this._soundPlayers.set(soundPlayer.id, soundPlayer);
soundPlayer.setPlaybackRate(playbackRate);
// Increase the volume
const engine = this.runtime.audioEngine;
const chain = engine.createEffectChain();
2018-09-05 18:36:01 -04:00
chain.set('volume', SPEECH_VOLUME);
soundPlayer.connect(chain);
2018-08-31 10:47:36 -04:00
2018-09-04 21:12:45 -04:00
soundPlayer.play();
soundPlayer.on('stop', () => {
this._soundPlayers.delete(soundPlayer.id);
resolve();
});
});
});
});
}
}
module.exports = Scratch3Text2SpeechBlocks;