-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
291 lines (267 loc) · 14.5 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'''
This program is written by Chiiko-H.
https://github.com/Chiiko-H
'''
import tkinter as tk
import pyaudio
import wave
import subprocess
import os
import time
import datetime
import threading
import pydub
pydub.AudioSegment.ffmpeg = "./ffmpeg/bin"
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master.geometry("500x230")
self.master.title("音声録音アプリ")
data = '''R0lGODlhAAEAAfQAAAAAAN1VVd5UVN9UVN5VVd9VVdxWVt1WVt1XV95WVt9WVt5X
V99XV95YWOBVVeFVVeBWVuFWVuBXV+FXV+JWVuBYWAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAACH5BAEAAAAAIf8LSW1hZ2VNYWdpY2sNZ2FtbWE9MC40NTQ1
NQAh/wtYTVAgRGF0YVhNUDw/eHBhY2tldCBiZWdpbj0n77u/JyBpZD0nVzVNME1w
Q2VoaUh6cmVTek5UY3prYzlkJz8+Cjx4OnhtcG1ldGEgeG1sbnM6eD0nYWRvYmU6
bnM6bWV0YS8nIHg6eG1wdGs9J0ltYWdlOjpFeGlmVG9vbCAxMi4xNic+CjxyZGY6
UkRGIHhtbG5zOnJkZj0naHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYt
c3ludGF4LW5zIyc+CgogPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9JycKICB4
bWxuczp0aWZmPSdodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyc+CiAgPHRp
ZmY6T3JpZW50YXRpb24+MTwvdGlmZjpPcmllbnRhdGlvbj4KIDwvcmRmOkRlc2Ny
aXB0aW9uPgo8L3JkZjpSREY+CjwveDp4bXBtZXRhPgogICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAog
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgIAo8P3hwYWNrZXQgZW5kPSd3Jz8+Af/+/fz7+vn49/b19PPy8fDv7u3s
6+rp6Ofm5eTj4uHg397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kk0fi/DJCfXOOcMr4CmBI0XlYA1V
pazbKLOISN3EZ5Z01COqBITLLgbSj/jpJREn6MoMpWGVm9zCAoO5wRsSE3OzyuXW
muaz5ukhlmTTkdqy6cXFcdMZh1jmN2mRvHEOKpXm1Ibn0sm4R7DzFXa03Ts7scV5
lk4U9qTOBfPpu3ryMz/X+Ccor4HNYgkTVwXlVCsJKVB2LaOhXAQGRLUZjomSqaIN
XSjCLgT6EH4C5Z0mAelKEjodmMSNagf9YTZNaj2qxdOkJOXjRVkavJi6kaa+bBhO
x7gvjbJUpzt1JqdeGtQW2dQsMy2qUNemVAo5qqmP5GVKd6oiqMLSb1a9KniyWrNO
chWCoPnq2CApVm/etKykowtR0QoZH7J1LlJJxVtbWBQZrHWumTiqOqaK11+lkCFV
6Gs49KqIZgoWK+JcDl8P60lUPJOxIT2DLca0A8iWtTxgsQ4jDWvZvurABkmogWgN
2VkAhAAAOw==
'''
self.master.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(data=data))
# parameter
self.INTERVAL = 10
self.start_time = 0
self.start_flag = False
self.after_id = 0
self.REC_DIR = "C:\\rec"
self.rec_data = []
self.rec_status = False
# initialize
def initialize():
recTimer.configure(text='00:00')
fileName.configure(text='')
audioCheck()
# audio
def audioCheck():
#print('audio check')
py = pyaudio.PyAudio()
audioList = py.get_device_info_by_index(1)
# print(audioList['name'])
if 'マイク' in audioList['name']:
self.value = "マイク接続済み"
recBtn["state"] = tk.NORMAL
else:
self.value = "マイクが接続していません\n接続してから起動してください"
recBtn["state"] = tk.DISABLED
# recording
def audiostart():
# print('audio start')
audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16,
rate=44100,
channels=1,
input_device_index=1,
input=True,
frames_per_buffer=1024)
return audio, stream
def audiostop(audio, stream):
# print('audio stop')
stream.stop_stream()
stream.close()
audio.terminate()
def read_plot_data(stream):
data = stream.read(1024)
return data
def rec_exec(data):
# print('save')
rec_data = data
dt_now = datetime.datetime.now()
filename = dt_now.strftime('%Y%m%d_%H%M%S')
file_path = self.REC_DIR + "\\" + filename + ".wav"
# 録音データをファイルに保存
wave_f = wave.open(file_path, 'wb')
wave_f.setnchannels(1)
wave_f.setsampwidth(2)
wave_f.setframerate(44100)
wave_f.writeframes(b''.join(rec_data))
wave_f.close()
# wav -> mp3
sound = pydub.AudioSegment.from_wav(file_path)
sound.export(self.REC_DIR + "\\" + filename + ".mp3", format="mp3")
mp3_filename = filename + ".mp3"
fileName.configure(text=mp3_filename)
# delete wav file
os.remove(file_path)
def recording():
# print('recording....')
(audio, stream) = audiostart()
rec_data = []
# print("Start")
while True:
try:
data = read_plot_data(stream)
rec_data.append(data)
if (self.rec_status == False):
# print('try stop')
break
except KeyboardInterrupt:
# print("KeyboardInterrupt")
break
audiostop(audio, stream)
rec_exec(rec_data)
# action
def openExplorer():
# print('open explorer')
if not os.path.exists(self.REC_DIR):
os.makedirs(self.REC_DIR)
subprocess.Popen(['explorer', self.REC_DIR], shell=True)
def convert(sec):
minits = sec // 60
second = sec % 60
milli_sec = (second - int(second)) * 1000
hour = minits // 60
min = minits % 60
return f"{'{0:02}'.format(int(min))}:{'{0:02}'.format(int(second))}"
def update_time():
self.after_id = self.master.after(self.INTERVAL, update_time)
now_time = time.time()
elapsed_time = now_time - self.start_time
elapsed_time_str = convert(elapsed_time)
recTimer.config(text=elapsed_time_str)
def recStart():
# print('rec start')
self.rec_status = True
recBtn.configure(text="■", command=recStop)
openFolder["state"] = tk.DISABLED
if not self.start_flag:
initialize()
self.start_flag = True
self.start_time = time.time()
self.after_id = self.master.after(self.INTERVAL, update_time)
thread1 = threading.Thread(target=recording)
thread1.start()
def recStop():
# print('rec stop')
self.rec_status = False
openFolder["state"] = tk.NORMAL
recBtn.configure(text="●", command=recStart)
if self.start_flag:
self.master.after_cancel(self.after_id)
self.start_flag = False
# button
recBtn = tk.Button(self.master, text="●", font=("", 18), width=5, height=2, fg='red', command=recStart)
openFolder = tk.Button(self.master, text='開く', font=("", 16), width=10, height=2, command=openExplorer)
#self.img = tk.PhotoImage(file="./img/renew.png")
#renewBtn = tk.Button(self.master, image=self.img, command=initialize, compound="top")
# label
audioCheck()
self.micStatus = tk.Label(self.master, borderwidth=2, text=self.value, relief="ridge", font=("", 16), width=25,
height=2)
recTimer = tk.Label(borderwidth=2, relief="ridge", text="00:00", font=("", 16), width=10, height=2)
fileName = tk.Label(font=("", 10), width=18, height=2)
# arrange
recBtn.place(x=10, y=120)#
openFolder.place(x=350, y=120)
# renewBtn.place(x=300, y=25)
self.micStatus.place(x=10, y=20)
recTimer.place(x=350, y=20)
fileName.place(x=150, y=120)
if __name__ == "__main__":
root = tk.Tk()
app = Application(master=root)
app.mainloop()