Spaces:
Running
Running
class ScoreBasis: | |
def __init__(self, name=None): | |
# the score operates on the specified rate | |
self.score_rate = None | |
# is the score intrusive or non-intrusive ? | |
self.intrusive = True #require a reference | |
self.name = name | |
def windowed_scoring(self, audios, score_rate): | |
raise NotImplementedError(f'In {self.name}, windowed_scoring is not yet implemented') | |
def scoring(self, data, window=None, score_rate=None, round_digits=None): | |
""" calling the `windowed_scoring` function that should be specialised | |
depending on the score.""" | |
# imports | |
import resampy | |
from museval.metrics import Framing | |
#checking rate | |
audios = data['audio'] | |
score_rate = data['rate'] | |
if self.score_rate is not None: | |
score_rate = self.score_rate | |
if score_rate != data['rate']: | |
for index, audio in enumerate(audios): | |
audio = resampy.resample(audio, data['rate'], score_rate, axis=0) | |
audios[index] = audio | |
data['rate'] = score_rate | |
data['audio'] = audios | |
if window is not None: | |
framer = Framing(window * score_rate, window * score_rate, maxlen) | |
nwin = framer.nwin | |
result = {} | |
for (t, win) in enumerate(framer): | |
result_t = self.windowed_scoring([audio[win] for audio in audios], score_rate) | |
if result_t is not None: | |
result[t] = result_t | |
else: | |
result = self.windowed_scoring(audios, score_rate) | |
if result is None or not result: return None | |
if round_digits is not None: | |
if isinstance(result, dict): | |
for key in result: | |
result[key] = round(result[key], round_digits) | |
else: result = round(result, round_digits) | |
return result | |