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)
예제 #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)
                score += 1
        # we may hit the end of the score here
        except IndexError, err:
            print "End of score at %d?" % note.time
#            print err
        
    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)
        #        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)