コード例 #1
0
    def setMidiStream(self, stream):
        if not self._midi is None:
            raise StreamPlayer2Exception('midi stream is already set.')
        streamMidiFile = midiTranslate.streamToMidiFile(stream)
        streamMidiWritten = streamMidiFile.writestr()
        self._midi = streamMidiFile

        return fluid_player_add_mem(self.player, streamMidiWritten,
                                    len(streamMidiWritten))
コード例 #2
0
ファイル: DataHandler.py プロジェクト: mzokov/dissertation
    def save_to_midi(self, stream, location='generated/'):
        """
        Creates a MIDI file from a music21.stream.Stream object.

        :param stream: a Stream that will be saved to file
        :param location: file path for the MIDI file.
        """
        midi_data = m21_translate.streamToMidiFile(self.create_note_stream(stream))
        midi_data.open(location + str(uuid.uuid4()) + '.mid', attrib='wb')
        midi_data.write()
コード例 #3
0
ファイル: midi_creation.py プロジェクト: ido4848/intelligence
def save_midi(save_path, item):
    midi_file = translate.streamToMidiFile(item)
    binfile = open(save_path, 'wb')
    # trick for getting music21 shut up
    _stderr = sys.stderr
    with open(os.devnull, 'w') as f:
        sys.stderr = f
        binfile.write(midi_file.writestr())
    sys.stderr = _stderr
    binfile.close()
コード例 #4
0
def save_midi(save_path, item):
    midi_file = translate.streamToMidiFile(item)
    binfile = open(save_path, 'wb')
    # trick for getting music21 shut up
    _stderr = sys.stderr
    with open(os.devnull, 'w') as f:
        sys.stderr = f
        binfile.write(midi_file.writestr())
    sys.stderr = _stderr
    binfile.close()
コード例 #5
0
    def save_to_midi(self, stream, location='generated/'):
        """
        Creates a MIDI file from a music21.stream.Stream object.

        :param stream: a Stream that will be saved to file
        :param location: file path for the MIDI file.
        """
        midi_data = m21_translate.streamToMidiFile(
            self.create_note_stream(stream))
        midi_data.open(location + str(uuid.uuid4()) + '.mid', attrib='wb')
        midi_data.write()
コード例 #6
0
def notelist_to_midi(notes, filename='test.mid'):
    s = stream.Stream()

    for n in notes:
        if n.midi == 0:
            midinote = note.Rest()
            print('REST!!')
        else:
            print('NOTE: midi=', n.midi)
            midinote = note.Note(midinote_to_pc_octave(n.midi))
        midinote.quarterLength = dur_string_to_quarterlength(n.dur_string)
        s.append(midinote)

    mf = streamToMidiFile(s)
    mf.open(filename, 'wb')
    mf.write()
    mf.close()
コード例 #7
0
def likelihood_melody(midi_path, temp_midi_path):
    r"""
    Given the full path to an APOPCALEAPS-generated midi file,
    return Temperley's estimation of the likelihood of the melody.

    #. `midi_path`: path to the midi file whose likelihood will be assessed
    #. `temp_midi_path`: a path where an intermediate file may safely be stored

    **Note:this function cannot be applied to arbitrarily long melodies.
    It should only be used for single measures.**
    """
    piece = parse(midi_path)
    melody = extract_melody(piece)
    midi_f = streamToMidiFile(melody)
    midi_f.open(temp_midi_path, 'wb')
    midi_f.write()
    midi_f.close()
    process_notelist = subprocess.Popen([notelist_path, temp_midi_path], stdout=PIPE)
    notelist = process_notelist.communicate()[0]
    process_melprob = subprocess.Popen([melprob_path], stdin=PIPE, stdout=PIPE)
    likelihood = float(process_melprob.communicate(notelist)[0])
    return likelihood
コード例 #8
0
def likelihood_melody(midi_path, temp_midi_path):
    r"""
    Given the full path to an APOPCALEAPS-generated midi file,
    return Temperley's estimation of the likelihood of the melody.

    #. `midi_path`: path to the midi file whose likelihood will be assessed
    #. `temp_midi_path`: a path where an intermediate file may safely be stored

    **Note:this function cannot be applied to arbitrarily long melodies.
    It should only be used for single measures.**
    """
    piece = parse(midi_path)
    melody = extract_melody(piece)
    midi_f = streamToMidiFile(melody)
    midi_f.open(temp_midi_path, 'wb')
    midi_f.write()
    midi_f.close()
    process_notelist = subprocess.Popen([notelist_path, temp_midi_path],
                                        stdout=PIPE)
    notelist = process_notelist.communicate()[0]
    process_melprob = subprocess.Popen([melprob_path], stdin=PIPE, stdout=PIPE)
    likelihood = float(process_melprob.communicate(notelist)[0])
    return likelihood
コード例 #9
0
 def getStringIOFile(self):
     streamMidiFile = midiTranslate.streamToMidiFile(self.streamIn)
     streamMidiWritten = streamMidiFile.writestr()
     return stringIOModule.StringIO(streamMidiWritten)
コード例 #10
0
 def getStringOrBytesIOFile(self):
     streamMidiFile = midiTranslate.streamToMidiFile(self.streamIn)
     streamMidiWritten = streamMidiFile.writestr()
     return StrByIO(streamMidiWritten)
コード例 #11
0
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)