File: echo.py

package info (click to toggle)
pyalsaaudio 0.10.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,632 kB
  • sloc: javascript: 16,483; ansic: 2,559; python: 517; makefile: 79
file content (40 lines) | stat: -rw-r--r-- 916 bytes parent folder | download
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
#!/usr/bin/env python3
# -*- mode: python; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-

import alsaaudio
import select

def echo(inpcm, outpcm):
	q = list()

	# setup the synchronous event loop	
	# See https://docs.python.org/3/library/select.html#poll-objects for background
	reactor = select.poll()

	infd, inmask = inpcm.polldecriptors()
	outfd, outmask = outpcm.polldescriptors()

	write_started = False

	def write():
	  	data = q.pop()
		written = outpcm.write(data)
		if written < len(data):
			q.insert(0, data[written:])

	reactor.register(infd, inmask)
	reactor.register(outfd, outmask)

	while True:
		events = reactor.poll()
		for fd, event in events:
			if event == select.POLLIN and fd == infd:
				data = inpcm.read()
				q.append(data)
				if not write_started:
					write()
					write_started = True
			elif event == select.POLLOUT and fd == outfd:
				if not q:
					return
				write()