In this tutorial we’re going to add sounds to our project. Especially we need a sound to tell us when the letter was pressed, because when we’re blinking to press the letter we can’t look at the screen at the same time.

Also we’re adding two other sounds, one to tell as when the left keyboard is activated and one to tell when the right one is activated.

We start doing this by installing a new library to play the audio, and it is called: pyglet.
To see how to install it, please watch the video tutorial.

We import the libraries pyglet and time.

import pyglet
import time

Then we load the three sounds.

sound = pyglet.media.load("sound.wav", streaming=False)
left_sound = pyglet.media.load("left.wav", streaming=False)
right_sound = pyglet.media.load("right.wav", streaming=False)

Finally the only thing we need to do is to call this sounds each time we want to play them.

Respectively, we call the sound that notify us when the letter is pressed just after we press the letter.

if blinking_frames == 5:
    text += active_letter
    sound.play()
    time.sleep(1)

And we call the sound “Right” and “Left” just after we selected the part of the keyboard we’re going to use.
Also we keep track of the keyboard that was selected, so we don’t keep playing the sound over and over again, unless we change keyboard side.

if gaze_ratio <= 0.9:
    keyboard_selected = "right"
    if keyboard_selected != last_keyboard_selected:
        right_sound.play()
        time.sleep(1)
        last_keyboard_selected = keyboard_selected
else:
    keyboard_selected = "left"
    if keyboard_selected != last_keyboard_selected:
        left_sound.play()
        time.sleep(1)
        last_keyboard_selected = keyboard_selected

To test this part of the code, once you run it, try looking on the left side of the screen, then on the right and try blinking your eyes for around a second or more.
If the three sounds play correctly then everything is working as it’s supposed to.