def lin2lin(cp, size, size2): _check_params(len(cp), size) _check_size(size2) if size == size2: return cp new_len = (len(cp) // size) * size2 rv = ffi.new("unsigned char[]", new_len) result = ffi.buffer(rv) for i in range(_sample_count(cp, size)): sample = _get_sample(cp, size, i) if size == 1: sample <<= 24 elif size == 2: sample <<= 16 if size2 == 1: sample >>= 24 elif size2 == 2: sample >>= 16 sample = _overflow(sample, size2) _put_sample(result, size2, i, sample) return result[:]
def lin2adpcm(cp, size, state): _check_params(len(cp), size) state = _check_state(state) rv = ffi.new("unsigned char[]", len(cp) // size // 2) state_ptr = ffi.new("int[]", state) lib.lin2adcpm(rv, cp, len(cp), size, state_ptr) return ffi.buffer(rv)[:], tuple(state_ptr)
def adpcm2lin(cp, size, state): _check_size(size) state = _check_state(state) rv = ffi.new("unsigned char[]", len(cp) * size * 2) state_ptr = ffi.new("int[]", state) lib.adcpm2lin(rv, cp, len(cp), size, state_ptr) return ffi.buffer(rv)[:], tuple(state_ptr)
def alaw2lin(cp, size): _check_size(size) rv = ffi.new("unsigned char[]", len(cp) * size) result = ffi.buffer(rv) for i, value in enumerate(cp): sample = lib.st_alaw2linear16(ord(value)) _put_lin_sample(result, size, i, sample) return result[:]
def tostereo(cp, size, fac1, fac2): _check_params(len(cp), size) sample_count = _sample_count(cp, size) rv = ffi.new("char[]", len(cp) * 2) lib.tostereo(rv, cp, len(cp), size, fac1, fac2) return ffi.buffer(rv)[:]
def tostereo(cp, size, fac1, fac2): _check_params(len(cp), size) sample_count = _sample_count(cp, size) rv = ffi.new("unsigned char[]", len(cp) * 2) lib.tostereo(rv, cp, len(cp), size, fac1, fac2) return ffi.buffer(rv)[:]
def add(cp1, cp2, size): _check_params(len(cp1), size) if len(cp1) != len(cp2): raise error("Lengths should be the same") rv = ffi.new("char[]", len(cp1)) lib.add(rv, cp1, cp2, len(cp1), size) return ffi.buffer(rv)[:]
def add(cp1, cp2, size): _check_params(len(cp1), size) if len(cp1) != len(cp2): raise error("Lengths should be the same") rv = ffi.new("unsigned char[]", len(cp1)) lib.add(rv, cp1, cp2, len(cp1), size) return ffi.buffer(rv)[:]
def ratecv(cp, size, nchannels, inrate, outrate, state, weightA=1, weightB=0): _check_params(len(cp), size) if nchannels < 1: raise error("# of channels should be >= 1") bytes_per_frame = size * nchannels frame_count = len(cp) // bytes_per_frame if bytes_per_frame // nchannels != size: raise OverflowError("width * nchannels too big for a C int") if weightA < 1 or weightB < 0: raise error("weightA should be >= 1, weightB should be >= 0") if len(cp) % bytes_per_frame != 0: raise error("not a whole number of frames") if inrate <= 0 or outrate <= 0: raise error("sampling rate not > 0") d = gcd(inrate, outrate) inrate //= d outrate //= d d = gcd(weightA, weightB) weightA //= d weightB //= d if state is None: d = -outrate prev_i = ffi.new('int[]', nchannels) cur_i = ffi.new('int[]', nchannels) else: d, samps = state if len(samps) != nchannels: raise error("illegal state argument") prev_i, cur_i = zip(*samps) prev_i = ffi.new('int[]', prev_i) cur_i = ffi.new('int[]', cur_i) state_d = ffi.new('int[]', (d,)) q = frame_count // inrate ceiling = (q + 1) * outrate nbytes = ceiling * bytes_per_frame rv = ffi.new("char[]", nbytes) cpbuf = ffi.from_buffer(cp) trim_index = lib.ratecv(rv, cpbuf, frame_count, size, nchannels, inrate, outrate, state_d, prev_i, cur_i, weightA, weightB) result = ffi.buffer(rv)[:trim_index] d = state_d[0] samps = zip(prev_i, cur_i) return (result, (d, tuple(samps)))
def reverse(cp, size): _check_params(len(cp), size) sample_count = _sample_count(cp, size) rv = ffi.new("unsigned char[]", len(cp)) result = ffi.buffer(rv) for i, sample in enumerate(_get_samples(cp, size)): _put_sample(result, size, sample_count - i - 1, sample) return result[:]
def ratecv(cp, size, nchannels, inrate, outrate, state, weightA=1, weightB=0): _check_params(len(cp), size) if nchannels < 1: raise error("# of channels should be >= 1") bytes_per_frame = size * nchannels frame_count = len(cp) // bytes_per_frame if bytes_per_frame // nchannels != size: raise OverflowError("width * nchannels too big for a C int") if weightA < 1 or weightB < 0: raise error("weightA should be >= 1, weightB should be >= 0") if len(cp) % bytes_per_frame != 0: raise error("not a whole number of frames") if inrate <= 0 or outrate <= 0: raise error("sampling rate not > 0") d = gcd(inrate, outrate) inrate //= d outrate //= d d = gcd(weightA, weightB) weightA //= d weightB //= d if state is None: d = -outrate prev_i = ffi.new('int[]', nchannels) cur_i = ffi.new('int[]', nchannels) else: d, samps = state if len(samps) != nchannels: raise error("illegal state argument") prev_i, cur_i = zip(*samps) prev_i = ffi.new('int[]', prev_i) cur_i = ffi.new('int[]', cur_i) state_d = ffi.new('int[]', (d,)) q = frame_count // inrate ceiling = (q + 1) * outrate nbytes = ceiling * bytes_per_frame rv = ffi.new("unsigned char[]", nbytes) trim_index = lib.ratecv(rv, cp, frame_count, size, nchannels, inrate, outrate, state_d, prev_i, cur_i, weightA, weightB) result = ffi.buffer(rv)[:trim_index] d = state_d[0] samps = zip(prev_i, cur_i) return (result, (d, tuple(samps)))
def bias(cp, size, bias): _check_params(len(cp), size) rv = ffi.new("unsigned char[]", len(cp)) result = ffi.buffer(rv) for i, sample in enumerate(_get_samples(cp, size)): sample = _overflow(sample + bias, size) _put_sample(result, size, i, sample) return result[:]
def mul(cp, size, factor): _check_params(len(cp), size) clip = _get_clipfn(size) rv = ffi.new("unsigned char[]", len(cp)) result = ffi.buffer(rv) for i, sample in enumerate(_get_samples(cp, size)): sample = clip(int(sample * factor)) _put_sample(result, size, i, sample) return result[:]
def byteswap(cp, size): if len(cp) % size != 0: raise error("not a whole number of frames") sample_count = _sample_count(cp, size) rv = ffi.new("unsigned char[]", len(cp)) base = size next_bump = 0 bump = 2 * size for i in range(len(cp)): base -= 1 rv[i] = cp[base] if base == next_bump: base += bump next_bump += size return ffi.buffer(rv)[:]
def tomono(cp, size, fac1, fac2): _check_params(len(cp), size) clip = _get_clipfn(size) sample_count = _sample_count(cp, size) rv = ffi.new("unsigned char[]", len(cp) // 2) result = ffi.buffer(rv) for i in range(0, sample_count, 2): l_sample = getsample(cp, size, i) r_sample = getsample(cp, size, i + 1) sample = (l_sample * fac1) + (r_sample * fac2) sample = int(clip(sample)) _put_sample(result, size, i // 2, sample) return result[:]
def lin2alaw(cp, size): _check_params(len(cp), size) rv = ffi.new("unsigned char[]", _sample_count(cp, size)) for i, sample in enumerate(_get_lin_samples(cp, size)): rv[i] = lib.st_linear2alaw(sample) return ffi.buffer(rv)[:]