sound.py 1.61 KB
Newer Older
1
#!/usr/bin/env python2
2

3 4
import pygame as pg
from threading import Timer
5
import zmq
6
import configparser
7 8


9 10 11 12
# fade out
def endMission():
    pg.mixer.Channel(0).fadeout(5)
    pass
13 14


15 16 17
def main():
    config = configparser.ConfigParser({'host': '10.10.10.1', 'port': '5556'})
    config.read('config.cfg')
18

Aljaž Srebrnič's avatar
Aljaž Srebrnič committed
19 20
    port = config["DEFAULT"]['port']
    host = config["DEFAULT"]['host']
21

Aljaž Srebrnič's avatar
Aljaž Srebrnič committed
22
    pg.mixer.pre_init(44100, size=-16, channels=1, buffer=4096)
23
    pg.mixer.init()
24
    pg.mixer.set_num_channels(3)
25

26
    # carico i file audio
27 28 29
    lancio = pg.mixer.Sound('sounds/lancio.ogg')
    stageOne = pg.mixer.Sound('sounds/stageOne.ogg')
    stageTwo = pg.mixer.Sound('sounds/stageTwo.ogg')
30

31 32
    # tempo di fine suono dopo secondo modulo
    t = Timer(30.0, endMission)
33

34 35 36
    # Socket to talk to server
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
37

38
    print("Connecting to coordinator...")
39
    socket.connect(f"tcp://{host}:{port}")
40

41
    topicfilter = "events"
42
    socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
43 44 45 46 47

    while True:
        string = socket.recv()
        topic, messagedata = string.split()

48
        if messagedata == b"pre_launch":
49
            pg.mixer.Channel(0).play(lancio)
50
        elif messagedata == b"stage_1_separation":
51
            pg.mixer.Channel(1).play(stageOne)
52
        elif messagedata == b"stage_2_separation":
53
            pg.mixer.Channel(2).play(stageTwo)
54
            t = Timer(30.0, endMission)
55 56 57 58 59
            t.start()
        else:
            print("Unhandled message")

        print(topic, messagedata)
60
    while pg.mixer.get_busy():
61
        pg.time.delay(10)
62

63

64 65
if __name__ == "__main__":
    main()