forked from kahst/BirdNET-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
55 lines (37 loc) · 1.39 KB
/
audio.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
import numpy as np
import config as cfg
RANDOM = np.random.RandomState(cfg.RANDOM_SEED)
def openAudioFile(path, sample_rate=48000, offset=0.0, duration=None):
# Open file with librosa (uses ffmpeg or libav)
import librosa
try:
sig, rate = librosa.load(path, sr=sample_rate, offset=offset, duration=duration, mono=True, res_type='kaiser_fast')
except:
sig, rate = [], sample_rate
return sig, rate
def saveSignal(sig, fname):
import soundfile as sf
sf.write(fname, sig, 48000, 'PCM_16')
def noise(sig, shape, amount=None):
# Random noise intensity
if amount == None:
amount = RANDOM.uniform(0.1, 0.5)
# Create Gaussian noise
try:
noise = RANDOM.normal(min(sig) * amount, max(sig) * amount, shape)
except:
noise = np.zeros(shape)
return noise.astype('float32')
def splitSignal(sig, rate, seconds, overlap, minlen):
# Split signal with overlap
sig_splits = []
for i in range(0, len(sig), int((seconds - overlap) * rate)):
split = sig[i:i + int(seconds * rate)]
# End of signal?
if len(split) < int(minlen * rate):
break
# Signal chunk too short?
if len(split) < int(rate * seconds):
split = np.hstack((split, noise(split, (int(rate * seconds) - len(split)), 0.5)))
sig_splits.append(split)
return sig_splits