def test(f_discretization=10, e_discretization=30, indep=False, selection=None, subset=None, corpus=None, smoothing=None, sensitivity=5, segmentation='reasonable'):
    if not selection:
        selection = (db.select())
    score = db.getScore1(selection)
    if not corpus:
        (f, e, m) = tools.chooseFeatures()
    else:
        (f, e, m) = tools.loadFeatures(corpus)
    if m['version'] != sf.version:
        print("Scorefeatures versions don't match! Corpus version: {0} Scorefeatures version: {1}".format(m['version'], sf.version))
        exit(0)
    if not subset:
        # Select a subset by hand
        subset = selectSubset(m['featureset'])
    print('\n\tPerforming {0}'.format(selection))
    print('\tFeatures version: {0}'.format(m['version']))
    print('\tFeatureset used: [', end=' ')
    print(', '.join([m['featureset'][i] for i in subset]), end=' ')
    print(']')
    print('\tScorefeatures discretization: {0}\n\tExpression discretization: {1}'.format(f_discretization, e_discretization))
    print('\tSensitivity: {0}'.format(sensitivity))
    print('\tSmoothing: {0}'.format(smoothing))
    print('\tCorpus: {0}\n'.format(corpus))
    if indep:
        hmm = HMM_indep(2, smoothing)
    else:
        hmm = HMM(2, smoothing)

    trainHMM(hmm, f, e, f_discretization, e_discretization, subset=subset, ignore=selection, sensitivity=sensitivity)
    #trainHMM(hmm, f, e, f_discretization, e_discretization, subset=subset)
    hmm.storeInfo('hmm2.txt')
    print("Loading score")
    melodyscore = Score(score).melody()
    melody = tools.parseScore(melodyscore)
    # Segmentate the the score
    print("Analysing score")
    if segmentation == 'reasonable':
        print('Using reasonable segmentation')
        onset = structure.reasonableSegmentation(melody)
    elif segmentation == 'new':
        print('Using new segmentation')
        onset = structure.newSegmentation(melody)
    elif segmentation == 'notelevel':
        print('Using notelevel segmentation')
        onset = structure.noteLevel(melody)

    #onset = structure.groupings(structure.list_to_tree(structure.first_order_tree(structure.onset, melody, 0.1)), 1)
    #namelist = []
    #for group in onset:
    #  namelist.append([leaf.name() for leaf in group])
    #print namelist

    (p, expression) = render(melodyscore, onset, hmm, f_discretization, e_discretization, subset, sensitivity=sensitivity)

    print("Done, resulting expression(with a probability of {1}): {0}".format(expression, p))

    performance = perform.perform(score, melodyscore, onset, expression, converter=melody)
    playPerformance(selection, performance, expression, onset, '{0}_{1}_on_{2}_discret_{3}-{4}_smoothing_{5}'.format(\
        selection[0], selection[1], corpus, f_discretization, e_discretization, smoothing))
def loadperformance():
    (selection, expression) = tools.loadPerformance()
    score = db.getScore1(selection)
    melodyscore = Score(score).melody()
    melody = tools.parseScore(melodyscore)
    segmentation = structure.reasonableSegmentation(melody)
    performance = perform.perform(score, Score(score).melody(), segmentation, expression, converter=melody)
    seq = Sequencer()
    seq.play(performance)
def plotCorpus():
    (f, e, m) = tools.chooseFeatures()
    d = input('Discretization? ')
    if not d == '':
        s = input('Sensitivity? [5]')
        if s == '':
            s = 5.0
        d = float(d)
        s = float(s)
    else: d = None

    for work in f:
        print('Plotting and saving {0}'.format(work))
        expression = e[work]
        if d:
            expression = [undiscretize_expression(discretize_expression(x, d, sensitivity=s), d, sensitivity=s) for x in expression]
        score = db.getScore1(work)
        segmentation = structure.reasonableSegmentation(tools.parseScore(Score(score).melody()))
        visualize(segmentation, expression, [0, 1, 2], '{0}-{1}-{2}'.format(work[0], work[1], work[2]), '{0}-{1} by {2}'.format(work[0], work[1], work[2]))
Ejemplo n.º 4
0
def train(trainset):

    expression = {}
    features = {}
    const = 0
    Max = 0
    Min = None
    count = 0
    print(">>> Loading scores and deviations, this will take hours and may eat all you memory")

    for query in trainset:
        print(">>> Loading: {0}".format(query))
        score = db.getScore1(query)
        deviations = db.getDeviation1(query)
        alignment = Alignment(score, deviations)
        melody = alignment.melody()
        #segments = structure.newSegmentation(tools.parseScore(melody))
        segments = structure.noteLevel(tools.parseScore(melody))
        const += len(segments)
        lengths = sum([len(s) for s in segments])
        m = max([len(s) for s in segments])
        mi = min([len(s) for s in segments])
        if m > Max:
            Max = m
        if not Min:
            Min = mi
        if mi < Min:
            Min = mi
        print('>>> Extracting features')
        expression[query] = performancefeatures.vanDerWeijExpression(alignment, segments)
        features[query] = scorefeatures.vanDerWeijFeatures(melody, segments)
        count += 1
        print('{0}/{1} done'.format(count, len(trainset)))

    print("Done, {0} segments found with an average length of: {1} (min: {2} max: {3})".format(const, lengths / float(const), Min, Max))
    tools.saveFeatures(features, expression)
     set = train.trainset(composers, pianist)
     train.train(set)
     exit(0)
 elif a[1] == 'align':
     import train
     set = [db.select()]
     train.train(set)
     exit(0)
 elif a[1] == 'plotcorpus':
     plotCorpus()
     exit(0)
 elif a[1] == 'plot':
     (selection, expression) = tools.loadPerformance()
     if selection == None:
         selection = db.select()
     score = db.getScore1(selection)
     melodyscore = Score(score).melody()
     melody = tools.parseScore(melodyscore)
     segmentation = structure.reasonableSegmentation(melody)
     visualize(segmentation, expression)
     exit(0)
 elif a[1] == 'corpora':
     for x in tools.datasets():
         print('Name:\t[{0}]\tInfo:\t{1}'.format(x, tools.corpusInfo(x)))
     exit(0)
 elif a[1] == 'corpusinfo':
     choice = util.menu("Select corpus", tools.datasets())
     print(tools.corpusInfo(tools.datasets()[choice]))
     tools.extendedCorpusInfo(tools.datasets()[choice])
     exit(0)
 elif a[1] == 'features':
Ejemplo n.º 6
0
    return [[n] for n in notes]


if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        l = [int(x) for x in sys.argv[1:]]
        print(relative_deltalist(test, l))
        print(second_order_tree(test, l, 0.0, deltalist_function=relative_deltalist))
        print(second_order_tree(test, l, 0.0))
        sys.exit(0)

    import database as db
    import tools
    w = db.select()
    score = Score(db.getScore1(w))
    melodyscore = score.melody()
    #melodyscore.show()
    melody = tools.parseScore(melodyscore, list(range(1, 9)))
    trees = [second_order_tree(onset, melody, 0.5), second_order_tree(pitch, melody, 0.0, ), first_order_tree(onset, melody, 0.0), first_order_tree(pitch, melody)]

    for tree in trees:
        print("Tree")
        print(tools.recursive_print(tree))


    for i in range(5):
        for j in range(len(trees)):
            groups = groupings(list_to_tree(trees[j]), i)
            avg_group = 0
            for group in groups:
Ejemplo n.º 7
0
import database as db
from alignment import *
from representation import *
from sequencer import *
from score import *
import tools

selection = db.select()
score = db.getScore1(selection)
notes = tools.parseScore(score)
seq = Sequencer()
seq.play(notes)
#s = Score(alignment.score, alignment)
Ejemplo n.º 8
0
import database as db
from sequencer import *
from alignment import *
import structure, tools
import scorefeatures as sf
import performancefeatures as pf
import perform

s = db.select()
a = Alignment(db.getScore1(s), db.getDeviation1(s))
melodyscore = a.melody()
melody = tools.parseScore(melodyscore)
onset = structure.groupings(structure.list_to_tree(structure.first_order_tree(structure.onset, melody, 0.1)), 1)
score = sf.vanDerWeijFeatures(melodyscore, onset)
performance = pf.vanDerWeijExpression(a, onset)

print(score)
print(performance)

seq = Sequencer()
seq.play(perform.vanDerWeijPerformSimple(a.score, melodyscore, onset, performance, bpm=a.deviations.bpm, converter=melody))
Ejemplo n.º 9
0
def square(l):
    result = []
    for i in l:
        result.append(i*i)
    return result

if len(sys.argv) > 1:
    notes = sys.argv[1]
    deltas = [int(x) for x in sys.argv[2]]
    s3 = structure.second_order_deltarule(notes, deltas, 0)
    print(tools.recursive_print(s3))
    sys.exit(0)


selection = db.select()
score = Score(db.getScore1(selection).stripTies())
melody = score.melody()
notes = tools.parseScore(melody)
deltas = []


print("COMBINED DELTA TREES")
#for feature in [structure.pitch, structure.onset]:
#  deltas.append(normalize(structure.repetition(feature, notes)))

for feature in [structure.onset, structure.duration, structure.pitch]:
    print(">>>> {0} absolute:".format(feature))
    print(structure.absolute_deltalist(feature, notes))
    print(">>>> {0} relative:".format(feature))
    print(structure.relative_deltalist(feature, notes))
Ejemplo n.º 10
0
import util, tools, structure


# Load all the scores and notelists

#while True:
#  print db.select()
print(">>>> Loading materials.....")

schumann = ('Schumann', 'kdz007', 'ashke', 'GPO-Concert-Steinway-ver2.sf2')
mozart1 = ('Mozart', 'snt331-3', 'nakam', 'GPO-Concert-Steinway-ver2.sf2')
chopin1 = ('Chopin', 'wlz010', 'ashke', 'Bosendorfer PIANO / GIGA')
mozart2 = ('Mozart', 'snt331-1', 'mo', None)
lastig = ('Bach', 'wtc219-f', 'richt', 'GPO-Concert-Steinway-ver2.sf2')

score = Score(db.getScore1(chopin1).stripTies())
melody = score.melody()
chopinnotes = tools.parseScore(melody)
delta = structure.absolute_deltalist(structure.onset, chopinnotes)
sodelta1 = structure.normalize(structure.second_order_deltalist(delta))

score = Score(db.getScore1(mozart2).stripTies())
melody = score.melody()
mozartnotes = tools.parseScore(melody)
delta2 = structure.square(structure.absolute_deltalist(structure.pitch, mozartnotes))
sodelta2 = structure.normalize(structure.second_order_deltalist(delta2))

s1 = structure.second_order_deltarule(chopinnotes, sodelta1, 0.1)
s2 = structure.second_order_deltarule(mozartnotes, sodelta2, 0.1)

while True: