Spaces:
Running
Running
Update basis.py
Browse files
basis.py
CHANGED
|
@@ -14,7 +14,6 @@ class ScoreBasis:
|
|
| 14 |
depending on the score."""
|
| 15 |
|
| 16 |
# imports
|
| 17 |
-
#import soundfile as sf
|
| 18 |
import resampy
|
| 19 |
from museval.metrics import Framing
|
| 20 |
|
|
@@ -29,6 +28,8 @@ class ScoreBasis:
|
|
| 29 |
for index, audio in enumerate(audios):
|
| 30 |
audio = resampy.resample(audio, data['rate'], score_rate, axis=0)
|
| 31 |
audios[index] = audio
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if window is not None:
|
| 34 |
framer = Framing(window * score_rate, window * score_rate, maxlen)
|
|
@@ -40,74 +41,5 @@ class ScoreBasis:
|
|
| 40 |
else:
|
| 41 |
result = self.windowed_scoring(audios, score_rate)
|
| 42 |
return result
|
| 43 |
-
"""
|
| 44 |
-
audios = []
|
| 45 |
-
maxlen = 0
|
| 46 |
-
if isinstance(test_files, str):
|
| 47 |
-
test_files = [test_files]
|
| 48 |
-
print(f'test_files: {test_files}')
|
| 49 |
-
if not self.intrusive and len(test_files) > 1:
|
| 50 |
-
if self.verbose:
|
| 51 |
-
print(' [%s] is non-intrusive. Processing first file only'
|
| 52 |
-
% self.name)
|
| 53 |
-
test_files = [test_files[0],]
|
| 54 |
-
for file in test_files:
|
| 55 |
-
# Loading sound file
|
| 56 |
-
if isinstance(file, str):
|
| 57 |
-
audio, rate = sf.read(file, always_2d=True)
|
| 58 |
-
else:
|
| 59 |
-
rate = array_rate
|
| 60 |
-
if rate is None:
|
| 61 |
-
raise ValueError('Sampling rate needs to be specified '
|
| 62 |
-
'when feeding numpy arrays.')
|
| 63 |
-
audio = file
|
| 64 |
-
# Standardize shapes
|
| 65 |
-
if len(audio.shape) == 1:
|
| 66 |
-
audio = audio[:, None]
|
| 67 |
-
if len(audio.shape) != 2:
|
| 68 |
-
raise ValueError('Please provide 1D or 2D array, received '
|
| 69 |
-
'{}D array'.format(len(audio.shape)))
|
| 70 |
-
|
| 71 |
-
if self.fixed_rate is not None and rate != self.fixed_rate:
|
| 72 |
-
if self.verbose:
|
| 73 |
-
print(' [%s] preferred is %dkHz rate. resampling'
|
| 74 |
-
% (self.name, self.fixed_rate))
|
| 75 |
-
audio = resampy.resample(audio, rate, self.fixed_rate, axis=0)
|
| 76 |
-
rate = self.fixed_rate
|
| 77 |
-
if self.mono and audio.shape[1] > 1:
|
| 78 |
-
if self.verbose:
|
| 79 |
-
print(' [%s] only supports mono. Will use first channel'
|
| 80 |
-
% self.name)
|
| 81 |
-
audio = audio[..., 0, None]
|
| 82 |
-
if self.mono:
|
| 83 |
-
audio = audio[..., 0]
|
| 84 |
-
maxlen = max(maxlen, audio.shape[0])
|
| 85 |
-
audios += [audio]
|
| 86 |
-
audio = audios[1]
|
| 87 |
-
audio[:maxlen-320] = audio[320:]
|
| 88 |
-
audios[1] = audio
|
| 89 |
-
for index, audio in enumerate(audios):
|
| 90 |
-
if audio.shape[0] != maxlen:
|
| 91 |
-
new = np.zeros((maxlen,) + audio.shape[1:])
|
| 92 |
-
new[:audio.shape[0]] = audio
|
| 93 |
-
audios[index] = new
|
| 94 |
-
|
| 95 |
-
if self.window is not None:
|
| 96 |
-
framer = Framing(self.window * rate,
|
| 97 |
-
self.hop * rate, maxlen)
|
| 98 |
-
nwin = framer.nwin
|
| 99 |
-
result = {}
|
| 100 |
-
for (t, win) in enumerate(framer):
|
| 101 |
-
result_t = self.test_window([audio[win] for audio in audios],
|
| 102 |
-
rate)
|
| 103 |
-
#or metric in result_t.keys():
|
| 104 |
-
# if metric not in result.keys():
|
| 105 |
-
# result[metric] = np.empty(nwin)
|
| 106 |
-
# result[metric][t] = result_t[metric]
|
| 107 |
-
result[t] = result_t
|
| 108 |
-
else:
|
| 109 |
-
result = self.test_window(audios, rate)
|
| 110 |
-
return result
|
| 111 |
-
"""
|
| 112 |
|
| 113 |
|
|
|
|
| 14 |
depending on the score."""
|
| 15 |
|
| 16 |
# imports
|
|
|
|
| 17 |
import resampy
|
| 18 |
from museval.metrics import Framing
|
| 19 |
|
|
|
|
| 28 |
for index, audio in enumerate(audios):
|
| 29 |
audio = resampy.resample(audio, data['rate'], score_rate, axis=0)
|
| 30 |
audios[index] = audio
|
| 31 |
+
data['rate'] = score_rate
|
| 32 |
+
data['audio'] = audios
|
| 33 |
|
| 34 |
if window is not None:
|
| 35 |
framer = Framing(window * score_rate, window * score_rate, maxlen)
|
|
|
|
| 41 |
else:
|
| 42 |
result = self.windowed_scoring(audios, score_rate)
|
| 43 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
|