File size: 4,458 Bytes
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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):
        """ calling the `windowed_scoring` function that should be specialised
        depending on the score."""

        # imports
        #import soundfile as sf
        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

        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)
        return result
        """
        audios = []
        maxlen = 0
        if isinstance(test_files, str):
            test_files = [test_files]
        print(f'test_files: {test_files}')
        if not self.intrusive and len(test_files) > 1:
            if self.verbose:
                print('  [%s] is non-intrusive. Processing first file only'
                      % self.name)
            test_files = [test_files[0],]
        for file in test_files:
            # Loading sound file
            if isinstance(file, str):
                audio, rate = sf.read(file, always_2d=True)
            else:
                rate = array_rate
                if rate is None:
                    raise ValueError('Sampling rate needs to be specified '
                                     'when feeding numpy arrays.')
                audio = file
                # Standardize shapes
                if len(audio.shape) == 1:
                    audio = audio[:, None]
                if len(audio.shape) != 2:
                    raise ValueError('Please provide 1D or 2D array, received '
                                     '{}D array'.format(len(audio.shape)))

            if self.fixed_rate is not None and rate != self.fixed_rate:
                if self.verbose:
                    print('  [%s] preferred is %dkHz rate. resampling'
                          % (self.name, self.fixed_rate))
                audio = resampy.resample(audio, rate, self.fixed_rate, axis=0)
                rate = self.fixed_rate
            if self.mono and audio.shape[1] > 1:
                if self.verbose:
                    print('  [%s] only supports mono. Will use first channel'
                          % self.name)
                audio = audio[..., 0, None]
            if self.mono:
                audio = audio[..., 0]
            maxlen = max(maxlen, audio.shape[0])
            audios += [audio]
        audio = audios[1]
        audio[:maxlen-320] = audio[320:]
        audios[1] = audio
        for index, audio in enumerate(audios):
            if audio.shape[0] != maxlen:
                new = np.zeros((maxlen,) + audio.shape[1:])
                new[:audio.shape[0]] = audio
                audios[index] = new

        if self.window is not None:
            framer = Framing(self.window * rate,
                             self.hop * rate, maxlen)
            nwin = framer.nwin
            result = {}
            for (t, win) in enumerate(framer):
                result_t = self.test_window([audio[win] for audio in audios],
                                            rate)
                #or metric in result_t.keys():
                #   if metric not in result.keys():
                #       result[metric] = np.empty(nwin)
                #   result[metric][t] = result_t[metric]
                result[t] = result_t
        else:
            result = self.test_window(audios, rate)
        return result
        """