def ioloop1(s, otheraddr): # # Watch out! data is in bytes, but the port counts in samples, # which are two bytes each (for 16-bit samples). # Luckily, we use mono, else it would be worse (2 samples/frame...) # SAMPSPERBUF = 500 BYTESPERSAMP = 2 # AL.SAMPLE_16 BUFSIZE = BYTESPERSAMP*SAMPSPERBUF QSIZE = 4*SAMPSPERBUF # config = al.newconfig() config.setqueuesize(QSIZE) config.setwidth(AL.SAMPLE_16) config.setchannels(AL.MONO) # pid = posix.fork() if pid: # Parent -- speaker/headphones handler log('parent started') spkr = al.openport('spkr', 'w', config) while 1: data = s.recv(BUFSIZE) if len(data) == 0: # EOF packet log('parent got empty packet; killing child') posix.kill(pid, 15) return # Discard whole packet if we are too much behind if spkr.getfillable() > len(data) / BYTESPERSAMP: if len(debug) >= 2: log('parent Q full; dropping packet') spkr.writesamps(data) else: # Child -- microphone handler log('child started') try: try: mike = al.openport('mike', 'r', config) # Sleep a while to let the other side get started time.sleep(1) # Drain the queue before starting to read data = mike.readsamps(mike.getfilled()) # Loop, sending packets from the mike to the net while 1: data = mike.readsamps(SAMPSPERBUF) s.sendto(data, otheraddr) except KeyboardInterrupt: log('child got interrupt; exiting') posix._exit(0) except error: log('child got error; exiting') posix._exit(1) finally: log('child got unexpected error; leaving w/ traceback')
def ioloop1(s, otheraddr): # # Watch out! data is in bytes, but the port counts in samples, # which are two bytes each (for 16-bit samples). # Luckily, we use mono, else it would be worse (2 samples/frame...) # SAMPSPERBUF = 500 BYTESPERSAMP = 2 # AL.SAMPLE_16 BUFSIZE = BYTESPERSAMP * SAMPSPERBUF QSIZE = 4 * SAMPSPERBUF # config = al.newconfig() config.setqueuesize(QSIZE) config.setwidth(AL.SAMPLE_16) config.setchannels(AL.MONO) # pid = posix.fork() if pid: # Parent -- speaker/headphones handler log('parent started') spkr = al.openport('spkr', 'w', config) while 1: data = s.recv(BUFSIZE) if len(data) == 0: # EOF packet log('parent got empty packet; killing child') posix.kill(pid, 15) return # Discard whole packet if we are too much behind if spkr.getfillable() > len(data) / BYTESPERSAMP: if len(debug) >= 2: log('parent Q full; dropping packet') spkr.writesamps(data) else: # Child -- microphone handler log('child started') try: try: mike = al.openport('mike', 'r', config) # Sleep a while to let the other side get started time.sleep(1) # Drain the queue before starting to read data = mike.readsamps(mike.getfilled()) # Loop, sending packets from the mike to the net while 1: data = mike.readsamps(SAMPSPERBUF) s.sendto(data, otheraddr) except KeyboardInterrupt: log('child got interrupt; exiting') posix._exit(0) except error: log('child got error; exiting') posix._exit(1) finally: log('child got unexpected error; leaving w/ traceback')
def __init__(self): parsetree = flp.parse_form('cmpaf_form','form') flp.create_full_form(self, parsetree) c = al.newconfig() c.setchannels(AL.MONO) c.setqueuesize(1800) self.iport = al.openport('cmpaf','r', c) self.oport = al.openport('cmpaf','w', c) self.do_adpcm = self.do_ulaw = self.do_diff = 0 self.acstate = None self.form.show_form(FL.PLACE_SIZE, 1, 'compare audio formats')
def open_audio(self): if self.aformat == A_OFF: return import aifc import al import AL import thread self.close_audio() params = [AL.INPUT_RATE, 0] al.getparams(AL.DEFAULT_DEVICE, params) rate = params[1] self.aout = aifc.open(self.afile, 'w') if self.aformat in (A_16_STEREO, A_8_STEREO): nch = AL.STEREO else: nch = AL.MONO if self.aformat in (A_16_STEREO, A_16_MONO): width = AL.SAMPLE_16 else: width = AL.SAMPLE_8 self.aout.setnchannels(nch) self.aout.setsampwidth(width) self.aout.setframerate(rate) c = al.newconfig() c.setqueuesize(8000) c.setchannels(nch) c.setwidth(width) self.aport = al.openport('Vb audio record', 'r', c) self.audio_stop = 0 self.audio_ok = 0 self.audio_busy = 1 thread.start_new_thread(self.record_audio, ())
def main(): s = socket(AF_INET, SOCK_DGRAM) s.bind('', PORT) oldparams = [AL.OUTPUT_RATE, 0] params = oldparams[:] al.getparams(AL.DEFAULT_DEVICE, oldparams) params[1] = AL.RATE_44100 try: al.setparams(AL.DEFAULT_DEVICE, params) config = al.newconfig() config.setwidth(AL.SAMPLE_16) config.setchannels(AL.STEREO) port = al.openport('CD Player', 'w', config) while 1: data = s.recv(CDDA_DATASIZE) if not data: print 'EOF' break port.writesamps(data) except KeyboardInterrupt: pass al.setparams(AL.DEFAULT_DEVICE, oldparams)
def handle_11(self): # openport name, mode, config_flat = self.unpacker.unpack_openport() self.turn_around() ## print 'openport:', name, mode, config_flat queuesize, width, channels, sampfmt, floatmax = config_flat try: c = al.newconfig() c.setqueuesize(queuesize) c.setwidth(width) c.setchannels(channels) c.setsampfmt(sampfmt) c.setfloatmax(floatmax) p = al.openport(name, mode, c) except RuntimeError: self.packer.pack_int(-1) # Should get error code? return for i in range(len(self.all_ports)): if self.all_ports[i] is None: self.all_ports[i] = p break else: i = len(self.all_ports) self.all_ports.append(p) ## print 'openport ->', i self.packer.pack_int(i)
def makeport(nchannels, sampwidth, samprate): c = al.newconfig() c.setchannels(nchannels) c.setwidth(sampwidth / 8) # can't set the rate... p = al.openport('', 'w', c) return p
def makeport(nchannels, sampwidth, samprate): c = al.newconfig() c.setchannels(nchannels) c.setwidth(sampwidth/8) # can't set the rate... p = al.openport('', 'w', c) return p
def main(): c = al.newconfig() c.setchannels(AL.MONO) c.setqueuesize(QSIZE) p = al.openport('', 'w', c) while 1: data = sys.stdin.read(BUFSIZE) p.writesamps(data)
def playit(buf): p = al.openport('hello', 'w') print 'playing...' p.writesamps(buf) while p.getfilled() > 0: time.sleep(0.01) print 'done.' p.closeport()
def main(): c = al.newconfig() c.setchannels(AL.MONO) c.setqueuesize(QSIZE) p = al.openport('', 'r', c) while 1: data = p.readsamps(BUFSIZE) sys.stdout.write(data)
def playit (buf) : p = al.openport('hello', 'w') print 'playing...' p.writesamps(buf) while p.getfilled() > 0: time.sleep(0.01) print 'done.' p.closeport()
def recordit () : p = al.openport('hello', 'r') print 'recording...' buf = p.readsamps(500000) print 'done.' p.closeport() return buf
def recordit(): p = al.openport('hello', 'r') print 'recording...' buf = p.readsamps(500000) print 'done.' p.closeport() return buf
def writeframes(self, data): if not self.__format or not self.__params[1]: raise Error, 'params not specified' if not self.__port: self.__port = al.openport('Python', 'w', self.__config) self.__oldparams = self.__params[:] al.getparams(al.DEFAULT_DEVICE, self.__oldparams) al.setparams(al.DEFAULT_DEVICE, self.__params) self.__port.writesamps(data)
def writeframes(self, data): if not (self.inited_outrate and self.inited_nchannels): raise error, 'params not specified' if not self.port: import al, AL self.port = al.openport('Python', 'w', self.config) self.oldparams = self.params[:] al.getparams(AL.DEFAULT_DEVICE, self.oldparams) al.setparams(AL.DEFAULT_DEVICE, self.params) if self.converter: data = self.converter(data) self.port.writesamps(data)
def main(): import sys, readcd, al, AL, CD, cdplayer verbose = 0 r = readcd.Readcd() prstatus(r.getstatus()) prtrackinfo(r.gettrackinfo()) cdinfo = cdplayer.Cdplayer(r.gettrackinfo()) if cdinfo.title <> '': print 'Title: "' + cdinfo.title + '"' if cdinfo.artist <> '': print 'Artist: ' + cdinfo.artist for arg in sys.argv[1:]: if arg == '-v': verbose = 1 continue x = eval(arg) try: l = len(x) r.appendstretch(x[0], x[1]) except TypeError: r.appendtrack(x) try: oldparams = [AL.OUTPUT_RATE, 0] params = oldparams[:] al.getparams(AL.DEFAULT_DEVICE, oldparams) params[1] = AL.RATE_44100 al.setparams(AL.DEFAULT_DEVICE, params) config = al.newconfig() config.setwidth(AL.SAMPLE_16) config.setchannels(AL.STEREO) port = al.openport('CD Player', 'w', config) for i in range(8): r.setcallback(i, callback, None) if verbose: r.setcallback(CD.PTIME, tcallback, None) r.setcallback(CD.ATIME, tcallback, None) else: r.removecallback(CD.PTIME) r.removecallback(CD.ATIME) r.setcallback(CD.PNUM, prtrack, cdinfo) r.setcallback(CD.AUDIO, playaudio, port) data = r.play() except KeyboardInterrupt: status = r.getstatus() print 'Interrupted at '+triple(status[2])+' into track '+ \ `status[1]`+' (absolute time '+triple(status[3])+')' al.setparams(AL.DEFAULT_DEVICE, oldparams)
def main(): import sys, readcd, al, AL, CD, cdplayer verbose = 0 r = readcd.Readcd().init() prstatus(r.getstatus()) prtrackinfo(r.gettrackinfo()) cdinfo = cdplayer.Cdplayer().init(r.gettrackinfo()) if cdinfo.title <> '': print 'Title: "' + cdinfo.title + '"' if cdinfo.artist <> '': print 'Artist: ' + cdinfo.artist for arg in sys.argv[1:]: if arg == '-v': verbose = 1 continue x = eval(arg) try: l = len(x) r.appendstretch(x[0], x[1]) except TypeError: r.appendtrack(x) try: oldparams = [AL.OUTPUT_RATE, 0] params = oldparams[:] al.getparams(AL.DEFAULT_DEVICE, oldparams) params[1] = AL.RATE_44100 al.setparams(AL.DEFAULT_DEVICE, params) config = al.newconfig() config.setwidth(AL.SAMPLE_16) config.setchannels(AL.STEREO) port = al.openport('CD Player', 'w', config) for i in range(8): r.setcallback(i, callback, None) if verbose: r.setcallback(CD.PTIME, tcallback, None) r.setcallback(CD.ATIME, tcallback, None) else: r.removecallback(CD.PTIME) r.removecallback(CD.ATIME) r.setcallback(CD.PNUM, prtrack, cdinfo) r.setcallback(CD.AUDIO, playaudio, port) data = r.play() except KeyboardInterrupt: status = r.getstatus() print 'Interrupted at '+triple(status[2])+' into track '+ \ `status[1]`+' (absolute time '+triple(status[3])+')' al.setparams(AL.DEFAULT_DEVICE, oldparams)
def initaudio(filename, stop, start, done): import thread, aifc afile = aifc.open(filename, 'w') afile.setnchannels(AL.MONO) afile.setsampwidth(AL.SAMPLE_8) params = [AL.INPUT_RATE, 0] al.getparams(AL.DEFAULT_DEVICE, params) print 'audio sampling rate =', params[1] afile.setframerate(params[1]) c = al.newconfig() c.setchannels(AL.MONO) c.setqueuesize(AQSIZE) c.setwidth(AL.SAMPLE_8) aport = al.openport(filename, 'r', c) thread.start_new_thread(audiorecord, (afile, aport, stop, start, done))
def initaudio(filename, stop, done): import thread, aifc afile = aifc.open(filename, 'w') afile.setnchannels(AL.MONO) afile.setsampwidth(AL.SAMPLE_8) params = [AL.INPUT_RATE, 0] al.getparams(AL.DEFAULT_DEVICE, params) print 'audio sampling rate =', params[1] afile.setframerate(params[1]) c = al.newconfig() c.setchannels(AL.MONO) c.setqueuesize(AQSIZE) c.setwidth(AL.SAMPLE_8) aport = al.openport(filename, 'r', c) thread.start_new_thread(audiorecord, (afile, aport, stop, done))
def initaudio(filename): import thread, aiff global recording, stop_recording afile = aiff.Aiff().init(filename, 'w') afile.nchannels = AL.MONO afile.sampwidth = AL.SAMPLE_8 params = [AL.INPUT_RATE, 0] al.getparams(AL.DEFAULT_DEVICE, params) print 'rate =', params[1] afile.samprate = params[1] c = al.newconfig() c.setchannels(AL.MONO) c.setqueuesize(AQSIZE) c.setwidth(AL.SAMPLE_8) aport = al.openport(filename, 'r', c) recording = thread.allocate_lock() recording.acquire() stop_recording = 0 thread.start_new_thread(recorder, (afile, aport)) return afile
def main(): if len(sys.argv) <> 2: sys.stderr.write('usage: ' + sys.argv[0] + ' hostname\n') sys.exit(2) hostname = sys.argv[1] cmd = 'exec rsh </dev/null ' + hostname + \ ' "cd /ufs/guido/mm/demo/audio; ' + \ 'exec /ufs/guido/bin/sgi/python record.py"' pipe = posix.popen(cmd, 'r') config = al.newconfig() config.setchannels(AL.MONO) config.setqueuesize(QSIZE) port = al.openport('', 'w', config) while 1: data = pipe.read(BUFSIZE) if not data: sts = pipe.close() sys.stderr.write(sys.argv[0] + ': end of data\n') if sts: sys.stderr.write('rsh exit status '+`sts`+'\n') sys.exit(1) port.writesamps(data) del data
def main(): if len(sys.argv) < 2: f = sys.stdin filename = sys.argv[0] else: if len(sys.argv) <> 2: sys.stderr.write('usage: ' + \ sys.argv[0] + ' filename\n') sys.exit(2) filename = sys.argv[1] f = open(filename, 'r') # magic = f.read(4) extra = '' if magic == '0008': rate = 8000 elif magic == '0016': rate = 16000 elif magic == '0032': rate = 32000 else: sys.stderr.write('no magic header; assuming 8k samples/sec.\n') rate = 8000 extra = magic # pv = [AL.OUTPUT_RATE, rate] al.setparams(AL.DEFAULT_DEVICE, pv) c = al.newconfig() c.setchannels(AL.MONO) c.setwidth(AL.SAMPLE_8) port = al.openport(filename, 'w', c) if extra: port.writesamps(extra) while 1: buf = f.read(BUFSIZE) if not buf: break port.writesamps(buf) while port.getfilled() > 0: time.millisleep(100)
def main(): if len(sys.argv) <> 2: sys.stderr.write('usage: ' + sys.argv[0] + ' hostname\n') sys.exit(2) hostname = sys.argv[1] cmd = 'exec rsh </dev/null ' + hostname + \ ' "cd /ufs/guido/mm/demo/audio; ' + \ 'exec /ufs/guido/bin/sgi/python record.py"' pipe = posix.popen(cmd, 'r') config = al.newconfig() config.setchannels(AL.MONO) config.setqueuesize(QSIZE) port = al.openport('', 'w', config) while 1: data = pipe.read(BUFSIZE) if not data: sts = pipe.close() sys.stderr.write(sys.argv[0] + ': end of data\n') if sts: sys.stderr.write('rsh exit status ' + ` sts ` + '\n') sys.exit(1) port.writesamps(data) del data
#! /usr/bin/env python # unicast host [port] # # Similar to "broadcast.py" but sends to a specific host only; # use "radio.py" on the designated host to receive. # This is less stressful on other hosts on the same ethernet segment # if you need to send to one host only. import sys, al from socket import * host = sys.argv[1] port = 5555 if sys.argv[2:]: port = eval(sys.argv[1]) s = socket(AF_INET, SOCK_DGRAM) p = al.openport('unicast', 'r') address = host, port while 1: # 700 samples equals 1400 bytes, or about the max packet size! data = p.readsamps(700) s.sendto(data, address)
"""Classes for manipulating audio devices (currently only for Sun and SGI)"""
import aiff
#
#! /usr/bin/env python
#! /usr/bin/env python # broadcast [port] # # Broadcast audio input on the network as UDP packets; # they can be received on any SGI machine with "radio.py". # This uses the input sampling rate, input source etc. set by apanel. # It uses the default sample width and #channels (16 bit/sample stereo). # (This is 192,000 Bytes at a sampling speed of 48 kHz, or ~137 # packets/second -- use with caution!!!) import sys, al from socket import * port = 5555 if sys.argv[1:]: port = eval(sys.argv[1]) s = socket(AF_INET, SOCK_DGRAM) s.allowbroadcast(1) p = al.openport('broadcast', 'r') address = '<broadcast>', port while 1: # 700 samples equals 1400 bytes, or about the max packet size! data = p.readsamps(700) s.sendto(data, address)
#! /usr/bin/env python # radio [port] # # Receive audio packets broadcast by "broadcast.py" on another SGI machine. # Use apanel to set the output sampling rate to match that of the broadcast. import sys, al from socket import * port = 5555 if sys.argv[1:]: port = eval(sys.argv[1]) s = socket(AF_INET, SOCK_DGRAM) s.bind('', port) p = al.openport('radio', 'w') while 1: data = s.recv(1400) p.writesamps(data)
# Compare different audio compression schemes.
#! /usr/local/bin/python # broadcast [port] # # Broadcast audio input on the network as UDP packets; # they can be received on any SGI machine with "radio.py". # This uses the input sampling rate, input source etc. set by apanel. # It uses the default sample width and #channels (16 bit/sample stereo). # (This is 192,000 Bytes at a sampling speed of 48 kHz, or ~137 # packets/second -- use with caution!!!) import sys, al from socket import * port = 5555 if sys.argv[1:]: port = eval(sys.argv[1]) s = socket(AF_INET, SOCK_DGRAM) s.allowbroadcast(1) p = al.openport('broadcast', 'r') address = '<broadcast>', port while 1: # 700 samples equals 1400 bytes, or about the max packet size! data = p.readsamps(700) s.sendto(data, address)
# Record mono 16bits samples from the audio device and send them to stdout.
ain.getnframes() * ain.getnchannels() * ain.getsampwidth() print 'Opening audio output port..' c = al.newconfig() c.setchannels(ain.getnchannels()) c.setwidth(ain.getsampwidth()) nullsample = '\0' * ain.getsampwidth() samples_per_second = ain.getnchannels() * ain.getframerate() if qsize <= 0: qsize = samples_per_second / 10 qsize = max(qsize, 512) c.setqueuesize(qsize) saveparams = [AL.OUTPUT_RATE, 0] al.getparams(AL.DEFAULT_DEVICE, saveparams) newparams = [AL.OUTPUT_RATE, ain.getframerate()] al.setparams(AL.DEFAULT_DEVICE, newparams) aport = al.openport(audiofile, 'w', c) print 'Opening video output window..' gl.foreground() gl.prefsize(vin.width, vin.height) wid = gl.winopen(videofile + ' + ' + audiofile) gl.clear() vin.initcolormap() print 'Playing..' gl.qdevice(DEVICE.ESCKEY) gl.qdevice(DEVICE.LEFTARROWKEY) gl.qdevice(DEVICE.RIGHTARROWKEY) ## gl.qdevice(DEVICE.UPARROWKEY) ## gl.qdevice(DEVICE.DOWNARROWKEY) gl.qdevice(DEVICE.SPACEKEY)
#! /usr/bin/env python # Play synchronous video and audio. # Highly experimental! import sys import getopt import string import os import VFile import aifc import gl, GL, DEVICE import al, AL def usage(): sys.stderr.write( \ 'usage: aplay [-o offset] [-q qsize] videofile audiofile\n') sys.exit(2) def main(): offset = 0 qsize = 0 # This defaults to 1/10 second of sound videofile = 'film.video' audiofile = 'film.aiff' try: opts, args = getopt.getopt(sys.argv[1:], 'o:q:') except getopt.error, msg: sys.stderr.write(msg + '\n') usage() try: for o, a in opts: if o == '-o': offset = string.atoi(a)
# intercom -- use mike and headset to *talk* to a person on another host.
#! /ufs/guido/bin/sgi/python # radio [port] # # Receive audio packets broadcast by "broadcast.py" on another SGI machine. # Use apanel to set the output sampling rate to match that of the broadcast. import sys, al from socket import * port = 5555 if sys.argv[1:]: port = eval(sys.argv[1]) s = socket(AF_INET, SOCK_DGRAM) s.bind('', port) p = al.openport('radio', 'w') while 1: data = s.recv(1400) p.writesamps(data)
# Play old style sound files (Guido's private format)