SAPI: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: SAPI
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## SAPI | |
| 2 | + | ||
| 3 | + | SAPI stands for Speech Application Programming Interface, a Microsoft Windows API used to perform text-to-speech (TTS). | |
| 4 | + | ||
| 5 | + | ### Use with Free Pascal | |
| 6 | + | ||
| 7 | + | Lazarus / Free Pascal can use this interface to perform TTS. Source: forum post: [1](http://lazarus.freepascal.org/index.php/topic,17852.0) | |
| 8 | + | ||
| 9 | + | On Windows Vista and above, you will run in trouble with the FPU interrupt mask (see [2](http://stackoverflow.com/questions/3032739/delphi-sapi-text-to-speech)). The code dealing with SavedCW is meant to work around this. | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | uses | |
| 13 | + | ...,comobj; | |
| 14 | + | var | |
| 15 | + | SavedCW: Word; | |
| 16 | + | SpVoice: Variant; | |
| 17 | + | begin | |
| 18 | + | SpVoice := CreateOleObject('SAPI.SpVoice'); | |
| 19 | + | // Change FPU interrupt mask to avoid SIGFPE exceptions | |
| 20 | + | SavedCW := Get8087CW; | |
| 21 | + | try | |
| 22 | + | Set8087CW(SavedCW or $4); | |
| 23 | + | SpVoice.Speak('hi', 0); | |
| 24 | + | finally | |
| 25 | + | // Restore FPU mask | |
| 26 | + | Set8087CW(SavedCW); | |
| 27 | + | end; | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | For the options available with SpVoice.Speak and other SpVoice methods see <http://msdn.microsoft.com/en-us/library/ms723609(v=vs.85>).aspx. | |
| 31 | + | ||
| 32 | + | > **Warning:** The OLE object created in above snippet will be destroyed automatically when the SpVoice variant goes out of scope. For larger texts the use of the asynchronous mode allows to keep your application reactive while speak is running. In that case it is especially important to control carefully the lifetime of the object and store it in a global or class variable. Destroying the object while speak is still running can cause crashes. | |
| 33 | + | ||
| 34 | + | Changing the FPU interrupt mask can be done at any moment before loading the ole object and if your application is not using any floating point arithmetic there is no need to reset the FPU interrupt mask to its original value. | |
| 35 | + | ||
| 36 | + | ### Assigning text to a variable. A WideString must used | |
| 37 | + | ||
| 38 | + | ``` pascal | |
| 39 | + | File:procedure TForm1.Button1Click(Sender: TObject); | |
| 40 | + | Var | |
| 41 | + | SpVoice1: Variant; | |
| 42 | + | VoiceString: WideString; // WideString must be used to assign variable for speech to function, can be Global. | |
| 43 | + | begin | |
| 44 | + | SpVoice1 := CreateOleObject('SAPI.SpVoice'); // Can be assigned in form.create | |
| 45 | + | VoiceString := Button1.Caption; // variable assignment | |
| 46 | + | SpVoice1.Speak(VoiceString,0); | |
| 47 | + | end; | |
| 48 | + | ``` | |
| 49 | + | ||
| 50 | + | Thank you nsunny from 2013 post. | |
| 51 | + | ||
| 52 | + | ## Alternatives | |
| 53 | + | ||
| 54 | + | See the [[Speech Synthesis]] article. | |
| 55 | + | ||
| 56 | + | ## See also | |
| 57 | + | ||
| 58 | + | - <https://github.com/red-prig/twitch_vip_bot/blob/trunk/AudioEngine.pas> for more details on using SAPI in Free Pascal. | |
| 59 | + | ||
| 60 | + | --- | |
| 61 | + | *From the Free Pascal wiki, page "SAPI", as it stood on 2021-09-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 62 | + | Category: FPC | |
| 63 | + | Category: Lazarus | |
| 64 | + | Category: Speech synthesis | |
| 65 | + | Category: Tutorials | |
| 66 | + | Category: Windows |
