- commit
- d3cca0e532f1e3f1c08534b6676af95fc0caf3bb
- parent
- 5a8e296961ac8fded52a8b02073971d36b8ab783
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2017-10-20 18:01
timbre inspired by echonest
Diffstat
| M | player.py | 24 | +++++++++++++++++++++++- |
| A | timbre.pickle | 0 |
2 files changed, 23 insertions, 1 deletions
diff --git a/player.py b/player.py
@@ -5,10 +5,16 @@ import os 5 5 import pickle 6 6 import struct 7 7 -1 8 from scipy.misc import imresize 8 9 import alsaaudio 9 10 import librosa 10 11 import numpy 11 12 -1 13 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) -1 14 -1 15 with open(os.path.join(BASE_DIR, 'timbre.pickle'), 'rb') as fh: -1 16 TIMBRE_PATTERNS = pickle.load(fh) -1 17 12 18 13 19 def enhance_diagonals(R, weight=0.2, steps=1): 14 20 for i in range(steps): @@ -27,11 +33,27 @@ def iter_beat_slices(y, beat_frames): 27 33 yield beat_samples[-1], len(y) - 1 28 34 29 35 -1 36 def timbre(y): -1 37 spectrum = numpy.abs(librosa.stft(y)) -1 38 resized = imresize(spectrum, (50, 70)) -1 39 l = [] -1 40 for pattern in TIMBRE_PATTERNS: -1 41 l.append(numpy.sum(pattern * resized)) -1 42 return l -1 43 -1 44 30 45 def analyze(y, sample_rate, beat_frames, bins_per_octave=12, n_octaves=7): 31 46 cqt = librosa.cqt(y=y, sr=sample_rate) 32 47 C = librosa.amplitude_to_db(cqt, ref=numpy.max) 33 48 sync = librosa.util.sync(C, beat_frames)34 -1 return librosa.segment.recurrence_matrix(sync, width=4, mode='affinity')-1 49 R_cqt = librosa.segment.recurrence_matrix(sync, width=4, mode='affinity') -1 50 -1 51 tim = numpy.array([ -1 52 timbre(y[s:e]) for s, e in iter_beat_slices(y, beat_frames) -1 53 ]).T -1 54 R_timbre = librosa.segment.recurrence_matrix(tim, width=4, mode='affinity') -1 55 -1 56 return (R_cqt + R_timbre) / 2 35 57 36 58 37 59 def load(filename, force=False):