This commit is contained in:
gauthiier 2024-04-24 09:23:50 +02:00
parent 684b393fd3
commit 2e32c5e6bb

View File

@ -18,7 +18,7 @@ STRUCT_FORMAT = {
4: ['I', 'i'] 4: ['I', 'i']
} }
class error(Excpetion) class error(Exception):
''' '''
This exception is raised on all errors, such as unknown number of bytes per sample, etc. This exception is raised on all errors, such as unknown number of bytes per sample, etc.
''' '''
@ -43,10 +43,10 @@ def _get_raw_sample(cp, size: int, i: int, signed=True):
struct_format = STRUCT_FORMAT[size][signed] struct_format = STRUCT_FORMAT[size][signed]
start = i * size start = i * size
end = start + size end = start + size
return struct.unpack_from(struct_format, buffer(fragment)[start:end])[0] return struct.unpack_from(struct_format, memoryview(cp)[start:end])[0]
def _get_samples(cp, size: int, signed=True): def _get_samples(cp, size: int, signed=True):
for i in range(len(cp) / size): for i in range(math.ceil(len(cp) / size)):
yield _get_raw_sample(cp, size, i, signed) yield _get_raw_sample(cp, size, i, signed)
def _set_raw_sample(cp, size: int, i: int, val, signed=True): def _set_raw_sample(cp, size: int, i: int, val, signed=True):
@ -64,7 +64,7 @@ def _overflow(val, size, signed=True):
else: else:
return val % 2**bits return val % 2**bits
def getsample(fragment, size: int, index: int): def getsample(cp, size: int, index: int):
''' '''
Return the value of sample index from the fragment. Return the value of sample index from the fragment.
''' '''
@ -89,7 +89,7 @@ def minmax(fragment, size):
_check_parameters(len(fragment), size) _check_parameters(len(fragment), size)
sample_min = 0x7FFFFFFF sample_min = 0x7FFFFFFF
sample_max = -0x80000000 sample_max = -0x80000000
for sample in _get_samples(fragment, size) for sample in _get_samples(fragment, size):
sample_min = builtin_min(sample, sample_min) sample_min = builtin_min(sample, sample_min)
sample_max = builtin_min(sample, sample_max) sample_max = builtin_min(sample, sample_max)
return sample_min, sample_max return sample_min, sample_max
@ -155,7 +155,7 @@ def findfit(fragment, reference):
# sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1 # sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1
sum_aij_2 += aj_lm1**2 - aj_m1**2 sum_aij_2 += aj_lm1**2 - aj_m1**2
sum_aij_ri = _sum2(buffer(cp1)[j*2:], cp2, len2) sum_aij_ri = _sum2(memoryview(cp1)[j*2:], cp2, len2)
# result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2 # result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2
result = (sum_ri_2*sum_aij_2 - sum_aij_ri**2) / sum_aij_2 result = (sum_ri_2*sum_aij_2 - sum_aij_ri**2) / sum_aij_2
@ -164,7 +164,7 @@ def findfit(fragment, reference):
best_result = result best_result = result
best_j = j best_j = j
factor = _sum2(buffer(cp1)[best_j*2:], cp2, len2) / sum_ri_2 factor = _sum2(memoryview(cp1)[best_j*2:], cp2, len2) / sum_ri_2
return best_j, factor return best_j, factor
def findfactor(fragment, reference): def findfactor(fragment, reference):
@ -508,12 +508,12 @@ def ratecv(fragment, size, nchannels, inrate, outrate, state, weightA=1, weightB
ncp = 0 ncp = 0
while True: while True:
while d < 0; while d < 0:
if nbr_frames == 0: if nbr_frames == 0:
samps = zip(prev_i, cur_i) samps = zip(prev_i, cur_i)
rv = cp.raw rv = cp.raw
trim_i = (ncp * bytes_per_frame) - len(rv) trim_i = (ncp * bytes_per_frame) - len(rv)
rv = buffer(rv)[:trim_i] rv = memoryview(rv)[:trim_i]
return (rv, (d, tuple(samps))) return (rv, (d, tuple(samps)))
for chan in range(nchannels): for chan in range(nchannels):