- commit
- eba36ac8db0b4fa49ab7fd11ffa8db44b623d1de
- parent
- 6d076817c7f103929086b253323d92a06e49a3f0
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2021-07-04 09:04
cleanup
Diffstat
| M | player.py | 36 | ++++++++++++++++-------------------- |
1 files changed, 16 insertions, 20 deletions
diff --git a/player.py b/player.py
@@ -29,37 +29,35 @@ def enhance_diagonals(R, weight=0.2, steps=1): 29 29 30 30 def iter_beat_slices(y, beat_frames): 31 31 beat_samples = librosa.frames_to_samples(beat_frames)32 -1 yield 0, beat_samples[0]-1 32 beat_samples = [0, *beat_samples, len(y) - 1] 33 33 for start, end in zip(beat_samples[0:-1], beat_samples[1:]): 34 34 yield start, end35 -1 yield beat_samples[-1], len(y) - 136 35 37 36 38 37 def timbre(y): 39 38 spectrum = numpy.abs(librosa.stft(y)) 40 39 resized = imresize(spectrum, (50, 70))41 -1 l = []-1 40 s = [] 42 41 for pattern in TIMBRE_PATTERNS:43 -1 l.append(numpy.sum(pattern * resized))44 -1 return l-1 42 s.append(numpy.sum(pattern * resized)) -1 43 return s 45 44 46 45 47 46 def analyze(y, sample_rate, beat_frames, bins_per_octave=12, n_octaves=7):48 -1 cqt = librosa.cqt(y=y, sr=sample_rate)49 -1 C = librosa.amplitude_to_db(cqt, ref=numpy.max)50 -1 sync = librosa.util.sync(C, beat_frames)51 -1 R_cqt = librosa.segment.recurrence_matrix(sync, width=4, mode='affinity')-1 47 # cqt = librosa.cqt(y=y, sr=sample_rate) -1 48 # C = librosa.amplitude_to_db(cqt, ref=numpy.max) -1 49 # sync = librosa.util.sync(C, beat_frames) -1 50 # R_cqt = librosa.segment.recurrence_matrix(sync, width=4, mode='affinity') -1 51 # return (R_cqt + R_timbre) / 2 52 52 53 53 tim = numpy.array([54 -1 timbre(y[s:e]) for s, e in iter_beat_slices(y, beat_frames)-1 54 timbre(y[start:end]) for start, end in iter_beat_slices(y, beat_frames) 55 55 ]).T56 -1 R_timbre = librosa.segment.recurrence_matrix(tim, width=4, mode='affinity')57 -158 -1 return (R_cqt + R_timbre) / 2-1 56 return librosa.segment.recurrence_matrix(tim, width=4, mode='affinity') 59 57 60 58 61 59 def load(filename, force=False):62 -1 y, sample_rate = librosa.load(filename, sr=None)-1 60 y, sample_rate = librosa.load(filename) 63 61 64 62 fn_inf = filename + '.inf' 65 63 if not force and os.path.exists(fn_inf): @@ -99,7 +97,6 @@ def normalize(R, threshold): 99 97 x_max = R.max() 100 98 x_min = x_max * threshold 101 99 y_max = (x_max + 0.5) / 2102 -1 # print('mapping {},{} to {},{}'.format(x_min, x_max, 0, y_max))103 100 R_norm = (R - x_min) / (x_max - x_min) * y_max 104 101 105 102 # privilege jumps back in order to prolong playing @@ -118,8 +115,8 @@ def normalize(R, threshold): 118 115 def compute_jumps(R): 119 116 jumps = [] 120 117 for row in R:121 -1 l = [(i, p) for i, p in enumerate(row) if p > 0]122 -1 jumps.append(sorted(l, key=lambda x: -x[1]))-1 118 new_jumps = [(i, p) for i, p in enumerate(row) if p > 0] -1 119 jumps.append(sorted(new_jumps, key=lambda ip: -ip[1])) 123 120 return jumps 124 121 125 122 @@ -138,14 +135,12 @@ def play(buffers, sample_rate, jumps): 138 135 139 136 for j, p in jumps[i]: 140 137 if p > random():141 -1 # print('jump', i, j)142 138 i = j 143 139 break 144 140 145 141 i = i + 1 146 142 147 143 if i >= n:148 -1 # print('reached end')149 144 i = 0 150 145 151 146 @@ -182,7 +177,8 @@ def main(): 182 177 jump_count = sum(len(row) for row in jumps) 183 178 184 179 print('Detected {} jump opportunities on {} beats'.format(185 -1 jump_count, len(buffers)))-1 180 jump_count, len(buffers) -1 181 )) 186 182 187 183 if args.plot: 188 184 plot(R)