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 even.



Create a speech module.

In it we organize two options for work:

  • connection to the NVDA screen speaker via its Controller dll;
  • directly to the windows synthesizer via SAPI5;



First we 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.

classSpeech: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 modules of the game 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 let's continue the initialization of our Speech.

# подключаем синтезатор как COM объект.self.speaker = win32com.client.Dispatch("Sapi.SpVoice")
        # Получаем все доступные синтезаторы в системе и сохраняем в спискеself.voices = self.speaker.GetVoices()
        # Создаем список имен полученных голосовself.voices_names = [voice.GetDescription() for voice inself.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 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 do not think that someone will want 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 = Falseself.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))
        if0 != 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 a voice from the list of available by index.

defset_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:

  • The flag in the settings, whether the user wants the game to use NVDA at all;
  • Possible errors when connecting to NVDA;

defset_speak_out(self):"""Set speak out: nvda or sapi."""if self.nvda andnot 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:

defspeak_nvda(self, phrase):
        self.sLib.nvdaController_speakText(phrase)

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


Also popular now: