-
Notifications
You must be signed in to change notification settings - Fork 0
/
playVoice.py
137 lines (110 loc) · 3.24 KB
/
playVoice.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
import pyaudio
import wave
import time
import threading
import sys
import os
from pynput import keyboard
filePath = ""
device = 0
playing = ""
importedVoices = {}
def initDevice():
global device
p = pyaudio.PyAudio()
# choose VoiceMeeter Input device
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
if len(sys.argv) == 2:
if sys.argv[1] == "-c":
print("请输入输出设备:")
for i in range(0, numdevices):
if p.get_device_info_by_index(i)["maxOutputChannels"] > 0:
print(str(p.get_device_info_by_index(i)["index"]) +
"\t" + p.get_device_info_by_index(i)["name"])
del p
device = int(input())
else:
print("参数错误")
sys.exit(0)
else:
for i in range(0, numdevices):
if p.get_device_info_by_index(i)["maxOutputChannels"] > 0 and "VoiceMeeter Input" in p.get_device_info_by_index(i)["name"]:
del p
device = i
break
def readConfig():
# read config file
with open("config.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
tkey = line[0:line.index(" ")]
tname = line[line.index(" ")+1:]
if tname[-1:] == "\n":
tname = tname[:-1]
importedVoices[tkey] = tname
# print config
for k, v in importedVoices.items():
print(k + "\t" + v)
def playAudio():
global playing
p = pyaudio.PyAudio()
wf = wave.open(filePath, "rb")
def callback(in_data, frame_count, time_info, status):
data = wf.readframes(frame_count)
return (data, pyaudio.paContinue)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True,
output_device_index=device,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
if playing == "":
break
else:
time.sleep(0.1)
stream.stop_stream()
stream.close()
wf.close()
p.terminate()
playing = ""
def cb(key):
tkey = ""
if(hasattr(key, "name")):
tkey = key.name
if(hasattr(key, "char")):
tkey = key.char
if tkey == "r":
os.system("cls")
importedVoices.clear()
initDevice()
readConfig()
return
def cPlay():
thread = threading.Thread(target=playAudio)
thread.start()
def cStop():
global playing
playing = ""
if tkey in importedVoices:
global playing
global filePath
if playing == "":
filePath = "./voice/" + importedVoices[tkey]
playing = tkey
cPlay()
else:
if playing == tkey:
cStop()
else:
cStop()
filePath = "./voice/" + importedVoices[tkey]
time.sleep(0.5)
playing = tkey
cPlay()
initDevice()
readConfig()
with keyboard.Listener(on_release=cb) as listener:
listener.join()