def cycle(self):
     """
     A single cycle that allows all agents to perform.
     """
     agents = self.agents
     random.shuffle(agents)
     for agent in agents:
         # make the agent perform
         output = agent.perform()
         group_structure = getNoteGroups(output)
         nominal_tempo = self.defaultTempo
         nominal_volume = self.defaultVolume
         tempo_events = [(event.time, event.tempo) for event in output.tracks[0].eventList if event.type == "tempo"]
         notes = [event for event in output.tracks[0].eventList if event.type == "note"]
         metrical_hierarchy,metrical_scores = getMetricStructure(output)
         keys = key_change.analyze_key_change(output)
         accentuation = accentuation_curve(analyze_melodic_accent(output), metrical_scores, keys, notes)
         r1_tempo, r1_loudness, r2, r3, r4_tempo, r4_loudness, r5 = \
             rule1_tempo(group_structure, nominal_tempo, tempo_events), \
             rule1_loudness(group_structure, nominal_volume), \
             rule2(group_structure, nominal_tempo, tempo_events), \
             rule3(notes, nominal_volume, accentuation), \
             rule4_tempo(notes, accentuation, nominal_tempo, tempo_events), \
             rule4_loudness(notes, accentuation, nominal_volume), \
             rule5(group_structure, nominal_tempo, tempo_events)
         # let all other agents evaluate the performance
         for a in agents:
             if a is not agent:
                 a.listen(output, r1_tempo, r1_loudness, r2, r3, r4_tempo, r4_loudness, r5)
 def __listen_own(self, nominal_tempo, nominal_volume):
     self.__logger.debug("Listening to self")
     performance = self.performance
     group_structure = getNoteGroups(performance)
     tempo_events = [(event.time, event.tempo)
                     for event in performance.tracks[0].eventList
                     if event.type == "tempo"]
     notes = [
         event for event in performance.tracks[0].eventList
         if event.type == "note"
     ]
     metrical_hierarchy, metrical_scores = getMetricStructure(performance)
     keys = key_change.analyze_key_change(performance)
     accentuation = accentuation_curve(analyze_melodic_accent(performance),
                                       metrical_scores, keys, notes)
     r1_tempo, r1_loudness, r2, r3, r4_tempo, r4_loudness, r5 = \
         rule1_tempo(group_structure, nominal_tempo, tempo_events), \
         rule1_loudness(group_structure, nominal_volume), \
         rule2(group_structure, nominal_tempo, tempo_events), \
         rule3(notes, nominal_volume, accentuation), \
         rule4_tempo(notes, accentuation, nominal_tempo, tempo_events), \
         rule4_loudness(notes, accentuation, nominal_volume), \
         rule5(group_structure, nominal_tempo, tempo_events)
     return self.evaluate(r1_tempo, r1_loudness, r2, r3, r4_tempo,
                          r4_loudness, r5)
 def __listen_own(self, nominal_tempo, nominal_volume):
     self.__logger.debug("Listening to self")
     performance = self.performance
     group_structure = getNoteGroups(performance)
     tempo_events = [(event.time, event.tempo) for event in performance.tracks[0].eventList if event.type == "tempo"]
     notes = [event for event in performance.tracks[0].eventList if event.type == "note"]
     metrical_hierarchy,metrical_scores = getMetricStructure(performance)
     keys = key_change.analyze_key_change(performance)
     accentuation = accentuation_curve(analyze_melodic_accent(performance), metrical_scores, keys, notes)
     r1_tempo, r1_loudness, r2, r3, r4_tempo, r4_loudness, r5 = \
         rule1_tempo(group_structure, nominal_tempo, tempo_events), \
         rule1_loudness(group_structure, nominal_volume), \
         rule2(group_structure, nominal_tempo, tempo_events), \
         rule3(notes, nominal_volume, accentuation), \
         rule4_tempo(notes, accentuation, nominal_tempo, tempo_events), \
         rule4_loudness(notes, accentuation, nominal_volume), \
         rule5(group_structure, nominal_tempo, tempo_events)
     return self.evaluate(r1_tempo, r1_loudness, r2, r3, r4_tempo, r4_loudness, r5)
Example #4
0
 def cycle(self):
     """
     A single cycle that allows all agents to perform.
     """
     agents = self.agents
     random.shuffle(agents)
     for agent in agents:
         # make the agent perform
         output = agent.perform()
         group_structure = getNoteGroups(output)
         nominal_tempo = self.defaultTempo
         nominal_volume = self.defaultVolume
         tempo_events = [(event.time, event.tempo)
                         for event in output.tracks[0].eventList
                         if event.type == "tempo"]
         notes = [
             event for event in output.tracks[0].eventList
             if event.type == "note"
         ]
         metrical_hierarchy, metrical_scores = getMetricStructure(output)
         keys = key_change.analyze_key_change(output)
         accentuation = accentuation_curve(analyze_melodic_accent(output),
                                           metrical_scores, keys, notes)
         r1_tempo, r1_loudness, r2, r3, r4_tempo, r4_loudness, r5 = \
             rule1_tempo(group_structure, nominal_tempo, tempo_events), \
             rule1_loudness(group_structure, nominal_volume), \
             rule2(group_structure, nominal_tempo, tempo_events), \
             rule3(notes, nominal_volume, accentuation), \
             rule4_tempo(notes, accentuation, nominal_tempo, tempo_events), \
             rule4_loudness(notes, accentuation, nominal_volume), \
             rule5(group_structure, nominal_tempo, tempo_events)
         # let all other agents evaluate the performance
         for a in agents:
             if a is not agent:
                 a.listen(output, r1_tempo, r1_loudness, r2, r3, r4_tempo,
                          r4_loudness, r5)
        
    return score
    
if __name__ == '__main__':
#    performance = mid.prepare_initial_midi("../../../res/midi_text.txt", "../../../res/sample.midi", 15200)
    import doctest
    doctest.testmod()
    exit()
    
    performance = mid.prepare_initial_midi("../../../res/midi_rule3_text.txt", "../../../res/sample_rule3.midi", 15200)
    group_structure = lbdm.getNoteGroups(performance)
    tempo_events = [(event.time, event.tempo) for event in performance.tracks[0].eventList if event.type == "tempo"]
    notes = [event for event in performance.tracks[0].eventList if event.type == "note"]
    nominal_tempo = 3947
    nominal_loudness = 100
    melodic_accent = melodic_accent.analyze_melodic_accent(performance)
    metric_structure, metrical_scores = metric_structure.getMetricStructure(performance)
    key = key_change.analyze_key_change(performance)
    accentuation = accentuation_curve.accentuation_curve(melodic_accent, metrical_scores, key, notes)
    print "Rule 1 tempo: %d" % rule1_tempo(group_structure, nominal_tempo, tempo_events)
    print "Rule 1 loudness: %d" % rule1_loudness(group_structure, nominal_loudness)
    print "Rule 2: %d" % rule2(group_structure, nominal_tempo, tempo_events)
    print "Rule 3: %d" % rule3(notes, nominal_loudness, accentuation)
    print "Rule 4 tempo: %d" % rule4_tempo(notes, accentuation, nominal_tempo, tempo_events)
    print "Rule 4 loudness: %d" % rule4_loudness(notes, accentuation, nominal_loudness)
    print "Rule 5: %d" % rule5(group_structure, nominal_tempo, tempo_events)
#    accentuation_curve(melodic_accent.analyze_melodic_accent(performance), metric_scores, key_change.analyze_key_change(performance), [note for note in performance.tracks[0].eventList if note.type == "note"])
    print "Rule 5: %d" % rule5(group_structure, nominal_tempo, tempo_events)
    metric_structure, metric_scores = metric_structure.getMetricStructure(performance)
    accentuation_curve(melodic_accent.analyze_melodic_accent(performance), metric_scores, key_change.analyze_key_change(performance), [note for note in performance.tracks[0].eventList if note.type == "note"])

#    print keys_final
#    print accentuation_curve

    return accentuation_curve

if __name__ == "__main__":
    from tools import midi as mid
    from tools.analysis import lbdm, metric_structure, melodic_accent, key_change
    performance = mid.prepare_initial_midi("../../../res/midi_text.txt",
                                           "../../../res/sample.midi", 15200)
    group_structure = lbdm.getNoteGroups(performance)
    tempo_events = [(event.time, event.tempo)
                    for event in performance.tracks[0].eventList
                    if event.type == "tempo"]
    notes = [
        event for event in performance.tracks[0].eventList
        if event.type == "note"
    ]
    nominal_tempo = 3947
    nominal_loudness = 100
    melodic_accent = melodic_accent.analyze_melodic_accent(performance)
    metric_structure, metrical_scores = metric_structure.getMetricStructure(
        performance)
    key = key_change.analyze_key_change(performance)
    accentuation = accentuation_curve(melodic_accent, metrical_scores, key,
                                      notes)
    from matplotlib import pyplot as plt
    plt.plot(range(len(accentuation)), accentuation)
    plt.show()
    accentuation_curve = [w_melodic * melodic + w_metric * metric for melodic, metric in zip(accents, metrics)]
    
#    print accentuation_curve
    keys_final = []
    for i in range(len(notes)):
        note = notes[i]
#        print i, note_find_measure(note)
        accentuation_curve[i] += w_key_change * keys[note_find_measure(note)]
        keys_final.append(keys[note_find_measure(note)])
#    print keys_final
#    print accentuation_curve

    
    return accentuation_curve

if __name__ == "__main__":
    from tools import midi as mid
    from tools.analysis import lbdm, metric_structure, melodic_accent, key_change
    performance = mid.prepare_initial_midi("../../../res/midi_text.txt", "../../../res/sample.midi", 15200)
    group_structure = lbdm.getNoteGroups(performance)
    tempo_events = [(event.time, event.tempo) for event in performance.tracks[0].eventList if event.type == "tempo"]
    notes = [event for event in performance.tracks[0].eventList if event.type == "note"]
    nominal_tempo = 3947
    nominal_loudness = 100
    melodic_accent = melodic_accent.analyze_melodic_accent(performance)
    metric_structure, metrical_scores = metric_structure.getMetricStructure(performance)
    key = key_change.analyze_key_change(performance)
    accentuation = accentuation_curve(melodic_accent, metrical_scores, key, notes)
    from matplotlib import pyplot as plt
    plt.plot(range(len(accentuation)), accentuation)
    plt.show()