什么是文本到语音转换器?! w6 B2 R' V/ J4 S+ _5 _
文字转语音 (TTS) 技术,将您的书面文字呈现为口头文字。有了这个工具,你可以把你的文字变成各种声音的语音。(文本到语音转换器应用程序)。如果您的文本超过 80 个字符,您还可以通过按停止和通话按钮暂停和恢复通话。, [; C& S. s! k+ e# m4 n
TTS 的目的是什么?8 T5 A$ Y. f# r. _7 B* C
TTS 可以将来自计算机或其他数字设备的文本转换为语音。TTS 除了对阅读有困难的孩子非常有益外,还可以帮助孩子写作、编辑,甚至集中注意力。 M) |4 E7 {& a
$ n5 l3 ~+ X- u' I+ }5 M
使用 HTML 和 JavaScript 的文本转语音6 x* b9 p8 [# C0 w8 b0 b' y& `
文本转语音 (TTS) 技术将您的文本转换为语音。您可以使用此项目(文本到语音转换器应用程序)将文本转换为不同声音的语音。如果您的文本字符长度超过 80,您还可以使用停止和说话按钮暂停和继续。
. i' t0 w, }/ @9 Q+ p" o/ P它是用 HTML、CSS 和纯 JavaScript 创建的。此 TTS 程序不使用外部 JavaScript 库或 API,希望您会喜欢我们的文章。* k+ `# ^; {& N# l6 L& \
现在,我们就开始吧。
/ E4 s8 p- j) c) e7 G第 1 步:文本转语音添加HTML代码3 L7 C' R# V' j! N
创建一个名为 index.html 的 HTML 文件并将以下代码粘贴到其中。请记住使用 .html 扩展名保存文件。0 W! F! e& J) t( E
- <html lang="zh">
- <head>
- <link rel="stylesheet" href="style.css">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- </head>
- <body>
- <!-- Works best in Chrome! -->
- <div class="voiceinator">
- <h1>Text-To-Speech Converter</h1>
- <select name="voice" id="voices">
- <option value="">Select A Voice</option>
- </select>
- <label for="rate">Rate:</label>
- <input name="rate" type="range" min="0" max="3" value="1" step="0.1">
- <label for="pitch">Pitch:</label>
- <input name="pitch" type="range" min="0" max="2" step="0.1">
- <textarea name="text" placeholder="Start typing...">Follow CodewithRandom</textarea>
- <button id="stop">Stop</button>
- <button id="speak">Speak</button>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
我们将从添加项目的结构开始,但首先我们需要在链接中包含某些项目,例如我们使用了多个 CSS 和 javascript 文件,我们需要将它们链接到我们的 HTML 中 文件。5 i# Y% K; D6 y0 C# Z
- //Head Section
- <link rel="stylesheet" href="style.css">
- //Body Section
- <script src="script.js"></script>
现在我们将文本到语音转换器的结构放在 body 标签中。<div> 标签将用于为我们的文本到语音转换器构建一个容器。
- _' Q) ~! N0 I- }" _0 T" K使用 <h1> 标签,我们将在我们的 div 中为我们的文本到语音转换器添加一个标题。
- @2 w- X6 i5 Y+ [我们将向我们的文本到语音转换器添加大量语音,并使用 <select> 元素为不同的语音生成备选列表。' r ~" q* G( n3 ?9 w% @4 i9 W' z; R
现在我们将使用类型设置为范围的 <input> 标签添加两个滑块。第一个滑块将用于更改语速。我们可以通过拖动滑块来改变声音的速度。第二个滑块将用于在高音和低音之间调整声音的音调。
1 o3 V/ B- }/ v1 B$ X1 g6 N现在,我们将利用 <textarea> 元素构建一个文本区域,用户可以在其中编写文本,画外音艺术家将阅读它。现在,我们将使用按钮标签制作两个按钮:停止和开始说话。# ]" {. ?! j( v7 R& Q$ [
我们不需要任何其他东西来开发文本到语音转换器的结构。既然我们已经学会了如何使用 CSS,我们就可以为 TTS 设置样式了。但首先,让我们看一下我们的框架。
% [- L# G4 E9 W v8 H" ^- d第 2 步:添加 CSS 代码
4 I8 B# Q. G0 o& C- w层叠样式表 (CSS) 是一种标记语言,用于描述以 HTML 或 XML 编写的文档的呈现方式。CSS 与 HTML 和 JavaScript 一样,是WEB开发的重要组成部分。创建一个名为 style.css 的 CSS 文件,并将以下代码粘贴到其中。始终使用 .css 扩展名保存文件。& o. N' W" W8 T X8 m5 w; Y
- html {
- font-size: 10px;
- box-sizing: border-box;
- }
- *,
- *:before,
- *:after {
- box-sizing: inherit;
- }
- body {
- margin: 0;
- padding: 0;
- font-family: "PT Sans", sans-serif;
- background-color: #d5d9e5;
- color: #292b2c;
- display: flex;
- min-height: 100vh;
- align-items: center;
- }
- .voiceinator {
- padding: 2rem;
- width: 50rem;
- margin: 0 auto;
- border-radius: 1rem;
- position: relative;
- background: #fff;
- overflow: hidden;
- z-index: 1;
- box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.1);
- }
- h1 {
- width: calc(100% + 4rem);
- margin: 0 0 2rem -2rem;
- padding: 0.5rem;
- text-align: center;
- font-size: 4rem;
- font-weight: 100;
- font-family: "PT Sans", sans-serif;
- }
- .voiceinator input,
- .voiceinator button,
- .voiceinator select,
- .voiceinator textarea {
- width: 100%;
- display: block;
- margin: 10px 0;
- padding: 10px;
- font-size: 2rem;
- background: #fbfbfc;
- outline: 0;
- font-family: "PT Sans", sans-serif;
- border: 1px solid #c8c7cb;
- border-radius: 2px;
- }
- label {
- font-size: 2rem;
- }
- textarea {
- height: 20rem;
- }
- .voiceinator button {
- background: #72a3da;
- color: #fff;
- border: 0;
- width: 49%;
- float: left;
- font-family: "PT Sans", sans-serif;
- margin-bottom: 0;
- font-size: 2rem;
- cursor: pointer;
- position: relative;
- }
- .voiceinator button:active {
- top: 2px;
- }
- .voiceinator button:nth-of-type(1) {
- margin-right: 2%;
- }
- input[type="range"] {
- -webkit-appearance: none;
- border: 1px solid transparent;
- padding-top: 8px;
- background: #fff;
- }
- input[type="range"]::-webkit-slider-runnable-track {
- height: 5px;
- background: #e1e1e3;
- border: none;
- }
- input[type="range"]::-webkit-slider-thumb {
- -webkit-appearance: none;
- border: none;
- height: 14px;
- width: 14px;
- border-radius: 50%;
- background: #72a3da;
- margin-top: -4px;
- }
- input[type="range"]:focus {
- outline: none;
- }
- input[type="range"]:focus::-webkit-slider-runnable-track {
- background: #ccc;
- }
- input[type="range"]::-moz-range-track {
- height: 5px;
- background: #e1e1e3;
- border: none;
- }
- input[type="range"]::-moz-range-thumb {
- border: none;
- height: 14px;
- width: 14px;
- border-radius: 50%;
- background: #72a3da;
- }
- input[type="range"]:-moz-focusring {
- outline: 1px solid #dcdde2;
- outline-offset: -1px;
|