-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudiorecorder.py
More file actions
56 lines (36 loc) · 1.29 KB
/
audiorecorder.py
File metadata and controls
56 lines (36 loc) · 1.29 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
# pip install pyaudio -> 이부분 먼저 컴퓨터에 깔아주기
import pyaudio
import wave
import sys
import os
def device() :
audio = pyaudio.PyAudio()
desc = audio.get_device_info_by_index(1)
rate = int(desc["defaultSampleRate"])
return rate
def audio_Recording(RECORD_SECONDS = 120) :
RATE = device()
audio = pyaudio.PyAudio()
# 녹음시작!
stream = audio.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
input_device_index= 1,
frames_per_buffer=1024)
#(format = 비트기 깊이, 16비트로 기본 설정, rate = sr값, input=음성을 입력할건지 아닌지, 여기선 맞으니까 True,input_device_index= 노트북에 따라 마이크 장치 번호 넣어줌 1이 제일 기본 마이크)
print("recording...")
frames = []
for i in range(0, int(RATE / 1024 * RECORD_SECONDS)):
data = stream.read(1024)
frames.append(data)
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open("audiofile.wav", 'wb')
waveFile.setnchannels(1)
waveFile.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
return "finished recording"