Speak: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 3: Line 3:
== Description ==
== Description ==


The Speak function takes the ''text'' and reads it out loud. It uses the default browser ''voice'', unless a different voice is specified.
The Speak function reads the ''text'' and out loud. It uses the default browser ''voice'', unless a different voice is specified.


The sound of a voice can be modified by setting the following global variables:
The sound of a voice can be modified by setting the following global variables:
Line 11: Line 11:
| NSB.speech.lang || The language code. Examples are en-US, de-DE, ar-SA.
| NSB.speech.lang || The language code. Examples are en-US, de-DE, ar-SA.
|-
|-
| NSB.speech.pitch || The pitch of the voice. A value between 0 and 2. Default is 1.
| NSB.speech.pitch || The pitch of the voice. A value between 0 and 2. Default is 1. Fractions are allowed.
|-
|-
| NSB.speech.rate || The speed of the voice. A value from 0 to 10. Default is 1.
| NSB.speech.rate || The speed of the voice. A value from 0 to 10. Default is 1. Fractions are allowed.
|-
|-
| NSB.speech.volume || The loudness of the voice. A value from 0 to 1. Default is 1.
| NSB.speech.volume || The loudness of the voice. A value from 0 to 1. Default is 1. Fractions are allowed.
|}
|}


Not all combinations of pitch, rate and volume are available for all voices.
Not all combinations of pitch, rate and volume are available for all voices.


To see the list of voices for a particular device, use the SpeedSynthesis sample.
To see the list of voices for a particular device, use the SpeedSynthesisAPI sample. You can also check the NSB.voices global variable.


There are differences in implementation between different browsers and operating systems. In particular, each OS has a different list of voices.  
There are differences in implementation between different browsers and operating systems. In particular, each OS has a different list of voices.


=== Safari/iOS ===
=== Safari/iOS ===
Line 32: Line 32:
Support is good. There are about 20 voices. Note that the names of the voices are not the same as in Safari, so different voice names are needed. No Arabic support.
Support is good. There are about 20 voices. Note that the names of the voices are not the same as in Safari, so different voice names are needed. No Arabic support.


=== Windows 10 ====
=== Windows 10 ===


Not supported, but it is under consideration.
Not supported, but it is under consideration.


== Example (BASIC) ==
== Example ==


<pre>
<tabber>
Rem Abs Example
JavaScript=
'Abs returns the absolute value of a number.
<syntaxhighlight lang="JavaScript">
Print "Abs(-2) = " & Abs(-2)
// Speak Example
Print "Abs(2) = " & Abs(2)
Speak("Hello World!");
</pre>


== Example (JavaScript) ==
// Set volume to quiet:
NSB.speech.volume = .2;
Speak("Hello World");
NSB.speech.volume = 1;


<pre>
// Read in German:
//Abs Example
NSB.speech.lang = "de-DE";
//Abs returns the absolute value of a number.
Speak(Button3.text, "Google Deutsch");
NSB.Print("Abs(-2) = " + Math.abs(-2));
NSB.speech.lang = navigator.language;
NSB.Print("Abs(2) = " + Math.abs(2));
 
</pre>
// Use a different voice:
 
var voice;
if (InStr(navigator.vendor, "Apple")) {
  voice = "Alex";
}
if (InStr(navigator.vendor, "Google")) {
  voice = "Google UK English Male";
}
Speak("Hello World", voice);</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
Rem Speak Example
Speak("Hello World!")
 
' Set volume to quiet:
NSB.speech.volume = .2
Speak("Hello World")
NSB.speech.volume = 1
 
' Read in German:
NSB.speech.lang = "de-DE"
Speak(Button3.text, "Google Deutsch")
NSB.speech.lang = navigator.language
 
' Use a different voice:
Dim voice
If InStr(navigator.vendor,"Apple") Then voice = "Alex"
If InStr(navigator.vendor,"Google") Then voice = "Google UK English Male"
Speak("Hello World", voice)
 
</syntaxhighlight>
</tabber>


== Output ==
== Output ==


<pre>
(no output - plays through device speaker)
Abs(-2) = 2
Abs(2) = 2
</pre>


== Related Items ==
== Related Items ==
[[sgn|Sgn]]


[[Category:Language Reference]]
[[Category:Language Reference]]


[[Category:Math]]
[[Category:Messages]]

Latest revision as of 23:16, 24 July 2019

Speak(text[,voice)

Description

The Speak function reads the text and out loud. It uses the default browser voice, unless a different voice is specified.

The sound of a voice can be modified by setting the following global variables:

NSB.speech.lang The language code. Examples are en-US, de-DE, ar-SA.
NSB.speech.pitch The pitch of the voice. A value between 0 and 2. Default is 1. Fractions are allowed.
NSB.speech.rate The speed of the voice. A value from 0 to 10. Default is 1. Fractions are allowed.
NSB.speech.volume The loudness of the voice. A value from 0 to 1. Default is 1. Fractions are allowed.

Not all combinations of pitch, rate and volume are available for all voices.

To see the list of voices for a particular device, use the SpeedSynthesisAPI sample. You can also check the NSB.voices global variable.

There are differences in implementation between different browsers and operating systems. In particular, each OS has a different list of voices.

Safari/iOS

Support is excellent. Mac OS and iOS have about 83 different voices. Most languages are supported, including Arabic.

Chrome/Android

Support is good. There are about 20 voices. Note that the names of the voices are not the same as in Safari, so different voice names are needed. No Arabic support.

Windows 10

Not supported, but it is under consideration.

Example

// Speak Example
Speak("Hello World!");

// Set volume to quiet:
NSB.speech.volume = .2;
Speak("Hello World");
NSB.speech.volume = 1;

// Read in German:
NSB.speech.lang = "de-DE";
Speak(Button3.text, "Google Deutsch");
NSB.speech.lang = navigator.language;

// Use a different voice:

var voice;
if (InStr(navigator.vendor, "Apple")) {
  voice = "Alex";
}
if (InStr(navigator.vendor, "Google")) {
  voice = "Google UK English Male";
}
Speak("Hello World", voice);

Rem Speak Example
Speak("Hello World!")

' Set volume to quiet:
NSB.speech.volume = .2
Speak("Hello World")
NSB.speech.volume = 1

' Read in German:
NSB.speech.lang = "de-DE"
Speak(Button3.text, "Google Deutsch")
NSB.speech.lang = navigator.language

' Use a different voice:
Dim voice
If InStr(navigator.vendor,"Apple") Then voice = "Alex"
If InStr(navigator.vendor,"Google") Then voice = "Google UK English Male"
Speak("Hello World", voice)

Output

(no output - plays through device speaker)

Related Items