├── .eslintrc.json ├── .github └── workflows │ └── ci-pipeline.yml ├── .gitignore ├── LICENSE ├── README.md ├── figures └── wikivoice.png ├── index.js ├── package-lock.json ├── package.json ├── src ├── wiki2ssml.js └── wikivoice.pegjs ├── test ├── helper.js ├── wiki2ssml-extension-test.js └── wiki2ssml-test.js └── yarn.lock /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "commonjs": true, 5 | "es6": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaVersion": 2017, 10 | "sourceType": "module" 11 | }, 12 | "rules": { 13 | "indent": [ 14 | "error", 15 | 4 16 | ], 17 | "linebreak-style": [ 18 | "error", 19 | "unix" 20 | ], 21 | "quotes": [ 22 | "error", 23 | "double" 24 | ], 25 | "semi": [ 26 | "error", 27 | "always" 28 | ] 29 | }, 30 | "globals": { 31 | "__dirname" : false, 32 | /* MOCHA */ 33 | "describe" : false, 34 | "it" : false, 35 | "before" : false, 36 | "beforeEach" : false, 37 | "after" : false, 38 | "afterEach" : false 39 | } 40 | } -------------------------------------------------------------------------------- /.github/workflows/ci-pipeline.yml: -------------------------------------------------------------------------------- 1 | name: ci pipeline 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | main: 9 | 10 | runs-on: ubuntu-latest 11 | 12 | strategy: 13 | matrix: 14 | node-version: [18.x, 20.x, 22.x, 23.x] 15 | 16 | steps: 17 | - uses: actions/checkout@v3 18 | - name: Node.js ${{ matrix.node-version }} 19 | uses: actions/setup-node@v3 20 | with: 21 | node-version: ${{ matrix.node-version }} 22 | - name: Dependency installation 23 | run: | 24 | npm ci 25 | npm run build --if-present 26 | - name: Linting 27 | run: npm run lint 28 | - name: Unit tests 29 | run: npm run test 30 | - name: Coverage 31 | run: npm run coverage 32 | - name: Codecov upload 33 | uses: codecov/codecov-action@v3 34 | with: 35 | token: ${{ secrets.CODECOV_TOKEN }} 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | npm-debug.log 4 | coverage 5 | .nyc_output 6 | TODO.txt 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2019 Xi Bai 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://github.com/baxtree/wiki2ssml/actions/workflows/ci-pipeline.yml/badge.svg?branch=master)](https://github.com/baxtree/wiki2ssml/actions/workflows/ci-pipeline.yml?query=branch%3Amaster) ![Codecov](https://img.shields.io/codecov/c/github/baxtree/wiki2ssml) ![Node](https://img.shields.io/static/v1?label=node.js&message=≥16.0.0&color=green) [![GitHub license](https://img.shields.io/github/license/baxtree/wiki2ssml)](https://github.com/baxtree/wiki2ssml/blob/master/LICENSE) 2 | 3 | 4 | # Wiki2SSML 5 | 6 | `wiki2ssml` can transform the `WikiVoice` markups into the W3C SSML widely supported by various text-to-speech services as an interchange format for synthesised voice tuning. 7 | 8 | # Install 9 | ```sh 10 | $ npm install wiki2ssml 11 | ``` 12 | or 13 | ```sh 14 | $ yarn add wiki2ssml 15 | ``` 16 | 17 | # Introduction 18 | `wiki2ssml` eases the burden of editors preparing scripts in SSML, widely understood by modern speech synthesisers including but not limited to Amazon Polly, Google TTS, IBM Watson TTS and Microsoft Azure TTS. It has been developed in Vanilla JavaScript and powered by `WikiVoice` which provides an unobtrusive solution of blending voice-tuning markups with free texts and creates seamless experiences of editing scripts and voices in one go. 19 | # 20 |

21 | WikiVoice 22 |

23 | 24 | # Format 25 | ``` 26 | [[attribute(:value)?(,attribute:value)*(|target)?]] 27 | ``` 28 | # Supported Markups 29 | | Expressions | Operations| 30 | | ------------- |-------------| 31 | | [[volume:SCALE|TEXT]] | Speaking volume | 32 | | [[speed:SCALE|TEXT]] | Speaking rate | 33 | | [[pitch:SCALE|TEXT]] | Speaking pitch | 34 | | [[silence:DURATION,strength:STRENGTH]] | Pause with duration and strength | 35 | | [[emphasis:LEVEL|TEXT]] | Emphasis with LEVEL | 36 | | [[audio:AUDIO_URI]] | Audio embedded into speech| 37 | | [[lang:LANGUAGE|TEXT]] | Language indicator | 38 | | [[paragraph|TEXT]] | Paragraph indicator | 39 | | [[sentence|TEXT]] | Sentence indicator | 40 | | [[type:TYPE|TEXT]] | Type it should be said as | 41 | | [[voice:NAME|TEXT]] | Voice name it should be said with | 42 | | [[pos:POS|TEXT]] | Part of speech it should be prounouced as | 43 | | [[substitute:TEXT1|TEXT2]] | Replace TEXT2 with TEXT1 as substitution | 44 | | [[alphabet:ALPHABET,pronunciation:PRONUNCIATION|TEXT]] | Phonetic pronunciation | 45 | | [[volume:SCALE,speed:SCALE,pitch:SCALE|TEXT]] | Speaking volume, rate and pitch | 46 | | [[type:TYPE,format:FORMAT,detail:DETAIL|TEXT]] | Type it should be said as | 47 | | [[mark:NAME]] | Mark referencing a location | 48 | | [[seeAlso:URI] | URI providing additional information about marked-up content] 49 | | [[cacheControl:no-cache]] | No caching on marked-up content | 50 | | [[lexicon:URI,type:TEXT]] | Location of the lexicon document and its media type | 51 | | *[[...]][[...]]...[[...]]* | <par> time container with one or more markups| 52 | | #[[...]][[...]]...[[...]]# | <seq> time container with one or more markups| 53 | 54 | # Vendor-Specific Markups 55 | | Expressions | Operations| 56 | | ------------- |-------------| 57 | | [[amzWhispered|TEXT]] | Whispering | 58 | | [[amzPhonation:PHONATION|TEXT]] | Speaking Softly | 59 | | [[amzTimbre:SCALE|TEXT]] | Controlling Timbre | 60 | | [[amzDRC|TEXT]] | Dynamic Range Compression | 61 | | [[amzBreathDuration:SCALE,amzBreathVolume:SCALE]] | Breathing based on the manual model | 62 | | [[amzDefaultBreath]] | Default breathing based on the manual model | 63 | | [[amzAutoBreathsVolume:SCALE,amzAutoBreathsFrequency:SCALE,amzAutoBreathsDuration:SCALE|TEXT]] | Breathing based on the automated model | 64 | | [[amzDefaultAutoBreaths]] | Default breathing based on the automated model | 65 | | [[amzSpeakingStyle:STYLE|TEXT]] | Speaking style | 66 | | [[amzEmotion:EMOTION,amzIntensity:SCALE|TEXT]] | Speaking emotionally | 67 | | [[amzMaxDuration:DURATION#124;TEXT]] | Maximum Speech duration | 68 | | [[gglMediaSpeak|TEXT]] | Media container for speech | 69 | | [[gglMediaSpeakEnd:DURATION|TEXT]] | Media container for speech with the ending time | 70 | | [[gglMediaSpeakFadeIn:DURATION,gglMediaSpeakFadeOut:DURATION|TEXT]] | Media container for speach with fade | 71 | | [[gglMediaAudio:URI]] | Media container for audio | 72 | | [[gglMediaAudioFadeIn:DURATION,gglMediaAudioFadeOut:DURATION,gglMediaAudio:URI]] | Media container for audio with fade | 73 | | [[ibmExprType:TYPE|TEXT]] | Expressiveness type | 74 | | [[ibmTransType:TYPE,ibmTransStrength:SCALE|TEXT]] | Voice transformation | 75 | | [[ibmTransBreathiness:SCALE,ibmTransPitchRange:SCALE,ibmTransTimbre:SCALE|TEXT]] | Voice custom transformation | 76 | | [[voice:NAME|[[mstExprType:TYPE|TEXT]]]] | Voice-specific speaking style | 77 | | [[mstBackgroundAudio:URI,mstBackgroundAudioVolume:SCALE]] | Background audio and its volume | 78 | | [[mstBackgroundAudio:URI,mstBackgroundAudioFadeIn:SCALE,mstBackgroundAudioFadeOut:SCALE]] | Background audio with fade-in and fade-out | 79 | | [[mstExprStyle:STYLE,mstExprDegree:SCALE|TEXT]] | Speaking style and its intensity | 80 | 81 | More details on canonical attribute values can be found at [Speech Synthesis Markup Language (SSML)](https://www.w3.org/TR/speech-synthesis/). For ranges of vendor-specific values please refer to their online documents. Each attribute name in camel case can be rewritten in kebab case (e.g., firstSecondThird <=> first-second-third). Non-vendor-specific attributes can be abbreviated into their first three letters. 82 | 83 | # parseToSsml(input, languageCode, options) 84 | - input `` (required) 85 | - languageCode `` (required: [RFC 1766](https://tools.ietf.org/html/rfc1766)) 86 | - options `` (optional) 87 | - version `` (default: "1.1") 88 | - pretty `` (default: false) 89 | - encoding `` (default: "UTF-8") 90 | 91 | # Example 92 | ```js 93 | var parser = require("wiki2ssml"); 94 | try { 95 | var input = "[[volume:+2dB,speed:50%|Speak this with the volume increased by 2dB at half the default speech rate.]]"; 96 | var ssml = parser.parseToSsml(input, "en-GB", {pretty: true}); 97 | console.log(ssml); 98 | } catch (e) { 99 | if (e instanceof parser.SyntaxError) { 100 | // The input does not have valid WikiVoice markups 101 | } else if (e instanceof parser.ArgumentError) { 102 | // Either the input or the language code is missing 103 | } else { 104 | // Handle any unspecified exceptions 105 | } 106 | } 107 | ``` 108 | will print out: 109 | ```xml 110 | 111 | 112 | Speak this with the volume increased by 2dB at half the default speech rate. 113 | 114 | ``` 115 | -------------------------------------------------------------------------------- /figures/wikivoice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baxtree/wiki2ssml/6cf59208a0953be8cd0d755c04dbca1f1d676fbc/figures/wikivoice.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = require("./src/wiki2ssml"); 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wiki2ssml", 3 | "version": "0.2.21", 4 | "description": "Wiki2SSML provides the WikiVoice markup language used for fine-tuning synthesised voice.", 5 | "license": "Apache-2.0", 6 | "main": "index.js", 7 | "scripts": { 8 | "lint": "./node_modules/eslint/bin/eslint.js ./src/wiki2ssml.js ./test/wiki2ssml-test.js", 9 | "test": "./node_modules/mocha/bin/_mocha ./test/*-test.js", 10 | "coverage": "./node_modules/nyc/bin/nyc.js --reporter=lcov mocha" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/baxtree/wiki2ssml.git" 15 | }, 16 | "keywords": [ 17 | "ssml", 18 | "wikitext", 19 | "text-to-speech", 20 | "voice tuning", 21 | "amazon polly", 22 | "google cloud tts", 23 | "ibm watson tts", 24 | "microsoft azure tts", 25 | "nuance" 26 | ], 27 | "author": { 28 | "name": "Xi Bai", 29 | "email": "xi.bai.ed@gmail.com" 30 | }, 31 | "bugs": { 32 | "url": "https://github.com/baxtree/wiki2ssml/issues" 33 | }, 34 | "homepage": "https://github.com/baxtree/wiki2ssml#readme", 35 | "dependencies": { 36 | "peggy": "^3.0.2", 37 | "prettify-xml": "^1.2.0" 38 | }, 39 | "devDependencies": { 40 | "chai": "^4.3.10", 41 | "eslint": "^8.43.0", 42 | "mocha": "^10.2.0", 43 | "nyc": "^15.1.0", 44 | "xml2js": "^0.6.2" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/wiki2ssml.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var peggy = require("peggy"); 4 | var fs = require("fs"); 5 | var prettifyXml = require("prettify-xml"); 6 | 7 | const _EXTNS = { 8 | "mstts": "http://www.w3.org/2001/mstts", 9 | "vxml": "http://www.w3.org/2001/vxml" 10 | }; 11 | 12 | module.exports = (() => { 13 | try { 14 | var _parser = peggy.generate(fs.readFileSync(__dirname + "/wikivoice.pegjs").toString()); 15 | } 16 | catch (e) { 17 | throw new SyntaxError(e.message); 18 | } 19 | 20 | var _getSsmlHead = (language, version, encoding) => { 21 | return "" + 22 | (version === "1.0" ? "" : "") + 23 | ""; 28 | }; 29 | 30 | var _getSsmlTail = () => { 31 | return ""; 32 | }; 33 | 34 | var _getSsmlAsString = (ssmlBody, language, options) => { 35 | var ext_ns = ""; 36 | for (var namespace in _EXTNS) { 37 | if (ssmlBody.indexOf(namespace + ":") > -1) { 38 | ext_ns += "xmlns:" + namespace + "=\"" + _EXTNS[namespace] + "\" "; 39 | } 40 | } 41 | var ssml = _getSsmlHead(language, options.version, options.encoding).replace("{{EXTNS_PLACEHOLDER}}", ext_ns) + ssmlBody + _getSsmlTail(); 42 | if (options.pretty) { 43 | return prettifyXml(ssml, {indent: 2, newline: "\n"}); 44 | } 45 | return ssml; 46 | }; 47 | 48 | var _parseToSsml = (input, languageCode, options) => { 49 | if (!input || input.length === 0) { 50 | throw new ArgumentError("Input is missing when calling parseToSsml"); 51 | } 52 | if (!languageCode) { 53 | throw new ArgumentError("Language code is missing when calling parseToSsml"); 54 | } 55 | options = (typeof(options) === "undefined") ? {} : options; 56 | if (typeof(options.version) === "undefined") { 57 | options.version = "1.1"; 58 | } 59 | if (typeof(options.pretty) === "undefined") { 60 | options.pretty = false; 61 | } 62 | if (typeof(options.encoding) === "undefined") { 63 | options.encoding = "UTF-8"; 64 | } 65 | try { 66 | var parsed = _parser.parse(input); 67 | } 68 | catch (e) { 69 | throw new SyntaxError(e.message); 70 | } 71 | return _getSsmlAsString(parsed, languageCode, options); 72 | }; 73 | 74 | class ArgumentError extends Error { 75 | constructor(message) { 76 | super(message); 77 | this.name = this.constructor.name; 78 | if (typeof Error.captureStackTrace === "function") { 79 | Error.captureStackTrace(this, this.constructor); 80 | } else { 81 | this.stack = (new Error(message)).stack; 82 | } 83 | } 84 | } 85 | 86 | class SyntaxError extends Error { 87 | constructor(message) { 88 | super(message); 89 | this.name = this.constructor.name; 90 | if (typeof Error.captureStackTrace === "function") { 91 | Error.captureStackTrace(this, this.constructor); 92 | } else { 93 | this.stack = (new Error(message)).stack; 94 | } 95 | } 96 | } 97 | 98 | return { 99 | parseToSsml: _parseToSsml, 100 | 101 | reloadGrammar: (grammar) => { 102 | try { 103 | _parser = peggy.generate(grammar); 104 | } 105 | catch (e) { 106 | throw new SyntaxError(e.message); 107 | } 108 | }, 109 | 110 | parseToPlainText: (input) => { 111 | var parsed = _parseToSsml(input, "ANY"); 112 | return parsed.replace(/(<([^>]+)>)/ig, ""); 113 | }, 114 | 115 | hasValidMarkups: (input) => { 116 | try { 117 | var ssmlBody = _parser.parse(input); 118 | return ssmlBody !== input; 119 | } 120 | catch (e) { 121 | return false; 122 | } 123 | }, 124 | 125 | ArgumentError: ArgumentError, 126 | SyntaxError: SyntaxError 127 | }; 128 | })(); -------------------------------------------------------------------------------- /src/wikivoice.pegjs: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | { 3 | /* eslint-enable */ 4 | function toText(matches) { 5 | for (var i = 0; i < matches.length; i++) { 6 | matches[i] = matches[i][1]; 7 | } 8 | return matches.join("").replace(/["'&<>]/g, (char) => { 9 | switch (char) { 10 | case "\"": return """; 11 | case "'": return "'"; 12 | case "&": return "&"; 13 | case "<": return "<"; 14 | case ">": return ">"; 15 | } 16 | }); 17 | } 18 | 19 | function toTime(matches) { 20 | var sign = matches[0] == null ? "" : matches[0]; 21 | var integral = matches[1].join(""); 22 | var decimal = matches[2] == undefined ? "" : matches[2][0] + matches[2][1].join("") 23 | var unit = matches[3]; 24 | return integral + decimal + unit; 25 | } 26 | 27 | function toVolume(matches) { 28 | if (matches[matches.length - 1] === "dB") { 29 | return matches[0].join("") + "dB"; 30 | } 31 | return matches; 32 | } 33 | 34 | function toRate(matches) { 35 | if (matches[matches.length - 1] === "%") { 36 | return matches[0].join("") + "%"; 37 | } 38 | return matches; 39 | } 40 | 41 | function toPitch(matches) { 42 | if (matches[matches.length - 1] === "%") { 43 | return matches[0].join("") + "%"; 44 | } 45 | else if (matches[matches.length - 1] === "Hz") { 46 | return matches[0].join("") + "Hz"; 47 | } 48 | else if (matches[matches.length - 1] === "st") { 49 | return matches[0].join("") + "st"; 50 | } 51 | return matches; 52 | } 53 | 54 | function toDetail(matches) { 55 | return matches.join(""); 56 | } 57 | 58 | function toFlattened(matches) { 59 | return matches.toString().split(",").join(""); 60 | } 61 | /* eslint-disable */ 62 | } 63 | BEGIN 64 | = text_and_statement:TextAndStatements* text:Text 65 | { 66 | return text_and_statement.join("") + text; 67 | } 68 | 69 | TextAndStatements 70 | = TextParallelStatements / TextSequentialStatements / TextStatement 71 | 72 | TextParallelStatements 73 | = text:Text statements:ParallelStatements 74 | { 75 | return text + statements; 76 | } 77 | 78 | TextSequentialStatements 79 | = text:Text statements:SequentialStatements 80 | { 81 | return text + statements; 82 | } 83 | 84 | TextStatement 85 | = text:Text statement:Statement 86 | { 87 | return text + statement; 88 | } 89 | 90 | Text 91 | = text:(!("[[" / "]]" / "*[[" / "]]*" / "#[[" / "]]#") .)* 92 | { 93 | return toText(text); 94 | } 95 | 96 | Target 97 | = text_left:Text statement:Statement* text_right:Text 98 | { 99 | return text_left + statement + text_right; 100 | } 101 | 102 | ParallelStatements 103 | = "*" statements:Statement+ "*" 104 | { 105 | return "" + statements.join("") + ""; 106 | } 107 | 108 | SequentialStatements 109 | = "#" statements:Statement+ "#" 110 | { 111 | return "" + statements.join("") + ""; 112 | } 113 | 114 | Statement 115 | = Prosody 116 | / Emphasis 117 | / Silence 118 | / Substitute 119 | / Audio 120 | / Lang 121 | / Paragraph 122 | / Sentence 123 | / Phoneme 124 | / Type 125 | / Voice 126 | / PartOfSpeech 127 | / Mark 128 | / SeeAlso 129 | / CacheControl 130 | / Lexicon 131 | / VendorExtension 132 | 133 | Prosody 134 | = SpeedPitchVolume / SpeedVolumePitch / PitchSpeedVolume / PitchVolumeSpeed / VolumeSpeedPitch / VolumePitchSpeed 135 | / SpeedPitch / SpeedVolume / PitchSpeed / PitchVolume / VolumeSpeed / VolumePitch 136 | / Speed / Pitch / Volume 137 | 138 | SpeedPitchVolume 139 | = "[[" _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "," _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "," _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "|" target:Target "]]" 140 | { 141 | return '' + target + ''; 142 | } 143 | 144 | SpeedVolumePitch 145 | = "[[" _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "," _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "," _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "|" target:Target "]]" 146 | { 147 | return '' + target + ''; 148 | } 149 | 150 | PitchSpeedVolume 151 | = "[[" _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "," _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "," _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "|" target:Target "]]" 152 | { 153 | return '' + target + ''; 154 | } 155 | 156 | PitchVolumeSpeed 157 | = "[[" _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "," _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "," _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "|" target:Target "]]" 158 | { 159 | return '' + target + ''; 160 | } 161 | 162 | VolumeSpeedPitch 163 | = "[[" _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "," _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "," _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "|" target:Target "]]" 164 | { 165 | return '' + target + ''; 166 | } 167 | 168 | VolumePitchSpeed 169 | = "[[" _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "," _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "," _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "|" target:Target "]]" 170 | { 171 | return '' + target + ''; 172 | } 173 | 174 | SpeedPitch 175 | = "[[" _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "," _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "|" target:Target "]]" 176 | { 177 | return '' + target + ''; 178 | } 179 | 180 | SpeedVolume 181 | = "[[" _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "," _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "|" target:Target "]]" 182 | { 183 | return '' + target + ''; 184 | } 185 | 186 | PitchSpeed 187 | = "[[" _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "," _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "|" target:Target "]]" 188 | { 189 | return '' + target + ''; 190 | } 191 | 192 | PitchVolume 193 | = "[[" _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "," _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "|" target:Target "]]" 194 | { 195 | return '' + target + ''; 196 | } 197 | 198 | VolumeSpeed 199 | = "[[" _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "," _ ("speed"i / "spe"i) _ ":" _ speed:RATE _ "|" target:Target "]]" 200 | { 201 | return '' + target + ''; 202 | } 203 | 204 | VolumePitch 205 | = "[[" _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "," _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "|" target:Target "]]" 206 | { 207 | return '' + target + ''; 208 | } 209 | 210 | Speed 211 | = "[[" _ ("speed"i / "spe"i) _ ":" _ speed:RATE _"|" target:Target "]]" 212 | { 213 | return '' + target + ''; 214 | } 215 | 216 | Pitch 217 | = "[[" _ ("pitch"i / "pit"i) _ ":" _ pitch:PITCH _ "|" target:Target "]]" 218 | { 219 | return '' + target + ''; 220 | } 221 | 222 | Volume 223 | = "[[" _ ("volume"i / "vol"i) _ ":" _ volume:VOLUME _ "|" target:Target "]]" 224 | { 225 | return '' + target + ''; 226 | } 227 | 228 | Emphasis 229 | = "[[" _ ("emphasis"i / "emp"i) _ ":" _ level:LEVEL _ "|" target:Target "]]" 230 | { 231 | return '' + target + ''; 232 | } 233 | 234 | Silence 235 | = SilenceTimeStrength / SilenceStrengthTime / SilenceTime / SilenceStrength 236 | 237 | SilenceTimeStrength 238 | = "[[" _ ("silence"i / "sil"i) _ ":" _ time:TIME _ "," _ ("strength"i / "str"i) _ ":" _ strength:STRENTH _ "]]" 239 | { 240 | return ''; 241 | } 242 | 243 | SilenceStrengthTime 244 | = "[[" _ ("strength"i / "str"i) _ ":" _ strength:STRENTH _ "," _ ("silence"i / "sil"i) _ ":" _ time:TIME _ "]]" 245 | { 246 | return ''; 247 | } 248 | 249 | SilenceTime 250 | = "[[" _ ("silence"i / "sil"i) _ ":" _ time:TIME _ "]]" 251 | { 252 | return ''; 253 | } 254 | 255 | SilenceStrength 256 | = "[[" _ ("strength"i / "str"i) _ ":" _ strength:STRENTH _ "]]" 257 | { 258 | return ''; 259 | } 260 | 261 | Substitute 262 | = "[[" _ ("substitute"i / "sub"i) _ ":" substitute:(!"|" .)+ "|" original:(!"]]" .)* "]]" 263 | { 264 | return '' + toText(original) + ''; 265 | } 266 | 267 | Audio 268 | = AudioSrc / AudioSoundLevel / AudioClipBeginEnd / AudioClipEndBegin 269 | / AudioClipBeginEndRepeatCount / AudioClipRepeatCountBeginEnd 270 | / AudioClipBeginEndSpeed / AudioClipSpeedBeginEnd 271 | / AudioClipRepeatCountDuration / AudioClipRepeatDurationCount 272 | 273 | AudioSrc 274 | = "[[" _ ("audio"i / "aud"i) _ ":" uri:(!("," / "]]") .)+ "]]" 275 | { 276 | return '