Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: audiodev.py
"""Classes for manipulating audio devices (currently only for Sun and SGI)"""
[0] Fix | Delete
from warnings import warnpy3k
[1] Fix | Delete
warnpy3k("the audiodev module has been removed in Python 3.0", stacklevel=2)
[2] Fix | Delete
del warnpy3k
[3] Fix | Delete
[4] Fix | Delete
__all__ = ["error","AudioDev"]
[5] Fix | Delete
[6] Fix | Delete
class error(Exception):
[7] Fix | Delete
pass
[8] Fix | Delete
[9] Fix | Delete
class Play_Audio_sgi:
[10] Fix | Delete
# Private instance variables
[11] Fix | Delete
## if 0: access frameratelist, nchannelslist, sampwidthlist, oldparams, \
[12] Fix | Delete
## params, config, inited_outrate, inited_width, \
[13] Fix | Delete
## inited_nchannels, port, converter, classinited: private
[14] Fix | Delete
[15] Fix | Delete
classinited = 0
[16] Fix | Delete
frameratelist = nchannelslist = sampwidthlist = None
[17] Fix | Delete
[18] Fix | Delete
def initclass(self):
[19] Fix | Delete
import AL
[20] Fix | Delete
self.frameratelist = [
[21] Fix | Delete
(48000, AL.RATE_48000),
[22] Fix | Delete
(44100, AL.RATE_44100),
[23] Fix | Delete
(32000, AL.RATE_32000),
[24] Fix | Delete
(22050, AL.RATE_22050),
[25] Fix | Delete
(16000, AL.RATE_16000),
[26] Fix | Delete
(11025, AL.RATE_11025),
[27] Fix | Delete
( 8000, AL.RATE_8000),
[28] Fix | Delete
]
[29] Fix | Delete
self.nchannelslist = [
[30] Fix | Delete
(1, AL.MONO),
[31] Fix | Delete
(2, AL.STEREO),
[32] Fix | Delete
(4, AL.QUADRO),
[33] Fix | Delete
]
[34] Fix | Delete
self.sampwidthlist = [
[35] Fix | Delete
(1, AL.SAMPLE_8),
[36] Fix | Delete
(2, AL.SAMPLE_16),
[37] Fix | Delete
(3, AL.SAMPLE_24),
[38] Fix | Delete
]
[39] Fix | Delete
self.classinited = 1
[40] Fix | Delete
[41] Fix | Delete
def __init__(self):
[42] Fix | Delete
import al, AL
[43] Fix | Delete
if not self.classinited:
[44] Fix | Delete
self.initclass()
[45] Fix | Delete
self.oldparams = []
[46] Fix | Delete
self.params = [AL.OUTPUT_RATE, 0]
[47] Fix | Delete
self.config = al.newconfig()
[48] Fix | Delete
self.inited_outrate = 0
[49] Fix | Delete
self.inited_width = 0
[50] Fix | Delete
self.inited_nchannels = 0
[51] Fix | Delete
self.converter = None
[52] Fix | Delete
self.port = None
[53] Fix | Delete
return
[54] Fix | Delete
[55] Fix | Delete
def __del__(self):
[56] Fix | Delete
if self.port:
[57] Fix | Delete
self.stop()
[58] Fix | Delete
if self.oldparams:
[59] Fix | Delete
import al, AL
[60] Fix | Delete
al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
[61] Fix | Delete
self.oldparams = []
[62] Fix | Delete
[63] Fix | Delete
def wait(self):
[64] Fix | Delete
if not self.port:
[65] Fix | Delete
return
[66] Fix | Delete
import time
[67] Fix | Delete
while self.port.getfilled() > 0:
[68] Fix | Delete
time.sleep(0.1)
[69] Fix | Delete
self.stop()
[70] Fix | Delete
[71] Fix | Delete
def stop(self):
[72] Fix | Delete
if self.port:
[73] Fix | Delete
self.port.closeport()
[74] Fix | Delete
self.port = None
[75] Fix | Delete
if self.oldparams:
[76] Fix | Delete
import al, AL
[77] Fix | Delete
al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
[78] Fix | Delete
self.oldparams = []
[79] Fix | Delete
[80] Fix | Delete
def setoutrate(self, rate):
[81] Fix | Delete
for (raw, cooked) in self.frameratelist:
[82] Fix | Delete
if rate == raw:
[83] Fix | Delete
self.params[1] = cooked
[84] Fix | Delete
self.inited_outrate = 1
[85] Fix | Delete
break
[86] Fix | Delete
else:
[87] Fix | Delete
raise error, 'bad output rate'
[88] Fix | Delete
[89] Fix | Delete
def setsampwidth(self, width):
[90] Fix | Delete
for (raw, cooked) in self.sampwidthlist:
[91] Fix | Delete
if width == raw:
[92] Fix | Delete
self.config.setwidth(cooked)
[93] Fix | Delete
self.inited_width = 1
[94] Fix | Delete
break
[95] Fix | Delete
else:
[96] Fix | Delete
if width == 0:
[97] Fix | Delete
import AL
[98] Fix | Delete
self.inited_width = 0
[99] Fix | Delete
self.config.setwidth(AL.SAMPLE_16)
[100] Fix | Delete
self.converter = self.ulaw2lin
[101] Fix | Delete
else:
[102] Fix | Delete
raise error, 'bad sample width'
[103] Fix | Delete
[104] Fix | Delete
def setnchannels(self, nchannels):
[105] Fix | Delete
for (raw, cooked) in self.nchannelslist:
[106] Fix | Delete
if nchannels == raw:
[107] Fix | Delete
self.config.setchannels(cooked)
[108] Fix | Delete
self.inited_nchannels = 1
[109] Fix | Delete
break
[110] Fix | Delete
else:
[111] Fix | Delete
raise error, 'bad # of channels'
[112] Fix | Delete
[113] Fix | Delete
def writeframes(self, data):
[114] Fix | Delete
if not (self.inited_outrate and self.inited_nchannels):
[115] Fix | Delete
raise error, 'params not specified'
[116] Fix | Delete
if not self.port:
[117] Fix | Delete
import al, AL
[118] Fix | Delete
self.port = al.openport('Python', 'w', self.config)
[119] Fix | Delete
self.oldparams = self.params[:]
[120] Fix | Delete
al.getparams(AL.DEFAULT_DEVICE, self.oldparams)
[121] Fix | Delete
al.setparams(AL.DEFAULT_DEVICE, self.params)
[122] Fix | Delete
if self.converter:
[123] Fix | Delete
data = self.converter(data)
[124] Fix | Delete
self.port.writesamps(data)
[125] Fix | Delete
[126] Fix | Delete
def getfilled(self):
[127] Fix | Delete
if self.port:
[128] Fix | Delete
return self.port.getfilled()
[129] Fix | Delete
else:
[130] Fix | Delete
return 0
[131] Fix | Delete
[132] Fix | Delete
def getfillable(self):
[133] Fix | Delete
if self.port:
[134] Fix | Delete
return self.port.getfillable()
[135] Fix | Delete
else:
[136] Fix | Delete
return self.config.getqueuesize()
[137] Fix | Delete
[138] Fix | Delete
# private methods
[139] Fix | Delete
## if 0: access *: private
[140] Fix | Delete
[141] Fix | Delete
def ulaw2lin(self, data):
[142] Fix | Delete
import audioop
[143] Fix | Delete
return audioop.ulaw2lin(data, 2)
[144] Fix | Delete
[145] Fix | Delete
class Play_Audio_sun:
[146] Fix | Delete
## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \
[147] Fix | Delete
## inited_nchannels, converter: private
[148] Fix | Delete
[149] Fix | Delete
def __init__(self):
[150] Fix | Delete
self.outrate = 0
[151] Fix | Delete
self.sampwidth = 0
[152] Fix | Delete
self.nchannels = 0
[153] Fix | Delete
self.inited_outrate = 0
[154] Fix | Delete
self.inited_width = 0
[155] Fix | Delete
self.inited_nchannels = 0
[156] Fix | Delete
self.converter = None
[157] Fix | Delete
self.port = None
[158] Fix | Delete
return
[159] Fix | Delete
[160] Fix | Delete
def __del__(self):
[161] Fix | Delete
self.stop()
[162] Fix | Delete
[163] Fix | Delete
def setoutrate(self, rate):
[164] Fix | Delete
self.outrate = rate
[165] Fix | Delete
self.inited_outrate = 1
[166] Fix | Delete
[167] Fix | Delete
def setsampwidth(self, width):
[168] Fix | Delete
self.sampwidth = width
[169] Fix | Delete
self.inited_width = 1
[170] Fix | Delete
[171] Fix | Delete
def setnchannels(self, nchannels):
[172] Fix | Delete
self.nchannels = nchannels
[173] Fix | Delete
self.inited_nchannels = 1
[174] Fix | Delete
[175] Fix | Delete
def writeframes(self, data):
[176] Fix | Delete
if not (self.inited_outrate and self.inited_width and self.inited_nchannels):
[177] Fix | Delete
raise error, 'params not specified'
[178] Fix | Delete
if not self.port:
[179] Fix | Delete
import sunaudiodev, SUNAUDIODEV
[180] Fix | Delete
self.port = sunaudiodev.open('w')
[181] Fix | Delete
info = self.port.getinfo()
[182] Fix | Delete
info.o_sample_rate = self.outrate
[183] Fix | Delete
info.o_channels = self.nchannels
[184] Fix | Delete
if self.sampwidth == 0:
[185] Fix | Delete
info.o_precision = 8
[186] Fix | Delete
self.o_encoding = SUNAUDIODEV.ENCODING_ULAW
[187] Fix | Delete
# XXX Hack, hack -- leave defaults
[188] Fix | Delete
else:
[189] Fix | Delete
info.o_precision = 8 * self.sampwidth
[190] Fix | Delete
info.o_encoding = SUNAUDIODEV.ENCODING_LINEAR
[191] Fix | Delete
self.port.setinfo(info)
[192] Fix | Delete
if self.converter:
[193] Fix | Delete
data = self.converter(data)
[194] Fix | Delete
self.port.write(data)
[195] Fix | Delete
[196] Fix | Delete
def wait(self):
[197] Fix | Delete
if not self.port:
[198] Fix | Delete
return
[199] Fix | Delete
self.port.drain()
[200] Fix | Delete
self.stop()
[201] Fix | Delete
[202] Fix | Delete
def stop(self):
[203] Fix | Delete
if self.port:
[204] Fix | Delete
self.port.flush()
[205] Fix | Delete
self.port.close()
[206] Fix | Delete
self.port = None
[207] Fix | Delete
[208] Fix | Delete
def getfilled(self):
[209] Fix | Delete
if self.port:
[210] Fix | Delete
return self.port.obufcount()
[211] Fix | Delete
else:
[212] Fix | Delete
return 0
[213] Fix | Delete
[214] Fix | Delete
## # Nobody remembers what this method does, and it's broken. :-(
[215] Fix | Delete
## def getfillable(self):
[216] Fix | Delete
## return BUFFERSIZE - self.getfilled()
[217] Fix | Delete
[218] Fix | Delete
def AudioDev():
[219] Fix | Delete
# Dynamically try to import and use a platform specific module.
[220] Fix | Delete
try:
[221] Fix | Delete
import al
[222] Fix | Delete
except ImportError:
[223] Fix | Delete
try:
[224] Fix | Delete
import sunaudiodev
[225] Fix | Delete
return Play_Audio_sun()
[226] Fix | Delete
except ImportError:
[227] Fix | Delete
try:
[228] Fix | Delete
import Audio_mac
[229] Fix | Delete
except ImportError:
[230] Fix | Delete
raise error, 'no audio device'
[231] Fix | Delete
else:
[232] Fix | Delete
return Audio_mac.Play_Audio_mac()
[233] Fix | Delete
else:
[234] Fix | Delete
return Play_Audio_sgi()
[235] Fix | Delete
[236] Fix | Delete
def test(fn = None):
[237] Fix | Delete
import sys
[238] Fix | Delete
if sys.argv[1:]:
[239] Fix | Delete
fn = sys.argv[1]
[240] Fix | Delete
else:
[241] Fix | Delete
fn = 'f:just samples:just.aif'
[242] Fix | Delete
import aifc
[243] Fix | Delete
af = aifc.open(fn, 'r')
[244] Fix | Delete
print fn, af.getparams()
[245] Fix | Delete
p = AudioDev()
[246] Fix | Delete
p.setoutrate(af.getframerate())
[247] Fix | Delete
p.setsampwidth(af.getsampwidth())
[248] Fix | Delete
p.setnchannels(af.getnchannels())
[249] Fix | Delete
BUFSIZ = af.getframerate()/af.getsampwidth()/af.getnchannels()
[250] Fix | Delete
while 1:
[251] Fix | Delete
data = af.readframes(BUFSIZ)
[252] Fix | Delete
if not data: break
[253] Fix | Delete
print len(data)
[254] Fix | Delete
p.writeframes(data)
[255] Fix | Delete
p.wait()
[256] Fix | Delete
[257] Fix | Delete
if __name__ == '__main__':
[258] Fix | Delete
test()
[259] Fix | Delete
[260] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function