midi-writer / app.py
johann22's picture
Update app.py
e6235c2 verified
raw
history blame
4.12 kB
from midiutil import MIDIFile
from musicpy import *
from musicpy.daw import *
import gradio as gr
def write_song():
guitar = (C('CM7', 3, 1/4, 1/8)^2 |
C('G7sus', 2, 1/4, 1/8)^2 |
C('A7sus', 2, 1/4, 1/8)^2 |
C('Em7', 2, 1/4, 1/8)^2 |
C('FM7', 2, 1/4, 1/8)^2 |
C('CM7', 3, 1/4, 1/8)@1 |
C('AbM7', 2, 1/4, 1/8)^2 |
C('G7sus', 2, 1/4, 1/8)^2) * 2
write(guitar, name="tmp.mid", bpm=100, instrument=25)
return "tmp.mid","tmp.mid"
def write_song2():
current_daw = daw(3)
current_daw.load(0, )
current_daw.load(1, 'Arachno.sf2')
bass1 = chord('A1') % (1,) * 4
bass2 = (chord('A1, A2') % (1/16, 1/8) * 4 |
chord('G1, G2') % (1/16, 1/8) * 4 |
chord('F1, F2') % (1/16, 1/8) * 4 |
chord('D1, D2') % (1/16, 1/8) * 4)
guitar1 = (C('Am/A', 3) @ [1,2,3,4,2,3,4,3] % (1, 1/8) |
C('G/A', 3) @ [1,2,3,4,2,3,4,3] % (1, 1/8) |
C('F/A', 3) @ [1,3,1.1,2.1,3,1.1,2.1,1.1] % (1, 1/8) |
C('Dm/A', 3) @ [1,3,1.1,2.1,3,1.1,2.1,1.1] % (1, 1/8))
guitar2 = (C('Am', 4) @ [2,3,1.1,2.1,3,1.1,2.1,1.1] % (1, 1/8) |
C('G', 4) @ [2,3,1.1,2.1,3,1.1,2.1,1.1] % (1, 1/8) |
C('F', 4) @ [2,3,1.1,2.1,3,1.1,2.1,1.1] % (1, 1/8) |
C('Dm', 4) @ [2,3,1.1,2.1,3,1.1,2.1,1.1] % (1, 1/8))
drum1 = drum('S[l:.8; i:.; r:4], S[l:.16; i:.], S[l:.8; i:.], S[l:.16; i:.], S[l:.8; i:.], S[l:.8; i:.]').notes
drum2 = drum('H, a:.16;., r:16').notes
drum1 &= drum2
drum3 = drum('K, H, S, H, K[l:.16; i:.], K[l:.8; i:.], H[l:.16; i:.], S[l:.8; i:.], H[l:.8; i:.]').notes
synth_pad1 = chord('E5, G5, C6') % (1,) | chord('E5, B5, D6') % (1,) | chord('E5, A5, C6') % (1,) | chord('D5, F5, A5') % (1,)
synth_pad1.set_volume(80)
bass_part = (bass1 * 2 | bass2 * 4) + 3
guitar_part = (guitar1 * 2 | guitar2 * 4) + 3
drum_part = (drum1 * 4 | drum3 * 16)
synth_pad_part = (synth_pad1 * 4) + 3
result = P(tracks=[bass_part, guitar_part, drum_part, synth_pad_part],
instruments=[34, 3, 1, 51],
channels=[0, 1, 9, 2],
daw_channels=[1, 0, 1, 1],
start_times=[0, 0, 4, 8],
bpm=165)
#current_daw.play(result)
current_daw.export(result, filename='song.wav') # export the piece type current_song to
# a wav file named "my first song.wav"
'''
export(obj,
mode='wav',
action='export',
filename='song.wav',
channel_num=0,
bpm=None,
length=None,
extra_length=None,
track_lengths=None,
track_extra_lengths=None,
export_args={},
show_msg=False,
soundfont_args=None)
'''
return 'song.wav','song.wav'
def write_song1():
w = (C('Cmadd9,add11',5) @ [1,3,1.1,4,2.1,5,2.1,4] % (1/2,1/8) * 2 |
C('A#add9,add11',4) @ [1,3,1.1,4,2.1,5,2.1,4] % (1/2,1/8) * 2 |
C('G#add9',4) @ [1,3,1.1,4,2.1,4,1.1,3] % (1/2,1/8) * 2 |
C('A#add9',4) @ [1,3,1.1,4,2.1,4,1.1,3] % (1/2,1/8) * 2)
w2 = translate('C2;C3[l:2], i:2, A#1;A#2[l:2], i:2, G#1;G#2[l:2], i:2, A#1;A#2[l:2]')
# Write the song as a MIDI file
write(w2,
bpm=100,
channel=0,
name='funky_song.mid',
save_as_file=True)
return "funky_song.mid"
def make_mid():
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
# automatically created)
MyMIDI.addTempo(track,time, tempo)
for pitch in degrees:
MyMIDI.addNote(track, channel, pitch, time, duration, volume)
time = time + 1
with open("major-scale.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
return "major-scale.mid"
with gr.Blocks() as iface:
btn=gr.Button()
outp=gr.Files()
aud=gr.Audio()
btn.click(write_song2,None,[outp,aud])
iface.launch()