<s> import __builtin__ <EOL> import math <EOL> import struct <EOL> from fractions import gcd <EOL> from ctypes import create_string_buffer <EOL> class error ( Exception ) : <EOL> pass <EOL> def _check_size ( size ) : <EOL> if size != <NUM_LIT:1> and size != <NUM_LIT:2> and size != <NUM_LIT:4> : <EOL> raise error ( "<STR_LIT>" ) <EOL> def _check_params ( length , size ) : <EOL> _check_size ( size ) <EOL> if length % size != <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> def _sample_count ( cp , size ) : <EOL> return len ( cp ) / size <EOL> def _get_samples ( cp , size , signed = True ) : <EOL> for i in range ( _sample_count ( cp , size ) ) : <EOL> yield _get_sample ( cp , size , i , signed ) <EOL> def _struct_format ( size , signed ) : <EOL> if size == <NUM_LIT:1> : <EOL> return "<STR_LIT:b>" if signed else "<STR_LIT:B>" <EOL> elif size == <NUM_LIT:2> : <EOL> return "<STR_LIT:h>" if signed else "<STR_LIT:H>" <EOL> elif size == <NUM_LIT:4> : <EOL> return "<STR_LIT:i>" if signed else "<STR_LIT:I>" <EOL> def _get_sample ( cp , size , i , signed = True ) : <EOL> fmt = _struct_format ( size , signed ) <EOL> start = i * size <EOL> end = start + size <EOL> return struct . unpack_from ( fmt , buffer ( cp ) [ start : end ] ) [ <NUM_LIT:0> ] <EOL> def _put_sample ( cp , size , i , val , signed = True ) : <EOL> fmt = _struct_format ( size , signed ) <EOL> struct . pack_into ( fmt , cp , i * size , val ) <EOL> def _get_maxval ( size , signed = True ) : <EOL> if signed and size == <NUM_LIT:1> : <EOL> return <NUM_LIT> <EOL> elif size == <NUM_LIT:1> : <EOL> return <NUM_LIT> <EOL> elif signed and size == <NUM_LIT:2> : <EOL> return <NUM_LIT> <EOL> elif size == <NUM_LIT:2> : <EOL> return <NUM_LIT> <EOL> elif signed and size == <NUM_LIT:4> : <EOL> return <NUM_LIT> <EOL> elif size == <NUM_LIT:4> : <EOL> return <NUM_LIT> <EOL> def _get_minval ( size , signed = True ) : <EOL> if not signed : <EOL> return <NUM_LIT:0> <EOL> elif size == <NUM_LIT:1> : <EOL> return - <NUM_LIT> <EOL> elif size == <NUM_LIT:2> : <EOL> return - <NUM_LIT> <EOL> elif size == <NUM_LIT:4> : <EOL> return - <NUM_LIT> <EOL> def _get_clipfn ( size , signed = True ) : <EOL> maxval = _get_maxval ( size , signed ) <EOL> minval = _get_minval ( size , signed ) <EOL> return lambda val : __builtin__ . max ( min ( val , maxval ) , minval ) <EOL> def _overflow ( val , size , signed = True ) : <EOL> minval = _get_minval ( size , signed ) <EOL> maxval = _get_maxval ( size , signed ) <EOL> if minval <= val <= maxval : <EOL> return val <EOL> bits = size * <NUM_LIT:8> <EOL> if signed : <EOL> offset = <NUM_LIT:2> ** ( bits - <NUM_LIT:1> ) <EOL> return ( ( val + offset ) % ( <NUM_LIT:2> ** bits ) ) - offset <EOL> else : <EOL> return val % ( <NUM_LIT:2> ** bits ) <EOL> def getsample ( cp , size , i ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> if not ( <NUM_LIT:0> <= i < len ( cp ) / size ) : <EOL> raise error ( "<STR_LIT>" ) <EOL> return _get_sample ( cp , size , i ) <EOL> def max ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> if len ( cp ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return __builtin__ . max ( abs ( sample ) for sample in _get_samples ( cp , size ) ) <EOL> def minmax ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> max_sample , min_sample = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for sample in _get_samples ( cp , size ) : <EOL> max_sample = __builtin__ . max ( sample , max_sample ) <EOL> min_sample = __builtin__ . min ( sample , min_sample ) <EOL> return min_sample , max_sample <EOL> def avg ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> if sample_count == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return sum ( _get_samples ( cp , size ) ) / sample_count <EOL> def rms ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> if sample_count == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> sum_squares = sum ( sample ** <NUM_LIT:2> for sample in _get_samples ( cp , size ) ) <EOL> return int ( math . sqrt ( sum_squares / sample_count ) ) <EOL> def _sum2 ( cp1 , cp2 , length ) : <EOL> size = <NUM_LIT:2> <EOL> total = <NUM_LIT:0> <EOL> for i in range ( length ) : <EOL> total += getsample ( cp1 , size , i ) * getsample ( cp2 , size , i ) <EOL> return total <EOL> def findfit ( cp1 , cp2 ) : <EOL> size = <NUM_LIT:2> <EOL> if len ( cp1 ) % <NUM_LIT:2> != <NUM_LIT:0> or len ( cp2 ) % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> if len ( cp1 ) < len ( cp2 ) : <EOL> raise error ( "<STR_LIT>" ) <EOL> len1 = _sample_count ( cp1 , size ) <EOL> len2 = _sample_count ( cp2 , size ) <EOL> sum_ri_2 = _sum2 ( cp2 , cp2 , len2 ) <EOL> sum_aij_2 = _sum2 ( cp1 , cp1 , len2 ) <EOL> sum_aij_ri = _sum2 ( cp1 , cp2 , len2 ) <EOL> result = ( sum_ri_2 * sum_aij_2 - sum_aij_ri * sum_aij_ri ) / sum_aij_2 <EOL> best_result = result <EOL> best_i = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:1> , len1 - len2 + <NUM_LIT:1> ) : <EOL> aj_m1 = _get_sample ( cp1 , size , i - <NUM_LIT:1> ) <EOL> aj_lm1 = _get_sample ( cp1 , size , i + len2 - <NUM_LIT:1> ) <EOL> sum_aij_2 += aj_lm1 ** <NUM_LIT:2> - aj_m1 ** <NUM_LIT:2> <EOL> sum_aij_ri = _sum2 ( buffer ( cp1 ) [ i * size : ] , cp2 , len2 ) <EOL> result = ( sum_ri_2 * sum_aij_2 - sum_aij_ri * sum_aij_ri ) / sum_aij_2 <EOL> if result < best_result : <EOL> best_result = result <EOL> best_i = i <EOL> factor = _sum2 ( buffer ( cp1 ) [ best_i * size : ] , cp2 , len2 ) / sum_ri_2 <EOL> return best_i , factor <EOL> def findfactor ( cp1 , cp2 ) : <EOL> size = <NUM_LIT:2> <EOL> if len ( cp1 ) % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> if len ( cp1 ) != len ( cp2 ) : <EOL> raise error ( "<STR_LIT>" ) <EOL> sample_count = _sample_count ( cp1 , size ) <EOL> sum_ri_2 = _sum2 ( cp2 , cp2 , sample_count ) <EOL> sum_aij_ri = _sum2 ( cp1 , cp2 , sample_count ) <EOL> return sum_aij_ri / sum_ri_2 <EOL> def findmax ( cp , len2 ) : <EOL> size = <NUM_LIT:2> <EOL> sample_count = _sample_count ( cp , size ) <EOL> if len ( cp ) % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> if len2 < <NUM_LIT:0> or sample_count < len2 : <EOL> raise error ( "<STR_LIT>" ) <EOL> if sample_count == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> result = _sum2 ( cp , cp , len2 ) <EOL> best_result = result <EOL> best_i = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:1> , sample_count - len2 + <NUM_LIT:1> ) : <EOL> sample_leaving_window = getsample ( cp , size , i - <NUM_LIT:1> ) <EOL> sample_entering_window = getsample ( cp , size , i + len2 - <NUM_LIT:1> ) <EOL> result -= sample_leaving_window ** <NUM_LIT:2> <EOL> result += sample_entering_window ** <NUM_LIT:2> <EOL> if result > best_result : <EOL> best_result = result <EOL> best_i = i <EOL> return best_i <EOL> def avgpp ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> prevextremevalid = False <EOL> prevextreme = None <EOL> avg = <NUM_LIT:0> <EOL> nextreme = <NUM_LIT:0> <EOL> prevval = getsample ( cp , size , <NUM_LIT:0> ) <EOL> val = getsample ( cp , size , <NUM_LIT:1> ) <EOL> prevdiff = val - prevval <EOL> for i in range ( <NUM_LIT:1> , sample_count ) : <EOL> val = getsample ( cp , size , i ) <EOL> diff = val - prevval <EOL> if diff * prevdiff < <NUM_LIT:0> : <EOL> if prevextremevalid : <EOL> avg += abs ( prevval - prevextreme ) <EOL> nextreme += <NUM_LIT:1> <EOL> prevextremevalid = True <EOL> prevextreme = prevval <EOL> prevval = val <EOL> if diff != <NUM_LIT:0> : <EOL> prevdiff = diff <EOL> if nextreme == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> return avg / nextreme <EOL> def maxpp ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> prevextremevalid = False <EOL> prevextreme = None <EOL> max = <NUM_LIT:0> <EOL> prevval = getsample ( cp , size , <NUM_LIT:0> ) <EOL> val = getsample ( cp , size , <NUM_LIT:1> ) <EOL> prevdiff = val - prevval <EOL> for i in range ( <NUM_LIT:1> , sample_count ) : <EOL> val = getsample ( cp , size , i ) <EOL> diff = val - prevval <EOL> if diff * prevdiff < <NUM_LIT:0> : <EOL> if prevextremevalid : <EOL> extremediff = abs ( prevval - prevextreme ) <EOL> if extremediff > max : <EOL> max = extremediff <EOL> prevextremevalid = True <EOL> prevextreme = prevval <EOL> prevval = val <EOL> if diff != <NUM_LIT:0> : <EOL> prevdiff = diff <EOL> return max <EOL> def cross ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> crossings = <NUM_LIT:0> <EOL> last_sample = <NUM_LIT:0> <EOL> for sample in _get_samples ( cp , size ) : <EOL> if sample <= <NUM_LIT:0> < last_sample or sample >= <NUM_LIT:0> > last_sample : <EOL> crossings += <NUM_LIT:1> <EOL> last_sample = sample <EOL> return crossings <EOL> def mul ( cp , size , factor ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> clip = _get_clipfn ( size ) <EOL> result = create_string_buffer ( len ( cp ) ) <EOL> for i , sample in enumerate ( _get_samples ( cp , size ) ) : <EOL> sample = clip ( int ( sample * factor ) ) <EOL> _put_sample ( result , size , i , sample ) <EOL> return result . raw <EOL> def tomono ( cp , size , fac1 , fac2 ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> clip = _get_clipfn ( size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> result = create_string_buffer ( len ( cp ) / <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:0> , sample_count , <NUM_LIT:2> ) : <EOL> l_sample = getsample ( cp , size , i ) <EOL> r_sample = getsample ( cp , size , i + <NUM_LIT:1> ) <EOL> sample = ( l_sample * fac1 ) + ( r_sample * fac2 ) <EOL> sample = clip ( sample ) <EOL> _put_sample ( result , size , i / <NUM_LIT:2> , sample ) <EOL> return result . raw <EOL> def tostereo ( cp , size , fac1 , fac2 ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> result = create_string_buffer ( len ( cp ) * <NUM_LIT:2> ) <EOL> clip = _get_clipfn ( size ) <EOL> for i in range ( sample_count ) : <EOL> sample = _get_sample ( cp , size , i ) <EOL> l_sample = clip ( sample * fac1 ) <EOL> r_sample = clip ( sample * fac2 ) <EOL> _put_sample ( result , size , i * <NUM_LIT:2> , l_sample ) <EOL> _put_sample ( result , size , i * <NUM_LIT:2> + <NUM_LIT:1> , r_sample ) <EOL> return result . raw <EOL> def add ( cp1 , cp2 , size ) : <EOL> _check_params ( len ( cp1 ) , size ) <EOL> if len ( cp1 ) != len ( cp2 ) : <EOL> raise error ( "<STR_LIT>" ) <EOL> clip = _get_clipfn ( size ) <EOL> sample_count = _sample_count ( cp1 , size ) <EOL> result = create_string_buffer ( len ( cp1 ) ) <EOL> for i in range ( sample_count ) : <EOL> sample1 = getsample ( cp1 , size , i ) <EOL> sample2 = getsample ( cp2 , size , i ) <EOL> sample = clip ( sample1 + sample2 ) <EOL> _put_sample ( result , size , i , sample ) <EOL> return result . raw <EOL> def bias ( cp , size , bias ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> result = create_string_buffer ( len ( cp ) ) <EOL> for i , sample in enumerate ( _get_samples ( cp , size ) ) : <EOL> sample = _overflow ( sample + bias , size ) <EOL> _put_sample ( result , size , i , sample ) <EOL> return result . raw <EOL> def reverse ( cp , size ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> sample_count = _sample_count ( cp , size ) <EOL> result = create_string_buffer ( len ( cp ) ) <EOL> for i , sample in enumerate ( _get_samples ( cp , size ) ) : <EOL> _put_sample ( result , size , sample_count - i - <NUM_LIT:1> , sample ) <EOL> return result . raw <EOL> def lin2lin ( cp , size , size2 ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> _check_size ( size2 ) <EOL> if size == size2 : <EOL> return cp <EOL> new_len = ( len ( cp ) / size ) * size2 <EOL> result = create_string_buffer ( new_len ) <EOL> for i in range ( _sample_count ( cp , size ) ) : <EOL> sample = _get_sample ( cp , size , i ) <EOL> if size < size2 : <EOL> sample = sample << ( <NUM_LIT:4> * size2 / size ) <EOL> elif size > size2 : <EOL> sample = sample >> ( <NUM_LIT:4> * size / size2 ) <EOL> sample = _overflow ( sample , size2 ) <EOL> _put_sample ( result , size2 , i , sample ) <EOL> return result . raw <EOL> def ratecv ( cp , size , nchannels , inrate , outrate , state , weightA = <NUM_LIT:1> , weightB = <NUM_LIT:0> ) : <EOL> _check_params ( len ( cp ) , size ) <EOL> if nchannels < <NUM_LIT:1> : <EOL> raise error ( "<STR_LIT>" ) <EOL> bytes_per_frame = size * nchannels <EOL> frame_count = len ( cp ) / bytes_per_frame <EOL> if bytes_per_frame / nchannels != size : <EOL> raise OverflowError ( "<STR_LIT>" ) <EOL> if weightA < <NUM_LIT:1> or weightB < <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> if len ( cp ) % bytes_per_frame != <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> if inrate <= <NUM_LIT:0> or outrate <= <NUM_LIT:0> : <EOL> raise error ( "<STR_LIT>" ) <EOL> d = gcd ( inrate , outrate ) <EOL> inrate /= d <EOL> outrate /= d <EOL> prev_i = [ <NUM_LIT:0> ] * nchannels <EOL> cur_i = [ <NUM_LIT:0> ] * nchannels <EOL> if state is None : <EOL> d = - outrate <EOL> else : <EOL> d , samps = state <EOL> if len ( samps ) != nchannels : <EOL> raise error ( "<STR_LIT>" ) <EOL> prev_i , cur_i = zip ( * samps ) <EOL> prev_i , cur_i = list ( prev_i ) , list ( cur_i ) <EOL> q = frame_count / inrate <EOL> ceiling = ( q + <NUM_LIT:1> ) * outrate <EOL> nbytes = ceiling * bytes_per_frame <EOL> result = create_string_buffer ( nbytes ) <EOL> samples = _get_samples ( cp , size ) <EOL> out_i = <NUM_LIT:0> <EOL> while True : <EOL> while d < <NUM_LIT:0> : <EOL> if frame_count == <NUM_LIT:0> : <EOL> samps = zip ( prev_i , cur_i ) <EOL> retval = result . raw <EOL> trim_index = ( out_i * bytes_per_frame ) - len ( retval ) <EOL> retval = buffer ( retval ) [ : trim_index ] <EOL> return ( retval , ( d , tuple ( samps ) ) ) <EOL> for chan in range ( nchannels ) : <EOL> prev_i [ chan ] = cur_i [ chan ] <EOL> cur_i [ chan ] = samples . next ( ) <EOL> cur_i [ chan ] = ( <EOL> ( weightA * cur_i [ chan ] + weightB * prev_i [ chan ] ) <EOL> / ( weightA + weightB ) <EOL> ) <EOL> frame_count -= <NUM_LIT:1> <EOL> d += outrate <EOL> while d >= <NUM_LIT:0> : <EOL> for chan in range ( nchannels ) : <EOL> cur_o = ( <EOL> ( prev_i [ chan ] * d + cur_i [ chan ] * ( outrate - d ) ) <EOL> / outrate <EOL> ) <EOL> _put_sample ( result , size , out_i , _overflow ( cur_o , size ) ) <EOL> out_i += <NUM_LIT:1> <EOL> d -= inrate <EOL> def lin2ulaw ( cp , size ) : <EOL> raise NotImplementedError ( ) <EOL> def ulaw2lin ( cp , size ) : <EOL> raise NotImplementedError ( ) <EOL> def lin2alaw ( cp , size ) : <EOL> raise NotImplementedError ( ) <EOL> def alaw2lin ( cp , size ) : <EOL> raise NotImplementedError ( ) <EOL> def lin2adpcm ( cp , size , state ) : <EOL> raise NotImplementedError ( ) <EOL> def adpcm2lin ( cp , size , state ) : <EOL> raise NotImplementedError ( ) </s>
