%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/local/lib/python2.7/
Upload File :
Create Path :
Current File : //usr/local/lib/python2.7/aifc.pyc


Mac@sadZddlZddlZdddgZdefdYZdZdZd	Zd
Z	dZ
dZd
ZdZ
dZdZdZdZdZdZddlmZdfdYZdfdYZedZeZedkr]ddlZejdr<ejjdnejdZeedZ zdGeGHd Ge j!GHd!Ge j"GHd"Ge j#GHd#Ge j$GHd$Ge j%GHd%Ge j&GHejd&rKejd&Z'd'Ge'GHee'd(Z(zEe(j)e j*x+e j+d)Z,e,r#Pne(j-e,q	WWde(j.Xd*GHnWde j.XndS(+sDStuff to parse AIFF-C and AIFF files.

Unless explicitly stated otherwise, the description below is true
both for AIFF-C files and AIFF files.

An AIFF-C file has the following structure.

  +-----------------+
  | FORM            |
  +-----------------+
  | <size>          |
  +----+------------+
  |    | AIFC       |
  |    +------------+
  |    | <chunks>   |
  |    |    .       |
  |    |    .       |
  |    |    .       |
  +----+------------+

An AIFF file has the string "AIFF" instead of "AIFC".

A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
big endian order), followed by the data.  The size field does not include
the size of the 8 byte header.

The following chunk types are recognized.

  FVER
      <version number of AIFF-C defining document> (AIFF-C only).
  MARK
      <# of markers> (2 bytes)
      list of markers:
          <marker ID> (2 bytes, must be > 0)
          <position> (4 bytes)
          <marker name> ("pstring")
  COMM
      <# of channels> (2 bytes)
      <# of sound frames> (4 bytes)
      <size of the samples> (2 bytes)
      <sampling frequency> (10 bytes, IEEE 80-bit extended
          floating point)
      in AIFF-C files only:
      <compression type> (4 bytes)
      <human-readable version of compression type> ("pstring")
  SSND
      <offset> (4 bytes, not used by this program)
      <blocksize> (4 bytes, not used by this program)
      <sound data>

A pstring consists of 1 byte length, a string of characters, and 0 or 1
byte pad to make the total length even.

Usage.

Reading AIFF files:
  f = aifc.open(file, 'r')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods read(), seek(), and close().
In some types of audio files, if the setpos() method is not used,
the seek() method is not necessary.

This returns an instance of a class with the following public methods:
  getnchannels()  -- returns number of audio channels (1 for
             mono, 2 for stereo)
  getsampwidth()  -- returns sample width in bytes
  getframerate()  -- returns sampling frequency
  getnframes()    -- returns number of audio frames
  getcomptype()   -- returns compression type ('NONE' for AIFF files)
  getcompname()   -- returns human-readable version of
             compression type ('not compressed' for AIFF files)
  getparams() -- returns a tuple consisting of all of the
             above in the above order
  getmarkers()    -- get the list of marks in the audio file or None
             if there are no marks
  getmark(id) -- get mark with the specified id (raises an error
             if the mark does not exist)
  readframes(n)   -- returns at most n frames of audio
  rewind()    -- rewind to the beginning of the audio stream
  setpos(pos) -- seek to the specified position
  tell()      -- return the current position
  close()     -- close the instance (make it unusable)
The position returned by tell(), the position given to setpos() and
the position of marks are all compatible and have nothing to do with
the actual position in the file.
The close() method is called automatically when the class instance
is destroyed.

Writing AIFF files:
  f = aifc.open(file, 'w')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods write(), tell(), seek(), and
close().

This returns an instance of a class with the following public methods:
  aiff()      -- create an AIFF file (AIFF-C default)
  aifc()      -- create an AIFF-C file
  setnchannels(n) -- set the number of channels
  setsampwidth(n) -- set the sample width
  setframerate(n) -- set the frame rate
  setnframes(n)   -- set the number of frames
  setcomptype(type, name)
          -- set the compression type and the
             human-readable compression type
  setparams(tuple)
          -- set all parameters at once
  setmark(id, pos, name)
          -- add specified mark to the list of marks
  tell()      -- return current position in output file (useful
             in combination with setmark())
  writeframesraw(data)
          -- write audio frames without pathing up the
             file header
  writeframes(data)
          -- write audio frames and patch up the file header
  close()     -- patch up the file header and close the
             output file
You should set the parameters before the first writeframesraw or
writeframes.  The total number of frames does not need to be set,
but when it is set to the correct value, the header does not have to
be patched up.
It is best to first set all parameters, perhaps possibly the
compression type, and then write audio frames using writeframesraw.
When all frames have been written, either call writeframes('') or
close() to patch up the sizes in the header.
Marks can be added anytime.  If there are any marks, you must call
close() after all frames have been written.
The close() method is called automatically when the class instance
is destroyed.

When a file is opened with the extension '.aiff', an AIFF file is
written, otherwise an AIFF-C file is written.  This default can be
changed by calling aiff() or aifc() before the first writeframes or
writeframesraw.
iNtErrortopentopenfpcBseZRS((t__name__t
__module__(((s /usr/local/lib/python2.7/aifc.pyRsl@QEcCsBy!tjd|jddSWntjk
r=tnXdS(Ns>lii(tstructtunpacktreadterrortEOFError(tfile((s /usr/local/lib/python2.7/aifc.pyt
_read_longs!cCsBy!tjd|jddSWntjk
r=tnXdS(Ns>Lii(RRRRR	(R
((s /usr/local/lib/python2.7/aifc.pyt_read_ulongs!cCsBy!tjd|jddSWntjk
r=tnXdS(Ns>hii(RRRRR	(R
((s /usr/local/lib/python2.7/aifc.pyt_read_shorts!cCsBy!tjd|jddSWntjk
r=tnXdS(Ns>Hii(RRRRR	(R
((s /usr/local/lib/python2.7/aifc.pyt_read_ushorts!cCs_t|jd}|dkr*d}n|j|}|d@dkr[|jd}n|S(Niit(tordR(R
tlengthtdatatdummy((s /usr/local/lib/python2.7/aifc.pyt_read_strings	gcCst|}d}|dkr1d}|d}nt|}t|}||kok|kokdknryd}n>|dkrt}n)|d}|d|td	|d
}||S(Niiiigii?lg@i?(R
Rt	_HUGE_VALtpow(tftexpontsignthimanttlomant((s /usr/local/lib/python2.7/aifc.pyt_read_floats
'		
cCs|jtjd|dS(Ns>h(twriteRtpack(Rtx((s /usr/local/lib/python2.7/aifc.pyt_write_shortscCs|jtjd|dS(Ns>H(RRR(RR((s /usr/local/lib/python2.7/aifc.pyt
_write_ushortscCs|jtjd|dS(Ns>l(RRR(RR((s /usr/local/lib/python2.7/aifc.pyt_write_longscCs|jtjd|dS(Ns>L(RRR(RR((s /usr/local/lib/python2.7/aifc.pyt_write_ulongscCs}t|dkr!tdn|jtjdt||j|t|d@dkry|jtdndS(Nis%string exceeds maximum pstring lengthtBii(tlent
ValueErrorRRRtchr(Rts((s /usr/local/lib/python2.7/aifc.pyt
_write_strings
c	Cshddl}|dkr+d}|d}nd}|dkrRd}d}d}n|j|\}}|dks|dks||kr|dB}d}d}n|d}|dkr|j||}d}n||B}|j|d}|j|}t|}|j||d}|j|}t|}t||t||t||dS(	Niiii@iii?i (tmathtfrexptldexptfloortlongR!R#(	RRR*RRRRtfmanttfsmant((s /usr/local/lib/python2.7/aifc.pyt_write_floats8
	$
	
	


(tChunkt	Aifc_readcBseZdZdZdZdZdZdZdZ	dZ
dZdZd	Z
d
ZdZdZd
ZdZdZdZdZdZdZdZdZRS(cCs^d|_d|_d|_g|_d|_||_t|}|jdkr`t	dn|j
d}|dkrd|_n!|dkrd|_n	t	dd|_d|_
xd|_yt|j}Wntk
rPnX|j}|d	kr|j|d|_nj|d
krO||_
|j
d}d|_n:|dkrmt||_n|d
kr|j|n|jqW|js|j
rt	dn|jrZ|jrZddl}|jd|j|jd|j|jg}|jdkr|j|d<n(|jdkr>|j|d<n	t	d|jj|ndS(NitFORMs file does not start with FORM iditAIFFtAIFCisnot an AIFF or AIFF-C filetCOMMtSSNDitFVERtMARKs$COMM chunk and/or SSND chunk missingiis$cannot compress more than 2 channels(t_versiontNonet_decompt_convertt_markerst	_soundpost_fileR2tgetnameRRt_aifct_comm_chunk_readt_ssnd_chunkt_ssnd_seek_neededR	t_read_comm_chunkRt	_readmarktskiptcltORIGINAL_FORMATtBITS_PER_COMPONENTt
_sampwidtht
FRAME_RATEt
_frameratet
_nchannelstMONOtSTEREO_INTERLEAVEDt	SetParams(tselfR
tchunktformdatat	chunknameRRJtparams((s /usr/local/lib/python2.7/aifc.pytinitfp%sb										

			cCs]t|trLtj|d}y|j|WqY|jqYXn
|j|dS(Ntrb(t
isinstancet
basestringt__builtin__RRYtclose(RTR((s /usr/local/lib/python2.7/aifc.pyt__init__Zs

cCs|jS(N(RA(RT((s /usr/local/lib/python2.7/aifc.pytgetfpiscCsd|_d|_dS(Nii(RFR@(RT((s /usr/local/lib/python2.7/aifc.pytrewindls	cCs>|j}z |r(d|_|jnWd|jjXdS(N(R=R<tCloseDecompressorRAR^(RTtdecomp((s /usr/local/lib/python2.7/aifc.pyR^ps		cCs|jS(N(R@(RT((s /usr/local/lib/python2.7/aifc.pyttellyscCs|jS(N(RP(RT((s /usr/local/lib/python2.7/aifc.pytgetnchannels|scCs|jS(N(t_nframes(RT((s /usr/local/lib/python2.7/aifc.pyt
getnframesscCs|jS(N(RM(RT((s /usr/local/lib/python2.7/aifc.pytgetsampwidthscCs|jS(N(RO(RT((s /usr/local/lib/python2.7/aifc.pytgetframeratescCs|jS(N(t	_comptype(RT((s /usr/local/lib/python2.7/aifc.pytgetcomptypescCs|jS(N(t	_compname(RT((s /usr/local/lib/python2.7/aifc.pytgetcompnamescCs:|j|j|j|j|j|jfS(N(ReRhRiRgRkRm(RT((s /usr/local/lib/python2.7/aifc.pyt	getparamsscCs t|jdkrdS|jS(Ni(R%R?R<(RT((s /usr/local/lib/python2.7/aifc.pyt
getmarkersscCs<x%|jD]}||dkr
|Sq
Wtd|fdS(Nismarker %r does not exist(R?R(RTtidtmarker((s /usr/local/lib/python2.7/aifc.pytgetmarkscCs=|dks||jkr'tdn||_d|_dS(Nisposition not in rangei(RfRR@RF(RTtpos((s /usr/local/lib/python2.7/aifc.pytsetposs	cCs|jrd|jjd|jjd}|j|j}|rX|jj|dnd|_n|dkrtdS|jj||j}|jr|r|j|}n|jt||j|j	|_|S(NiiR(
RFREtseekRR@t
_framesizeR>R%RPRM(RTtnframesRRsR((s /usr/local/lib/python2.7/aifc.pyt
readframess	$cCsNddl}|jj|jt|d}|jjt||j|S(Nii(RJR=tSetParamtFRAME_BUFFER_SIZER%t
DecompressRP(RTRRJR((s /usr/local/lib/python2.7/aifc.pyt_decomp_datas
cCsddl}|j|dS(Nii(taudiooptulaw2lin(RTRR}((s /usr/local/lib/python2.7/aifc.pyt	_ulaw2linscCsLddl}t|ds'd|_n|j|d|j\}|_|S(Nit_adpcmstatei(R}thasattrR<Rt	adpcm2lin(RTRR}((s /usr/local/lib/python2.7/aifc.pyt
_adpcm2linscCspt||_t||_t|dd|_tt||_|j|j|_|j	rZd}|j
dkrd}dGHd|_
n|jd|_|rt
|jjd}|d@dkr|d}n|j
||_
|jjd	dnt||_|jd
krl|jdkrryd	dl}Wntk
rUqrX|j|_d|_dSnyd	dl}Wnitk
r|jdkry)d	dl}|j|_d|_dSWqtk
rqXntdnX|jdkr	|j}n$|jdkr$|j}n	td|j||_|j|_d|_qlnd
|_d|_dS(NiiiiisWarning: bad COMM chunk sizeiiitNONEtG722itULAWtulaws#cannot read compressed AIFF-C filestALAWtalawsunsupported compression typesnot compressed(RR(RR(RR(R
RPRRfRMtintRRORvRCt	chunksizeRRjRR
RuRRlR}tImportErrorRR>RJRRt	G711_ULAWt	G711_ALAWtOpenDecompressorR=R|(RTRUtkludgeRR}RJtscheme((s /usr/local/lib/python2.7/aifc.pyRGsd	

	
	

		cCst|}ygx`t|D]R}t|}t|}t|}|sR|r|jj|||fqqWWnKtk
rdGt|jGt|jdkrdGndGdG|GHnXdS(Ns!Warning: MARK chunk contains onlyiRqtmarkerss
instead of(R
trangeRRR?tappendR	R%(RTRUtnmarkerstiRpRstname((s /usr/local/lib/python2.7/aifc.pyRHs$

N(RRR<RARYR_R`RaR^RdReRgRhRiRkRmRnRoRrRtRxR|RRRGRH(((s /usr/local/lib/python2.7/aifc.pyR3s.$	5																						<t
Aifc_writecBs@eZd"ZdZdZdZdZdZdZ	dZ
dZdZd	Z
d
ZdZdZd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#d Z$d!Z%RS(#cCsft|tr*|}tj|d}nd}|j||ddkrYd|_n	d|_dS(Ntwbs???is.aiffii(R[R\R]RRYRC(RTRtfilename((s /usr/local/lib/python2.7/aifc.pyR_@s
cCs||_t|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_d|_
d|_g|_d|_d|_dS(NRsnot compressedii(RAt
_AIFC_versionR;RjRlR<t_compR>RPRMRORft_nframeswrittent_datawrittent_datalengthR?t_marklengthRC(RTR
((s /usr/local/lib/python2.7/aifc.pyRYMs 															cCs|jr|jndS(N(RAR^(RT((s /usr/local/lib/python2.7/aifc.pyt__del___s	cCs"|jrtdnd|_dS(Ns0cannot change parameters after starting to writei(RRRC(RT((s /usr/local/lib/python2.7/aifc.pytaifffs	cCs"|jrtdnd|_dS(Ns0cannot change parameters after starting to writei(RRRC(RT((s /usr/local/lib/python2.7/aifc.pytaifcks	cCs:|jrtdn|dkr-tdn||_dS(Ns0cannot change parameters after starting to writeisbad # of channels(RRRP(RTt	nchannels((s /usr/local/lib/python2.7/aifc.pytsetnchannelsps
	cCs|jstdn|jS(Nsnumber of channels not set(RPR(RT((s /usr/local/lib/python2.7/aifc.pyRews	cCsF|jrtdn|dks-|dkr9tdn||_dS(Ns0cannot change parameters after starting to writeiisbad sample width(RRRM(RTt	sampwidth((s /usr/local/lib/python2.7/aifc.pytsetsampwidth|s
	cCs|jstdn|jS(Nssample width not set(RMR(RT((s /usr/local/lib/python2.7/aifc.pyRhs	cCs:|jrtdn|dkr-tdn||_dS(Ns0cannot change parameters after starting to writeisbad frame rate(RRRO(RTt	framerate((s /usr/local/lib/python2.7/aifc.pytsetframerates
	cCs|jstdn|jS(Nsframe rate not set(ROR(RT((s /usr/local/lib/python2.7/aifc.pyRis	cCs"|jrtdn||_dS(Ns0cannot change parameters after starting to write(RRRf(RTRw((s /usr/local/lib/python2.7/aifc.pyt
setnframess	cCs|jS(N(R(RT((s /usr/local/lib/python2.7/aifc.pyRgscCsC|jrtdn|d	kr-tdn||_||_dS(
Ns0cannot change parameters after starting to writeRRRRRRsunsupported compression type(RRRRRR(RRRjRl(RTtcomptypetcompname((s /usr/local/lib/python2.7/aifc.pytsetcomptypes		cCs|jS(N(Rj(RT((s /usr/local/lib/python2.7/aifc.pyRkscCs|jS(N(Rl(RT((s /usr/local/lib/python2.7/aifc.pyRmscCs|\}}}}}}|jr-tdn|d	krEtdn|j||j||j||j||j||dS(
Ns0cannot change parameters after starting to writeRRRRRRsunsupported compression type(RRRRRR(RRRRRRR(RTtinfoRRRRwRR((s /usr/local/lib/python2.7/aifc.pyt	setparamss	



cCsR|js|js|jr*tdn|j|j|j|j|j|jfS(Nsnot all parameters set(RPRMRORRfRjRl(RT((s /usr/local/lib/python2.7/aifc.pyRnscCs|dkrtdn|dkr0tdnt|tdkrTtdnxNtt|jD]7}||j|dkrj|||f|j|<dSqjW|jj|||fdS(Nismarker ID must be > 0smarker position must be >= 0Rsmarker name must be a string(RttypeRR%R?R(RTRpRsRR((s /usr/local/lib/python2.7/aifc.pytsetmarkscCs<x%|jD]}||dkr
|Sq
Wtd|fdS(Nismarker %r does not exist(R?R(RTRpRq((s /usr/local/lib/python2.7/aifc.pyRrscCs t|jdkrdS|jS(Ni(R%R?R<(RT((s /usr/local/lib/python2.7/aifc.pyRoscCs|jS(N(R(RT((s /usr/local/lib/python2.7/aifc.pyRdscCs|jt|t||j|j}|jrH|j|}n|jj||j||_|jt||_dS(N(	t_ensure_header_writtenR%RMRPR>RARRR(RTRRw((s /usr/local/lib/python2.7/aifc.pytwriteframesraws	cCsB|j||j|jks1|j|jkr>|jndS(N(RRRfRRt_patchheader(RTR((s /usr/local/lib/python2.7/aifc.pytwriteframess
cCs|jdkrdSz|jd|jd@rY|jjtd|jd|_n|j|j|jks|j	|jks|j
r|jn|jr|jj
d|_nWdd|_|j}d|_|jXdS(Nii(RAR<RRRR't
_writemarkersRRfRRRRtCloseCompressorR>R^(RTR((s /usr/local/lib/python2.7/aifc.pyR^s&


	
	
			cCs^ddl}|jj|jt|}|jj|jt|}|jj|j|S(Ni(RJRRyRzR%tCOMPRESSED_BUFFER_SIZEtCompressRf(RTRRJR((s /usr/local/lib/python2.7/aifc.pyt
_comp_datascCsddl}|j|dS(Nii(R}tlin2ulaw(RTRR}((s /usr/local/lib/python2.7/aifc.pyt	_lin2ulaw
scCsLddl}t|ds'd|_n|j|d|j\}|_|S(NiRi(R}RR<Rt	lin2adpcm(RTRR}((s /usr/local/lib/python2.7/aifc.pyt
_lin2adpcmscCs|js|jdkrK|js-d|_n|jdkrKtdqKn|jdkr|jsod|_n|jdkrtdqn|jstd	n|jstd
n|jstdn|j|ndS(
NRRRRis9sample width must be 2 when compressing with ULAW or ALAWRs:sample width must be 2 when compressing with G7.22 (ADPCM)s# channels not specifiedssample width not specifiedssampling rate not specified(RRRR(RRjRMRRPROt
_write_header(RTtdatasize((s /usr/local/lib/python2.7/aifc.pyRs$						c
Cs|jdkr|j|_dSyddl}Wn`tk
r|jdkry ddl}|j|_dSWqtk
rqXntdnX|jdkr|j}n$|jdkr|j	}n	td|j
||_|jd	|j
|jd
|j|j|jd|jdg
}|jdkr?|j|d<n(|jd
kr^|j|d<n	td|jj||jjd	d}|j|_dS(NRiRRs$cannot write compressed AIFF-C filesRRsunsupported compression typeiiidiis$cannot compress more than 2 channelsR(RR(RR(RR(RjRR>RJRR}RRRRtOpenCompressorRRKRLRMRNRORzRRPRQRRRSRR(RTRJR}RRXR((s /usr/local/lib/python2.7/aifc.pyt_init_compression-sB


				cCs'|jr%|jdkr%|jn|jjd|jsX||j|j|_n|j|j|j|_|jd@r|jd|_n|jr&|jdkr|jd|_|jd@r#|jd|_q#q&|jd	kr&|jd
d|_|jd@r#|jd|_q#q&ny|jj	|_
Wn ttfk
r^d|_
nX|j|j}|jr|jjd|jjd
t|jdt|j|jn|jjd|jjdt|j|t|j|j|j
dk	r'|jj	|_nt|j|j|jdkr\t|jdnt|j|jdt|j|j|jr|jj|jt|j|jn|jjd|j
dk	r|jj	|_nt|j|jdt|jdt|jddS(NRR4iRRRRiRiiR6R9R5R7iR8i(RRRR(RRRRR(RCRjRRARRfRPRMRRdt_form_length_postAttributeErrortIOErrorR<t_write_form_lengthR#R;R t_nframes_posR1ROR)Rlt_ssnd_length_pos(RTt
initlengtht
commlength((s /usr/local/lib/python2.7/aifc.pyRSs^
	
	


		cCsw|jr<d	t|j}|d@r3|d}nd}nd}d}t|jd||jd|d||S(
Niiiiiiiii(RCR%RlR#RAR(RTt
datalengthRt
verslength((s /usr/local/lib/python2.7/aifc.pyRs	

		"cCs6|jj}|jd@rB|jd}|jjtdn	|j}||jkr|j|jkr|jdkr|jj	|ddS|jj	|j
d|j|}|jj	|jdt
|j|j|jj	|jdt
|j|d|jj	|d|j|_||_dS(Niii(RARdRRR'RRfRRRuRRRR#R(RTtcurposRR((s /usr/local/lib/python2.7/aifc.pyRs&

	cCst|jdkrdS|jjdd}x[|jD]P}|\}}}|t|dd}t|d@dkr9|d}q9q9Wt|j||d|_t|jt|jxP|jD]E}|\}}}t|j|t|j|t|j|qWdS(NiR:iiii(R%R?RARR#RR R)(RTRRqRpRsR((s /usr/local/lib/python2.7/aifc.pyRs"
N(&RRR<RAR_RYRRRRReRRhRRiRRgRRkRmRRnRRrRoRdRRR^RRRRRRRRR(((s /usr/local/lib/python2.7/aifc.pyR sF	
																		
												&	3	
	cCsi|dkr0t|dr'|j}q0d}n|dkrFt|S|dkr\t|StddS(	NtmodeRZtrtwRs$mode must be 'r', 'rb', 'w', or 'wb'(RRZ(RR(R<RRR3RR(RR((s /usr/local/lib/python2.7/aifc.pyRs	

t__main__is/usr/demos/data/audio/bach.aiffRtReadingsnchannels =snframes   =ssampwidth =sframerate =scomptype  =scompname  =itWritingRisDone.(/t__doc__RR]t__all__t	ExceptionRRRRR
RRRRR R!R"R#R)R1RUR2R3RR<RRRtsystargvRtfnRReRgRhRiRkRmtgntgRRnRxRRR^(((s /usr/local/lib/python2.7/aifc.pyt<module>sj					
							!"

	

	

Zerion Mini Shell 1.0