I have created this code in OpenAI to convert a music notes .txt file into a MIDI file, but it is giving me some errors. If anyone could provide kind assistance to fix it, I would be very grateful.
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()
I tried to reinstall openai but not working i have tried in 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()
CodePudding user response:
The package librosa
has a method note_to_midi(note)
but mido
does not. So install and import librosa
and change pitch = mido.note_to_midi(note)
to pitch = librosa.note_to_midi(note)
if you then give it a text file e.g. containing C♯3 B C D E C♯3
(note the spaces) it works.
See https://librosa.org/doc/main/generated/librosa.note_to_midi.html