-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.py
71 lines (51 loc) · 1.62 KB
/
master.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import socket
import alsaaudio
import math
from mplayer import Player, CmdPrefix
# ----------- UDP ------------
localIP = "192.168.1.136"
localPort = 44444
bufferSize = 1024
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# ---------- ALSA ------------
# Init alsa mixer
m = alsaaudio.Mixer('Digital')
# ---------- MPlayer ----------
# Set default prefix for all Player instances
Player.cmd_prefix = CmdPrefix.PAUSING_KEEP
# Since autospawn is True by default, no need to call player.spawn() manually
player = Player()
# -------- RADIO ----------
# Load Radio URLs
radio_stations_file = open("radio_stations.txt", "r")
radio_stations = radio_stations_file.read().split("\n")
radio_id = 0
player.loadfile(radio_stations[radio_id])
# ---------- LOOP ------------
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
if message == "Play":
player.loadfile(radio_stations[radio_id])
elif message == "Stop":
player.pause()
elif message == "Next":
if radio_id+1 < len(radio_stations):
radio_id = radio_id + 1
else:
radio_id = 0
player.loadfile(radio_stations[radio_id])
elif message == "Exit":
player.quit()
exit()
# Extract volume
vol = message[5]
# Set volume
m.setvolume(int(50.0*math.log10(1.0 + vol/255.0*99.0)))
player.quit()