-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.py
More file actions
executable file
·101 lines (80 loc) · 2.56 KB
/
Copy pathrecord.py
File metadata and controls
executable file
·101 lines (80 loc) · 2.56 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
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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
##
## moodstick.py - moodstick.
##
## Copyright (c) 2013-2014 Willem Jan Faber
##
## this program is free software: you can redistribute it and/or modify
## it under the terms of the gnu general public license as published by
## the free software foundation, either version 3 of the license, or
## (at your option) any later version.
##
## this program is distributed in the hope that it will be useful,
## but without any warranty; without even the implied warranty of
## merchantability or fitness for a particular purpose. see the
## gnu general public license for more details.
##
## you should have received a copy of the gnu general public license
## along with this program. if not, see <http://www.gnu.org/licenses/>.
##
"""
PyAudio example:
Record a few seconds of audio and save to a WAVE file.
"""
from fe2.mood import leds.LED_controll
import time
import analyse
import pyaudio
import numpy
import sys
from ctypes import *
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 48000
RECORD_SECONDS = 0.2
WAVE_OUTPUT_FILENAME = "output.wav"
def record_sample():
# This basicly is the record.py as found in the example directory for pyaudio
#<stackoverflow>
ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
def py_error_handler(filename, line, function, err, fmt):
pass
c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)
asound = cdll.LoadLibrary('libasound.so')
asound.snd_lib_error_set_handler(c_error_handler)
#</stackoverflow>
p = pyaudio.PyAudio()
try:
stream = p.open(format = FORMAT,
channels = CHANNELS,
input_device_index = 1,
rate = RATE,
input = True,
frames_per_buffer = chunk)
except:
return
raw_sample = []
for i in range(0, int(RATE / chunk * RECORD_SECONDS)):
try:
data = stream.read(chunk)
raw_sample.append(data)
except:
pass
stream.stop_stream()
stream.close()
p.terminate()
return(raw_sample)
volume_array = []
for i in range(1000):
raw_sample_data = record_sample()
time.sleep(0.2)
base_volume_established = False
if raw_sample_data:
for raw_sample in raw_sample_data:
sample = numpy.fromstring(raw_sample, dtype=numpy.int16)
volume_array.append(analyse.loudness(sample))
if len(volume_array) > 10:
base_volume_established = True
if base_volume_established: