-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio_processing.py
More file actions
81 lines (67 loc) · 2.62 KB
/
Copy pathaudio_processing.py
File metadata and controls
81 lines (67 loc) · 2.62 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
import librosa
import numpy as np
import aubio
import pitch
import soundfile as sf
from IPython.display import Audio
import aubio
from scipy.io import wavfile
from pydub import AudioSegment
from pydub.playback import play
import pyaudio
def extract_vocals(filename, margin_i=1, margin_v=10, power=1):
y, sr = librosa.load(filename)
S_full, phase = librosa.magphase(librosa.stft(y))
S_filter = librosa.decompose.nn_filter(S_full,
aggregate=np.median,
metric='cosine',
width=int(librosa.time_to_frames(2, sr=sr)))
S_filter = np.minimum(S_full, S_filter)
mask_i = librosa.util.softmask(S_filter,
margin_i * (S_full - S_filter),
power=power)
mask_v = librosa.util.softmask(S_full - S_filter,
margin_v * S_filter,
power=power)
S_foreground = mask_v * S_full
S_background = mask_i * S_full
new_y = librosa.istft(S_foreground * phase)
sf.write("output.wav", new_y, samplerate=sr, subtype='PCM_24')
"""
def extract_vocals(filename):
y, sr = librosa.load(filename)
S_full, phase = librosa.magphase(librosa.stft(y))
S_filter = librosa.decompose.nn_filter(S_full,
aggregate=np.median,
metric='cosine',
width=int(librosa.time_to_frames(2, sr=sr)))
S_filter = np.minimum(S_full, S_filter)
margin_i, margin_v = 2, 10
power = 2
mask_i = librosa.util.softmask(S_filter,
margin_i * (S_full - S_filter),
power=power)
mask_v = librosa.util.softmask(S_full - S_filter,
margin_v * S_filter,
power=power)
S_foreground = mask_v * S_full
S_background = mask_i * S_full
new_y = librosa.istft(S_foreground*phase)
sf.write("output.wav", new_y, samplerate=sr, subtype='PCM_24')
"""
def get_freqs(filename):
y, sr = librosa.load(filename)
hop_length = 128
pitch_o = aubio.pitch("mcomb", hop_length, hop_length, sr)
pitch_o.set_unit("Hz")
pitch_o.set_tolerance(0.2)
pitches = []
total_frames = len(y) // hop_length
for i in range(total_frames):
frame = y[i * hop_length: (i + 1) * hop_length]
pitch = pitch_o(frame)[0]
pitches.append(pitch)
return pitches
def get_info(filename):
y, sr = librosa.load(filename)
return (y, sr)