Esempio n. 1
0
def main():
    usage= 'usage: %prog [options] infname'
    parser= OptionParser(usage=usage)
    parser.add_option('-o', '--output', 
                        dest='output_folder', default='patterns',
                        help='output folder')
    parser.add_option('-c', '--no-cache', 
                        dest='cache', action='store_false', default=True, 
                        help='discard cache')

    options, args= parser.parse_args(argv[1:])
    if len(args) < 1: parser.error('not enaught args')

    parser= parserclass('models_cache')

    infname= args[0]
    score= parser.parse(infname)
    score= quantize(score)

    output_folder_prefix= options.output_folder

    pat_sizes= [4]
    margins= range(3)
    f= 2
    key= lambda n:('%s|%s' % (n.duration, n.is_silence),)

    writer= writerclass()

    results= {}
    patterns= get_score_patterns(score, pat_sizes, margins, f, key)
    instr, all_notes= score.notes_per_instrument.iteritems().next()

    output_folder= os.path.join(output_folder_prefix, infname[:-4])
    if not os.path.exists(output_folder):
        os.mkdir(output_folder)

    for i, (pat, matches) in enumerate(patterns.iteritems()):
        pattern_folder= os.path.join(output_folder, 'pat_%s' % i)
        if not os.path.exists(pattern_folder):
            os.mkdir(pattern_folder)
        for j, (start, end) in enumerate(matches):
            notes= all_notes[start:end]
            notes= [n.copy() for n in notes]

            notes[0].start= 0
            for prev, next in zip(notes, notes[1:]):
                next.start= prev.start+prev.duration
                            
            s= score.copy()
            s.notes_per_instrument={instr:notes}

            writer.dump(s, os.path.join(pattern_folder, 'match_%s.mid' % j))
Esempio n. 2
0
def main():
    usage= 'usage: %prog [options] infname1 [infname2 ...]'
    parser= OptionParser(usage=usage)
    parser.add_option('-o', '--output', 
                        dest='output_fname', default='cluster.out',
                        help='output fname')
    parser.add_option('-c', '--no-cache', 
                        dest='cache', action='store_false', default=True, 
                        help='discard cache')
    parser.add_option('-k', '--resulting-dimensionality', 
                        dest='k', default=150, 
                        help='el numero de dimensiones resultantes')

    options, args= parser.parse_args(argv[1:])
    if len(args) < 1: parser.error('not enaught args')

    parser= parserclass('models_cache')

    infnames= args
    outfname= options.output_fname

    print 'parsing scores'
    scores= [parser.parse(infname, cache=options.cache) for infname in infnames]

    nclusterss= [2] # range(2,5)
    npasss= [5,10, 15, 20, 25]
    methods= ['a', 'm']
    dists= ['e', 'b', 'c', 'a', 'u', 'x', 's', 'k']
    configs= list(combine(nclusterss, npasss, methods, dists))

    results= {}
    for k in range(2,10,2):
        print 'k=', k
        concept_vectors= apply_lsa(scores, k)
        step= len(configs)/10
        for i, (nclusters, npass, method, dist) in enumerate(configs):
            if (i+1)%step == 0: print '\t', ((i+1)*100)/len(configs)
            r= Pycluster.kcluster(concept_vectors, 
                                  nclusters= nclusters, 
                                  method= method, dist= dist)
            results[(k, nclusters, npass, method, dist)]= r

    
    f= open('clusters_results.pickle', 'w')
    pickle.dump(results, f, 2)
    f.close()
Esempio n. 3
0
def main():
    usage= 'usage: %prog [options] infname'
    parser= OptionParser(usage=usage)
    parser.add_option('-c', '--no-cache', 
                        dest='cache', action='store_false', default=True, 
                        help='discard cache')


    options, args= parser.parse_args(argv[1:])
    if len(args) < 1: parser.error('not enaught args')

    parser= parserclass('models_cache')
    
    infname= args[0]
    score= parser.parse(infname, cache=options.cache)
    #import ipdb;ipdb.set_trace()
    notes= score.notes_per_instrument.values()[0]
    for n in notes: print n
Esempio n. 4
0
def main():
    usage= 'usage: %prog [options] infname [outfname]'
    parser= OptionParser(usage=usage)
    parser.add_option('-f', '--from', dest='moment_from', help='start moment in ticks', default=0)
    parser.add_option('-t', '--to', dest='moment_to', help='end moment in ticks')
    parser.add_option('-d', '--duration', dest='duration', help='duration of the slice')
    parser.add_option('-c', '--no-cache', dest='cache', action='store_false', default=True, help='discard cache')

    options, args= parser.parse_args(argv[1:])
    if len(args) < 1: parser.error('not enaught args')

    parser= parserclass('models_cache')
    moment_from= int(options.moment_from)
    if options.moment_to is None and options.duration is None:
        parser.error('falta --to y --duration')
    elif options.moment_to is None:
        moment_to= int(options.moment_from)+int(options.duration)
    else:
        moment_to= int(options.moment_to)

    infname= args[0]
    if len(args) == 1: outfname= '%s-%s-%s.mid' % (infname[:-4], moment_from, moment_to)
    else: outfname= args[1]

    print 'creating score'
    score= parser.parse(infname, cache=options.cache)
    beat_duration= float(score.tempo)/1e6
    divisions= score.divisions
    for instrument, notes in score.notes_per_instrument.iteritems():
        new_notes= [n for n in notes  \
                      if n.start/divisions*beat_duration >= moment_from and \
                         (n.start + n.duration)/divisions*beat_duration< moment_to]
        score.notes_per_instrument[instrument]= new_notes

    print 'dumping'
    writer= writerclass()
    writer.dump(score, outfname)
Esempio n. 5
0
from config import parserclass, writerclass
from sys import argv

in_file= argv[1]
channel= int(argv[2])
out_file = in_file.replace('.mid', '-%s.mid' % channel)
parser= parserclass()
score= parser.parse(in_file)
for instr, notes in score.notes_per_instrument.iteritems():
    if instr.channel == channel: break
#instr.is_drums=True
score.notes_per_instrument= {instr:notes}
writer= writerclass()
writer.dump(score, out_file)

Esempio n. 6
0
def train2(options, args):
    partition_algorithm= options.partition_algorithm
    patch= options.patch
    channel= options.channel
    level= options.level
    infname= args[0]
    outfname= args[1]

    parser= parserclass()
    score= parser.parse(infname)
    #score= quantize(score)
    orig_score= score.copy()
    if not score: import ipdb;ipdb.set_trace() # por que podria devolver None?

    #########
    # BOCHA DE AJUSTES DE PARSING
    instr= score.notes_per_instrument.keys()[0]
    patch= instr.patch
    channel= instr.channel
    #########
    #########


    if options.partition_algorithm == 'MGRID':
        interval_size= metrical_grid_interval_size(score, notes, level)
    elif options.partition_algorithm == 'MEASURE':
        interval_size= measure_interval_size(score, options.n_measures)

    nintervals= 16
    nintervals= orig_score.get_notes()[-1].end/interval_size
    nphrases= 5
    composition_length= interval_size*nintervals
    alpha= nphrases/log(nintervals,2)

    # para que la copie del tema original
    chords_notes_alg= ScoreHarmonicContext(orig_score)
    rythm_alg= RythmHMM(interval_size, multipart=False, instrument=patch, channel=channel)
    melody_alg= HarmonyHMM(instrument=patch, channel=channel)
    algorithm= StackAlgorithm(rythm_alg, chords_notes_alg, melody_alg)

    # para que arme la base
    #chords_notes_alg= HMMHarmonicContext(3)
    #chords_rythm_alg= RythmHMM(interval_size, multipart=True, instrument=patch, channel=channel)
    #chord_maker= StackAlgorithm(chords_rythm_alg, chords_notes_alg)

    #phrase_maker= PhraseAlgorithm(orig_score.divisions, alpha, chord_maker)

    #rythm_alg= RythmHMM(interval_size, multipart=True, instrument=patch, channel=channel)
    #melody_alg= HarmonyHMM(instrument=patch, channel=channel)

    #algorithm= StackAlgorithm(phrase_maker, rythm_alg, phrase_maker, melody_alg)

    algorithm.train(score)
    applier= AlgorithmsApplier(algorithm)
    notes= applier.create_melody(composition_length, print_info=True)
    
    #for c1, c2 in zip(phrase_maker.ec.chords, phrase_maker.ec.chords[1:]):
    #    if c1.end != c2.start: import ipdb;ipdb.set_trace()

    if options.print_model: print algorithm.model

    drums= Instrument(is_drums=True)
    #drums.patch= int('0x12', 16)
    instrument= Instrument()
    instrument2= Instrument()
    instrument3= Instrument()
    instrument.patch= 33
    instrument2.patch= 21
    instrument3.patch= 0

    for i, ns in orig_score.notes_per_instrument.iteritems():
        for n in ns: n.volume= 75

    full_new= False
    if full_new:
        chords= []
        duration= 0
        metro= []
        for i in xrange(0, nintervals*interval_size, score.divisions):
            metro.append(PlayedNote(31, i, i+1, 65))


        for chord in phrase_maker.ec.chords:
            for note in chord.notes:
                chords.append(PlayedNote(note.pitch, chord.start, chord.duration, chord.volume))
        orig_score.notes_per_instrument= {instrument3:chords, instrument:notes}#, drums:metro}
        #orig_score.notes_per_instrument= {drums:metro}
        #orig_score.notes_per_instrument= {instrument:notes}
        orig_score.notes_per_instrument= {instrument3:chords}#, drums:metro}
    else:        
        orig_score.notes_per_instrument[instrument]= notes
    writer= writerclass()
    writer.dump(orig_score, outfname)
    print 'done!'