Preview All Installed Voices

From IronPython Cookbook

Preview All Installed Voices

This example shows how to create a simple WinForms app that lets you try out all the voices on your system. This example uses ListBox, TextBox, and NumericUpDown widgets.

You will need to have .NET 3.x installed to run this example.

You probably already have the "Microsoft Sam" voice, but to get more voices, you will need to download Microsoft Speech SDK 5.1. To get the "Microsoft Simplified Chinese" voice, you will also need to download the Speech SDK 5.1 Language Pack (available on the same page).

Voice Previewer Screenshot

import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Speech, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
from System import Decimal
from System.Drawing import Font
from System.Windows.Forms import (Form, DockStyle, TextBox, ListBox, Panel,
                                  NumericUpDown)
from System.Speech.Synthesis import SpeechSynthesizer

spk = SpeechSynthesizer()
spk.Rate = 0
voices = [v.VoiceInfo.Name for v in spk.GetInstalledVoices()]

f = Form()
f.Text = 'Voice Previewer'

lb = ListBox()
for voice in voices:
    lb.Items.Add(voice)
lb.SelectedIndex = 0

nud = NumericUpDown()
nud.Value = Decimal(0)
nud.Minimum = Decimal(-10)
nud.Maximum = Decimal(10)

tb = TextBox()
tb.Text = 'Hello World!'
tb.Multiline = True
tb.Font = Font('serif', 20)

# Layout
f.Width = 500
f.Height = 300

pnl = Panel()
pnl.Dock = DockStyle.Left

lb.Dock = DockStyle.Fill
lb.Width = 200
nud.Dock = DockStyle.Bottom
tb.Dock = DockStyle.Fill

f.Controls.Add(tb)
f.Controls.Add(pnl)
pnl.Controls.Add(lb)
pnl.Controls.Add(nud)
f.ActiveControl = lb

# Event handling
def Speak():
    spk.SelectVoice(voices[lb.SelectedIndex])
    spk.Speak(tb.Text)

def OnKeyPress(sender, args):
    if args.KeyChar == u'\r':
        Speak()
lb.KeyPress += OnKeyPress

def OnValueChanged(sender, args):
    spk.Rate = Decimal.ToInt32(nud.Value)
nud.ValueChanged += OnValueChanged

nud.KeyPress += lambda sender, args: Speak()

f.ShowDialog()


Back to Contents.

TOOLBOX
LANGUAGES