1
1
from pathlib import Path
2
- from typing import Tuple
2
+ from typing import Tuple , Union
3
3
from numpy .typing import ArrayLike , NDArray
4
4
5
5
import click
6
6
import numpy
7
+ import resampy
7
8
import soundfile
8
9
9
10
from qdft import QDFT
15
16
def findpeaks (x : ArrayLike , n : int ) -> NDArray :
16
17
17
18
x = numpy .atleast_2d (x )
19
+
18
20
assert len (x .shape ) == 2
19
21
assert x .shape [0 ] > 0
20
22
assert x .shape [1 ] > 3
@@ -29,16 +31,37 @@ def findpeaks(x: ArrayLike, n: int) -> NDArray:
29
31
return j + 1
30
32
31
33
34
+ def resample (file : Path , samplerate : Union [int , None ]) -> Tuple [NDArray , int ]:
35
+
36
+ samples , origin = soundfile .read (file )
37
+ samples = numpy .atleast_1d (samples )
38
+
39
+ assert len (samples .shape ) <= 2
40
+ assert samples .shape [0 ] > 0
41
+
42
+ if samples .ndim > 1 :
43
+ samples = numpy .mean (samples , axis = - 1 )
44
+
45
+ if samplerate is None :
46
+ samplerate = origin
47
+
48
+ if samplerate != origin :
49
+ samples = resampy .resample (samples , origin , samplerate )
50
+
51
+ assert samplerate is not None
52
+ return samples , samplerate
53
+
54
+
32
55
def analyze (src : Path , opts : RemucsOptions ) -> Tuple [NDArray , NDArray ]:
33
56
34
57
if not opts .quiet :
35
58
click .echo (f'Analyzing { src .resolve ()} ' )
36
59
37
- x , samplerate = soundfile . read ( src )
38
- x = numpy . atleast_2d ( x ). mean ( axis = - 1 )
60
+ samplerate = 8000
61
+ x , samplerate = resample ( src , samplerate )
39
62
40
63
reference = 440
41
- bandwidth = (100 , 3000 )
64
+ bandwidth = (100 , 4000 )
42
65
resolution = int (1200 / 25 )
43
66
batchsize = int (1 * samplerate )
44
67
numpeaks = 3
0 commit comments