Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50692d6

Browse files
committed
Initial revision
1 parent 4f5eafb commit 50692d6

12 files changed

Lines changed: 536 additions & 0 deletions

File tree

Demo/sgi/al/alwatch.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import time
2+
import al
3+
dev = 1
4+
name = ['input source', 'left input atten', 'right input atten', \
5+
'input rate', 'output rate', \
6+
'left speaker gain', 'right speaker gain', \
7+
'input count', 'output count', 'unused count', \
8+
'sync input to aes', 'sync output to aes', \
9+
]
10+
x = al.queryparams(dev)
11+
al.getparams(dev, x)
12+
while 1:
13+
time.millisleep(100)
14+
y = x[:]
15+
al.getparams(dev, x)
16+
if x <> y:
17+
for i in range(0, len(x), 2):
18+
if x[i+1] <> y[i+1]:
19+
print name[x[i]], ':', y[i+1], '-->', x[i+1]

Demo/sgi/al/broadcast.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#! /ufs/guido/bin/sgi/python
2+
3+
# broadcast [port]
4+
#
5+
# Broadcast audio input on the network as UDP packets;
6+
# they can be received on any SGI machine with "radio.py".
7+
# This uses the input sampling rate, input source etc. set by apanel.
8+
# It uses the default sample width and #channels (16 bit/sample stereo).
9+
# (This is 192,000 Bytes at a sampling speed of 48 kHz, or ~137
10+
# packets/second -- use with caution!!!)
11+
12+
import sys, al
13+
from socket import *
14+
15+
port = 54321
16+
if sys.argv[1:]: port = eval(sys.argv[1])
17+
18+
s = socket(AF_INET, SOCK_DGRAM)
19+
s.allowbroadcast(1)
20+
21+
p = al.openport('broadcast', 'r')
22+
23+
address = '<broadcast>', port
24+
while 1:
25+
# 700 samples equals 1400 bytes, or about the max packet size!
26+
data = p.readsamps(700)
27+
s.sendto(data, address)

Demo/sgi/al/intercom.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# intercom -- use mike and headset to *talk* to a person on another host.
2+
# For SGI 4D/35 or Indigo running IRIX 4.0.
3+
# Uses 16 bit sampling at 16000 samples/sec, or 32000 bytes/sec,
4+
# tranmitted in 32 1000-byte UDP packets. (In each direction!)
5+
#
6+
# usage:
7+
# intercom hostname - start talking to person on other host
8+
# intercom -r hostname - called remotely to do the setup
9+
10+
import sys, time, posix, gl, fl, FL, al, AL, getopt, rand
11+
from socket import *
12+
13+
# Hack sys.path so AL can be found
14+
LIB = '/ufs/guido/lib/python'
15+
if LIB not in sys.path: sys.path.insert(0, LIB)
16+
17+
# Python binary to be used on remote machine
18+
PYTHON = '/ufs/guido/bin/sgi/python'
19+
20+
# Directory where the programs live
21+
AUDIODIR = '/ufs/guido/mm/demo/audio'
22+
23+
# UDP port numbers used (one for each direction!)
24+
PORT1 = 51042
25+
PORT2 = PORT1+1
26+
27+
# Figure out the user name
28+
try:
29+
user = posix.environ['LOGNAME']
30+
except:
31+
user = posix.environ['USER']
32+
33+
# Debug flags (Implemented as a list; non-empty means debugging is on)
34+
debug = []
35+
36+
def main():
37+
remote = 0
38+
opts, args = getopt.getopt(sys.argv[1:], 'rd')
39+
for opt, arg in opts:
40+
if opt = '-r': remote = 1
41+
elif opt = '-d': debug.append(opt)
42+
if len(args) <> 1:
43+
msg = 'usage: intercom [-d] [-r] hostname'
44+
msg = msg + ' (-r is for internal use only!)\n'
45+
sys.stderr.write(msg)
46+
sys.exit(2)
47+
if remote:
48+
server(args[0])
49+
else:
50+
client(args[0])
51+
52+
def client(hostname):
53+
print 'client starting'
54+
cmd = 'rsh ' + hostname + ' "cd ' + AUDIODIR
55+
cmd = cmd + '; DISPLAY=:0; export DISPLAY'
56+
cmd = cmd + '; exec ' + PYTHON + ' intercom.py -r '
57+
for flag in debug: cmd = cmd + flag + ' '
58+
cmd = cmd + gethostname()
59+
cmd = cmd + '"'
60+
pipe = posix.popen(cmd, 'r')
61+
ack = 0
62+
nak = 0
63+
while 1:
64+
line = pipe.readline()
65+
if not line: break
66+
sys.stdout.write('remote: ' + line)
67+
if line = 'NAK\n':
68+
nak = 1
69+
break
70+
elif line = 'ACK\n':
71+
ack = 1
72+
break
73+
if nak:
74+
print 'Remote user doesn\'t want to talk to you.'
75+
return
76+
if not ack:
77+
print 'No acknowledgement (remote side crashed?).'
78+
return
79+
#
80+
print 'Ready...'
81+
#
82+
s = socket(AF_INET, SOCK_DGRAM)
83+
s.bind('', PORT2)
84+
#
85+
otheraddr = gethostbyname(hostname), PORT1
86+
try:
87+
ioloop(s, otheraddr)
88+
except KeyboardInterrupt:
89+
log('client got intr')
90+
except error:
91+
log('client got error')
92+
finally:
93+
s.sendto('', otheraddr)
94+
log('client finished sending empty packet to server')
95+
#
96+
log('client exit')
97+
print 'Done.'
98+
99+
def server(hostname):
100+
print 'server starting'
101+
sys.stdout.flush()
102+
#
103+
if not remotedialog():
104+
print 'NAK'
105+
return
106+
#
107+
print 'ACK'
108+
#
109+
s = socket(AF_INET, SOCK_DGRAM)
110+
s.bind('', PORT1)
111+
#
112+
# Close std{in,out,err} so rsh will exit; reopen them as dummies
113+
#
114+
sys.stdin.close()
115+
sys.stdin = open('/dev/null', 'r')
116+
sys.stdout.close()
117+
sys.stdout = open('/dev/null', 'w')
118+
sys.stderr.close()
119+
if debug:
120+
sys.stderr = open('/tmp/intercom.err', 'a')
121+
else:
122+
sys.stderr = open('/dev/null', 'w')
123+
#
124+
ioloop(s, (gethostbyname(hostname), PORT2))
125+
log('server exit')
126+
sys.exit(0)
127+
128+
def remotedialog():
129+
gl.foreground()
130+
gl.ringbell()
131+
m1 = user + ' wants to talk to you over the audio channel.'
132+
m2 = 'If it\'s OK, put on your headset and click Yes.'
133+
m3 = 'If you\'re too busy, click No.'
134+
return fl.show_question(m1, m2, m3)
135+
136+
def ioloop(s, otheraddr):
137+
#
138+
dev = AL.DEFAULT_DEVICE
139+
params = al.queryparams(dev)
140+
al.getparams(dev, params)
141+
time.sleep(1)
142+
saveparams = params[:]
143+
for i in range(0, len(params), 2):
144+
if params[i] in (AL.INPUT_RATE, AL.OUTPUT_RATE):
145+
params[i+1] = AL.RATE_16000
146+
elif params[i] = AL.INPUT_SOURCE:
147+
params[i+1] = AL.INPUT_MIC
148+
try:
149+
al.setparams(dev, params)
150+
ioloop1(s, otheraddr)
151+
finally:
152+
al.setparams(dev, saveparams)
153+
154+
def ioloop1(s, otheraddr):
155+
#
156+
# Watch out! data is in bytes, but the port counts in samples,
157+
# which are two bytes each (for 16-bit samples).
158+
# Luckily, we use mono, else it would be worse (2 samples/frame...)
159+
#
160+
SAMPSPERBUF = 500
161+
BYTESPERSAMP = 2 # AL.SAMPLE_16
162+
BUFSIZE = BYTESPERSAMP*SAMPSPERBUF
163+
QSIZE = 4*SAMPSPERBUF
164+
#
165+
config = al.newconfig()
166+
config.setqueuesize(QSIZE)
167+
config.setwidth(AL.SAMPLE_16)
168+
config.setchannels(AL.MONO)
169+
#
170+
pid = posix.fork()
171+
if pid:
172+
# Parent -- speaker/headphones handler
173+
log('parent started')
174+
spkr = al.openport('spkr', 'w', config)
175+
while 1:
176+
data = s.recv(BUFSIZE)
177+
if len(data) = 0:
178+
# EOF packet
179+
log('parent got empty packet; killing child')
180+
posix.kill(pid, 15)
181+
return
182+
# Discard whole packet if we are too much behind
183+
if spkr.getfillable() > len(data) / BYTESPERSAMP:
184+
if len(debug) >= 2:
185+
log('parent Q full; dropping packet')
186+
spkr.writesamps(data)
187+
else:
188+
# Child -- microphone handler
189+
log('child started')
190+
try:
191+
mike = al.openport('mike', 'r', config)
192+
# Sleep a while to let the other side get started
193+
time.sleep(1)
194+
# Drain the queue before starting to read
195+
data = mike.readsamps(mike.getfilled())
196+
# Loop, sending packets from the mike to the net
197+
while 1:
198+
data = mike.readsamps(SAMPSPERBUF)
199+
s.sendto(data, otheraddr)
200+
except KeyboardInterrupt:
201+
log('child got interrupt; exiting')
202+
posix._exit(0)
203+
except error:
204+
log('child got error; exiting')
205+
posix._exit(1)
206+
finally:
207+
log('child got unexpected error; leaving w/ traceback')
208+
209+
def log(msg):
210+
if not debug: return
211+
if type(msg) <> type(''):
212+
msg = `msg`
213+
214+
f = open('/tmp/intercom.log', 'a')
215+
f.write(`sys.argv` + ' ' + `posix.getpid()` + ': ' + msg + '\n')
216+
f.close()
217+
218+
main()

Demo/sgi/al/listen.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Listen to the input on host argv[1].
2+
3+
import sys, al, AL, posix
4+
5+
BUFSIZE = 2000
6+
QSIZE = 4000
7+
8+
def main():
9+
if len(sys.argv) <> 2:
10+
sys.stderr.write('usage: ' + sys.argv[0] + ' hostname\n')
11+
sys.exit(2)
12+
hostname = sys.argv[1]
13+
cmd = 'exec rsh </dev/null ' + hostname + \
14+
' "cd /ufs/guido/mm/demo/audio; ' + \
15+
'exec /ufs/guido/bin/sgi/python record.py"'
16+
pipe = posix.popen(cmd, 'r')
17+
config = al.newconfig()
18+
config.setchannels(AL.MONO)
19+
config.setqueuesize(QSIZE)
20+
port = al.openport('', 'w', config)
21+
while 1:
22+
data = pipe.read(BUFSIZE)
23+
if not data:
24+
sts = pipe.close()
25+
sys.stderr.write(sys.argv[0] + ': end of data\n')
26+
if sts: sys.stderr.write('rsh exit status '+`sts`+'\n')
27+
sys.exit(1)
28+
port.writesamps(data)
29+
del data
30+
31+
try:
32+
main()
33+
except KeyboardInterrupt:
34+
sys.exit(1)

Demo/sgi/al/playaiff.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import aiff
2+
import al
3+
import sys
4+
import time
5+
6+
def main():
7+
v = 1
8+
c = al.newconfig()
9+
nchannels = c.getchannels()
10+
nsampframes = 0 # ???
11+
sampwidth = c.getwidth()
12+
samprate = 0.0 # unknown
13+
filename = sys.argv[1]
14+
f = open(filename, 'r')
15+
type, totalsize = aiff.read_chunk_header(f)
16+
if type <> 'FORM':
17+
raise aiff.Error, 'FORM chunk expected at start of file'
18+
aiff.read_form_chunk(f)
19+
while 1:
20+
try:
21+
type, size = aiff.read_chunk_header(f)
22+
except EOFError:
23+
break
24+
if v: print 'header:', `type`, size
25+
if type = 'COMM':
26+
nchannels, nsampframes, sampwidth, samprate = \
27+
aiff.read_comm_chunk(f)
28+
if v: print nchannels, nsampframes, sampwidth, samprate
29+
elif type = 'SSND':
30+
offset, blocksize = aiff.read_ssnd_chunk(f)
31+
if v: print offset, blocksize
32+
data = f.read(size-8)
33+
if size%2: void = f.read(1)
34+
p = makeport(nchannels, sampwidth, samprate)
35+
play(p, data, offset, blocksize)
36+
elif type in aiff.skiplist:
37+
aiff.skip_chunk(f, size)
38+
else:
39+
raise aiff.Error, 'bad chunk type ' + type
40+
41+
def makeport(nchannels, sampwidth, samprate):
42+
c = al.newconfig()
43+
c.setchannels(nchannels)
44+
c.setwidth(sampwidth/8)
45+
# can't set the rate...
46+
p = al.openport('', 'w', c)
47+
return p
48+
49+
def play(p, data, offset, blocksize):
50+
data = data[offset:]
51+
p.writesamps(data)
52+
while p.getfilled() > 0: time.millisleep(10)
53+
54+
main()

Demo/sgi/al/playback.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Read mono 16bit samples from stdin and write them to the audio device.
2+
# Assume the sampling rate is compatible.
3+
# Use a small queue size to minimize delays.
4+
5+
import al, sys
6+
import AL
7+
8+
BUFSIZE = 2000
9+
QSIZE = 4000
10+
11+
def main():
12+
c = al.newconfig()
13+
c.setchannels(AL.MONO)
14+
c.setqueuesize(QSIZE)
15+
p = al.openport('', 'w', c)
16+
while 1:
17+
data = sys.stdin.read(BUFSIZE)
18+
p.writesamps(data)
19+
20+
try:
21+
main()
22+
except KeyboardInterrupt:
23+
sys.exit(1)

0 commit comments

Comments
 (0)