Ejemplo n.º 1
0
    def select_features(cls, features_id, file_struct, annot_beats, framesync):
        """Selects the features from the given parameters.

        Parameters
        ----------
        features_id: str
            The identifier of the features (it must be a key inside the
            `features_registry`)
        file_struct: msaf.io.FileStruct
            The file struct containing the files to extract the features from
        annot_beats: boolean
            Whether to use annotated (`True`) or estimated (`False`) beats
        framesync: boolean
            Whether to use framesync (`True`) or beatsync (`False`) features

        Returns
        -------
        features: obj
            The actual features object that inherits from `msaf.Features`
        """
        if framesync:
            feat_type = FeatureTypes.est_beatsync
        elif annot_beats and not framesync:
            feat_type = FeatureTypes.ann_beatsync
        elif not annot_beats and not framesync:
            feat_type = FeatureTypes.est_beatsync
        else:
            raise FeatureTypeNotFound("Type of features not valid.")

        # Select features with default parameters
        return features_registry[features_id](file_struct, feat_type)
Ejemplo n.º 2
0
    def features(self):
        """This getter will compute the actual features if they haven't
        been computed yet.

        Returns
        -------
        features: np.array
            The actual features. Each row corresponds to a feature vector.
        """
        # Compute features if needed
        if self._features is None:
            try:
                self.read_features()
            except (NoFeaturesFileError, FeaturesNotFound,
                    WrongFeaturesFormatError, FeatureParamsError) as e:
                try:
                    self._compute_all_features()
                    self.write_features()
                except IOError:
                    if isinstance(e, FeaturesNotFound) or \
                            isinstance(e, FeatureParamsError):
                        msg = "Computation of the features is needed for " \
                            "current parameters but no audio file was found." \
                            "Please, change your parameters or add the audio" \
                            " file in %s"
                    else:
                        msg = "Couldn't find audio file in %s"
                    raise NoAudioFileError(msg % self.file_struct.audio_file)

        # Choose features based on type
        if self.feat_type is FeatureTypes.framesync:
            self._features = self._framesync_features
        elif self.feat_type is FeatureTypes.est_beatsync:
            self._features = self._est_beatsync_features
        elif self.feat_type is FeatureTypes.ann_beatsync:
            if self._ann_beatsync_features is None:
                raise FeatureTypeNotFound(
                    "Feature type %s is not valid because no annotated beats "
                    "were found" % self.feat_type)
            self._features = self._ann_beatsync_features
        else:
            raise FeatureTypeNotFound("Feature type %s is not valid." %
                                      self.feat_type)

        return self._features
Ejemplo n.º 3
0
 def frame_times(self):
     """This getter returns the frame times, for the corresponding type of
     features."""
     frame_times = None
     # Make sure we have already computed the features
     features = self.features
     if self.feat_type is FeatureTypes.framesync:
         frame_times = np.array([
             i * self.hop_length / float(self.sr)
             for i in np.arange(features.shape[0])
         ])
     elif self.feat_type is FeatureTypes.est_beatsync:
         frame_times = self._est_beats_times
     elif self.feat_type is FeatureTypes.ann_beatsync:
         if self._ann_beatsync_features is None:
             raise FeatureTypeNotFound(
                 "Feature type %s is not valid because no annotated beats "
                 "were found" % self.feat_type)
         frame_times = self._ann_beats_times
     else:
         raise FeatureTypeNotFound("Feature type %s is not valid" %
                                   self.feat_type)
     return frame_times