client.py 16 KB

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