Exemple #1
0
def load(filename: str) -> panscore.Score:
    """
    打开nn文件,返回panscore.Score对象
    """
    with open(filename, "r", encoding="utf8") as file:
        line = file.readline().split(" ")
        tempo = float(line[0])
        beats = (int(line[1]), int(line[2]))
        file.readline()
        note = []
        for i in file.readlines():
            line = i.strip(" \n").split(" ")
            hanzi = line[0]
            pinyin = line[1]
            start = int(line[2]) * 60
            length = int(line[3]) * 60
            notenum = 83 - int(line[4])
            note += [
                panscore.Note(start=start,
                              length=length,
                              notenum=notenum,
                              lyric=pinyin)
            ]
            #TODO:支持选择汉字还是拼音
    return panscore.Score(track=[panscore.Track(note=note)])
    pass
Exemple #2
0
def load(filename: str) -> panscore.Score:
    #打开文件,返回panscore.Score对象
    mf = mido.MidiFile(filename)
    tracks = []
    for mtr in mf.tracks:
        notes = []
        tick = 0
        lyric = panscore.defaultlyric
        note: Dict[int, Tuple[str, int]] = {}  #{音高:(歌词,开始时间)}
        for signal in mtr:
            tick += signal.time
            if (signal.type == "note_on"):  #音符开始事件
                #将新音符注册到键位-音符字典中
                note[signal.note] = (lyric, tick)
            elif (signal.type == "note_off"):
                #从键位-音符字典中找到音符,并弹出
                if (signal.note in note):  #音符结束事件
                    n = note.pop(signal.note)
                    notes.append(
                        panscore.Note(
                            start=int(n[1] * 480 / mf.ticks_per_beat),
                            length=int(
                                (tick - n[1]) * 480 / mf.ticks_per_beat),
                            notenum=signal.note,
                            lyric=n[0]))
                    lyric = panscore.defaultlyric
            elif (signal.type == "lyrics"):  #歌词事件
                lyric = signal.text
        tracks.append(panscore.Track(name=mtr.name, note=notes))
    return panscore.Score(track=tracks)
Exemple #3
0
def load(filename: str) -> panscore.Score:
    #打开文件,返回panscore.Score对象
    #由于编码不确定,先用二进制打开文件
    with open(filename, 'rb') as f:
        file = f.read()

    #读取编码
    if (b"Charset=UTF-8" in file):
        encoding = "utf-8"
    else:
        encoding = "shift-JIS"
    #分块
    blocks = []
    block = []
    for line in file.split(b"\n"):
        line = line.strip(b"\r")
        #逐行解码
        try:
            linestr = str(line, encoding=encoding)
        except UnicodeDecodeError:
            #如果某行编码与其他行不同,则尝试用各种编码解析
            for i in ["gbk", "utf-8", "shift-JIS"]:
                try:
                    linestr = str(line, encoding=i)
                    break
                except UnicodeDecodeError:
                    pass
            else:
                linestr = ""
        if (linestr.startswith("[")):
            blocks.append(block)
            block = []
        block.append(linestr)
    #读文件头
    """
    fileproperties={}
    for line in blocks[2]:
        if("=" in line):
            [key,value]=line.split("=")
            if(value!=""):
                fileproperties[key]=ustvaluetyper(key,value)
    tempo=fileproperties.pop("Tempo",120.0)
    """
    #读音符
    notes = []
    time = 0
    for block in blocks[3:]:
        noteproperties = {}
        length = 0
        notenum = 60
        lyric = "R"
        for line in block:
            if ("=" in line):
                [key, value] = line.split("=")
                if (key == "Length"):
                    length = int(value)
                elif (key == "NoteNum"):
                    notenum = int(value)
                elif (key == "Lyric"):
                    lyric = value.strip(" \n")
        if (not (lyric in {"R", "r"})):
            notes.append(
                panscore.Note(start=time,
                              length=length,
                              notenum=notenum,
                              lyric=lyric))
        time += length
    return panscore.Score(track=[panscore.Track(note=notes)])
    #TODO
    pass
Exemple #4
0
def load(filename: str) -> panscore.Score:
    #打开文件,返回panscore.Score对象
    with open(filename, "rb") as file:
        #文件头
        file.read(48)
        #读曲速标记
        tempo = []
        for i in range(0, skreadint(file)):
            tempo += [(skreadint(file), skreadint(file) / 100)]
        file.read(4)
        #读节拍标记
        beats = []
        for i in range(0, skreadint(file)):
            beats += [(skreadint(file), skreadint(file), skreadint(file))]
        tracks = []
        for i in range(0, skreadint(file)):  #读音轨
            notes = []
            tracktype = skreadint(file)  #合成音轨0,伴奏1
            if (tracktype == 0):  #合成音轨
                trackname = skreadstr(file)
                mute = (file.read(1) == b'\x01')
                solo = (file.read(1) == b'\x01')
                volume = skreadint(file)
                file.read(4)  #左右声道平衡
                file.read(4)  #区段占用空间
                for i in range(0, skreadint(file)):  #读区段
                    segstart = skreadint(file)
                    seglength = skreadint(file)
                    segname = skreadstr(file)
                    singer = skreadstr(file)
                    file.read(4)  #音符占用空间
                    for i in range(0, skreadint(file)):  #读音符
                        start = skreadint(file)
                        length = skreadint(file)
                        notenum = 115 - skreadint(file)
                        skreadint(file)  #颤音长度
                        pinyin = skreadstr(file)
                        hanzi = skreadstr(file)
                        file.read(1)
                        skreadbytes(file)  #数据块1,包含颤音幅度线和颤音速度线
                        skreadbytes(file)  #未知数据块2
                        file.read(18)  #音素
                        skreadint(file)  #弯曲深度
                        skreadint(file)  #弯曲长度
                        skreadint(file)  #尾部滑音长度
                        skreadint(file)  #头部滑音长度
                        skreadint(file)  #音阶
                        skreadstr(file)  #交叉拼音
                        skreadint(file)  #交叉音阶
                        notes.append(
                            panscore.Note(start=start + segstart,
                                          length=length,
                                          notenum=notenum,
                                          lyric=pinyin))
                    #TODO:支持选择汉字还是拼音
                    #以下是区段参数
                    skreadbytes(file)  #音量Volume,取值范围[0,256]
                    skreadbytes(file)  #音调Pitch,以音分为单位,转换成midi标准的100倍,0表示按默认音调
                    skreadbytes(file)
                    skreadbytes(file)  #气声Breathness,取值范围[0,256]
                    skreadbytes(file)  #声线(性别)Gender,取值范围[0,256]
                    skreadbytes(file)
                    skreadbytes(file)
                tracks.append(panscore.Track(name=trackname, note=notes))
        #TODO
    #TODO
    return panscore.Score(track=tracks)
    pass
Exemple #5
0
def parsetrack(trackobject)->panscore.Track:
    #解析音轨对象
    return panscore.Track(name=trackobject.Name,
                     note=[parsenote(i) for i in trackobject.NoteList.GetEnumerator()])