forked from luyishisi/The_python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluyin.py
More file actions
66 lines (58 loc) · 1.54 KB
/
luyin.py
File metadata and controls
66 lines (58 loc) · 1.54 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
#!usr/bin/env python
#coding=utf-8
import numpy as np
from pyaudio import PyAudio,paInt16
from datetime import datetime
import wave
from Tkinter import *
#define of params
NUM_SAMPLES = 2000
framerate = 8000
channels = 1
sampwidth = 2
#record time
TIME = 10
def save_wave_file(filename, data):
'''save the date to the wav file'''
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes("".join(data))
wf.close()
def my_button(root,label_text,button_text,button_func):
'''''function of creat label and button'''
#label details
label = Label(root)
label['text'] = label_text
label.pack()
#label details
button = Button(root)
button['text'] = button_text
button['command'] = button_func
button.pack()
def record_wave():
#open the input of wave
pa = PyAudio()
stream = pa.open(format = paInt16, channels = 1,
rate = framerate, input = True,
frames_per_buffer = NUM_SAMPLES)
save_buffer = []
count = 0
while count < TIME*4:
#read NUM_SAMPLES sampling data
string_audio_data = stream.read(NUM_SAMPLES)
save_buffer.append(string_audio_data)
count += 1
print '.'
#filename = datetime.now().strftime("%Y-%m-%d_%H_%M_%S")+".wav"
filename = "1.wav"
save_wave_file(filename, save_buffer)
save_buffer = []
print filename, "saved"
def main():
root = Tk()
my_button(root,"Record a wave","clik to record",record_wave)
root.mainloop()
if __name__ == "__main__":
main()