forked from denjhang/m2v-conv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2v.py
160 lines (141 loc) · 5.53 KB
/
m2v.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import configparser
import os
import re
class mv2_conv:
config_file = 'settings.ini'
input_file_list = []
tone_list = []
channel_list = []
p_str = 'v(?P<volume>[0-9]+)'
o_str = 'o(?P<pitch>[0-9]+)'
t_str = '@(?P<tone>[0-9]+)'
c_str = "'[A-Z][s]?(?P<channel>[0-9]+)"
conf = 0
channel = 'F'
tmax = 0
tmin = 0
vmax = 0
vmin = 0
mode = 0
out_name = '-out'
vrate = 0.1
pitch_inc = 0
def __init__(self):
self.conf = configparser.ConfigParser()
self.conf.read('settings.ini', encoding='utf-8')
items = self.conf.items('m2v')
for x, y in items:
if x == 'tmax':
self.tmax = int(y)
elif x == 'tmin':
self.tmin = int(y)
elif x == 'mode':
self.mode = int(y)
elif x == 'vrate':
self.vrate = float(y)
elif x == 'outname':
self.out_name = y
elif x == 'channel':
self.channel = y
for x in os.listdir(os.getcwd()):
if x.endswith('.gwi') and self.out_name not in x:
self.input_file_list.append(x)
if self.mode == 0:
self.automode()
else:
self.mnmode()
def normalize(self, value, max_val, min_val):
return (value - min_val) / (max_val - min_val)
def plus(self, matched):
value = int(matched.group('volume'))
return 'v' + str(round(self.normalize(value, self.vmax, self.vmin) * (self.tmax - self.tmin) + self.tmin))
def add_pitch(self, matched):
value = int(matched.group('pitch'))
return 'o' + str(value + self.pitch_inc)
def modi_tone(self, matched):
value = int(matched.group('tone'))
return '@' + str(self.tone_list.pop(0))
def modi_channel(self, matched):
value = int(matched.group('channel'))
return '\'' + self.channel + str(self.channel_list.pop(0))
def ajust_volume(self):
for input_file in self.input_file_list:
with open(input_file, 'r', encoding='utf8') as f:
data = f.read()
pattern = re.compile(self.p_str)
result = pattern.findall(data)
self.vmax = max([int(x) for x in result])
self.vmin = min([int(x) for x in result])
output_data = re.sub(self.p_str, self.plus, data)
output_file = input_file[:-4] + self.out_name + '.gwi'
with open(output_file, 'w', encoding='utf8') as f:
f.write(output_data)
print('Output to ' + output_file)
def ajust_pitch(self):
self.pitch_inc = int(input('Input pitch increment:'))
for input_file in self.input_file_list:
with open(input_file, 'r', encoding='utf8') as f:
data = f.read()
output_data = re.sub(self.o_str, self.add_pitch, data)
output_file = input_file[:-4] + self.out_name + '.gwi'
with open(output_file, 'w', encoding='utf8') as f:
f.write(output_data)
print('Output to ' + output_file)
def ajust_tone(self):
for input_file in self.input_file_list:
with open(input_file, 'r', encoding='utf8') as f:
data = f.read()
pattern = re.compile(self.t_str)
result = pattern.findall(data)
self.tone_list = [i for i in result]
print('All tones:')
for i, val in enumerate(result):
print(str(i + 1) + '. ' + val)
index = int(input('Input index(0 to exit):'))
while index != 0:
new_tone = input('Input new tone(integer)')
self.tone_list[index - 1] = int(new_tone)
index = int(input('Input index(0 to exit):'))
output_data = re.sub(self.t_str, self.modi_tone, data)
output_file = input_file[:-4] + self.out_name + '.gwi'
with open(output_file, 'w', encoding='utf8') as f:
f.write(output_data)
print('Output to ' + output_file)
def ajust_channel(self):
for input_file in self.input_file_list:
with open(input_file, 'r', encoding='utf8') as f:
data = f.read()
pattern = re.compile(self.c_str)
result = pattern.findall(data)
self.channel_list = [i for i in result]
print('All channels:')
for i, val in enumerate(result):
print(str(i + 1) + '. ' + val)
index = int(input('Input channel(0 to exit):'))
while index != 0:
new_channel = input('Input new channel(integer)')
self.tone_list[index - 1] = int(new_channel)
index = int(input('Input index(0 to exit):'))
output_data = re.sub(self.c_str, self.modi_channel, data)
output_file = input_file[:-4] + self.out_name + '.gwi'
with open(output_file, 'w', encoding='utf8') as f:
f.write(output_data)
print('Output to ' + output_file)
def automode(self):
self.ajust_volume()
def mnmode(self):
print('1. Adjust volume')
print('2. Adjust pitch')
print('3. Adjust tone')
print('4. Adjust channel')
option = int(input('Input your chioce:'))
if option == 1:
self.ajust_volume()
elif option == 2:
self.ajust_pitch()
elif option == 3:
self.ajust_tone()
elif option == 4:
self.ajust_channel()
if __name__ == '__main__':
m = mv2_conv()