2783030 0

 

Project Progressing... 

from music21 import note, stream
from decorators import get_halftones, get_basenote
from random import randint


# 把作曲的N多方法封为一类
class Compose():
    # 初始化
    def __init__ (self):
        self.music = []
        self.time_signature = [4,4]
        numerator = self.time_signature[0]
        denominator = self.time_signature[1]
        self.default_bar_QL = numerator / denominator * 4 
        self.key_signature = 'C_maj'
        self.tempo = 88
 
    # 随机作曲方法,装饰器get_halftones会把不是数字的音乐范围转化为数字
    @get_halftones
    @get_basenote
    def random_bars (self, **kw):
        # 实例变量赋值
        self.music_length = kw['music_length']
        self.base_note = kw['base_note']
        self.compose_range = kw['compose_range']

        # 逐个小节生成音符
        for i in range(self.music_length):
            self.music.append([])

            # 生成当前小节的音符数量
            note_num = randint(1,8)
            
            # 计算每小节的 QL