| import sys
|
| import socket
|
| import struct
|
|
|
| multicast_group = '239.0.1.64'
|
| server_address = ('', 11001)
|
|
|
| seconds = 10
|
| if len(sys.argv) > 1:
|
| seconds = int(sys.argv[1])
|
|
|
|
|
| sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
| sock.settimeout(2.0)
|
|
|
|
|
| sock.bind(server_address)
|
|
|
|
|
|
|
| group = socket.inet_aton(multicast_group)
|
| mreq = struct.pack('4sL', group, socket.INADDR_ANY)
|
| sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
|
|
| sock.sendto(b"Hey, open the port!", ("192.168.0.121", 11001))
|
|
|
|
|
| previous = None
|
| total_length = 0
|
|
|
| with open("audio.wav", "wb") as outfile:
|
| for frame in range(250 * seconds):
|
| data, address = sock.recvfrom(1024)
|
| (number, ) = struct.unpack("<H", data[0:2])
|
| if previous and (previous + 1) != number:
|
| print ('X', end = '')
|
| else:
|
| print ('.', end = '')
|
|
|
| sys.stdout.flush()
|
| outfile.write(data[2:])
|
| total_length += len(data) - 2
|
| previous = number
|
|
|
| header = struct.pack(">L", 0x52494646)
|
| header += struct.pack("<L", total_length - 8)
|
| header += struct.pack(">L", 0x57415645)
|
|
|
| header += struct.pack(">L", 0x666d7420)
|
| header += struct.pack("<LHHLLHH", 16, 1, 2, 48000, 48000 * 4, 4, 16)
|
|
|
| header += struct.pack(">L", 0x64617461)
|
| header += struct.pack("<L", total_length - 44)
|
|
|
| outfile.seek(0)
|
| outfile.write(header)
|
|
|
| print ("Done.")
|
|
|