slaveCoder
slaveCoder

Reputation: 537

How to play a dynamic string with espeak in python

I am using the espeak library for text to speech conversion.I am able to generate a dynmic sound from a string to do this.

os.system('espeak "hello"')

This works. But what i need is to generate sound from a string. This is what i did

string='hello'
os.system('espeak string')

Upvotes: 2

Views: 3030

Answers (2)

lolamontes69
lolamontes69

Reputation: 39

Why not use commands.getoutput() instead?

import commands

def espeak(string):
    output = 'espeak "%s"' % string
    a = commands.getoutput(output)


espeak("Mario Balotelli") 

Upvotes: 1

Sukrit Kalra
Sukrit Kalra

Reputation: 34543

Just interpolate the string that you want to speak into your command.

>>> string = "Hello"
>>> os.system('espeak "{}"'.format(string))

You could use the subprocess.Popen function if you are accepting user input for the string.

>>> import subprocess
>>> p = subprocess.Popen(['espeak', string])

Upvotes: 2

Related Questions