From bdacf2c58d995686632f557e3187d0b2f84bf276 Mon Sep 17 00:00:00 2001 From: Andreas Jansson Date: Mon, 27 Aug 2012 09:55:56 +0100 Subject: [PATCH] working dreiklangs --- inspect | 4 ++-- keydetection/audio.py | 2 +- keydetection/chroma.py | 10 +++++----- keydetection/nklang.py | 16 ++++++++-------- keydetection/profile.py | 15 ++++++++++----- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/inspect b/inspect index fc2d2c7..b35e212 100755 --- a/inspect +++ b/inspect @@ -6,9 +6,9 @@ import pickle from keydetection import * def summarise(model, limit = None): - print 'C major matrix:' + print 'C major profile:' model[0].print_summary(limit) - print '\nC minor matrix:' + print '\nC minor profile:' model[12].print_summary(limit) if __name__ == '__main__': diff --git a/keydetection/audio.py b/keydetection/audio.py index 64ca4ca..a80955b 100644 --- a/keydetection/audio.py +++ b/keydetection/audio.py @@ -220,7 +220,7 @@ def get_klangs(mp3 = None, audio = None, time_limit = None, n = 2): tuner = Tuner(bins, global_tuning = True) ts = tuner.tune(cs) - logging.debug('Returning klags') + logging.debug('Returning klangs') klangs = [(i * winlength / float(fs), t.get_nklang(n = n)) for i, t in enumerate(ts)] return klangs diff --git a/keydetection/chroma.py b/keydetection/chroma.py index e698949..53d8ba2 100644 --- a/keydetection/chroma.py +++ b/keydetection/chroma.py @@ -88,13 +88,13 @@ def get_nklang(self, threshold = .1, silent = 100, n = 2, filter_adjacent = True if filter_adjacent: note_amps.sort(key = operator.itemgetter(1)) all_amps = [0] * 12 - for n, a in note_amps: - all_amps[n] = a + for note, a in note_amps: + all_amps[note] = a notes = [] - for n, a in note_amps: - if all_amps[(n - 1) % 12] < a and all_amps[(n + 1) % 12] < a: - notes.append(n) + for note, a in note_amps: + if all_amps[(note - 1) % 12] < a and all_amps[(note + 1) % 12] < a: + notes.append(note) else: notes = map(operator.itemgetter(0), note_amps) diff --git a/keydetection/nklang.py b/keydetection/nklang.py index 78023f6..786c303 100644 --- a/keydetection/nklang.py +++ b/keydetection/nklang.py @@ -66,17 +66,17 @@ def __repr__(self): -def klang_number_to_name(number): +def klang_number_to_name(number, n): ''' - Helper that returns the name of a Zweiklang. + Helper that returns the name of an nklang. ''' if number == -1: return 'Silence' else: - first = number % 12 - second = int(math.floor(number / 12)) - if first == second: - return note_names[first] - else: - return note_names[first] + ', ' + note_names[second] + names = [] + for i in range(n): + names.append('%-2s' % note_names[number % 12]) + number /= 12 + + return ', '.join(names) diff --git a/keydetection/profile.py b/keydetection/profile.py index 3da8a1c..770304a 100644 --- a/keydetection/profile.py +++ b/keydetection/profile.py @@ -20,6 +20,7 @@ def __init__(self, length = None): def from_values(values): profile = Profile() profile.values = values + print len(values) profile.length = len(values) return profile @@ -52,19 +53,23 @@ def normalise(self): if sum > 0: self.values /= sum + def get_n(self): + return int(math.log(len(self.values), 12)) + def __repr__(self): return '' % (self.length, np.sum(self.values)) def print_summary(self, max_lines = 20): seq = copy(self.values) seq.sort() - seq = np.unique(values)[::-1] + seq = np.unique(seq)[::-1] i = 0 lines = 0 + n = self.get_n() while(seq[i] > 0 and i < len(seq)): where = np.where(seq[i] == self.values)[0] for index in where: - print '%-6s: %.3f' % (klang_number_to_name(index), seq[i]) + print '%-6s: %.3f' % (klang_number_to_name(index, n), seq[i]) lines += 1 if lines > max_lines: return @@ -91,7 +96,7 @@ def get_trained_model(filenames, n = 2): profiles_list = [] for mp3, keylab_file in filenames: logging.info('Analysing %s' % mp3) - profiles = get_training_profiles(mp3, keylab_file) + profiles = get_training_profiles(mp3, keylab_file, n) if profiles: logging.debug('Appending profiles') profiles_list.append(profiles) @@ -119,7 +124,7 @@ def get_training_profiles(mp3, keylab_file, n = 2): try: logging.debug('About to get klangs') - klangs = get_klangs(mp3) + klangs = get_klangs(mp3, n = n) except Exception, e: logging.warning('Failed to analyse %s: %s' % (mp3, e)) return None @@ -159,7 +164,7 @@ def get_test_profile(mp3, time_limit = None, n = 2): if cache.exists(): return cache.get() - klangs = get_klangs(mp3, time_limit = time_limit) + klangs = get_klangs(mp3, time_limit = time_limit, n = n) profile = Profile(12 ** n) for t, klang in klangs: if klang is not None and \