gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:46:48 25 4
gpt4 key购买 nike

我在 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 code 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()

最佳答案

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

关于python - 用openai图表txt文件编码到midi转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74831022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com