-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadText.py
More file actions
185 lines (158 loc) · 6.76 KB
/
ReadText.py
File metadata and controls
185 lines (158 loc) · 6.76 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
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
#-*- coding: gbk -*-
'''
Created on Nov 2, 2011
@author: Neal : njugui@gmail.com
'''
import Tkinter
import os
import winsound
import pyaudio
import wave
if (os.path.exists("not_check_list.txt")):
NotCheckList = open("not_check_list.txt", "a+")
else:
NotCheckList = open("not_check_list.txt", "w+")
words_not_check = NotCheckList.readlines()
class cReadText(object):
def __init__(self):
self.Sound_path = r'C:\chenguichun\workspace\ReadText\sound'
self.wav = 'sound.wav'
self.flag_dualword = True
self.UI()
def UI(self):
self.root = Tkinter.Tk()
self.root.title('Text Reader')
self.text_input = Tkinter.Text(master=self.root, width=30, height=10)
self.text_input.pack()
self.text_input.insert(Tkinter.END, u"ÎÒÊÇË")
button_input = Tkinter.Button(master=self.root, text='Read them!', width=30, height=3, command=self.ReadText)
button_input.pack()
self.status = Tkinter.StringVar()
self.label_status = Tkinter.Label(master=self.root, width=30,textvariable=self.status, height=3, fg='red')
self.label_status.pack()
frame0 = Tkinter.Frame(master=self.root)
self.newword = Tkinter.StringVar()
self.label_word = Tkinter.Label(master=frame0, textvariable=self.newword, width=2, height=3, font=("Helvetica", 40), wraplength=1)
self.label_word.pack(side='left')
self.button_record = Tkinter.Button(master=frame0, state=Tkinter.DISABLED, text='Record NewWord', width=20, height=1, command=lambda:self.RecordNewword(self.NewwordPath))
self.button_record.pack()
self.button_change = Tkinter.Button(master=frame0, state=Tkinter.DISABLED, text='Change NewWord', width=20, height=1, command=self.ChangeNewword)
self.button_change.pack(side='right')
frame0.pack()
self.root.mainloop()
def wordExist(self, word):
path = self.Sound_path + '\\' + word +'.wav'
if os.path.exists(path):
self.wordPath = path
print path + ' Exists!'
return True
else:
self.NewwordPath = path
return False
def ReadText(self):
text = self.text_input.get(1.0, Tkinter.END)
self.RecordNewword(self.wav)
self.wavfiles = []
i = 0
while( i <= len(text)-2):
if(self.flag_dualword and i!=len(text)-2): #Try dual words first
word = text[i]+text[i+1]
wordCode = word +'\n'
if (wordCode in words_not_check):
print 'word not check'
self.flag_dualword=False
while(1):
if(self.wordExist(word)): #Read word existed
self.wavfiles.append(self.wordPath)
i=i+2
break
else: #Record new word
self.status.set('New word found!')
self.newword.set(word)
self.button_change.config(state = Tkinter.NORMAL)
self.button_record.config(state = Tkinter.NORMAL)
self.label_word.update()
if(not self.flag_dualword):
break
else:
word = text[i]
while(1):
if(self.wordExist(word)): #Read word existed
self.wavfiles.append(self.wordPath)
i=i+1
self.flag_dualword = True
break
else: #Record new word
self.status.set('New word found!')
self.newword.set(word)
self.button_change.config(state = Tkinter.NORMAL)
self.button_record.config(state = Tkinter.NORMAL)
self.label_word.update()
print self.wavfiles
self.CombineWavs(self.wavfiles)
self.PlayWav()
self.status.set('Read complete!')
self.newword.set('')
# self.label_word.update()
def PlayWav(self):
winsound.PlaySound(self.wav, winsound.SND_FILENAME)
def ReadWord(self):
winsound.PlaySound(self.wordPath, winsound.SND_FILENAME)
def CombineWavs(self, wavfiles):
for infile in wavfiles:
self.Combine2wav(self.wav, infile)
def Combine2wav(self, wav1, wav2):
outfile = self.wav
data= []
w = wave.open(wav1, 'rb')
data.append( [w.getparams(), w.readframes(w.getnframes())] )
w.close()
w = wave.open(wav2, 'rb')
data.append( [w.getparams(), w.readframes(w.getnframes())] )
w.close()
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.close()
def RecordNewword(self, FilePath):
self.button_change.config(state = Tkinter.DISABLED)
self.button_record.config(state = Tkinter.DISABLED)
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 10000
RECORD_SECONDS = 1
WAVE_OUTPUT_FILENAME = FilePath
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)
print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"
stream.close()
p.terminate()
# write data to WAVE file
data = ''.join(all)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
def ChangeNewword(self):
self.button_change.config(state = Tkinter.DISABLED)
self.button_record.config(state = Tkinter.DISABLED)
self.flag_dualword = False
newline = str(self.newword.get())+'\n'
print newline
NotCheckList.write(newline)
print 'Change to one word only'
if __name__ == '__main__':
app = cReadText()