Speech synthesis DEMO for AmiBroker 6.29 (and above)

Hello. I would like to present a short AFL code for AmiBroker 6.29, allowing to perform simple speech synthesis tasks. Using this code, everyone can:

  1. Test how many SAPI voices are installed on the computer
  2. Select one of the voices
  3. Select voice rate
  4. Select voice volume
  5. Enter sample text to be read
  6. Read the text from the clipboard (like news articles etc.)
  7. Automatically test/hear all installed voices (clicking the Demo button)

S1

_SECTION_BEGIN( "AB Speech synthesis DEMO" );

// --------- AmiBroker speech synthesis DEMO by: Milosz Mazurkiewicz  -----------------------
/// @link https://forum.amibroker.com/t/speech-synthesis-demo-for-amibroker-6-29/7816

Version( 6.29 ); // AmiBroker 6.29 required to run this code

GfxSetBkMode( 1 );
GfxGradientRect( 1, 1, Status( "pxwidth" ), Status( "pxheight" ), colorWhite, colorLightGrey );

GfxSelectFont( "Segoe UI", 10, 700 ); //GfxSelectFont( "Tahoma", 9, 700 );
GfxSetTextColor( colorGreen );
GfxTextOut( "AB 6.29 Speech synthesis DEMO ", 10, 5 );

GuiSetFont( "Segoe UI", 9 ); //GuiSetFont( "Arial", 9 );
GfxSelectFont( "Segoe UI", 9, 500 ); //GfxSelectFont( "Arial", 9, 500 );
GfxSetTextColor( colorBlack );


function SP( GuiNumber, DefaultText, x, y, width, height )

{

	GuiEditTextStatic = StaticVarGetText( "GuiEditTextStatic" + GuiNumber );

    if( GuiEdit( GuiNumber, x, y, width, height, notifyEditChange ) == guiNew )

    {

        if ( GuiEditTextStatic == "" )

        {
            GuiEditTextStatic = GuiEditText = DefaultText;
            StaticVarSetText( "GuiEditTextStatic" + GuiNumber, DefaultText, True );
        }

        GuiSetText( GuiEditText = GuiEditTextStatic, GuiNumber );
    }
    
    else
    
    {

    GuiEditText =  GuiGetText( GuiNumber );

    if( GuiEditText != GuiEditTextStatic ) StaticVarSetText( "GuiEditTextStatic" + GuiNumber, GuiEditText, True );
    
    }

    return( GuiEditText );

}

VC = VoiceCount();
GfxTextOut( "SAPI voices found :                  " + Vc , 10, 40 );
GfxTextOut( "Voice selected :   (0 - " + ( Vc - 1 ) + ")" , 10, 70 );
GfxTextOut( "Voice rate :  (-10 - 10)" , 10, 100 );
GfxTextOut( "Volume :   (0-100)" , 10, 130 );
GfxTextOut( "Enter text to speak : " , 10, 160 );
VoiceNumber = StrToNum( SP( 10, "0", 160, 70, 50, 20 ) );
VoiceRate = StrToNum( SP( 11, "0", 160, 100, 50, 20 ) );
VoiceVolume = StrToNum( SP( 12, "100", 160, 130, 50, 20 ) );
Text = SP( 13, "Sample text to speak", 10, 195, 200, 20 );

GuiButton( "Read", 15, 160, 160, 50, 25, 1 );
GuiButton( "Clipboard", 16, 10, 230, 60, 25, 1 );
GuiButton( "Demo", 17, 80, 230, 60, 25, 1 );
GuiButton( "Stop", 18, 150, 230, 60, 25, 1 );

id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );

if( event == 1 )

{
    VoiceSelect( VoiceNumber );
    VoiceSetRate( VoiceRate );
    VoiceSetVolume( VoiceVolume );

}

if( id == 15 AND event == 1 ) Say( "" + Text, 0 );

if( id == 18 AND event == 1 ) Say( " Stop!", 1 );

if( id == 16 AND event == 1 ) Say( "" + ClipboardGet(), 0 );

if( id == 17 AND event == 1 ) for( i = 0 ; i < VC - 1; i++ )

    {

        VoiceSelect( i );
        Say( "This is an example of voice " + i + ". ", 0 );

    }

_SECTION_END();

I have tested it only on WIN7. If for any reason it doesn't work properly on WIN10 or other platforms, let me know. You can also easily change the fonts used in this code if they don't look good.

Have fun and take advantage of the Speech synthesis in AmiBroker :slight_smile:

19 Likes

@Milosz, nice example. Thanks for sharing it.

I noticed a small issue; the last available voice is not played in the "Demo" due to prematurely ending the loop caused by the incorrect statement i < VC - 1;

Change the line to:

if( id == 17 AND event == 1 ) for( i = 0 ; i < VC; i++ )
4 Likes

Nice work @Milosz, I have used voice notification on windows 10 some time ago and it works fine, However I used it as voice alert to keep me informed with tickers of interest as soon as they show a signal for buy or sell.
Thank you again @Milosz, keep good works.

1 Like

@beppe - thanks for correcting my mistake :+1: I have so many SAPI voices installed, that I haven't noticed this issue :wink:

@mohamed.gad - thanks for your kind words.

This is a very simple code which I posted to show/remind that the latest AB versions offer a few additional speech synthesis features which some users might not be aware of. Besides I always try to encourage everyone to take advantage of the speech synthesis. In my opinion it is indispensable in a daytrading. It gives a trader one or more additional pair of eyes as he/she can easily perceive and comprehend much more information coming from the market.

AmiBroker is not only about charting and backtesting. AFL gives a wide range of possibilities. I'm not a programmer and I don't know C/C++, but as I have shown in this thread I was able to code in AFL my own news aggregator which gathers and processes information coming from various sources - like dozens different web pages, news sources, blogs, tweets, RSS streams etc. Such custom solutions give me an edge on my local market - especially during the session when every minute and every second counts. But this and other similar ones, would be much, much worse without speech synthesis...

6 Likes