troubleshooting Question

Read the values from Inifile.

Avatar of Peter Kiers
Peter KiersFlag for Netherlands asked on
Delphi
13 Comments1 Solution558 ViewsLast Modified:
Hi,

I have made a dialogform called SettingsDlg.
On this form I have a listbox (called lbVoices) where
the speech-engines get loaded. And 2 trackbars
for the Volume and Rate(called tbVolume and tbRate).

The selections that are made in the SettingsDlg
will be saved and loaded from an INI-file. The
purpose of the SettingsDlg is to set the default
Speech-engine, rate and volume.

But on the main form (called MainForm) I have
a Combobox called (cbVoices) and 2 spinedits
for the volume and rate (VolspEdt and RateEdt) also.

What I would like is that alle the speech-engines
get loaded in the cbvoices aslo but the engine selected
that is mentioned in the ini-file. And the spinedits have
to get their value from the ini-file too.

Is that possible?
If so who can help me?

I have put the code of the SettingsDlg in the code-section.

and these are the procedures of the mainform that has to be changed:

procedure TMainForm.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);
    cbVoices.Items.AddObject(SOToken.GetDescription(0), TObject(SOToken));
    SOToken._AddRef;
  end;
  if cbVoices.Items.Count > 0 then
  begin
    cbVoices.ItemIndex := 0;
    cbVoices.OnChange(cbVoices);
  end;
  RateSpEdt.Value := SpVoice1.Rate;
  VolSpEdt.Value:= SpVoice1.Volume;
end;

procedure TMainForm.RateSpEdtChange(Sender: TObject);
begin
  StopBtn.Click;
  SpVoice1.Rate :=RateSpEdt.Value;
end;

procedure TMainForm.VolSpEdtChange(Sender: TObject);
begin
  StopBtn.Click;
  SpVoice1.Volume := VolSpEdt.Value;
end;

Peter

unit Settings;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ComCtrls, ExtCtrls, Spin, IniFiles, DBCtrls, OleServer, SpeechLib_TLB;

type
  TSettingsDlg = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    OKBtn: TButton;
    CancelBtn: TButton;
    HelpBtn: TButton;
    Label2: TLabel;
    Label7: TLabel;
    reText: TRichEdit;
    SpVoice2: TSpVoice;
    TestBtn: TButton;
    LbVoices: TListBox;
    tbRate: TTrackBar;
    tbVolume: TTrackBar;
    tbPitch: TTrackBar;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    lblRate: TLabel;
    lblVolume: TLabel;
    lblPitch: TLabel;
    procedure SpVoice2StartStream(ASender: TObject; StreamNumber: Integer;
      StreamPosition: OleVariant);
    procedure SpVoice2EndStream(ASender: TObject; StreamNumber: Integer;
      StreamPosition: OleVariant);
    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
  SettingsFile := ChangeFileExt(Application.ExeName, '.ini');
  SpVoice2.EventInterests := SVEAllEvents;
  SOTokens := SpVoice2.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;
  tbRate.Position := SpVoice2.Rate;
  lblRate.Caption := IntToStr(tbRate.Position);
  tbVolume.Position := SpVoice2.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 
 SOToken := ISpeechObjectToken(Pointer( lbVoices.Items.Objects[lbVoices.ItemIndex])); 
 SpVoice2.Voice := SOToken; 
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.LoadSettings;
var
 ini: TIniFile;
 defVoice: string;
begin
 ini := TIniFile.Create(SettingsFile);
 try
  if lbVoices.Items.Count > 0
   then defVoice := lbVoices.Items[0]
   Else defVoice := '';
  lbVoices.ItemIndex := lbVoices.Items.IndexOf(ini.ReadString('Voice','Voice', defVoice));
  tbVolume.Position := ini.ReadInteger('Voice', 'Volume', SpVoice2.Volume);
  tbRate.Position := ini.ReadInteger('Voice', 'Rate', SpVoice2.Rate);
 finally
  FreeAndNil(ini);
 end;
 LbVoicesClick(lbVoices);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.SaveSettings;
var ini: TIniFile;
    defVoice: string;
begin
  ini := TIniFile.Create(SettingsFile);
  try
  defVoice := '';
  if lbVoices.ItemIndex >= 0 then
  defVoice := lbVoices.Items[lbVoices.ItemIndex];
    ini.WriteString('Voice', 'Voice', defVoice);
    ini.WriteInteger('Voice', 'Volume', SpVoice2.Volume);
    ini.WriteInteger('Voice', 'Rate', SpVoice2.Rate);
  finally
    FreeAndNil(ini)
  end;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.SpVoice2EndStream(ASender: TObject;
  StreamNumber: Integer; StreamPosition: OleVariant);
begin
  TestBtn.Caption := 'Test';
  tbPitch.Enabled := true;
  tbVolume.Enabled := true;
  tbRate.Enabled := true;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.SpVoice2StartStream(ASender: TObject;
  StreamNumber: Integer; StreamPosition: OleVariant);
begin
  TestBtn.Caption := 'Stop';
  tbPitch.Enabled := false;
  tbVolume.Enabled := false;
  tbRate.Enabled := false;
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.tbPitchChange(Sender: TObject);
begin
  lblPitch.Caption := IntToStr(tbPitch.Position);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.tbRateChange(Sender: TObject);
begin
  SpVoice2.Rate := tbRate.Position;
  lblRate.Caption := IntToStr(tbRate.Position);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.tbVolumeChange(Sender: TObject);
begin
  SpVoice2.Volume := tbVolume.Position;
  lblVolume.Caption := IntToStr(tbVolume.Position);
end;
(*---------------------------------------------------*)
procedure TSettingsDlg.TestBtnClick(Sender: TObject);
begin
 If TestBtn.Caption = 'Test' then
 begin
   TestBtn.Caption := 'Stop';
   SpVoice2.Speak(reText.Text, SVSFlagsAsync);
 end else begin
   TestBtn.Caption := 'Test';
   SpVoice2.Skip('Sentence', MaxInt);
 end;
end;
(*---------------------------------------------------*)
end.

Open in new window

ASKER CERTIFIED SOLUTION
Emmanuel PASQUIER
Freelance Project Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2010

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros