Open Zeyu-Li opened 3 years ago
building on the example file given with the repository
#!/usr/bin/env python
from midiutil import MIDIFile
degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created
# automatically)
MyMIDI.addTempo(track, time, tempo)
for i, pitch in enumerate(degrees):
MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
with open("major-scale.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
adding this code onto the end will give you the needed mp3.
import os
mp3 = "timidity major-scale.mid -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k major-scale.mp3"
print(mp3)
pmp3 = os.popen(mp3)
print(pmp3.read())
print(pmp3.close())
it uses a terminal command:
timidity major-scale.mid -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 64k major-scale.mp3
which uses ffmpeg to convert the midi file into an mp3 which I took from this blog.
Convert Midi to mp3 in Ubuntu - http://stepsilon.com/ubuntu/convert-midi-mp3-ubuntu
so you still have the midi file which you can manually delete or add this second block of code to delete the midi file.
remove = "rm *.midi"
print(remove)
prem = os.popen(remove)
print(prem.read())
print(prem.close())
Is there an easy way of writing a mp3 file instead of midi?