2

スクリプトがハングアップして CPU が 100% ロードされることがあるため、music21 ライブラリを使用して midi ファイルを並列に解析したいと考えています。問題は、私の関数がファイルからメモのリストを返しcommon.runParallel、この関数を使用すると取得されることですTypeError: 'list' object is not callable

from music21 import converter, instrument, note, chord, common

for file in glob.glob("midi/preludes/*.mid"):
    files.append("midi/preludes"+file)


def get_notes():
    notes = []

    midi = converter.parse(file)

    print("Parsing %s" % file)

    notes_to_parse = None

    try:   
        s2 = instrument.partitionByInstrument(midi)
        notes_to_parse = s2.parts[0].recurse()
    except:  
        notes_to_parse = midi.flat.notes

    for element in notes_to_parse:
        if isinstance(element, note.Note):
            notes.append(str(element.pitch))
        elif isinstance(element, chord.Chord):
            print('.'.join(str(n) for n in element.normalOrder))
            notes.append('.'.join(str(n) for n in element.normalOrder))

    with open('data/notes', 'wb') as filepath:
        pickle.dump(notes, filepath)

    return notes

output = common.runParallel(files, parallelFunction=get_notes())

どうすればこれを修正できますか?

EDID

関数を次のように変更します。

def get_notes_parallel(file):
    notes = []

    midi = converter.parse(file)

    print("Parsing %s" % file)

    notes_to_parse = None

    try:   
        s2 = instrument.partitionByInstrument(midi)
        notes_to_parse = s2.parts[0].recurse()
    except:  
        notes_to_parse = midi.flat.notes

    for element in notes_to_parse:
        if isinstance(element, note.Note):
            notes.append(str(element.pitch))
        elif isinstance(element, chord.Chord):
            print('.'.join(str(n) for n in element.normalOrder))
            notes.append('.'.join(str(n) for n in element.normalOrder))

    with open('data/notes', 'wb') as filepath:
        pickle.dump(notes, filepath)

    return notes

notes = common.runParallel(files, parallelFunction=get_notes_parallel)
notes = [item for list in notes for item in list]

そしてそれはうまくいっています

4

1 に答える 1