speed_perturb.py 1.8 KB
Newer Older
chrisxu2014's avatar
chrisxu2014 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
"""Contain the speech perturbation augmentation model."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from data_utils.augmentor.base import AugmentorBase


class SpeedPerturbAugmentor(AugmentorBase):
    """Augmentation model for adding speed perturbation.

    See reference paper here:
    http://www.danielpovey.com/files/2015_interspeech_augmentation.pdf

    :param rng: Random generator object.
    :type rng: random.Random
X
xushaoyong 已提交
17
    :param min_speed_rate: Lower bound of new speed rate to sample and should
X
xushaoyong 已提交
18
                           not be smaller than 0.9.
chrisxu2014's avatar
chrisxu2014 已提交
19
    :type min_speed_rate: float
X
xushaoyong 已提交
20
    :param max_speed_rate: Upper bound of new speed rate to sample and should
X
xushaoyong 已提交
21
                           not be larger than 1.1.
chrisxu2014's avatar
chrisxu2014 已提交
22 23 24 25
    :type max_speed_rate: float
    """

    def __init__(self, rng, min_speed_rate, max_speed_rate):
X
xushaoyong 已提交
26 27 28 29 30 31
        if min_speed_rate < 0.9:
            raise ValueError(
                "Sampling speed below 0.9 can cause unnatural effects")
        if max_speed_rate > 1.1:
            raise ValueError(
                "Sampling speed above 1.1 can cause unnatural effects")
chrisxu2014's avatar
chrisxu2014 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44
        self._min_speed_rate = min_speed_rate
        self._max_speed_rate = max_speed_rate
        self._rng = rng

    def transform_audio(self, audio_segment):
        """Sample a new speed rate from the given range and
        changes the speed of the given audio clip.

        Note that this is an in-place transformation.

        :param audio_segment: Audio segment to add effects to.
        :type audio_segment: AudioSegment|SpeechSegment
        """
45 46
        sampled_speed = self._rng.uniform(self._min_speed_rate,
                                          self._max_speed_rate)
chrisxu2014's avatar
chrisxu2014 已提交
47
        audio_segment.change_speed(sampled_speed)