Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions daobei.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from musing import MusingRhythm,Clip
r = '.. - -|--- -|- - - -|-- - -|-- - -|- - - -|-'
mr = MusingRhythm(r,unit_time=1.8)
m = ['G5','B5',\
'C6','E6' , \
'F6','G6','B6','C7',
'G6','B6','C7',\
'F6','E6','F6', \
'D6','C6','B5','D6',\
'C6']
# 3

# for i in range(1,7):
# Clip(mr,m,offset=i).play()
# c = Clip(mr,m,offset=3)
c = Clip(mr,m)
c.init_last_time(start = 0.9)
c.add_column_chords("G4M",beat_time=1.8,velocity=15)
c.add_column_chords("C5M",beat_time=1.8,velocity=15)
c.add_column_chords("F5M",beat_time=1.8,velocity=15)
c.add_column_chords("G5M",beat_time=1.8,velocity=15)
c.add_column_chords("F5M",beat_time=1.8,velocity=15)
c.add_column_chords("D5M",beat_time=1.8,velocity=15)
c.add_column_chords("C5M",beat_time=1.8,velocity=15)
c.play()
35 changes: 24 additions & 11 deletions musing/musing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
'dim7', 'maj7', 'aug7', 'sus2', 'sus4']
MINUTE = 60.00000
DEFAULT_MIDI_FILE = 'midi_data/40126.mid'
STRONG = 36
SUB_STRONG = 27
WEAK = 18
DEFAULT_VELOCITY = 36

VELOCITY_DICT = {
1:[36],
2:[36,18],
3:[36,18,18],
4:[36,18,27,18],
5:[36,18,27,18,18],
6:[36,18,18,27,18,18],
}

class Musing(object):
'''
Expand Down Expand Up @@ -349,7 +359,10 @@ class Clip(object):

last_time = 0.0

def __init__(self, rhythms=False, notes=False, velocity_list=[], start_time=.0):
def init_last_time(self,start = 0.0):
self.last_time = start

def __init__(self, rhythms=False, notes=False, velocity_list=[], start_time=.0, default_velocity=True,offset=0):

self.midi = Musing()

Expand All @@ -367,7 +380,7 @@ def __init__(self, rhythms=False, notes=False, velocity_list=[], start_time=.0):

for rhythm, note in zip(rhythms, notes):
note = pretty_midi.Note(
velocity=DEFAULT_VELOCITY, pitch=pretty_midi.note_name_to_number(note), start=start_time + rhythm[0], end=start_time + rhythm[1])
velocity=rhythm[2] if default_velocity else DEFAULT_VELOCITY, pitch=pretty_midi.note_name_to_number(note)-offset, start=start_time + rhythm[0], end=start_time + rhythm[1])
self.note_list.append(note)

def add_notes(self, notes=False, start_time=.0):
Expand All @@ -391,15 +404,14 @@ def play(self):
def last(self,time):
self.last_time += time

def add_column_chords(self,note_list,beat_time=1.0,p='5'):
def add_column_chords(self,note_list,beat_time=1.0,p='5',velocity=SUB_STRONG,repeat=2):

if isinstance(note_list,basestring):

note_list = Musing.parse_chord(note_list)

for note in note_list:

self.note_list.append(pretty_midi.Note( velocity=DEFAULT_VELOCITY, pitch=pretty_midi.note_name_to_number("%s%s" % (note,p)), start= self.last_time, end=self.last_time + beat_time))
for repeat_div in range(0,repeat):
for note in note_list:
self.note_list.append(pretty_midi.Note( velocity=velocity-3*repeat_div, pitch=pretty_midi.note_name_to_number("%s%s" % (note,p)), start= self.last_time+beat_time/float(repeat)*repeat_div, end=self.last_time + beat_time/float(repeat)*repeat_div))

elif isinstance(note_list,list):

Expand Down Expand Up @@ -468,6 +480,7 @@ def __repr__(self):

def __iter__(self):
return self.time_list.__iter__()

def __len__(self):
return len(self.time_list)

Expand Down Expand Up @@ -498,13 +511,13 @@ def parse_rhythm_bar(self, rhythm_bar, now_time, unit_time=.5):
s_list = []

unit = unit_time / float(t)

for r in r_list:
for index,r in enumerate(r_list):

if '-' in r:
start_time = now_time[0]
last_time = len(r) * unit
m_list.append((start_time, last_time + start_time))
m_list.append((start_time, last_time + start_time,VELOCITY_DICT.get(t,[27,27,27,27,27,27,27,27,27])[index]))
now_time[0] += last_time
elif '.' in r:
start_time = now_time[0]
Expand Down