This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
66 lines (54 loc) · 2.1 KB
/
record.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
# This Python file uses the following encoding: utf-8
import pyaudio
import wave, os
from PySide6.QtCore import QObject, SignalInstance, Signal, Slot
class record:
def __init__(self):
self.CHUNK = 2048
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 44100
self.signal = Communicate()
self.stopped = False
def initPyaduio(self):
self.p = pyaudio.PyAudio()
self.signal.finished.emit()
def record_audio(self, wave_out_path):
try:
print(wave_out_path)
os.makedirs(os.path.dirname(wave_out_path), exist_ok=True)
self.stream = self.p.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
self.wf = wave.open(wave_out_path, 'wb')
self.wf.setnchannels(self.CHANNELS)
self.wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
self.wf.setframerate(self.RATE)
print("Start")
while(not self.stopped):
data = self.stream.read(self.CHUNK)
self.wf.writeframes(data)
except AttributeError:
pass
def stopRecording(self):
self.stopped = True
self.stream.stop_stream()
self.stream.close()
self.wf.close()
print("End")
def playRecording(self,wave_out_path):
self.wf = wave.open(wave_out_path, 'rb')
self.stream = self.p.open(format=self.p.get_format_from_width(self.wf.getsampwidth()),
channels=self.wf.getnchannels(),
rate=self.wf.getframerate(),
output=True)
data = self.wf.readframes(self.CHUNK)
while data != '':
self.stream.write(data)
data = self.wf.readframes(self.CHUNK)
self.stream.stop_stream()
self.stream.close()
class Communicate(QObject):
finished = Signal()