- commit
- 491175d30d3f24c28558ae9c7f1448d2c07e66e6
- parent
- 60ddec6b064493c89aa7d6ee859b322837c24a40
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2021-07-04 09:49
rename R to jumps
Diffstat
| M | player.py | 46 | +++++++++++++++++++++++----------------------- |
1 files changed, 23 insertions, 23 deletions
diff --git a/player.py b/player.py
@@ -17,13 +17,13 @@ with open(os.path.join(BASE_DIR, 'timbre.pickle'), 'rb') as fh: 17 17 TIMBRE_PATTERNS = pickle.load(fh) 18 18 19 1920 -1 def enhance_diagonals(R, weight=0.2, steps=1):-1 20 def enhance_diagonals(jumps, weight=0.2, steps=1): 21 21 for i in range(steps): 22 22 # combine each cell with its diagonal neighbors23 -1 R1 = numpy.roll(R, (1, 1), (0, 1))24 -1 R2 = numpy.roll(R, (-1, -1), (0, 1))25 -1 R = (weight * (R1 + R2) + (1 - weight) * R) / 226 -1 return R-1 23 jumps1 = numpy.roll(jumps, (1, 1), (0, 1)) -1 24 jumps2 = numpy.roll(jumps, (-1, -1), (0, 1)) -1 25 jumps = (weight * (jumps1 + jumps2) + (1 - weight) * jumps) / 2 -1 26 return jumps 27 27 28 28 29 29 def iter_beat_slices(y, beat_frames): @@ -68,16 +68,16 @@ def load(filename, force=False): 68 68 fn_inf = filename + '.inf' 69 69 if not force and os.path.exists(fn_inf): 70 70 with gzip.open(fn_inf, 'rb') as fh:71 -1 beat_frames, R = pickle.load(fh)-1 71 beat_frames, jumps = pickle.load(fh) 72 72 else: 73 73 print('Analyzing…') 74 74 tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sample_rate)75 -1 R = analyze(y, sample_rate, beat_frames)-1 75 jumps = analyze(y, sample_rate, beat_frames) 76 76 77 77 with gzip.open(fn_inf, 'wb') as fh:78 -1 pickle.dump((beat_frames, R), fh)-1 78 pickle.dump((beat_frames, jumps), fh) 79 7980 -1 return y, sample_rate, beat_frames, R-1 80 return y, sample_rate, beat_frames, jumps 81 81 82 82 83 83 def compute_buffers(y, beat_frames): @@ -91,32 +91,32 @@ def compute_buffers(y, beat_frames): 91 91 return buffers 92 92 93 9394 -1 def normalize(R, threshold):95 -1 n = len(R)-1 94 def normalize(jumps, threshold): -1 95 n = len(jumps) 96 9697 -1 R = enhance_diagonals(R, 0.8, 4)-1 97 jumps = enhance_diagonals(jumps, 0.8, 4) 98 98 99 99 # scale100 -1 x_max = R.max()-1 100 x_max = jumps.max() 101 101 x_min = x_max * threshold 102 102 y_max = (x_max + 0.5) / 2103 -1 R_norm = (R - x_min) / (x_max - x_min) * y_max-1 103 jumps_norm = (jumps - x_min) / (x_max - x_min) * y_max 104 104 105 105 # privilege jumps back in order to prolong playing106 -1 R *= numpy.ones((n, n)) * 0.9 + numpy.tri(n, k=-1) * 0.1-1 106 jumps *= numpy.ones((n, n)) * 0.9 + numpy.tri(n, k=-1) * 0.1 107 107 108 108 # privilege wide jumps 109 109 M = numpy.zeros((n, n)) 110 110 for i in range(1, n): 111 111 M += numpy.tri(n, k=-i) 112 112 M += numpy.tri(n, k=-i).T113 -1 R *= (M / (n - 1)) ** 0.1-1 113 jumps *= (M / (n - 1)) ** 0.1 114 114115 -1 return R_norm * (R_norm > 0)-1 115 return jumps_norm * (jumps_norm > 0) 116 116 117 117118 -1 def get_next_position(i, R):119 -1 for j, p in sorted(enumerate(R[i]), key=lambda jp: -jp[1]):-1 118 def get_next_position(i, jumps): -1 119 for j, p in sorted(enumerate(jumps[i]), key=lambda jp: -jp[1]): 120 120 if p > random(): 121 121 return j + 1 122 122 return i + 1 @@ -151,17 +151,17 @@ def main(): 151 151 args = parse_args() 152 152 153 153 print('Loading', args.filename)154 -1 y, sample_rate, beat_frames, R = load(args.filename, args.force)155 -1 R = normalize(R, args.threshold)-1 154 y, sample_rate, beat_frames, jumps = load(args.filename, args.force) -1 155 jumps = normalize(jumps, args.threshold) 156 156 buffers = compute_buffers(y, beat_frames)157 -1 jump_count = sum(sum(R > 0))-1 157 jump_count = sum(sum(jumps > 0)) 158 158 159 159 print('Detected {} jump opportunities on {} beats'.format( 160 160 jump_count, len(buffers) 161 161 )) 162 162 163 163 print('Playing… (Press Ctrl-C to stop)')164 -1 play(buffers, sample_rate, R)-1 164 play(buffers, sample_rate, jumps) 165 165 166 166 167 167 if __name__ == '__main__':