-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio_capture.py
106 lines (80 loc) · 2.58 KB
/
audio_capture.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
"""
create a function who takes in input a sound from internal macbook pro microphone and trigger when a sound is detected
"""
import pyaudio
import wave
import time
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy.io import wavfile
from scipy.fftpack import fft
from scipy.signal import find_peaks
from scipy.signal import butter, lfilter
from scipy.signal import freqz
from scipy.signal import correlate
from scipy.signal import argrelextrema
def capture_sound_from_microphone():
"""
capture sound from microphone and save it in a wav file
"""
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 20
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
sound_count_detected = 0
last_sound_detected = 0
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
sound_stream = np.fromstring(data, dtype=np.int16)
sound_detected = trigger_event_from_sound_detected(sound_stream, 40.0)
if sound_detected:
sound_count_detected += 1
with wave.open(WAVE_OUTPUT_FILENAME.format(sound_count_detected), 'wb') as f:
f.setnchannels(CHANNELS)
f.setsampwidth(p.get_sample_size(FORMAT))
f.setframerate(RATE)
f.writeframes(b''.join(frames[last_sound_detected:]))
last_sound_detected = i
# plot_sound_stream(frames)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def trigger_event_from_sound_detected(sound_stream, threshold):
"""
trigger event when sound is detected in rms level
"""
rms_sound_stream = np.sqrt(np.mean(sound_stream ** 2))
if rms_sound_stream < threshold:
print("sound detected")
return True
def plot_sound_stream(sound_stream):
"""
plot sound stream
"""
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sound_stream)
plt.show()
if __name__ == '__main__':
capture_sound_from_microphone()