packet.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #Simple packet type for the simple protocol
  2. import struct
  3. class Packet(object):
  4. def __init__(self, cmd, *data):
  5. self.cmd = cmd
  6. self.data = data
  7. if len(data) > 8:
  8. raise ValueError('Too many data')
  9. self.data = list(self.data) + [0] * (8-len(self.data))
  10. @classmethod
  11. def FromStr(cls, s):
  12. parts = struct.unpack('>9L', s)
  13. return cls(parts[0], *parts[1:])
  14. def as_float(self, i):
  15. return struct.unpack('>f', struct.pack('>L', self.data[i]))[0]
  16. def __str__(self):
  17. return struct.pack('>L'+(''.join('f' if isinstance(i, float) else 'L' for i in self.data)), self.cmd, *self.data)
  18. class CMD:
  19. KA = 0 # No important data
  20. PING = 1 # Data are echoed exactly
  21. QUIT = 2 # No important data
  22. PLAY = 3 # seconds, microseconds, frequency (Hz), amplitude (0.0 - 1.0), port, flags
  23. CAPS = 4 # ports, client type (1), user ident (2-7)
  24. PCM = 5 # 16 samples, encoded S16_LE
  25. PCMSYN = 6 # number of samples which should be buffered right now
  26. ARTP = 7 # voice (or -1 = OBLIGATE_POLYPHONE for global), index, value(f32)
  27. class PLF:
  28. SAMEPHASE = 0x1
  29. def itos(i):
  30. return struct.pack('>L', i).rstrip('\0')
  31. def stoi(s):
  32. return struct.unpack('>L', s.ljust(4, '\0'))[0]
  33. OBLIGATE_POLYPHONE = 0xffffffff