client.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # A simple client that generates sine waves via python-pyaudio
  2. import signal
  3. import pyaudio
  4. import sys
  5. import socket
  6. import time
  7. import math
  8. import struct
  9. import socket
  10. import optparse
  11. import array
  12. import random
  13. import threading
  14. import thread
  15. from packet import Packet, CMD, stoi
  16. parser = optparse.OptionParser()
  17. parser.add_option('-t', '--test', dest='test', action='store_true', help='Play a test sequence (440,<rest>,880,440), then exit')
  18. parser.add_option('-g', '--generator', dest='generator', default='math.sin', help='Set the generator (to a Python expression)')
  19. parser.add_option('--generators', dest='generators', action='store_true', help='Show the list of generators, then exit')
  20. parser.add_option('-u', '--uid', dest='uid', default='', help='Set the UID (identifier) of this client in the network')
  21. parser.add_option('-p', '--port', dest='port', type='int', default=13676, help='Set the port to listen on')
  22. parser.add_option('-r', '--rate', dest='rate', type='int', default=44100, help='Set the sample rate of the audio device')
  23. parser.add_option('-V', '--volume', dest='volume', type='float', default=1.0, help='Set the volume factor (>1 distorts, <1 attenuates)')
  24. parser.add_option('-n', '--streams', dest='streams', type='int', default=1, help='Set the number of streams this client will play back')
  25. parser.add_option('-G', '--gui', dest='gui', default='', help='set a GUI to use')
  26. parser.add_option('--pg-fullscreen', dest='fullscreen', action='store_true', help='Use a full-screen video mode')
  27. parser.add_option('--pg-samp-width', dest='samp_width', type='int', help='Set the width of the sample pane (by default display width / 2)')
  28. parser.add_option('--pg-bgr-width', dest='bgr_width', type='int', help='Set the width of the bargraph pane (by default display width / 2)')
  29. parser.add_option('--pg-height', dest='height', type='int', help='Set the height of the window or full-screen video mode')
  30. options, args = parser.parse_args()
  31. PORT = options.port
  32. STREAMS = options.streams
  33. IDENT = 'TONE'
  34. UID = options.uid
  35. LAST_SAMPS = [0] * STREAMS
  36. LAST_SAMPLES = []
  37. FREQS = [0] * STREAMS
  38. PHASES = [0] * STREAMS
  39. RATE = options.rate
  40. FPB = 64
  41. Z_SAMP = '\x00\x00\x00\x00'
  42. MAX = 0x7fffffff
  43. AMPS = [MAX] * STREAMS
  44. MIN = -0x80000000
  45. EXPIRATIONS = [0] * STREAMS
  46. def lin_interp(frm, to, p):
  47. return p*to + (1-p)*frm
  48. # GUIs
  49. GUIs = {}
  50. def GUI(f):
  51. GUIs[f.__name__] = f
  52. return f
  53. @GUI
  54. def pygame_notes():
  55. import pygame
  56. import pygame.gfxdraw
  57. pygame.init()
  58. dispinfo = pygame.display.Info()
  59. DISP_WIDTH = 640
  60. DISP_HEIGHT = 480
  61. if dispinfo.current_h > 0 and dispinfo.current_w > 0:
  62. DISP_WIDTH = dispinfo.current_w
  63. DISP_HEIGHT = dispinfo.current_h
  64. SAMP_WIDTH = DISP_WIDTH / 2
  65. if options.samp_width > 0:
  66. SAMP_WIDTH = options.samp_width
  67. BGR_WIDTH = DISP_WIDTH / 2
  68. if options.bgr_width > 0:
  69. BGR_WIDTH = options.bgr_width
  70. HEIGHT = DISP_HEIGHT
  71. if options.height > 0:
  72. HEIGHT = options.height
  73. flags = 0
  74. if options.fullscreen:
  75. flags |= pygame.FULLSCREEN
  76. disp = pygame.display.set_mode((SAMP_WIDTH + BGR_WIDTH, HEIGHT), flags)
  77. WIDTH, HEIGHT = disp.get_size()
  78. SAMP_WIDTH = WIDTH / 2
  79. BGR_WIDTH = WIDTH - SAMP_WIDTH
  80. PFAC = HEIGHT / 128.0
  81. sampwin = pygame.Surface((SAMP_WIDTH, HEIGHT))
  82. lastsy = HEIGHT / 2
  83. clock = pygame.time.Clock()
  84. while True:
  85. disp.fill((0, 0, 0), (BGR_WIDTH, 0, SAMP_WIDTH, HEIGHT))
  86. disp.scroll(-1, 0)
  87. for i in xrange(STREAMS):
  88. FREQ = FREQS[i]
  89. AMP = AMPS[i]
  90. if FREQ > 0:
  91. try:
  92. pitch = 12 * math.log(FREQ / 440.0, 2) + 69
  93. except ValueError:
  94. pitch = 0
  95. else:
  96. pitch = 0
  97. col = [int((AMP / MAX) * 255)] * 3
  98. disp.fill(col, (BGR_WIDTH - 1, HEIGHT - pitch * PFAC - PFAC, 1, PFAC))
  99. sampwin.scroll(-len(LAST_SAMPLES), 0)
  100. x = max(0, SAMP_WIDTH - len(LAST_SAMPLES))
  101. sampwin.fill((0, 0, 0), (x, 0, SAMP_WIDTH - x, HEIGHT))
  102. for i in LAST_SAMPLES:
  103. sy = int((float(i) / MAX) * (HEIGHT / 2) + (HEIGHT / 2))
  104. pygame.gfxdraw.line(sampwin, x - 1, lastsy, x, sy, (0, 255, 0))
  105. x += 1
  106. lastsy = sy
  107. del LAST_SAMPLES[:]
  108. #w, h = SAMP_WIDTH, HEIGHT
  109. #pts = [(BGR_WIDTH, HEIGHT / 2), (w + BGR_WIDTH, HEIGHT / 2)]
  110. #x = w + BGR_WIDTH
  111. #for i in reversed(LAST_SAMPLES):
  112. # pts.insert(1, (x, int((h / 2) + (float(i) / MAX) * (h / 2))))
  113. # x -= 1
  114. # if x < BGR_WIDTH:
  115. # break
  116. #if len(pts) > 2:
  117. # pygame.gfxdraw.aapolygon(disp, pts, [0, 255, 0])
  118. disp.blit(sampwin, (BGR_WIDTH, 0))
  119. pygame.display.flip()
  120. for ev in pygame.event.get():
  121. if ev.type == pygame.KEYDOWN:
  122. if ev.key == pygame.K_ESCAPE:
  123. thread.interrupt_main()
  124. pygame.quit()
  125. exit()
  126. elif ev.type == pygame.QUIT:
  127. thread.interrupt_main()
  128. pygame.quit()
  129. exit()
  130. clock.tick(60)
  131. # Generator functions--should be cyclic within [0, 2*math.pi) and return [-1, 1]
  132. GENERATORS = [{'name': 'math.sin', 'args': None, 'desc': 'Sine function'},
  133. {'name':'math.cos', 'args': None, 'desc': 'Cosine function'}]
  134. def generator(desc=None, args=None):
  135. def inner(f, desc=desc, args=args):
  136. if desc is None:
  137. desc = f.__doc__
  138. GENERATORS.append({'name': f.__name__, 'desc': desc, 'args': args})
  139. return f
  140. return inner
  141. @generator('Simple triangle wave (peaks/troughs at pi/2, 3pi/2)')
  142. def tri_wave(theta):
  143. if theta < math.pi/2:
  144. return lin_interp(0, 1, theta/(math.pi/2))
  145. elif theta < 3*math.pi/2:
  146. return lin_interp(1, -1, (theta-math.pi/2)/math.pi)
  147. else:
  148. return lin_interp(-1, 0, (theta-3*math.pi/2)/(math.pi/2))
  149. @generator('Saw wave (line from (0, 1) to (2pi, -1))')
  150. def saw_wave(theta):
  151. return lin_interp(1, -1, theta/(math.pi * 2))
  152. @generator('Simple square wave (piecewise 1 at x<pi, 0 else)')
  153. def square_wave(theta):
  154. if theta < math.pi:
  155. return 1
  156. else:
  157. return -1
  158. @generator('Random (noise) generator')
  159. def noise(theta):
  160. return random.random() * 2 - 1
  161. @generator('File generator', '(<file>[, <bits=8>[, <signed=True>[, <0=linear interp (default), 1=nearest>[, <swapbytes=False>]]]])')
  162. class file_samp(object):
  163. LINEAR = 0
  164. NEAREST = 1
  165. TYPES = {8: 'B', 16: 'H', 32: 'L'}
  166. def __init__(self, fname, bits=8, signed=True, samp=LINEAR, swab=False):
  167. tp = self.TYPES[bits]
  168. if signed:
  169. tp = tp.lower()
  170. self.max = float((2 << bits) - 1)
  171. self.buffer = array.array(tp)
  172. self.buffer.fromstring(open(fname, 'rb').read())
  173. if swab:
  174. self.buffer.byteswap()
  175. self.samp = samp
  176. def __call__(self, theta):
  177. norm = theta / (2*math.pi)
  178. if self.samp == self.LINEAR:
  179. v = norm*len(self.buffer)
  180. l = int(math.floor(v))
  181. h = int(math.ceil(v))
  182. if l == h:
  183. return self.buffer[l]/self.max
  184. if h >= len(self.buffer):
  185. h = 0
  186. return lin_interp(self.buffer[l], self.buffer[h], v-l)/self.max
  187. elif self.samp == self.NEAREST:
  188. return self.buffer[int(math.ceil(norm*len(self.buffer) - 0.5))]/self.max
  189. @generator('Harmonics generator (adds overtones at f, 2f, 3f, 4f, etc.)', '(<generator>, <amplitude of f>, <amp 2f>, <amp 3f>, ...)')
  190. class harmonic(object):
  191. def __init__(self, gen, *spectrum):
  192. self.gen = gen
  193. self.spectrum = spectrum
  194. def __call__(self, theta):
  195. return max(-1, min(1, sum([amp*self.gen((i+1)*theta % (2*math.pi)) for i, amp in enumerate(self.spectrum)])))
  196. @generator('General harmonics generator (adds arbitrary overtones)', '(<generator>, <factor of f>, <amplitude>, <factor>, <amplitude>, ...)')
  197. class genharmonic(object):
  198. def __init__(self, gen, *harmonics):
  199. self.gen = gen
  200. self.harmonics = zip(harmonics[::2], harmonics[1::2])
  201. def __call__(self, theta):
  202. return max(-1, min(1, sum([amp * self.gen(i * theta % (2*math.pi)) for i, amp in self.harmonics])))
  203. @generator('Mix generator', '(<generator>[, <amp>], [<generator>[, <amp>], [...]])')
  204. class mixer(object):
  205. def __init__(self, *specs):
  206. self.pairs = []
  207. i = 0
  208. while i < len(specs):
  209. if i+1 < len(specs) and isinstance(specs[i+1], (float, int)):
  210. pair = (specs[i], specs[i+1])
  211. i += 2
  212. else:
  213. pair = (specs[i], None)
  214. i += 1
  215. self.pairs.append(pair)
  216. tamp = 1 - min(1, sum([amp for gen, amp in self.pairs if amp is not None]))
  217. parts = float(len([None for gen, amp in self.pairs if amp is None]))
  218. for idx, pair in enumerate(self.pairs):
  219. if pair[1] is None:
  220. self.pairs[idx] = (pair[0], tamp / parts)
  221. def __call__(self, theta):
  222. return max(-1, min(1, sum([amp*gen(theta) for gen, amp in self.pairs])))
  223. @generator('Phase offset generator (in radians; use math.pi)', '(<generator>, <offset>)')
  224. class phase_off(object):
  225. def __init__(self, gen, offset):
  226. self.gen = gen
  227. self.offset = offset
  228. def __call__(self, theta):
  229. return self.gen((theta + self.offset) % (2*math.pi))
  230. if options.generators:
  231. for item in GENERATORS:
  232. print item['name'],
  233. if item['args'] is not None:
  234. print item['args'],
  235. print '--', item['desc']
  236. exit()
  237. #generator = math.sin
  238. #generator = tri_wave
  239. #generator = square_wave
  240. generator = eval(options.generator)
  241. #def sigalrm(sig, frm):
  242. # global FREQ
  243. # FREQ = 0
  244. def lin_seq(frm, to, cnt):
  245. step = (to-frm)/float(cnt)
  246. samps = [0]*cnt
  247. for i in xrange(cnt):
  248. p = i / float(cnt-1)
  249. samps[i] = int(lin_interp(frm, to, p))
  250. return samps
  251. def samps(freq, amp, phase, cnt):
  252. global RATE
  253. samps = [0]*cnt
  254. for i in xrange(cnt):
  255. samps[i] = int(amp / float(STREAMS) * max(-1, min(1, options.volume*generator((phase + 2 * math.pi * freq * i / RATE) % (2*math.pi)))))
  256. return samps, (phase + 2 * math.pi * freq * cnt / RATE) % (2*math.pi)
  257. def to_data(samps):
  258. return struct.pack('i'*len(samps), *samps)
  259. def mix(a, b):
  260. return [i + j for i, j in zip(a, b)]
  261. def gen_data(data, frames, tm, status):
  262. global FREQS, PHASE, Z_SAMP, LAST_SAMP, LAST_SAMPLES
  263. fdata = [0] * frames
  264. for i in range(STREAMS):
  265. FREQ = FREQS[i]
  266. LAST_SAMP = LAST_SAMPS[i]
  267. AMP = AMPS[i]
  268. EXPIRATION = EXPIRATIONS[i]
  269. PHASE = PHASES[i]
  270. if FREQ != 0:
  271. if time.clock() > EXPIRATION:
  272. FREQ = 0
  273. if FREQ == 0:
  274. PHASES[i] = 0
  275. if LAST_SAMP != 0:
  276. vdata = lin_seq(LAST_SAMP, 0, frames)
  277. fdata = mix(fdata, vdata)
  278. LAST_SAMPS[i] = vdata[-1]
  279. else:
  280. vdata, PHASE = samps(FREQ, AMP, PHASE, frames)
  281. fdata = mix(fdata, vdata)
  282. PHASES[i] = PHASE
  283. LAST_SAMPS[i] = vdata[-1]
  284. if options.gui:
  285. LAST_SAMPLES.extend(fdata)
  286. return (to_data(fdata), pyaudio.paContinue)
  287. pa = pyaudio.PyAudio()
  288. stream = pa.open(rate=RATE, channels=1, format=pyaudio.paInt32, output=True, frames_per_buffer=FPB, stream_callback=gen_data)
  289. if options.gui:
  290. guithread = threading.Thread(target=GUIs[options.gui])
  291. guithread.setDaemon(True)
  292. guithread.start()
  293. if options.test:
  294. FREQ = 440
  295. time.sleep(1)
  296. FREQ = 0
  297. time.sleep(1)
  298. FREQ = 880
  299. time.sleep(1)
  300. FREQ = 440
  301. time.sleep(2)
  302. exit()
  303. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  304. sock.bind(('', PORT))
  305. #signal.signal(signal.SIGALRM, sigalrm)
  306. while True:
  307. data = ''
  308. while not data:
  309. try:
  310. data, cli = sock.recvfrom(4096)
  311. except socket.error:
  312. pass
  313. pkt = Packet.FromStr(data)
  314. print 'From', cli, 'command', pkt.cmd
  315. if pkt.cmd == CMD.KA:
  316. pass
  317. elif pkt.cmd == CMD.PING:
  318. sock.sendto(data, cli)
  319. elif pkt.cmd == CMD.QUIT:
  320. break
  321. elif pkt.cmd == CMD.PLAY:
  322. voice = pkt.data[4]
  323. dur = pkt.data[0]+pkt.data[1]/1000000.0
  324. FREQS[voice] = pkt.data[2]
  325. AMPS[voice] = MAX * max(min(pkt.as_float(3), 1.0), 0.0)
  326. EXPIRATIONS[voice] = time.clock() + dur
  327. #signal.setitimer(signal.ITIMER_REAL, dur)
  328. elif pkt.cmd == CMD.CAPS:
  329. data = [0] * 8
  330. data[0] = STREAMS
  331. data[1] = stoi(IDENT)
  332. for i in xrange(len(UID)/4):
  333. data[i+2] = stoi(UID[4*i:4*(i+1)])
  334. sock.sendto(str(Packet(CMD.CAPS, *data)), cli)
  335. else:
  336. print 'Unknown cmd', pkt.cmd