def __data_generation_dnn(self, list_dirs_temp):

        # Initialization
        X = np.empty((self.batch_size, 39 * 21))
        target = np.empty((self.batch_size, 8))
        for i, item_path in enumerate(list_dirs_temp):
            if self.feat == 'mfcc':
                if self.precomputed:
                    feat = np.load(item_path)[:, :, 0].T
                else:
                    waveform, sr = sf.read(item_path)
                    feat_item = mfcc(waveform + 1e-9,
                                     maxfreq=sr / 2.0,
                                     nwin=.128,
                                     shift=.032)[0]
                    feat_delta1 = compute_delta(feat_item)
                    feat_delta2 = compute_delta(feat_delta1)
                    feat = np.concatenate(
                        (feat_item, feat_delta1, feat_delta2), axis=1)
            else:
                if self.precomputed:
                    feat = np.load(item_path)[:, :, 0].T
                else:
                    waveform, sr = sf.read(item_path)
                    feat_item = plp(waveform + 1e-9,
                                    fs=sr,
                                    rasta=False,
                                    nwin=.128,
                                    shift=.032)[0]
                    feat_delta1 = compute_delta(feat_item)
                    feat_delta2 = compute_delta(feat_delta1)
                    feat = np.concatenate(
                        (feat_item, feat_delta1, feat_delta2), axis=1)

            mirror_feat = np.pad(feat, ((10, ), (0, )), 'reflect')
            frames = []
            for j in range(10 + i * 25, 10 + (i + 1) * 25):
                frames.append(np.reshape(mirror_feat[j - 10:j + 11, :], -1))
            X[25 * i:25 * (i + 1), ] = np.array(frames)

            # Store class
            label = item_path.split('/')[-3]

            target_i = keras.utils.to_categorical(self.target_to_class[label],
                                                  num_classes=self.n_classes)
            target[25 * i:25 * (i + 1), :] = np.repeat(target_i.reshape(
                (1, -1)),
                                                       repeats=25,
                                                       axis=0)
        return X, target
    def _delta_and_2delta(self, cep):
        """
        Add deltas and double deltas.
        :param cep: a matrix of cepstral cefficients

        :return: the cepstral coefficient stacked with deltas and double deltas
        """
        if self.delta:
            logging.debug('add delta')
            delta = compute_delta(cep, filt=self.delta_filter)
            cep = numpy.column_stack((cep, delta))
            if self.double_delta:
                logging.debug('add delta delta')
                double_delta = compute_delta(delta, filt=self.delta_filter)
                cep = numpy.column_stack((cep, double_delta))
        return cep
 def __data_generation_lstm(self, item_path):
     # Generate data
     waveform, sr = sf.read(item_path)
     if self.feat == 'mfcc':
         feat_item = mfcc(waveform + 1e-9, maxfreq=sr / 2.0)[0]
     else:
         feat_item = plp(waveform + 1e-9, fs=sr, rasta=False)[0]
     feat_delta1 = compute_delta(feat_item)
     feat_delta2 = compute_delta(feat_delta1)
     feat = np.concatenate((feat_item, feat_delta1, feat_delta2), axis=1)
     # Store class
     label = item_path.split('/')[-3]
     y = self.target_to_class[label]
     target = np.zeros((feat.shape[0], self.n_classes))
     target[:, y] = 1
     return feat.reshape(1, *feat.shape), target
    def __data_generation_cnn(self, list_dirs_temp):

        # Initialization
        y = np.empty((self.batch_size), dtype=int)
        if self.feat == 'combined':
            X1 = np.empty((self.batch_size, *self.dim1, self.n_channels))
            X2 = np.empty((self.batch_size, *self.dim2, self.n_channels))
        else:
            X = np.empty((self.batch_size, *self.dim, self.n_channels))

        # Generate data
        labels = []
        for i, item_path in enumerate(list_dirs_temp):

            if self.feat == 'melspec':
                waveform, sr = sf.read(item_path)
                mel = librosa.feature.melspectrogram(waveform, sr=16000)
                ps_db = librosa.power_to_db(mel, ref=np.max).reshape(
                    (*self.dim, 1))
                X[i, ] = (ps_db - np.mean(ps_db)) / np.var(ps_db)
            elif self.feat == 'mfcc':
                if self.precomputed:
                    X[i, ] = np.load(item_path)
                else:
                    waveform, sr = sf.read(item_path)
                    feat_item = mfcc(waveform + 1e-9,
                                     maxfreq=sr / 2.0,
                                     nwin=.128,
                                     shift=.032)[0]
                    feat_delta1 = compute_delta(feat_item)
                    feat_delta2 = compute_delta(feat_delta1)
                    feat = np.concatenate(
                        (feat_item, feat_delta1, feat_delta2),
                        axis=1).T.reshape((*self.dim, 1))
                    X[i, ] = (feat - np.mean(feat)) / np.var(feat)
            elif self.feat == 'plp':
                if self.precomputed:
                    X[i, ] = np.load(item_path)
                else:
                    waveform, sr = sf.read(item_path)
                    feat_item = plp(waveform + 1e-9,
                                    fs=sr,
                                    rasta=False,
                                    nwin=.128,
                                    shift=.032)[0]
                    feat_delta1 = compute_delta(feat_item)
                    feat_delta2 = compute_delta(feat_delta1)
                    feat = np.concatenate(
                        (feat_item, feat_delta1, feat_delta2),
                        axis=1).T.reshape((*self.dim, 1))
                    X[i, ] = (feat - np.mean(feat)) / np.var(feat)
            elif self.feat == 'combined':
                waveform, sr = sf.read(item_path)
                mel = librosa.feature.melspectrogram(waveform, sr=16000)
                ps_db = librosa.power_to_db(mel, ref=np.max).reshape(
                    (*self.dim2, 1))
                X2[i, ] = (ps_db - np.mean(ps_db)) / np.var(ps_db)
                feat_item = mfcc(waveform + 1e-9,
                                 maxfreq=sr / 2.0,
                                 nwin=.128,
                                 shift=.032)[0]
                feat_delta1 = compute_delta(feat_item)
                feat_delta2 = compute_delta(feat_delta1)
                feat = np.concatenate((feat_item, feat_delta1, feat_delta2),
                                      axis=1).T.reshape((*self.dim1, 1))
                X1[i, ] = (feat - np.mean(feat)) / np.var(feat)
            # Store class
            label = item_path.split('/')[-3]
            labels.append(item_path)
            y[i] = self.target_to_class[label]

        target = keras.utils.to_categorical(y, num_classes=self.n_classes)
        if self.feat == 'combined':
            return (X1, X2), target
        return X, target