-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
329 lines (266 loc) · 9.87 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from __future__ import print_function
import BIC.speech_segmentation as bic_seg
import tkinter
from tkinter import Button
from tkinter import Label
from tkinter import Entry
from tkinter import Scale
from tkinter import Label,PhotoImage
from PIL import Image,ImageTk
from tkinter import messagebox
from tkinter import Toplevel
from pymediainfo import MediaInfo
import re
from tkinter import Message
import threading
import pygame
import time
import os
import random
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
from tkinter import StringVar
from pydub import AudioSegment
import wave
import shutil
import sys
from sys import path
import easygui
from playsound import playsound
import pyaudio
import wave
import os.path
import soundfile as sf
top=tkinter.Tk()
top.geometry("800x450")
top.title("英语听力播放器")
def printsrceen(texts):
t=int(texts)
top.attributes("-alpha",t/100)
screenwidth = top.winfo_screenwidth()
screenheight = top.winfo_screenheight() - 100
pygame.init()
path=StringVar()
paths=StringVar()
patht=StringVar()
v=StringVar()
v1=StringVar()
def callback():#搜索本地文件
path_= askopenfilename()
return path_
def selectPath():#随机播放
folder_path="D:/音乐"
folder_list = os.listdir(folder_path)#遍历文件夹里面每个文件
list=[]
count=0
for i in folder_list:#将文件夹里的文件按顺序传提给变量i 此处区别os.walk()
if os.path.splitext(i)[1]=='.flac':#提取特定后缀文件'.***'
list.append (i)
#print(type(list))
count=count+1
#print(count)
s=random.randint(0,(count-1))#获取随机数
file=list[s]
fil=folder_path+"\\"+file
pygame.mixer.music.load(fil)
pygame.mixer.music.play(1,0)
media_info = MediaInfo.parse(fil)
data = media_info.to_json()#medio到json()这两行是获取文件的所有属性
rst=re.search('other_duration.*?(.*?)min(.*?)s.*?',data)
t=int(rst.group(0)[19:20])
r=int(rst.group(0)[-4:-2])
m=(t*60+r)*1000
musictime=str(t)+':'+str(r)
l2.config(text=file)
l3.config(text=musictime)
lbTime=tkinter.Label(top,anchor='w')
lbTime.place(x=25,y=150)
def autoclose():
for i in range(m//1000):
lbTime['text']='-{} /'.format((m//1000)-i)
time.sleep(1)
t=threading.Thread(target=autoclose)
t.start()
loopl=top.after(m,selectPath)
def printScale(text):
t=int(text)
pygame.mixer.music.set_volume(t/100)
def update_timeText():
# Get the current time, note you can change the format as you wish
current = time.strftime("%H:%M:%S")#获取当前时间
# Update the timeText Label box with the current time
timeText.configure(text=current)
# Call the update_timeText() function after 1 second
top.after(1000, update_timeText)
def play():#播放音乐
f=callback()#选择制定文件
pygame.mixer.music.load(f)
pygame.mixer.music.play()
path.set(f)
media_info = MediaInfo.parse(f)
data = media_info.to_json()#medio到json()这两行是获取文件的所有属性
rst=re.search('other_duration.*?(.*?)min(.*?)s.*?',data)
t=int(rst.group(0)[19:20])
r=int(rst.group(0)[-4:-2])
m=(t*60+r)*1000
musictime=str(t)+':'+str(r)
# l2.config(text=f)
# l3.config(text=musictime)
lbTime=tkinter.Label(top,anchor='w')
lbTime.place(x=25,y=150)
def autoclose():
for i in range(m//1000):
lbTime['text']='-{} /'.format((m//1000)-i)
time.sleep(1)
t=threading.Thread(target=autoclose)
t.start()
# loopl=top.after(m,selectPath)
def cut_the_record():#切割音频
f1=callback()#选择制定文件
#如果给定的音乐不是wav,是MP3,则mp3 转wav
if os.path.splitext(f1)[1]=='.mp3':
sound = AudioSegment.from_mp3(f1)
sound.export('%s.wav'%os.path.splitext(f1)[0],format ='wav')
f1=os.path.splitext(f1)[0]+'.wav'
easygui.msgbox("即将开始分割,可能耗费大量系统资源!但我认为这非常值得,如果您认为这样不好,请关闭该程序!", ok_button="我同意!")
sig, sample_rate = sf.read(f1)
qiegetime=(sig.shape[0]/sample_rate)*0.07
qiegetime=str(qiegetime)
easygui.msgbox(("我们预计需要大约"+qiegetime+"秒"), ok_button="嗯!")
# -*- coding:UTF-8 -*-
frame_size = 256
frame_shift = 128
sr = 16000
seg_point = bic_seg.multi_segmentation(f1, sr, frame_size, frame_shift, plot_seg=True, save_seg=True,cluster_method='bic')
print('The segmentation point for this audio file is listed (Unit: /s)', seg_point)
easygui.msgbox("我们已对您的音频完成分割处理,所有文件保存在save_audio文件夹内!", ok_button="谢谢!")
def lead_the_reading():#领读音频
# f2=callback()#选择制定文件
easygui.msgbox("我们即将开始领读,在此其他,我们需要使用您的麦克风用来录音!", ok_button="我同意!")
timed=0
a=0
b=0
HowLong = easygui.enterbox(msg='您希望播放的最短时长是(默认值为0):', title='请输入', default='', strip=True, image=None, root=None)
# HowLong=input("How long do you want to stop: ")
HowLong=int(HowLong)
print(HowLong)
# 音频存放文件夹相对路径
filedir = "D:\py_speech_seg_master\save_audio"
# 获取目录下所有文件
files = os.listdir(filedir)
# 获取目录下所有的WAV文件
wav_files = list()
for i in files:
if os.path.splitext(i)[1] == '.wav':
wav_files.append(i)
print(wav_files)
print(len(wav_files))
for i in range(len(wav_files)):
#playsound("D:\py_speech_seg_master\save_audio\\%s.wav"%i)
musicFileName = "D:\py_speech_seg_master\save_audio\\%s.wav"%i
sig, sample_rate = sf.read(musicFileName)
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 16000
#RECORD_SECONDS = sig.shape[0]/sample_rate+1
#WAVE_OUTPUT_FILENAME = "D:\py_speech_seg_master\save_audio\\record%s.wav"%i
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
timed=timed+(sig.shape[0]/sample_rate)
print(timed)
if timed > HowLong:
RECORD_SECONDS = timed+1
while a <= i:
playsound("D:\py_speech_seg_master\save_audio\\%s.wav"%a)
a=a+1
playsound("start_record.wav")
print("开始录音,请说话......")
frames = []
WAVE_OUTPUT_FILENAME = "D:\py_speech_seg_master\save_audio\\record%s.wav"%b
b=b+1
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("录音结束!")
playsound("end_record.wav")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
timed=0
def stop():
pygame.mixer.music.stop()#停止播放
top.after_cancel(loopl)
def pause():
pygame.mixer.music.pause()#暂停
def unpause():
pygame.mixer.music.unpause()#继续播放
def choosepic():#保存的路径不能有中文,若需要中文则把/换成\
path_s=askopenfilename()
paths.set(path_s)
img_open=Image.open(e1.get())
img=ImageTk.PhotoImage(img_open)
l1.config(image=img)
l1.image=img
def create():
top = Toplevel()
top.title('使用提示')
top.geometry("450x400")
t="请根据说明使用领读软件。"
msg = Message(top,text = t)
msg.config( font=('times', 24, 'italic'))
msg.place(x=0,y=0)
def loop():
top.after(60*60000,reminds)
top.after(60*59500,remind)
def loops():
selectPath()
def gettime():
t=time.strftime('%H%M%S')
s=int(t[0:2])
d=int(t[2:4])
f=int(t[4:6])
g=s*60*60+d*60+f
return g
errmsg = 'Error!'
#时间
timeText = Label(top, text="", font=("Helvetica", 15))
timeText.place(x=200,y=385)
update_timeText()
#选择文件直接播放
Button(top,text="选择文件直接播放",command=play,width=13,bg="deepskyblue").place(x=16,y=20)
Entry(top,text=path,width=25,state='readonly').place(x=120,y=20)
#选择文件切割
Button(top,text="选择文件进行切割",command=cut_the_record,width=13,bg="deepskyblue").place(x=16,y=55)
Entry(top,text=path,width=25,state='readonly').place(x=120,y=55)
#选择文件领读播放
Button(top,text="选择文件领读播放",command=lead_the_reading,width=13,bg="deepskyblue").place(x=16,y=90)
Entry(top,text=path,width=25,state='readonly').place(x=120,y=90)
#选择图片
Button(top,text='选择图片', command=choosepic,width=13,bg="deepskyblue").place(x=16,y=125)
e1=Entry(top,text=paths,state='readonly',width=25)
e1.place(x=120,y=125)
l1=Label(top)#图片放置位置
l1.place(x=320,y=0)
#暂停,继续播放,结束播放
Button(top,text="暂停",command=pause,width=7,bg="deepskyblue").place(x=16,y=260)
Button(top,text="继续播放",command=unpause,width=7,bg="deepskyblue").place(x=96,y=260)
Button(top,text="结束播放",command=stop,width=7,bg="deepskyblue").place(x=176,y=260)
#使用说明
Button(top,text="使用说明",command = create,width=10,bg="deepskyblue").place(x=20,y=385)
#音量
w1 = Scale(top, from_=0,to=100, orient="horizontal",length=75,variable=v,command=printScale,label="音量")
w1.place(x=200,y=300)
w2 = Scale(top, from_=30,to=100, orient="horizontal",length=100,variable=v1,command=printsrceen,label="透明度")
w2.place(x=20,y=300)
top.mainloop()