File size: 1,838 Bytes
936f6fa
 
 
 
 
 
 
 
 
 
 
ced5887
936f6fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5db2663
 
936f6fa
 
 
 
 
 
 
 
 
 
ced5887
 
 
 
 
 
936f6fa
 
 
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
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)
                result[t] = result_t
        else:
            result = self.windowed_scoring(audios, score_rate)
            
        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