• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Procedure question

Hi,

I have a form with a listbox on it (lbVoices), where the speech-engines will be loaded.
and a small richedit (reText) where the user can enter text that he/she would like to test.
and a test-button (TestBtn). And 3 trackbars for the volume, pitch and rate
(tbVolume, tbRate and tbPitch).

What I would like my test button to do is:

1. at creation of the form the button has the caption: Test (nothing have to be done here).
2. After pressing on it, it has to speak to spoken text of the reText (nothing have to be done here).

    => SpVoice1.Speak(reText.Text, SVSFlagsAsync)

3. But when this line "SpVoice1.Speak(reText.Text, SVSFlagsAsync)"
    is exectuted the caption of the button has to be changed to: Stop

4. When the user presses the button with Stop on it this line will be executed:

  => SpVoice1.Skip('Sentence', MaxInt)

    and the caption has to be changed to TEST again...

5. It's not aloud to change the pitch, volume or rate when the speach-engine is speaking.
    so the trackbars have to be disabled if the speach-engine is speaking and enable
    when the speach-engine is not speaking.

Who can help me with this?

P
   
type
  TSettingsDlg = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    OKBtn: TButton;
    CancelBtn: TButton;
    HelpBtn: TButton;
    Label2: TLabel;
    Label7: TLabel;
    reText: TRichEdit;
    SpVoice1: TSpVoice;
    TestBtn: TButton;
    LbVoices: TListBox;
    tbRate: TTrackBar;
    tbVolume: TTrackBar;
    tbPitch: TTrackBar;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    lblRate: TLabel;
    lblVolume: TLabel;
    lblPitch: TLabel;
    procedure tbPitchChange(Sender: TObject);
    procedure tbRateChange(Sender: TObject);
    procedure tbVolumeChange(Sender: TObject);
    procedure TestBtnClick(Sender: TObject);
    procedure LbVoicesClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    SettingsFile: string;
    procedure LoadSettings;
    procedure SaveSettings;
  public
    { Public declarations }
  end;

var
  SettingsDlg: TSettingsDlg;

implementation

uses Main;

{$R *.dfm}

procedure TSettingsDlg.FormCreate(Sender: TObject);
var
  I: Integer;
  SOToken: ISpeechObjectToken;
  SOTokens: ISpeechObjectTokens;
begin
  SpVoice1.EventInterests := SVEAllEvents;
  SOTokens := SpVoice1.GetVoices('', '');
  for I := 0 to SOTokens.Count - 1 do
  begin
    SOToken := SOTokens.Item(I);
    lbVoices.Items.AddObject(SOToken.GetDescription(0), TObject(SOToken));
    SOToken._AddRef;
  end;
  if lbVoices.Items.Count > 0 then
  begin
    lbVoices.ItemIndex := 0;
  end;
  tbRate.Position := SpVoice1.Rate;
  lblRate.Caption := IntToStr(tbRate.Position);
  tbVolume.Position := SpVoice1.Volume;
  lblVolume.Caption := IntToStr(tbVolume.Position);
  LoadSettings;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.FormDestroy(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to lbVoices.Items.Count - 1 do
  ISpeechObjectToken(Pointer(lbVoices.Items.Objects[I]))._Release;
  SaveSettings;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.LbVoicesClick(Sender: TObject);
var
  SOToken: ISpeechObjectToken;
begin
  begin
    SOToken := ISpeechObjectToken(Pointer(
      lbVoices.Items.Objects[lbVoices.ItemIndex]));
    SpVoice1.Voice := SOToken;
  end
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.tbPitchChange(Sender: TObject);
begin
  lblPitch.Caption := IntToStr(tbPitch.Position);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.tbRateChange(Sender: TObject);
begin
  SpVoice1.Rate := tbRate.Position;
  lblRate.Caption := IntToStr(tbRate.Position);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.tbVolumeChange(Sender: TObject);
begin
  SpVoice1.Volume := tbVolume.Position;
  lblVolume.Caption := IntToStr(tbVolume.Position);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.TestBtnClick(Sender: TObject);
begin
    SpVoice1.Speak(reText.Text, SVSFlagsAsync)
end;
(*---------------------------------------------------*)
end.

Open in new window

0
peterkiers
Asked:
peterkiers
  • 4
  • 3
1 Solution
 
cebassoCommented:
procedure TSettingsDlg.TestBtnClick(Sender: TObject);
begin
  case TestBtn.Tag of
  0:
  begin
    //isnt started, so lets start it
    TestBtn.Caption := 'Stop';
    TestBtn.Tag := 1;
    SpVoice1.Speak(reText.Text, SVSFlagsAsync);
    //you can disable others things here
  end;
  1:
  begin
    //is started, lets stop it
    TestBtn.Caption := 'Test';
    TestBtn.Tag := 0;
    SpVoice1.Skip('Sentence', MaxInt);
    //and enable things here
  end;
end;

Regards,
0
 
cebassoCommented:
ops... i forget an end for the case...

procedure TSettingsDlg.TestBtnClick(Sender: TObject); 
begin
 case TestBtn.Tag of
 0:
 begin
   //isnt started, so lets start it
   TestBtn.Caption := '&Stop';
   TestBtn.Tag := 1;
   SpVoice1.Speak(reText.Text, SVSFlagsAsync);
   //you can disable others things here
 end;
 1:
 begin
   //is started, lets stop it
   TestBtn.Caption := '&Test';
   TestBtn.Tag := 0;
   SpVoice1.Skip('Sentence', MaxInt);
   //and/or enable things here
  end;
  end;
end;

Open in new window

0
 
peterkiersAuthor Commented:
I have ; expected but . found

Do I have an End to short.
I don't understand there are as many Begin's
as End's!

p.
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
peterkiersAuthor Commented:
One little detail.

After the speach-engine has spoken the caption of the button has to
become Test again. And it doesn't...

0
 
cebassoCommented:
Hello!

Have you tried the second code?

for each begin we have an end, and case need an end too...
i did outside delphi... sorry
0
 
cebassoCommented:
"One little detail.

After the speach-engine has spoken the caption of the button has to
become Test again. And it doesn't..."

hmm its a little more complex since with your code i can't figure out how to "know" when the engine has terminatted...
spVoice seems to be a component, so take a look if there is a Event or something like on it where you are able to know that
OnTerminatted? OnSpoken?
0
 
peterkiersAuthor Commented:
Yes your right there is an event for that.

I have found it. Thanks.
0
 
Geert GruwezOracle dbaCommented:
just my 2 cents:

as i look at it, unless the speech engine uses a threading approach, this will not work

to update the Caption button immediately you need to let it be repainted,
the component update procedure can solve this

procedure TSettingsDlg.TestBtnClick(Sender: TObject);
begin
 case TestBtn.Tag of
   0:
   begin
     //isnt started, so lets start it
     TestBtn.Caption := '&Stop';
     TestBtn.Tag := 1;
     TestBtn.Update;
     SpVoice1.Speak(reText.Text, SVSFlagsAsync);
     //you can disable others things here
   end;
   1:
   begin
     //is started, lets stop it
     TestBtn.Caption := '&Test';
     TestBtn.Tag := 0;
     SpVoice1.Skip('Sentence', MaxInt);
     //and/or enable things here
    end;
  end;
end;

with no threads the form will not respond to mouse-clicks, and you will not be able to stop the text speaking
this is because the main thread can only do 1 thing: run the speech engine

in a threading approach, you would start a thread to let the speech engine run
the main thread could then still handle the mouse and user input (like the click on stop)
and then respond by pausing or killing the thread for the speech engine
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now