1 2import numpy as np 3 4 5def find_audio_period(aclip, t_min=.1, t_max=2, t_res=.01): 6 """ Finds the period, in seconds of an audioclip. 7 8 The beat is then given by bpm = 60/T 9 10 t_min and _tmax are bounds for the returned value, t_res 11 is the numerical precision 12 """ 13 chunksize = int(t_res*aclip.fps) 14 chunk_duration = 1.0*chunksize/aclip.fps 15 # v denotes the list of volumes 16 v = np.array([(c**2).sum() for c in 17 aclip.iter_chunks(chunksize)]) 18 v = v-v.mean() 19 corrs = np.correlate(v, v, mode = 'full')[-len(v):] 20 corrs[:int(t_min/chunk_duration)]=0 21 corrs[int(t_max/chunk_duration):]=0 22 return chunk_duration*np.argmax(corrs) 23