📜 ⬆️ ⬇️

Pygame games availability

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 

And in the absence of NVDA on the computer, we work directly with the SAPI synthesizer through win32api.

 import win32com.client 


We create a class for our govoriki.

 class Speech: def __init__(self, config): """Initialize speech class.""" self.config = config 

There probably need to explain about the config. In the general Game class, which is engaged in the initialization of all game modules and twists the main loop, the game settings are loaded.

Settings can be downloaded from where it is more convenient: ini files, json, sqlite, or any other convenient option.


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] 

Set up the connected synthesizer with some parameters from the settings.
In this example, I just take the index of the installed voice (the default index is 0), but you can make settings with a choice from the drop-down list by name, obtained as described above.

Voice speed is set in the range from -10 to +10. But I don’t think that someone wants to listen to the voice at a speed lower than 5. You can experiment on your own by changing the value in the settings.

And of course the volume of the voice. Here it is standard from 0 to 100.

  self.set_voice(self.config.voice) self.speaker.Rate = self.config.rate self.speaker.Volume = self.config.volume 

Well, finally, we initialize nvda.

  self.nvda = self.config.nvda self.nvda_error = False self.sLib = ctypes.windll.LoadLibrary('./nvdaControllerClient32.dll') 

Immediately check whether our program can connect to a working program NVDA.

  nvda_error = self.sLib.nvdaController_testIfRunning() errorMessage = str(ctypes.WinError(nvda_error)) if 0 != nvda_error: print('NVDA error: ' + errorMessage) self.nvda_error = True 

After both the SAPI synthesizer has been initialized, and nvda dll, you can run the audio output selection feature.
  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) 

And this is the function for pronunciation directly on the synthesizer:
  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