forked from mathewthe2/Game2Text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordaudio.py
103 lines (89 loc) · 3.72 KB
/
recordaudio.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
import threading
import pyaudio
import wave
import os
import platform
from audio import valid_output_device, convert_audio
from config import r_config, LOG_CONFIG
class RecordThread(threading.Thread):
def __init__(self, deviceIndex=-1, frames=512):
threading.Thread.__init__(self)
self.isRecording = False
self.bRecord = True
self.deviceIndex = deviceIndex
self.recorded_frames = []
self.audiofile = "out.wav"
self.duration = 10
self.frames = frames
self.hasAudio = False
def run(self):
if self.deviceIndex == -1:
print("recorder called but device index not specified")
return
if not valid_output_device(self.deviceIndex):
print("recorder called but device invalid")
return
p = pyaudio.PyAudio()
device_info = p.get_device_info_by_index(self.deviceIndex)
is_input = device_info["maxInputChannels"] > 0
is_wasapi = (p.get_host_api_info_by_index(device_info["hostApi"])["name"]).find("WASAPI") != -1
useloopback = is_wasapi and not is_input
# Open stream
channelcount = device_info["maxInputChannels"] if (device_info["maxOutputChannels"] < device_info["maxInputChannels"]) else device_info["maxOutputChannels"]
try:
stream_parameters = {
'format': pyaudio.paInt16,
'channels': channelcount,
'rate': int(device_info["defaultSampleRate"]),
'input': True,
'frames_per_buffer': self.frames,
'input_device_index': device_info["index"]
}
is_windows = (platform.system() == 'Windows')
if is_windows:
stream_parameters['as_loopback'] = useloopback
stream = p.open(**stream_parameters)
# Start recording
self.isRecording = True
self.hasAudio = False
while self.bRecord:
self.recorded_frames.append(stream.read(self.frames, exception_on_overflow = True))
self.hasAudio = True
stream.stop_stream()
stream.close()
# Don't save file if duration is 0
if (self.duration == 0):
p.terminate()
return
file = self.audiofile
filename, file_extension = os.path.splitext(file)
file_needs_conversion = file_extension != '.wav'
if file_needs_conversion:
file = filename + '.wav'
waveFile = wave.open(file, 'wb')
waveFile.setnchannels(channelcount)
waveFile.setsampwidth(p.get_sample_size(pyaudio.paInt16))
waveFile.setframerate(int(device_info["defaultSampleRate"]))
start_frame = 0
trim_audio = (self.duration != -1)
if trim_audio:
start_frame = len(self.recorded_frames) - int(int(device_info["defaultSampleRate"]) / self.frames * self.duration)
waveFile.writeframes(b''.join(self.recorded_frames[start_frame:]))
waveFile.close()
p.terminate()
# Convert to mp3 or other formats
if file_needs_conversion:
convert_audio(file, self.audiofile)
os.remove(file)
except Exception as e:
print('Error: cannot record audio with selected device', e)
def stop_recording(self, audiofile='out.wav', duration = 10):
self.audiofile = audiofile
self.duration = duration
self.bRecord = False
def restart_recording(self):
self.bRecord = False
def is_recording(self):
return self.isRecording
def has_audio(self):
return self.hasAudio