auf windows SAPI 5 (sprach sythese) zugreifen

Code-Stücke können hier veröffentlicht werden.
Antworten
Costi
User
Beiträge: 544
Registriert: Donnerstag 17. August 2006, 14:21

Montag 11. Mai 2009, 14:39

"Microsoft Sam" ist standartmaessig in xp und vista instaliert und kann fuer fruehere versionen nachinstaliert werden.
alternativ laueft eSpeak unter windows

Code: Alles auswählen

import win32com.client

class Speaker():
	def __init__(self):
		self._speak = win32com.client.Dispatch('Sapi.SpVoice')

	def get_voices(self, arg=None):
		c = self._speak.getVoices
		voices = c() if arg is None else c(arg)
		return [voices.Item(i).getDescription() for i in range(voices.count)]
	
	def speak(self, text):
		self._speak.Speak(text)
	
	def _set_voice(self, voice):
		voices = self._speak.getVoices('Name='+voice)
		if not voices.count:
			raise ValueError('unknown voice: ' + str(voice))
		self._speak.Voice = voices.item(0)
	def _get_voice(self):
		return self._speak.voice.getDescription()
		
	
	def _mkprop(attr):
		def setter(self, val):
			self._speak.__setattr__(attr, val)
		def getter(self):
			return
		return property(getter, setter)
	
	voice = property(_get_voice, _set_voice)
	volume = _mkprop('Volume')
	rate = _mkprop('Rate')

def main():
	text = "mein name ist bender, bitte diskette einlegen"

	s = Speaker()
	s.voice = "Microsoft Sam"
	s.volume = 100
	print(s.get_voices())
	s.speak(text)

if __name__ == "__main__":
	main()
cp != mv
Costi
User
Beiträge: 544
Registriert: Donnerstag 17. August 2006, 14:21

Montag 11. Mai 2009, 15:42

vieleicht kann auch das nhemanden irgendwan mal hilfreich sein:

Code: Alles auswählen

class PeriodicallyNonBlockingSpeak():
	def __init__(self, text, speaker_args):
		self.text = text
		self.speaker_args = speaker_args
		self._conn, client_pipe = multiprocessing.Pipe()
		self._proc = multiprocessing.Process(target=self._run_in_process, args=(client_pipe, speaker_args, text))
		self._proc.start()
	
	def start(self): self._conn.send('speak')
	def stop_later(self): self._conn.send('stop')
	
	def _run_in_process(self, pipe, speaker_args, text):
		speaker = Speaker()
		for key, value in speaker_args.items():
			setattr(speaker, key, value)
		
		speak = False
		while True:
			p = pipe.poll(0.1)
			if p:
				speak = pipe.recv() == 'speak'
			if speak:
				speaker.speak(text)
	
	def stop(self):
		self._proc.terminate()
		PeriodicallyNonBlockingSpeak.__init__(self, self.text, self.speaker_args)
	
	


def main():
	text = "mein name ist bender, bitte diskette einlegen"

	s = PeriodicallyNonBlockingSpeak(text, {'voice': "Microsoft Sam", 'rate': 5})
	
	while True:
		s.start()
		s.text = input('enter text to repete>')
		s.stop()
	

if __name__ == "__main__":
	main()
cp != mv
Antworten