Skip to content

Commit

Permalink
working dreiklangs
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjansson committed Aug 27, 2012
1 parent 69d0fc1 commit bdacf2c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions inspect
Expand Up @@ -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__':
Expand Down
2 changes: 1 addition & 1 deletion keydetection/audio.py
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions keydetection/chroma.py
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions keydetection/nklang.py
Expand Up @@ -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)
15 changes: 10 additions & 5 deletions keydetection/profile.py
Expand Up @@ -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

Expand Down Expand Up @@ -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 '<Profile length %s, sum %f>' % (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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down

0 comments on commit bdacf2c

Please sign in to comment.