-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiano.py
More file actions
119 lines (115 loc) · 4.51 KB
/
piano.py
File metadata and controls
119 lines (115 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from pydub import AudioSegment
import os
def musicmake(melody):
x = len(melody)
tune = ["" for i in range (500)]
tunen = -1
flag = 0
for i in range(0,x):
if (melody[i] == "(" or melody[i] == ")" or melody[i].isdigit()) and flag == 0:
tunen = tunen + 1
if melody[i] == "(" or melody[i] == ")":
flag = 1
elif melody[i].isdigit() and flag == 1:
flag = 0
tune[tunen] = tune[tunen] + melody[i]
with open(file=os.path.abspath(os.path.join(os.path.dirname(__file__), 'piano', 'number.txt')), mode='r', encoding="utf-8") as data:
numbe = int(data.readline())
numbe = numbe + 1
with open(file=os.path.abspath(os.path.join(os.path.dirname(__file__), 'piano', 'number.txt')), mode='w', encoding="utf-8") as data:
data.write(str(numbe))
for i in range(0,tunen+1):
height = 5
length = 3
number = -1
numn = 0
halftune = 0
long = len(tune[i])
tori = tune[i]
tfinal = ""
for j in range (long):
if tori[j]==")":
height = height + 1
elif tori[j]=="(":
height = height - 1
elif tori[j].isdigit():
if int(tori[j])>=8:
numn = 2
else:
number = int(tori[j])
numn = numn + 1
elif tori[j]=="#":
if number == 3 or number == 7 or number == 0:
halftune = 2
else:
if number == 0 and height != 5:
return -1
number = number + 1
halftune = halftune + 1
elif tori[j]=="b":
if number == 1 or number == 4 or number == 0:
halftune = 2
else:
halftune = halftune + 1
elif tori[j]=="+":
length = length + 1
elif tori[j]=="-":
length = length - 1
else:
print("Input Error!")
return -1
if height > 8 or height < 2 or length > 5 or length < 1 or numn != 1 or halftune > 1:
print("Input Error!")
return -1
if number == 1:
tfinal = "C"
elif number == 2:
tfinal = "D"
elif number == 3:
tfinal = "E"
elif number == 4:
tfinal = "F"
elif number == 5:
tfinal = "G"
elif number == 6:
tfinal = "A"
elif number == 7:
tfinal = "B"
elif number == 0:
tfinal = "0"
if halftune == 1:
tfinal = tfinal + "b"
if number!=0 :
tfinal = tfinal + str(height)
if length == 2:
tfinal = tfinal + "_4"
elif length == 3:
tfinal = tfinal + "_8"
elif length == 4:
tfinal = tfinal + "_16"
elif length == 5:
tfinal = tfinal + "_32"
print(tfinal)
new2 = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data', 'voices', tfinal + ".mp3"))
print(new2)
sound2 = AudioSegment.from_mp3(new2)
if i == 0:
if tunen == 0:
sound2.export(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data', 'voices', str(numbe) + ".mp3")), format="mp3")
print("合成完成")
return numbe
else:
sound2.export(os.path.abspath(os.path.join(os.path.dirname(__file__), "piano" , str(numbe) + "_0.mp3")), format="mp3")
else:
new1=os.path.abspath(os.path.join(os.path.dirname(__file__), "piano" , str(numbe) + "_" + str(i-1) + ".mp3"))
sound1=AudioSegment.from_mp3(new1)
sound = sound1 + sound2
if i==tunen:
sound.export(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data', 'voices', str(numbe) + ".mp3")), format="mp3")
os.remove(new1)
print("合成完成")
return numbe
else:
os.path.abspath(os.path.join(os.path.dirname(__file__), "piano" , str(numbe) + "_" + str(i) + ".mp3"))
sound.export(os.path.abspath(os.path.join(os.path.dirname(__file__), "piano" , str(numbe) + "_" + str(i) + ".mp3")), format="mp3")
os.remove(new1)