簡體   English   中英

music21:寫入midi文件后和弦轉為音符?

[英]music21:the chord turn to note after write into midi files?

這就是我得到注意的方式:

def read_notes_from_file(file):
  notes = []
  song = converter.parse(file)
  notes_in_song = None
  try:
      s2 = instrument.partitionByInstrument(song)
      notes_in_song = s2.parts[0].recurse()
  except:
      notes_in_song = song.flat.notes
  for element in notes_in_song:
      if isinstance(element, note.Note):
          notes.append(str(element.pitch))
      elif isinstance(element, chord.Chord):
          notes.append('.'.join(str(n) for n in element.normalOrder))

  return notes

這就是我創建 MIDI 的方式:

def create_midi(prediction_output, filename):
    offset = 0
    output_notes = []
    for item in prediction_output:
        pattern = item[0]

        if ('.' in pattern) or pattern.isdigit():
            notes_in_chord = pattern.split('.')
            notes = []
            for current_note in notes_in_chord:
                new_note = note.Note(int(current_note))
                new_note.storedInstrument = instrument.Piano()
                notes.append(new_note)
            new_chord = chord.Chord(notes)
            new_chord.offset = offset
            output_notes.append(new_chord)

        else:
            new_note = note.Note(pattern)
            new_note.offset = offset
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

        offset += 0.5

    midi_stream = stream.Stream(output_notes)
    midi_stream.write('midi', fp='{}.mid'.format(filename))

這是我輸入到 create_midi 中的音符:

['G1', 'G6', 'C#5', '9.0.2.4', 'E-7', '2.5.7.9', 'C#6', '7.9.1.2', '11.2.4.7', '4.9.10', '3.5.7.11', '8.9.0', '11.2.3', '5.7.10', '7.0', '6.10.11', '2.3.5.9', '2.8', '5.11', '2.4.5', '5.6.7', '5.7.9.11', '1.2.5.8', '6.7.11.1.2', '1.5.8.9', '2', '3.4.8', '10.11.3.6', '4.5.8.9', '3.6.7', '4.5.7.11', '4.5.9', '3.7.11', '3.5.7.8', '5.9', '5.7.10.11', '2.4.6.10', '5.7.10.11', '10.11.0.2.3', '3.4.8', '11.4', '10.0.4.5', '5.8', '11.2.4.6', '2.6.7', '3.6.7.8.9', '4.5.8', '5.8.11.1', '2.5.7', '11.1.4', '1.4.6.9', '11.1', '4.6.7.11', '11.3.6.7', '2.4.6.9', '2.4', '2.8', '4.8.11', '11.2.5', '2.4.6.10', '10.11.3.6', '2.6.10', '5.8.10.0.1', '2.5.8.10', '0.6', '3.7.10.11', '3.6', '3.6.10', '2.4.6', '5.11', '11.0.4.7', '10.2.5', '11.1.2', '3.6.9', '5.6.9', '5.7.9.11.2', '5.8.10.1', '1.2.4.6.9', '6.7.11.2', '3.5.8.9', '2.3.7.10', '3.6', '6.8.9.11.1', '1.4.7.9.10', '2.5.8.10', '6.10.11', '1.4.7', '4.5.8', '6.8.9.1', '5.8', '2.3.6.9', '3.4.6.8.11', '11.2.4.7', '10.1', '11.3.4.6', '4.5.7', '3.6.9', '11.2.4.7', '5.7.10', '3.7.10']

這是我將上面的音符創建的 midi 輸入到 read_notes_from_file 后得到的音符:

['G4', 'G4', 'C4', 'A4', 'E4', 'D4', 'C4', 'G4', 'C#4', 'E4', 'E-4', 'G#4', 'C#4', 'F4', 'G4', 'F#4', 'D4', 'D4', 'F4', 'D4', 'F4', 'F4', 'C#4', 'F#4', 'C#4', 'D4', 'E-4', 'C#4', 'E4', 'E-4', 'E4', 'E4', 'E-4', 'E-4', 'F4', 'F4', 'D4', 'F4', 'C#4', 'E-4', 'C#4', 'C#4', 'F4', 'C#4', 'D4', 'E-4', 'E4', 'F4', 'D4', 'C#4', 'C#4', 'C#4', 'E4', 'C#4', 'D4', 'D4', 'D4', 'E4', 'C#4', 'D4', 'C#4', 'D4', 'F4', 'D4', 'C4', 'E-4', 'E-4', 'E-4', 'D4', 'F4', 'C#4', 'C#4', 'C#4', 'E-4', 'F4', 'F4', 'F4', 'C#4', 'F#4', 'E-4', 'D4', 'E-4', 'F#4', 'C#4', 'D4', 'F#4', 'C#4', 'E4', 'F#4', 'F4', 'D4', 'E-4', 'C#4', 'C#4', 'C#4', 'E4', 'E-4', 'C#4', 'F4', 'E-4']

它們只是和弦的音符,我不知道如何處理它。代碼有什么問題嗎?

如果您在pattern = item[0]之后添加print(pattern) ,您會發現您正在以意想不到的方式修剪輸入。 C#變成C2.5.7變成2 這就是和弦作為單個音符導出的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM