python-3.x 用openai图表txt文件编码到midi转换器

ryoqjall  于 2022-12-20  发布在  Python
关注(0)|答案(1)|浏览(197)

我在OpenAI中创建了这段代码,用来将一个音乐笔记.txt文件转换成MIDI文件,但是它给了我一些错误。如果有人能提供帮助来修复它,我将非常感激。

import mido

# Function to convert a music chart string into a MIDI track
def chart_to_track(chart, instrument):
    # Split the chart into a list of measures
    measures = chart.strip().split("|")
    # Initialize an empty list to store the MIDI events
    events = []
    # Set the current time to zero
    time = 0
    # Iterate through the measures
    for measure in measures:
        # Split the measure into a list of notes
        notes = measure.strip().split()
        # Iterate through the notes
        for note in notes:
            # Convert the note name to a MIDI pitch
            pitch = mido.note_to_midi(note)
            # Add a NOTE_ON event to the events list
            events.append(mido.Message('note_on', note=pitch, velocity=127, time=time))
            # Add a NOTE_OFF event to the events list after a quarter note duration
            events.append(mido.Message('note_off', note=pitch, velocity=127, time=480))
            # Increase the current time by a quarter note duration
            time += 480
    # Create a MIDI track with the events and the specified instrument
    track = mido.MidiTrack()
    track.extend(events)
    track.program = instrument
    return track

# Function to prompt the user to choose an instrument
def choose_instrument():
    # Print a list of instrument options
    print("Choose an instrument:")
    print("1. Acoustic Grand Piano")
    print("2. Electric Guitar (clean)")
    print("3. Violin")
    print("4. Trumpet")
    print("5. Clarinet")
    # Prompt the user to enter a number
    choice = input("Enter the number of your choice: ")
    # Return the corresponding instrument number
    if choice == "1":
        return 0
    elif choice == "2":
        return 25
    elif choice == "3":
        return 40
    elif choice == "4":
        return 57
    elif choice == "5":
        return 71
    else:
        print("Invalid choice. Please try again.")
        return choose_instrument()

# Main function
def main():
    # Prompt the user to enter the name of the text file
    filename = input("Enter the name of the text file: ")
    # Open the file and read the contents into a string
    with open(filename, "r") as f:
        chart = f.read()
    # Choose an instrument
    instrument = choose_instrument()
    # Convert the chart to a MIDI track
    track = chart_to_track(chart, instrument)
    # Create a MIDI file and add the track
    midi = mido.MidiFile()
    midi.tracks.append(track)
    # Prompt the user to enter the name of the MIDI file
    output_filename = input("Enter the name of the MIDI file: ")
    # Save the MIDI file
    midi.save(output_filename)

# Run the main function
main()

我试图重新安装openai,但不工作,我已经尝试在wsl2 ubuntu 22. 04 lts & visual basic代码windows 10 '

**Outout of the Error **
Exception has occurred: AttributeError
module 'mido' has no attribute 'note_to_midi'
  File "D:\Astro\music.py", line 18, in chart_to_track
    pitch = mido.note_to_midi(note)
  File "D:\Astro\music.py", line 67, in main
    track = chart_to_track(chart, instrument)
  File "D:\Astro\music.py", line 77, in <module>
    main()
k3bvogb1

k3bvogb11#

librosa有一个方法note_to_midi(note),但是mido没有,所以安装并导入librosa,然后将pitch = mido.note_to_midi(note)更改为pitch = librosa.note_to_midi(note)
如果你给它一个文本文件,例如包含C♯3 B C D E C♯3(注意空格),它就能工作。
参见https://librosa.org/doc/main/generated/librosa.note_to_midi.html

相关问题