Skip to content

Instantly share code, notes, and snippets.

@margusmartsepp
Created May 27, 2011 17:17
Show Gist options
  • Save margusmartsepp/995713 to your computer and use it in GitHub Desktop.
Save margusmartsepp/995713 to your computer and use it in GitHub Desktop.
Text to Speech
unit TextToSpeech;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, AppEvnts, StdCtrls, ClipBrd, SpeechLib_TLB, ComObj,
AsyncCalls, ComCtrls, shellapi, Menus;
type
TForm2 = class(TForm)
TrayIcon1: TTrayIcon;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
TrackBar1: TTrackBar;
Panel1: TPanel;
PopupMenu1: TPopupMenu;
ff1: TMenuItem;
Open1: TMenuItem;
Speak1: TMenuItem;
StopSpeaking1: TMenuItem;
About1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Open1Click(Sender: TObject);
procedure ff1Click(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
procedure About1Click(Sender: TObject);
private
procedure start(); overload;
procedure start(data: String); overload;
public
{ Public declarations }
protected
procedure WMHotKey(var Message: TMessage); message WM_HOTKEY;
end;
var
Form2: TForm2;
SpVoice: TSpVoice;
isSpeaking: Boolean;
implementation
{$R *.dfm}
{
Starts speaking textbox contents asyncronously.
}
procedure TForm2.start(data: String);
begin
SpVoice.Rate := TrackBar1.Position;
SpVoice.Speak(Memo1.Text, SVSFlagsAsync or SVSFPurgeBeforeSpeak);
end;
{
Fills textbox with clipboard text.
Redirects to method that starts speaking asyncronously.
}
procedure TForm2.start();
begin
Memo1.Text := Clipboard.AsText;
start(Memo1.Text);
end;
{
Global hotkey 'Ctrl + Alt + Space' handler.
}
procedure TForm2.WMHotKey(var Message: TMessage);
begin
start();
end;
{
Displays text to speech about info.
}
procedure TForm2.About1Click(Sender: TObject);
begin
TrayIcon1.BalloonHint :=
'Text to speech program (CC BY-SA 3.0). Double click on me, to make me speak the contents of the clipboard (global shortcut: ctrl+alt+space) and click on me, to make Me stop. ';
TrayIcon1.ShowBalloonHint();
BringToFront();
end;
{
Event that triggers .start(data: String); method.
}
procedure TForm2.Button1Click(Sender: TObject);
begin
start(Memo1.Text);
end;
{
Event that stop speaking.
}
procedure TForm2.Button2Click(Sender: TObject);
begin
SpVoice.Speak('', SVSFPurgeBeforeSpeak);
end;
{
Event that triggers .start(); method.
}
procedure TForm2.Button3Click(Sender: TObject);
begin
start();
end;
{
Event that triggers program termination.
}
procedure TForm2.ff1Click(Sender: TObject);
begin
Application.Terminate;
end;
{
Set up.
}
procedure TForm2.FormCreate(Sender: TObject);
begin
RegisterHotKey(Handle, 1987, MOD_CONTROL or MOD_ALT, VK_SPACE);
SpVoice := TSpVoice.Create(self);
TrayIcon1.Visible := True;
About1Click(Sender);
end;
{
Tear down.
}
procedure TForm2.FormDestroy(Sender: TObject);
begin
UnregisterHotKey(Handle, 1987);
end;
{
Program to tray.
}
procedure TForm2.FormResize(Sender: TObject);
begin
if windowstate = wsMinimized then
begin
Hide();
TrayIcon1.Visible := True;
TrayIcon1.BalloonHint := 'I''m here now.';
TrayIcon1.BalloonFlags := bfInfo;
TrayIcon1.ShowBalloonHint();
BringToFront();
end;
end;
{
Restore program from tray.
}
procedure TForm2.Open1Click(Sender: TObject);
begin
Application.Restore;
windowstate := wsNormal;
Application.BringToFront;
self.Show;
end;
{
Passing events.
}
procedure TForm2.PopupMenu1Popup(Sender: TObject);
begin
BringToFront();
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment