예제 #1
0
def _assemble_measures(full_path):
    r"""
    Return a list of all the measures present in a generated midi.

    #. `full_path`: the absolute path to the generated midi file
    """
    LOG.debug("Path to piece: {full_path}".format(**locals()))
    piece = parse(full_path)
    # range is exclusive, so 13 + 1
    measures = [m for m in extract_melody_measures(piece, range(1, 13 + 1))]
    return measures
def _assemble_measures(full_path):
    r"""
    Return a list of all the measures present in a generated midi.

    #. `full_path`: the absolute path to the generated midi file
    """
    LOG.debug("Path to piece: {full_path}".format(**locals()))
    piece = parse(full_path)
    # range is exclusive, so 13 + 1
    measures = [m for m in extract_melody_measures(piece, range(1, 13 + 1))]
    return measures
def _test_generated_measures(gui_subpath, result_path, total_measures, measure_num, rhythm_percentiles, melody_percentiles, percentile_rhythm, percentile_melody, rhythm_chain, melody_chain, mode='Relative'):
    r"""
    Check the quality of newly generated measures.

    Returns `True` if all measures meet all requirements.
    """
    LOG.debug('Commencing test')
    thematic_structure = _parse_themes(result_path, total_measures)
    measures = _assemble_measures(gui_subpath(MIDI_FN))  # 13 in all

    # these were just generated by _generate_pre_test - test these
    generated_nums = _to_be_generated(thematic_structure, measure_num)
    # these were completed before above generation
    completed_measures = _completed_measures(thematic_structure, measure_num)

    # set up stage to check last measure - rest is included in "_multi"
    recent_measure = max(generated_nums)
    existing = completed_measures | set([m for m in generated_nums if m <= recent_measure])
    LOG.debug('Existing measures, i.e. old plus new: {existing}'.format(**locals()))
    history_nums = sorted(list(existing))
    history_measures = [measures[index - 1] for index in history_nums]
    LOG.debug('As history: {history_measures}'.format(**locals()))

    tested_indices = [history_nums.index(num) for num in generated_nums]
    LOG.debug('Testing measures with indices: {tested_indices}'.format(**locals()))

    rhythm_entries_sequence = [[str(rhythm) for rhythm in _measure_rhythms(measure)] for measure in history_measures]
    rhythm_likelihoods = list(_multi_log_likelihoods(rhythm_entries_sequence, rhythm_chain))
    tested_r_likelihoods = [rhythm_likelihoods[i] for i in tested_indices]

#-----------------------------------------------------------------#
    if mode == 'Temperley':
        melody_likelihoods = []
        melody_measures = extract_melody_measures(music21.converter.parse(gui_subpath(MIDI_FN)), list(generated_nums))
        for measure in melody_measures:
            LOG.debug('Generated measure: {measure}'.format(**locals()))
            midi_f = streamToMidiFile(measure)
            midi_path = gui_subpath('temperley_measure.midi')
            # following is not a file, but a safe output location
            temp_midi_path = gui_subpath('temp_measure.midi')
            midi_f.open(midi_path, 'wb')
            midi_f.write()
            midi_f.close()
            likelihood = likelihood_melody(midi_path=gui_subpath(midi_path),
                                           temp_midi_path=temp_midi_path)
            melody_likelihoods.append(likelihood)
            LOG.debug("Adding (Temperley's) likelihood: {l}".format(l=likelihood))
        tested_m_likelihoods = melody_likelihoods  # no linear combinations...
    else:
        if mode == 'Relative':
            melody_entries_sequence = [[str(melody) for melody in _measure_melodies(measure)] for measure in history_measures]
        elif mode == 'Mixed':
            key = _parse_key(result_path)
            chords = [_parse_chord(result_path, m) for m in history_nums]
            measure_chords = zip(history_measures, chords)
            melody_entries_sequence = [[str(melody) for melody in _measure_melodies_mixed(measure, key, chord)] for (measure, chord) in measure_chords]
            LOG.debug('Melody entries sequence: {melody_entries_sequence}'.format(**locals()))
        melody_likelihoods = list(_multi_log_likelihoods(melody_entries_sequence, melody_chain))
        tested_m_likelihoods = [melody_likelihoods[i] for i in tested_indices]
#-----------------------------------------------------------------#

    LOG.debug('Likelihoods for tested rhythms: {tested_r_likelihoods}'.format(**locals()))
    LOG.debug('Likelihoods for tested melodies: {tested_m_likelihoods}'.format(**locals()))

    generated_rhythm_percentiles = [belongs_to_percentile(likelihood, rhythm_percentiles) for likelihood in tested_r_likelihoods]
    generated_melody_percentiles = [belongs_to_percentile(likelihood, melody_percentiles) for likelihood in tested_m_likelihoods]

    LOG.debug('Percentile sections for rhythms: {generated_rhythm_percentiles}'.format(**locals()))
    LOG.debug('Percentile sections for melodies: {generated_melody_percentiles}'.format(**locals()))

    right_rhythm = True#all((percentile == percentile_rhythm for percentile in generated_rhythm_percentiles))
    right_melody = True#all((percentile == percentile_melody for percentile in generated_melody_percentiles))

    return (right_rhythm and right_melody)