Skip to content

Commit 58bcabf

Browse files
committed
Reasonable resample the source file
1 parent c0db045 commit 58bcabf

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/remucs/tuning.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from pathlib import Path
2-
from typing import Tuple
2+
from typing import Tuple, Union
33
from numpy.typing import ArrayLike, NDArray
44

55
import click
66
import numpy
7+
import resampy
78
import soundfile
89

910
from qdft import QDFT
@@ -15,6 +16,7 @@
1516
def findpeaks(x: ArrayLike, n: int) -> NDArray:
1617

1718
x = numpy.atleast_2d(x)
19+
1820
assert len(x.shape) == 2
1921
assert x.shape[0] > 0
2022
assert x.shape[1] > 3
@@ -29,16 +31,37 @@ def findpeaks(x: ArrayLike, n: int) -> NDArray:
2931
return j + 1
3032

3133

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+
3255
def analyze(src: Path, opts: RemucsOptions) -> Tuple[NDArray, NDArray]:
3356

3457
if not opts.quiet:
3558
click.echo(f'Analyzing {src.resolve()}')
3659

37-
x, samplerate = soundfile.read(src)
38-
x = numpy.atleast_2d(x).mean(axis=-1)
60+
samplerate = 8000
61+
x, samplerate = resample(src, samplerate)
3962

4063
reference = 440
41-
bandwidth = (100, 3000)
64+
bandwidth = (100, 4000)
4265
resolution = int(1200 / 25)
4366
batchsize = int(1 * samplerate)
4467
numpeaks = 3

0 commit comments

Comments
 (0)