-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
100 lines (81 loc) · 3.46 KB
/
script.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
99
100
import threading
import sounddevice as sd
import numpy as np
import wavio
from patterns import *
#print(sd.query_devices())
def list_audio_devices():
devices = sd.query_devices()
for i, device in enumerate(devices):
print(f"Device ID: {i}")
print(f" Name: {device['name']}")
print(f" Max Input Channels: {device['max_input_channels']}")
print(f" Max Output Channels: {device['max_output_channels']}")
print(f" Default Sample Rate: {device['default_samplerate']}")
print("---")
def record_audio(filename, duration, samplerate, device=5):
print()
print(f"Ready to record: {filename}")
input("Press Enter to start recording.")
print(f"Recording {duration} seconds...")
audio_data = sd.rec(
int(samplerate * duration),
samplerate=samplerate,
channels=2,
dtype='int16',
device=device
)
sd.wait()
print(f"...saved.")
print()
silenced_audio_data = remove_starting_silence(audio_data)
peak_value = np.max(np.abs(audio_data))
target_peak = 32767 * 10**(-3 / 20)
if peak_value < target_peak:
try:
normalization_factor = target_peak / peak_value
normalized_audio = audio_data * normalization_factor
wavio.write(f"recordings/normalized/{filename}_lowout.wav", silenced_audio_data[:, 0], samplerate, sampwidth=2)
wavio.write(f"recordings/normalized/{filename}_highout.wav", silenced_audio_data[:, 1], samplerate, sampwidth=2)
except:
pass
left_channel = audio_data[:, 0]
right_channel = audio_data[:, 1]
wavio.write(f"recordings/raw/{filename}_lowout.wav", left_channel, samplerate, sampwidth=2)
wavio.write(f"recordings/raw/{filename}_highout.wav", right_channel, samplerate, sampwidth=2)
try:
wavio.write(f"recordings/leading_silence_removed/{filename}_lowout.wav", silenced_audio_data[:, 0], samplerate, sampwidth=2)
wavio.write(f"recordings/leading_silence_removed/{filename}_highout.wav", silenced_audio_data[:, 1], samplerate, sampwidth=2)
except:
pass
print(f"Saved {filename}_lowout.wav and {filename}_highout.wav")
return audio_data
#def remove_silence(audio_data, threshold=1000):
#audio_abs = np.abs(audio_data)
#silence_removed = audio_data[np.max(audio_abs, axis=1) > threshold]
#return silence_removed
def remove_starting_silence(audio_data, silence_threshold=1000):
for i, sample in enumerate(audio_data):
if np.linalg.norm(sample) > silence_threshold:
return audio_data[i:]
return np.array([]) # return empty array if all samples are silent
if __name__ == "__main__":
# Ask for the BPM
list_audio_devices()
print()
bpm = input("Tell me the bpm: ")
# Want to record for n loops
n_loops = 10
#n_loops = 5
beats = n_loops * 4
minutes = 1/int(bpm) * beats
duration = int(minutes * 60 + 1.5) # seconds
samplerate = 44100 # Hertz
patterns = beat_patterns + latin_patterns + top_row_patterns
patterns = latin_patterns + top_row_patterns
#patterns = latin_patterns
# Recording loop
for pattern in patterns:
#record_audio(f"{pattern}_{bpm}-noBass", duration, samplerate)
record_audio(f"{pattern}_{bpm}-noBass-noSnare-noCymbal-noGuiro", duration, samplerate)
#record_audio(f"{pattern}_{bpm}", duration, samplerate)