Python音乐制作-紧急修复版v1.2

话说我自己维护的时候发现了一个bug,现在修复一下^_^ 

具体的bug内容见下

-1出现了当对本位符(即大调的1音)进行操作时的错误现象

修复内容

-1修复bug

# coding:utf-8

"""

----------------------------------
*    author: Unconquerable       *
----------------------------------

a great tool to convert melody text to music.

"""

class sound(object):
    high=4

    def __init__(self,high=4):
        self.high=high
    def __del__(self):
        del self.high
    def __str__(self):
        return ""

    def upLoud(self,up=1):
        self.high+=up
    def downLoud(self,down=1):
        self.high-=down
    def absoluteLoud(self,abv=0):
        self.high+=abv
    absLoud=absoluteLoud
    def resetLoud(self,value=4):
        self.high=value
    init=resetLoud

    @property
    def toString(self):
        return self.__str__()
    real=toString

    def plus(self):
        ...

    def plus_(self,num):
        ...

    def nplus(self):
        ...


class C(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "c"+str(self.high)

    def plus(self):
        return AddC(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return B(self.high-1)



class AddC(sound):
    high = 4
    def __init__(self,high):
        super().__init__(high)
    def __str__(self):
        return "c#"+str(self.high)
    def plus(self):
        return D(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return C(self.high)

class D(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "d"+str(self.high)
    def plus(self):
        return AddD(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return AddC(self.high)

class AddD(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "d#"+str(self.high)
    def plus(self):
        return E(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return D(self.high)

class E(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "e"+str(self.high)
    def plus(self):
        return F(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return AddD(self.high)

class F(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "f"+str(self.high)
    def plus(self):
        return AddF(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return E(self.high)

class AddF(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "f#"+str(self.high)
    def plus(self):
        return G(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return F(self.high)

class G(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "g"+str(self.high)
    def plus(self):
        return AddG(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return AddF(self.high)

class AddG(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "g#"+str(self.high)
    def plus(self):
        return A(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return G(self.high)

class A(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "a"+str(self.high)
    def plus(self):
        return AddA(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return AddG(self.high)

class AddA(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "a#"+str(self.high)
    def plus(self):
        return B(self.high)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return A(self.high)

class B(sound):
    high = 4
    def __init__(self,high=4):
        super().__init__(high=high)
    def __str__(self):
        return "b"+str(self.high)
    def plus(self):
        return C(self.high+1)

    def plus_(self, num):
        if num == 0: return self
        if num < 0:
            return self.nplus().plus_(num + 1)
        if num > 0:
            return self.plus().plus_(num - 1)

    def nplus(self):
        return AddA(self.high)

class NULL(sound):
    def __str__(self):
        return "r"
class time(object):
    real = 0
    def __init__(self,time=4):
        self.real= 4/time

class note(object):
    sound_ = sound()
    time_ = time()

    def __init__(self,sd,t):
        self.soundSet(sd)
        self.timeSet(t)

    def soundSet(self,sd):
        if isinstance(sd,str):
            nd=sd[:-1].upper()
            dct={"C":C,"C#":AddC,"D":D,"D#":AddD,"E":E,"F":F,
                 "F#":AddF,"G":G,"G#":AddG,"A":A,"A#":AddA,"B":B,"R":NULL}
            self.sound_=dct[nd](int(sd[-1]))
        if isinstance(sd,sound):
            self.sound_=sd
        if isinstance(sd,tuple):
            nd = sd[:-1].upper()
            dct = {"C": C, "C#": AddC, "D": D, "D#": AddD, "E": E, "F": F,
                   "F#": AddF, "G": G, "G#": AddG, "A": A, "A#": AddA, "B": B, "R": NULL}
            self.sound_ = dct[nd](int(sd[-1]))

    def timeSet(self,t):
        if isinstance(t,(int,float)):
            self.time_=time(t)
        if isinstance(t,time):
            self.time_=t

    @property
    def out(self):
        return self.sound_.toString,self.time_.real
class wav:
    melody=C(4)
    sound=[]
    staticTime=1
    def __init__(self,text=None,split=' '):
        if text:
            self.load(text=text,split=split)
    def __del__(self):
        pass

    @property
    def getMelody(self):
        return self.melody

    @property
    def getSound(self):
        return self.sound

    @property
    def getTime(self):
        return self.staticTime

    def setTime(self,time):
        self.staticTime=time

    def setMelody(self,melody):
        self.melody=melody
    def loadOneNote(self,n):
        if isinstance(n,note):
            self.sound.append(n.out)
        if isinstance(n,str):
            n=n.strip()
            if n[0]=="0":
                t=self.staticTime
                for i in n[1:]:
                    if i == "-":  # 延音符号
                        t += self.staticTime
                    if i == "_":  # 半音符号
                        t /= 2
                    # (!)不要让延音符号和半音符号同时出现
                    if i == ".":  # 附点音符
                        t += t / 2

                self.sound.append(("r",4/t))
                return
            #简谱
            t=self.staticTime
            dct={
                1:0,
                2:2,
                3:4,
                4:5,
                5:7,
                6:9,
                7:11
            }
            l=self.melody.plus_(dct[int(n[0])])
            a=0

            for i in n[1:]:
                if i=="#":#升音符号
                    l=l.plus()
                if i=="&":#降音符号
                    l=l.nplus()
                if i=="-":#延音符号
                    t+=self.staticTime
                if i=="_":#半音符号
                    t/=2
                # (!)不要让延音符号和半音符号同时出现
                if i==".":#附点音符
                    t+=t/2
                if i=="+":#升音
                    l.upLoud(1)
                    if dct[int(n[0])] == 0:
                        a-=1
                if i=="!":#降音
                    l.downLoud(1)
                    if dct[int(n[0])]==0:
                        a+=1

            mixed=note(l,t).out
            self.sound.append(mixed)

            del mixed,t,l
            self.melody.absLoud(a)

    def load(self,text,split=''):
        if split!='':
            for i in text.split(split):
                self.loadOneNote(i)
        else:
            tp=""
            for i in text:
                if i.isnumeric():
                    if tp.strip() != "":
                        self.loadOneNote(tp.strip())
                    tp=""
                tp+=i

    def make_wav(self,module="pysynth",bpm=120,transpose=0,pause=.05,boost=1.1,
                 repeat=0,fn="out.wav", silent=False):
        try:
            __import__(module.lower()).make_wav(song=self.sound,bpm=bpm,transpose=transpose,
                                    pause=pause,boost=boost,repeat=repeat,
                                    fn=fn, silent=silent)
        except TypeError:
            try:
                __import__(module.lower()).make_wav(song=self.sound,bpm=bpm,repeat=repeat,fn=fn)
            except TypeError:
                __import__(module.lower()).make_wav(song=self.sound*(repeat+1), fn=fn)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unconquerable p

给点吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值