-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioProcessing.py
More file actions
79 lines (69 loc) · 2.65 KB
/
AudioProcessing.py
File metadata and controls
79 lines (69 loc) · 2.65 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
import pyaudio
import wave
# You need to have all of the dependencies installed on your machine for this to work
# either use Anaconda or pip to install (preferably in a virtual environment or conda environment)
# Useful tutorial: https://publish.illinois.edu/augmentedlistening/tutorials/music-processing/tutorial-1-introduction-to-audio-processing-in-python/
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
def save_audio_files(streams, frames):
for i in range(len(streams)):
wave_output_filename = "{}_audio_output.wav".format(i)
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[i]))
wf.close()
def stop_streams(streams):
for stream in streams:
stream.stop_stream()
stream.close()
p.terminate()
if __name__ == "__main__":
p = pyaudio.PyAudio()
streams = []
num_devices = p.get_device_count()
print(f'num found devices {num_devices}')
mics = []
for i in range(num_devices):
# try:
# stream = p.open(format=FORMAT,
# channels=CHANNELS,
# rate=RATE,
# input=True,
# input_device_index=i)
# streams.append(stream)
# mics.append(i)
# except:
# print("Failed to use mic {}".format(i))
device_info = p.get_device_info_by_index(i)
print(device_info)
device_max_channels = device_info['maxInputChannels']
device_host_api = device_info['hostApi']
if 'USB' in device_info['name'] and device_max_channels == 1:
stream = p.open(format=FORMAT,
channels=1,
rate=RATE,
input=True,
input_device_index=i)
streams.append(stream)
mics.append(i)
num_used_mics = len(mics)
frames = [[] for i in range(num_used_mics)]
print("started recording")
print(f'number microphones {len(mics)}')
print(f'number streams {len(streams)}')
while True:
try:
for i in range(num_used_mics):
stream = streams[i]
data = stream.read(CHUNK, exception_on_overflow=False)
frames[i].append(data)
except KeyboardInterrupt:
# except (KeyboardInterrupt, GeneratorExit, SystemExit, InterruptedError) as e:
# save audio files
save_audio_files(streams, frames)
stop_streams(streams)
break