Hello.
In the article Sound games: an invisible market waiting for heroes , sound games with a cool surround sound, and libraries for its creation were considered.
Well, I decided to start small, and to begin to organize the synthesizer dubbing of actions in turn-based games on pygame.
')
Of course, this technique is not suitable for all games, but in some it is very much the same.
Create a speech module.
In it we organize two options for work:
First, import all the necessary modules.
To connect nvdaControllerClient32.dll we need ctypes.
import ctypes
import win32com.client
We create a class for our govoriki.
class Speech: def __init__(self, config): """Initialize speech class.""" self.config = config
But we will continue initialization of our Speech.
# COM . self.speaker = win32com.client.Dispatch("Sapi.SpVoice") # self.voices = self.speaker.GetVoices() # self.voices_names = [voice.GetDescription() for voice in self.voices]
self.set_voice(self.config.voice) self.speaker.Rate = self.config.rate self.speaker.Volume = self.config.volume
self.nvda = self.config.nvda self.nvda_error = False self.sLib = ctypes.windll.LoadLibrary('./nvdaControllerClient32.dll')
nvda_error = self.sLib.nvdaController_testIfRunning() errorMessage = str(ctypes.WinError(nvda_error)) if 0 != nvda_error: print('NVDA error: ' + errorMessage) self.nvda_error = True
self.set_speak_out()
Add a function to install voices from the list available by index.
def set_voice(self, index): """Set voice for speak.""" try: self.speaker.Voice = self.voices[index] self.speak_sapi(self.voices_names[index]) except: print('error: do not set voice')
And now the function of the choice of audio output of speech. Here we actually choose what we will use for work: nvda or a synthesizer directly.
The choice consists of two parameters:
def set_speak_out(self): """Set speak out: nvda or sapi.""" if self.nvda and not self.nvda_error: self.speak = self.speak_nvda else: self.speak = self.speak_sapi
And of course we will write the functions of pronunciation.
For NVDA:
def speak_nvda(self, phrase): self.sLib.nvdaController_speakText(phrase)
def speak_sapi(self, phrase): self.speaker.Speak(phrase)
That's all. Now, anywhere in the game logic, we send the necessary information to speech.speak ().
I hope this article will be useful to someone and more available games will appear.
Source: https://habr.com/ru/post/424477/
All Articles