话说我自己维护的时候发现了一个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)