audio.py 24.6 KB
Newer Older
1 2 3 4 5
"""Contains the audio segment class."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

6 7 8
import numpy as np
import io
import soundfile
chrisxu2014's avatar
chrisxu2014 已提交
9 10
import scikits.samplerate
from scipy import signal
chrisxu2014's avatar
chrisxu2014 已提交
11
import random
chrisxu2014's avatar
chrisxu2014 已提交
12
import copy
13 14 15 16


class AudioSegment(object):
    """Monaural audio segment abstraction.
17 18 19 20 21 22

    :param samples: Audio samples [num_samples x num_channels].
    :type samples: ndarray.float32
    :param sample_rate: Audio sample rate.
    :type sample_rate: int
    :raises TypeError: If the sample data type is not float or int.
23 24 25
    """

    def __init__(self, samples, sample_rate):
26 27 28 29 30
        """Create audio segment from samples.

        Samples are convert float32 internally, with int scaled to [-1, 1].
        """
        self._samples = self._convert_samples_to_float32(samples)
31 32 33 34
        self._sample_rate = sample_rate
        if self._samples.ndim >= 2:
            self._samples = np.mean(self._samples, 1)

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    def __eq__(self, other):
        """Return whether two objects are equal."""
        if type(other) is not type(self):
            return False
        if self._sample_rate != other._sample_rate:
            return False
        if self._samples.shape != other._samples.shape:
            return False
        if np.any(self.samples != other._samples):
            return False
        return True

    def __ne__(self, other):
        """Return whether two objects are unequal."""
        return not self.__eq__(other)

    def __str__(self):
        """Return human-readable representation of segment."""
        return ("%s: num_samples=%d, sample_rate=%d, duration=%.2fsec, "
                "rms=%.2fdB" % (type(self), self.num_samples, self.sample_rate,
                                self.duration, self.rms_db))

57
    @classmethod
58 59 60 61 62 63 64 65 66
    def from_file(cls, file):
        """Create audio segment from audio file.
        
        :param filepath: Filepath or file object to audio file.
        :type filepath: basestring|file
        :return: Audio segment instance.
        :rtype: AudioSegment
        """
        samples, sample_rate = soundfile.read(file, dtype='float32')
67 68 69 70
        return cls(samples, sample_rate)

    @classmethod
    def from_bytes(cls, bytes):
71 72 73 74 75 76 77
        """Create audio segment from a byte string containing audio samples.
        
        :param bytes: Byte string containing audio samples.
        :type bytes: str
        :return: Audio segment instance.
        :rtype: AudioSegment
        """
78 79 80 81
        samples, sample_rate = soundfile.read(
            io.BytesIO(bytes), dtype='float32')
        return cls(samples, sample_rate)

chrisxu2014's avatar
chrisxu2014 已提交
82 83
    @classmethod
    def concatenate(cls, *segments):
chrisxu2014's avatar
chrisxu2014 已提交
84 85
        """Concatenate an arbitrary number of audio segments together.

chrisxu2014's avatar
chrisxu2014 已提交
86 87
        :param *segments: Input audio segments to be concatenated.
        :type *segments: tuple of AudioSegment
chrisxu2014's avatar
chrisxu2014 已提交
88
        :return: Audio segment instance as concatenating results.
chrisxu2014's avatar
chrisxu2014 已提交
89
        :rtype: AudioSegment
chrisxu2014's avatar
chrisxu2014 已提交
90
        :raises ValueError: If the number of segments is zero, or if the 
chrisxu2014's avatar
chrisxu2014 已提交
91 92
                            sample_rate of any segments does not match.
        :raises TypeError: If any segment is not AudioSegment instance.
chrisxu2014's avatar
chrisxu2014 已提交
93 94
        """
        # Perform basic sanity-checks.
chrisxu2014's avatar
chrisxu2014 已提交
95
        if len(segments) == 0:
chrisxu2014's avatar
chrisxu2014 已提交
96 97
            raise ValueError("No audio segments are given to concatenate.")
        sample_rate = segments[0]._sample_rate
chrisxu2014's avatar
chrisxu2014 已提交
98 99
        for seg in segments:
            if sample_rate != seg._sample_rate:
chrisxu2014's avatar
chrisxu2014 已提交
100 101
                raise ValueError("Can't concatenate segments with "
                                 "different sample rates")
chrisxu2014's avatar
chrisxu2014 已提交
102
            if type(seg) is not cls:
chrisxu2014's avatar
chrisxu2014 已提交
103
                raise TypeError("Only audio segments of the same type "
chrisxu2014's avatar
chrisxu2014 已提交
104
                                "can be concatenated.")
chrisxu2014's avatar
chrisxu2014 已提交
105
        samples = np.concatenate([seg.samples for seg in segments])
chrisxu2014's avatar
chrisxu2014 已提交
106
        return cls(samples, sample_rate)
chrisxu2014's avatar
chrisxu2014 已提交
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    def to_wav_file(self, filepath, dtype='float32'):
        """Save audio segment to disk as wav file.
        
        :param filepath: WAV filepath or file object to save the
                         audio segment.
        :type filepath: basestring|file
        :param dtype: Subtype for audio file. Options: 'int16', 'int32',
                      'float32', 'float64'. Default is 'float32'.
        :type dtype: str
        :raises TypeError: If dtype is not supported.
        """
        samples = self._convert_samples_from_float32(self._samples, dtype)
        subtype_map = {
            'int16': 'PCM_16',
            'int32': 'PCM_32',
            'float32': 'FLOAT',
            'float64': 'DOUBLE'
        }
        soundfile.write(
            filepath,
            samples,
            self._sample_rate,
            format='WAV',
            subtype=subtype_map[dtype])

chrisxu2014's avatar
chrisxu2014 已提交
133 134
    @classmethod
    def slice_from_file(cls, file, start=None, end=None):
chrisxu2014's avatar
chrisxu2014 已提交
135 136 137
        """Loads a small section of an audio without having to load
        the entire file into the memory which can be incredibly wasteful.

chrisxu2014's avatar
chrisxu2014 已提交
138 139
        :param file: Input audio filepath or file object.
        :type file: basestring|file
chrisxu2014's avatar
chrisxu2014 已提交
140 141 142 143 144 145 146 147
        :param start: Start time in seconds. If start is negative, it wraps
                      around from the end. If not provided, this function 
                      reads from the very beginning.
        :type start: float
        :param end: End time in seconds. If end is negative, it wraps around
                    from the end. If not provided, the default behvaior is
                    to read to the end of the file.
        :type end: float
chrisxu2014's avatar
chrisxu2014 已提交
148 149
        :return: AudioSegment instance of the specified slice of the input
                 audio file.
chrisxu2014's avatar
chrisxu2014 已提交
150
        :rtype: AudioSegment
chrisxu2014's avatar
chrisxu2014 已提交
151 152
        :raise ValueError: If start or end is incorrectly set, e.g. out of
                           bounds in time.
chrisxu2014's avatar
chrisxu2014 已提交
153 154 155 156 157 158 159 160 161 162 163 164
        """
        sndfile = soundfile.SoundFile(file)
        sample_rate = sndfile.samplerate
        duration = float(len(sndfile)) / sample_rate
        start = 0. if start is None else start
        end = 0. if end is None else end
        if start < 0.0:
            start += duration
        if end < 0.0:
            end += duration
        if start < 0.0:
            raise ValueError("The slice start position (%f s) is out of "
chrisxu2014's avatar
chrisxu2014 已提交
165
                             "bounds." % start)
chrisxu2014's avatar
chrisxu2014 已提交
166
        if end < 0.0:
chrisxu2014's avatar
chrisxu2014 已提交
167 168
            raise ValueError("The slice end position (%f s) is out of bounds." %
                             end)
chrisxu2014's avatar
chrisxu2014 已提交
169 170 171 172
        if start > end:
            raise ValueError("The slice start position (%f s) is later than "
                             "the slice end position (%f s)." % (start, end))
        if end > duration:
chrisxu2014's avatar
chrisxu2014 已提交
173 174
            raise ValueError("The slice end position (%f s) is out of bounds "
                             "(> %f s)" % (end, duration))
chrisxu2014's avatar
chrisxu2014 已提交
175 176 177 178
        start_frame = int(start * sample_rate)
        end_frame = int(end * sample_rate)
        sndfile.seek(start_frame)
        data = sndfile.read(frames=end_frame - start_frame, dtype='float32')
chrisxu2014's avatar
chrisxu2014 已提交
179
        return cls(data, sample_rate)
chrisxu2014's avatar
chrisxu2014 已提交
180

chrisxu2014's avatar
chrisxu2014 已提交
181 182
    @classmethod
    def make_silence(cls, duration, sample_rate):
chrisxu2014's avatar
chrisxu2014 已提交
183
        """Creates a silent audio segment of the given duration and sample rate.
chrisxu2014's avatar
chrisxu2014 已提交
184

chrisxu2014's avatar
chrisxu2014 已提交
185
        :param duration: Length of silence in seconds.
chrisxu2014's avatar
chrisxu2014 已提交
186
        :type duration: float
chrisxu2014's avatar
chrisxu2014 已提交
187
        :param sample_rate: Sample rate.
chrisxu2014's avatar
chrisxu2014 已提交
188
        :type sample_rate: float
chrisxu2014's avatar
chrisxu2014 已提交
189
        :return: Silent AudioSegment instance of the given duration.
chrisxu2014's avatar
chrisxu2014 已提交
190 191 192
        :rtype: AudioSegment
        """
        samples = np.zeros(int(duration * sample_rate))
chrisxu2014's avatar
chrisxu2014 已提交
193 194
        return cls(samples, sample_rate)

chrisxu2014's avatar
chrisxu2014 已提交
195
    def superimpose(self, other):
chrisxu2014's avatar
chrisxu2014 已提交
196 197 198
        """Add samples from another segment to those of this segment
        (sample-wise addition, not segment concatenation).

chrisxu2014's avatar
chrisxu2014 已提交
199 200
        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
201 202 203
        :param other: Segment containing samples to be added in.
        :type other: AudioSegments
        :raise TypeError: If type of two segments don't match.
chrisxu2014's avatar
chrisxu2014 已提交
204 205
        :raise ValueError: If the sample rates of the two segments are not
                           equal, or if the lengths of segments don't match.
chrisxu2014's avatar
chrisxu2014 已提交
206 207 208 209 210 211 212 213 214
        """
        if type(self) != type(other):
            raise TypeError("Cannot add segments of different types: %s "
                            "and %s." % (type(self), type(other)))
        if self._sample_rate != other._sample_rate:
            raise ValueError("Sample rates must match to add segments.")
        if len(self._samples) != len(other._samples):
            raise ValueError("Segment lengths must match to add segments.")
        self._samples += other._samples
chrisxu2014's avatar
chrisxu2014 已提交
215

216 217 218
    def to_bytes(self, dtype='float32'):
        """Create a byte string containing the audio content.
        
chrisxu2014's avatar
chrisxu2014 已提交
219
        :param dtype: Data type for export samples. Options: 'int16', 'int32',
220 221 222 223 224 225 226 227
                      'float32', 'float64'. Default is 'float32'.
        :type dtype: str
        :return: Byte string containing audio content.
        :rtype: str
        """
        samples = self._convert_samples_from_float32(self._samples, dtype)
        return samples.tostring()

228
    def apply_gain(self, gain):
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        """Apply gain in decibels to samples.

        Note that this is an in-place transformation.
        
        :param gain: Gain in decibels to apply to samples. 
        :type gain: float
        """
        self._samples *= 10.**(gain / 20.)

    def change_speed(self, speed_rate):
        """Change the audio speed by linear interpolation.

        Note that this is an in-place transformation.
        
        :param speed_rate: Rate of speed change:
                           speed_rate > 1.0, speed up the audio;
                           speed_rate = 1.0, unchanged;
                           speed_rate < 1.0, slow down the audio;
                           speed_rate <= 0.0, not allowed, raise ValueError.
        :type speed_rate: float
        :raises ValueError: If speed_rate <= 0.0.
        """
        if speed_rate <= 0:
            raise ValueError("speed_rate should be greater than zero.")
        old_length = self._samples.shape[0]
        new_length = int(old_length / speed_rate)
        old_indices = np.arange(old_length)
        new_indices = np.linspace(start=0, stop=old_length, num=new_length)
        self._samples = np.interp(new_indices, old_indices, self._samples)

chrisxu2014's avatar
chrisxu2014 已提交
259
    def normalize(self, target_db=-20, max_gain_db=300.0):
chrisxu2014's avatar
chrisxu2014 已提交
260
        """Normalize audio to be of the desired RMS value in decibels.
chrisxu2014's avatar
chrisxu2014 已提交
261 262 263

        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
264 265
        :param target_db: Target RMS value in decibels. This value should be
                          less than 0.0 as 0.0 is full-scale audio.
chrisxu2014's avatar
chrisxu2014 已提交
266 267
        :type target_db: float
        :param max_gain_db: Max amount of gain in dB that can be applied for
chrisxu2014's avatar
chrisxu2014 已提交
268 269 270
                            normalization. This is to prevent nans when
                            attempting to normalize a signal consisting of
                            all zeros.
chrisxu2014's avatar
chrisxu2014 已提交
271 272 273
        :type max_gain_db: float
        :raises ValueError: If the required gain to normalize the segment to
                            the target_db value exceeds max_gain_db.
chrisxu2014's avatar
chrisxu2014 已提交
274 275 276 277
        """
        gain = target_db - self.rms_db
        if gain > max_gain_db:
            raise ValueError(
chrisxu2014's avatar
chrisxu2014 已提交
278 279 280
                "Unable to normalize segment to %f dB because the "
                "the probable gain have exceeds max_gain_db (%f dB)" %
                (target_db, max_gain_db))
chrisxu2014's avatar
chrisxu2014 已提交
281
        self.apply_gain(min(max_gain_db, target_db - self.rms_db))
chrisxu2014's avatar
chrisxu2014 已提交
282 283 284 285 286 287

    def normalize_online_bayesian(self,
                                  target_db,
                                  prior_db,
                                  prior_samples,
                                  startup_delay=0.0):
chrisxu2014's avatar
chrisxu2014 已提交
288 289 290
        """Normalize audio using a production-compatible online/causal
        algorithm. This uses an exponential likelihood and gamma prior to
        make online estimates of the RMS even when there are very few samples.
chrisxu2014's avatar
chrisxu2014 已提交
291 292 293

        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
294
        :param target_db: Target RMS value in decibels.
chrisxu2014's avatar
chrisxu2014 已提交
295
        :type target_bd: float
chrisxu2014's avatar
chrisxu2014 已提交
296
        :param prior_db: Prior RMS estimate in decibels.
chrisxu2014's avatar
chrisxu2014 已提交
297
        :type prior_db: float
chrisxu2014's avatar
chrisxu2014 已提交
298
        :param prior_samples: Prior strength in number of samples.
chrisxu2014's avatar
chrisxu2014 已提交
299
        :type prior_samples: float
chrisxu2014's avatar
chrisxu2014 已提交
300
        :param startup_delay: Default 0.0s. If provided, this function will
chrisxu2014's avatar
chrisxu2014 已提交
301 302
                              accrue statistics for the first startup_delay 
                              seconds before applying online normalization.
chrisxu2014's avatar
chrisxu2014 已提交
303
        :type startup_delay: float
chrisxu2014's avatar
chrisxu2014 已提交
304
        """
chrisxu2014's avatar
chrisxu2014 已提交
305
        # Estimate total RMS online.
chrisxu2014's avatar
chrisxu2014 已提交
306 307 308 309 310
        startup_sample_idx = min(self.num_samples - 1,
                                 int(self.sample_rate * startup_delay))
        prior_mean_squared = 10.**(prior_db / 10.)
        prior_sum_of_squares = prior_mean_squared * prior_samples
        cumsum_of_squares = np.cumsum(self.samples**2)
chrisxu2014's avatar
chrisxu2014 已提交
311
        sample_count = np.arange(len(self.num_samples)) + 1
chrisxu2014's avatar
chrisxu2014 已提交
312 313 314 315 316 317 318 319
        if startup_sample_idx > 0:
            cumsum_of_squares[:startup_sample_idx] = \
                cumsum_of_squares[startup_sample_idx]
            sample_count[:startup_sample_idx] = \
                sample_count[startup_sample_idx]
        mean_squared_estimate = ((cumsum_of_squares + prior_sum_of_squares) /
                                 (sample_count + prior_samples))
        rms_estimate_db = 10 * np.log10(mean_squared_estimate)
chrisxu2014's avatar
chrisxu2014 已提交
320
        # Compute required time-varying gain.
chrisxu2014's avatar
chrisxu2014 已提交
321 322 323 324
        gain_db = target_db - rms_estimate_db
        self.apply_gain(gain_db)

    def resample(self, target_sample_rate, quality='sinc_medium'):
chrisxu2014's avatar
chrisxu2014 已提交
325
        """Resample the audio to a target sample rate.
chrisxu2014's avatar
chrisxu2014 已提交
326 327 328

        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
329
        :param target_sample_rate: Target sample rate.
chrisxu2014's avatar
chrisxu2014 已提交
330
        :type target_sample_rate: int
chrisxu2014's avatar
chrisxu2014 已提交
331
        :param quality: One of {'sinc_fastest', 'sinc_medium', 'sinc_best'}.
chrisxu2014's avatar
chrisxu2014 已提交
332 333
                        Sets resampling speed/quality tradeoff.
                        See http://www.mega-nerd.com/SRC/api_misc.html#Converters
chrisxu2014's avatar
chrisxu2014 已提交
334
        :type quality: str
chrisxu2014's avatar
chrisxu2014 已提交
335 336
        """
        resample_ratio = target_sample_rate / self._sample_rate
chrisxu2014's avatar
chrisxu2014 已提交
337
        self._samples = scikits.samplerate.resample(
chrisxu2014's avatar
chrisxu2014 已提交
338
            self._samples, r=resample_ratio, type=quality)
chrisxu2014's avatar
chrisxu2014 已提交
339
        self._sample_rate = target_sample_rate
340

341
    def pad_silence(self, duration, sides='both'):
chrisxu2014's avatar
chrisxu2014 已提交
342
        """Pad this audio sample with a period of silence.
chrisxu2014's avatar
chrisxu2014 已提交
343 344 345

        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
346
        :param duration: Length of silence in seconds to pad.
chrisxu2014's avatar
chrisxu2014 已提交
347
        :type duration: float
chrisxu2014's avatar
chrisxu2014 已提交
348 349 350
        :param sides: Position for padding:
                     'beginning' - adds silence in the beginning;
                     'end' - adds silence in the end;
chrisxu2014's avatar
chrisxu2014 已提交
351 352
                     'both' - adds silence in both the beginning and the end.
        :type sides: str
chrisxu2014's avatar
chrisxu2014 已提交
353
        :raises ValueError: If sides is not supported.
chrisxu2014's avatar
chrisxu2014 已提交
354 355 356
        """
        if duration == 0.0:
            return self
chrisxu2014's avatar
chrisxu2014 已提交
357
        cls = type(self)
chrisxu2014's avatar
chrisxu2014 已提交
358
        silence = self.make_silence(duration, self._sample_rate)
chrisxu2014's avatar
chrisxu2014 已提交
359
        if sides == "beginning":
chrisxu2014's avatar
chrisxu2014 已提交
360
            padded = cls.concatenate(silence, self)
chrisxu2014's avatar
chrisxu2014 已提交
361
        elif sides == "end":
chrisxu2014's avatar
chrisxu2014 已提交
362
            padded = cls.concatenate(self, silence)
chrisxu2014's avatar
chrisxu2014 已提交
363
        elif sides == "both":
chrisxu2014's avatar
chrisxu2014 已提交
364
            padded = cls.concatenate(silence, self, silence)
chrisxu2014's avatar
chrisxu2014 已提交
365
        else:
chrisxu2014's avatar
chrisxu2014 已提交
366
            raise ValueError("Unknown value for the sides %s" % sides)
chrisxu2014's avatar
chrisxu2014 已提交
367
        self._samples = padded._samples
368 369

    def subsegment(self, start_sec=None, end_sec=None):
chrisxu2014's avatar
chrisxu2014 已提交
370 371 372
        """Cut the AudioSegment between given boundaries.

        Note that this is an in-place transformation.
chrisxu2014's avatar
chrisxu2014 已提交
373

chrisxu2014's avatar
chrisxu2014 已提交
374
        :param start_sec: Beginning of subsegment in seconds.
chrisxu2014's avatar
chrisxu2014 已提交
375
        :type start_sec: float
chrisxu2014's avatar
chrisxu2014 已提交
376
        :param end_sec: End of subsegment in seconds.
chrisxu2014's avatar
chrisxu2014 已提交
377
        :type end_sec: float
chrisxu2014's avatar
chrisxu2014 已提交
378 379
        :raise ValueError: If start_sec or end_sec is incorrectly set, e.g. out
                           of bounds in time.
chrisxu2014's avatar
chrisxu2014 已提交
380
        """
chrisxu2014's avatar
chrisxu2014 已提交
381 382
        start_sec = 0.0 if start_sec is None else start_sec
        end_sec = self.duration if end_sec is None else end_sec
chrisxu2014's avatar
chrisxu2014 已提交
383 384 385 386
        if start_sec < 0.0:
            start_sec = self.duration + start_sec
        if end_sec < 0.0:
            end_sec = self.duration + end_sec
chrisxu2014's avatar
chrisxu2014 已提交
387 388 389 390 391 392 393 394 395 396 397 398
        if start_sec < 0.0:
            raise ValueError("The slice start position (%f s) is out of "
                             "bounds." % start_sec)
        if end_sec < 0.0:
            raise ValueError("The slice end position (%f s) is out of bounds." %
                             end_sec)
        if start_sec > end_sec:
            raise ValueError("The slice start position (%f s) is later than "
                             "the end position (%f s)." % (start_sec, end_sec))
        if end_sec > self.duration:
            raise ValueError("The slice end position (%f s) is out of bounds "
                             "(> %f s)" % (end_sec, self.duration))
chrisxu2014's avatar
chrisxu2014 已提交
399 400
        start_sample = int(round(start_sec * self._sample_rate))
        end_sample = int(round(end_sec * self._sample_rate))
chrisxu2014's avatar
chrisxu2014 已提交
401
        self._samples = self._samples[start_sample:end_sample]
chrisxu2014's avatar
chrisxu2014 已提交
402 403

    def random_subsegment(self, subsegment_length, rng=None):
chrisxu2014's avatar
chrisxu2014 已提交
404 405 406
        """Cut the specified length of the audiosegment randomly.

        Note that this is an in-place transformation.
chrisxu2014's avatar
chrisxu2014 已提交
407 408

        :param subsegment_length: Subsegment length in seconds.
chrisxu2014's avatar
chrisxu2014 已提交
409
        :type subsegment_length: float
chrisxu2014's avatar
chrisxu2014 已提交
410
        :param rng: Random number generator state.
chrisxu2014's avatar
chrisxu2014 已提交
411
        :type rng: random.Random
chrisxu2014's avatar
chrisxu2014 已提交
412 413
        :raises ValueError: If the length of subsegment is greater than
                            the origineal segemnt.
chrisxu2014's avatar
chrisxu2014 已提交
414
        """
chrisxu2014's avatar
chrisxu2014 已提交
415
        rng = random.Random() if rng is None else rng
chrisxu2014's avatar
chrisxu2014 已提交
416 417 418 419
        if subsegment_length > self.duration:
            raise ValueError("Length of subsegment must not be greater "
                             "than original segment.")
        start_time = rng.uniform(0.0, self.duration - subsegment_length)
chrisxu2014's avatar
chrisxu2014 已提交
420
        self.subsegment(start_time, start_time + subsegment_length)
chrisxu2014's avatar
chrisxu2014 已提交
421

chrisxu2014's avatar
chrisxu2014 已提交
422
    def convolve(self, impulse_segment, allow_resample=False):
chrisxu2014's avatar
chrisxu2014 已提交
423
        """Convolve this audio segment with the given impulse segment.
chrisxu2014's avatar
chrisxu2014 已提交
424

chrisxu2014's avatar
chrisxu2014 已提交
425
        Note that this is an in-place transformation.
chrisxu2014's avatar
chrisxu2014 已提交
426

chrisxu2014's avatar
chrisxu2014 已提交
427 428
        :param impulse_segment: Impulse response segments.
        :type impulse_segment: AudioSegment
chrisxu2014's avatar
chrisxu2014 已提交
429 430 431 432
        :param allow_resample: Indicates whether resampling is allowed when
                               the impulse_segment has a different sample 
                               rate from this signal.
        :type allow_resample: bool
chrisxu2014's avatar
chrisxu2014 已提交
433
        :raises ValueError: If the sample rate is not match between two
chrisxu2014's avatar
chrisxu2014 已提交
434
                            audio segments when resample is not allowed.
chrisxu2014's avatar
chrisxu2014 已提交
435 436 437 438 439 440 441 442 443
        """
        if allow_resample and self.sample_rate != impulse_segment.sample_rate:
            impulse_segment = impulse_segment.resample(self.sample_rate)
        if self.sample_rate != impulse_segment.sample_rate:
            raise ValueError("Impulse segment's sample rate (%d Hz) is not"
                             "equal to base signal sample rate (%d Hz)." %
                             (impulse_segment.sample_rate, self.sample_rate))
        samples = signal.fftconvolve(self.samples, impulse_segment.samples,
                                     "full")
chrisxu2014's avatar
chrisxu2014 已提交
444 445
        self._samples = samples

chrisxu2014's avatar
chrisxu2014 已提交
446
    def convolve_and_normalize(self, impulse_segment, allow_resample=False):
chrisxu2014's avatar
chrisxu2014 已提交
447 448 449
        """Convolve and normalize the resulting audio segment so that it
        has the same average power as the input signal.

chrisxu2014's avatar
chrisxu2014 已提交
450 451
        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
452 453
        :param impulse_segment: Impulse response segments.
        :type impulse_segment: AudioSegment
chrisxu2014's avatar
chrisxu2014 已提交
454 455 456 457
        :param allow_resample: Indicates whether resampling is allowed when
                               the impulse_segment has a different sample
                               rate from this signal.
        :type allow_resample: bool
chrisxu2014's avatar
chrisxu2014 已提交
458
        """
chrisxu2014's avatar
chrisxu2014 已提交
459 460 461
        target_db = self.rms_db
        self.convolve(impulse_segment, allow_resample=allow_resample)
        self.normalize(target_db)
chrisxu2014's avatar
chrisxu2014 已提交
462 463 464 465 466 467 468

    def add_noise(self,
                  noise,
                  snr_dB,
                  allow_downsampling=False,
                  max_gain_db=300.0,
                  rng=None):
chrisxu2014's avatar
chrisxu2014 已提交
469
        """Add the given noise segment at a specific signal-to-noise ratio.
chrisxu2014's avatar
chrisxu2014 已提交
470 471 472
        If the noise segment is longer than this segment, a random subsegment
        of matching length is sampled from it and used instead.

chrisxu2014's avatar
chrisxu2014 已提交
473 474
        Note that this is an in-place transformation.

chrisxu2014's avatar
chrisxu2014 已提交
475
        :param noise: Noise signal to add.
chrisxu2014's avatar
chrisxu2014 已提交
476
        :type noise: AudioSegment
chrisxu2014's avatar
chrisxu2014 已提交
477
        :param snr_dB: Signal-to-Noise Ratio, in decibels.
chrisxu2014's avatar
chrisxu2014 已提交
478
        :type snr_dB: float
chrisxu2014's avatar
chrisxu2014 已提交
479 480 481 482 483 484 485
        :param allow_downsampling: Whether to allow the noise signal to be
                                   downsampled to match the base signal sample
                                   rate.
        :type allow_downsampling: bool
        :param max_gain_db: Maximum amount of gain to apply to noise signal
                            before adding it in. This is to prevent attempting
                            to apply infinite gain to a zero signal.
chrisxu2014's avatar
chrisxu2014 已提交
486
        :type max_gain_db: float
chrisxu2014's avatar
chrisxu2014 已提交
487
        :param rng: Random number generator state.
chrisxu2014's avatar
chrisxu2014 已提交
488 489
        :type rng: None|random.Random
        :raises ValueError: If the sample rate does not match between the two
chrisxu2014's avatar
chrisxu2014 已提交
490 491
                            audio segments when downsampling is not allowed, or
                            if the duration of noise segments is shorter than
chrisxu2014's avatar
chrisxu2014 已提交
492
                            original audio segments.
chrisxu2014's avatar
chrisxu2014 已提交
493
        """
chrisxu2014's avatar
chrisxu2014 已提交
494
        rng = random.Random() if rng is None else rng
chrisxu2014's avatar
chrisxu2014 已提交
495 496 497
        if allow_downsampling and noise.sample_rate > self.sample_rate:
            noise = noise.resample(self.sample_rate)
        if noise.sample_rate != self.sample_rate:
chrisxu2014's avatar
chrisxu2014 已提交
498 499 500
            raise ValueError("Noise sample rate (%d Hz) is not equal to base "
                             "signal sample rate (%d Hz)." % (noise.sample_rate,
                                                              self.sample_rate))
chrisxu2014's avatar
chrisxu2014 已提交
501
        if noise.duration < self.duration:
chrisxu2014's avatar
chrisxu2014 已提交
502 503
            raise ValueError("Noise signal (%f sec) must be at least as long as"
                             " base signal (%f sec)." %
chrisxu2014's avatar
chrisxu2014 已提交
504
                             (noise.duration, self.duration))
chrisxu2014's avatar
chrisxu2014 已提交
505
        noise_gain_db = min(self.rms_db - noise.rms_db - snr_dB, max_gain_db)
chrisxu2014's avatar
chrisxu2014 已提交
506 507 508 509
        noise_new = copy.deepcopy(noise)
        noise_new.random_subsegment(self.duration, rng=rng)
        noise_new.apply_gain(noise_gain_db)
        self.superimpose(noise_new)
chrisxu2014's avatar
chrisxu2014 已提交
510

511 512
    @property
    def samples(self):
513 514 515 516 517
        """Return audio samples.

        :return: Audio samples.
        :rtype: ndarray
        """
518 519 520 521
        return self._samples.copy()

    @property
    def sample_rate(self):
522 523 524 525 526
        """Return audio sample rate.

        :return: Audio sample rate.
        :rtype: int
        """
527 528 529
        return self._sample_rate

    @property
530 531
    def num_samples(self):
        """Return number of samples.
532

533 534 535
        :return: Number of samples.
        :rtype: int
        """
chrisxu2014's avatar
chrisxu2014 已提交
536
        return self._samples.shape[0]
537

538 539 540
    @property
    def duration(self):
        """Return audio duration.
541

542 543 544 545
        :return: Audio duration in seconds.
        :rtype: float
        """
        return self._samples.shape[0] / float(self._sample_rate)
546 547

    @property
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    def rms_db(self):
        """Return root mean square energy of the audio in decibels.

        :return: Root mean square energy in decibels.
        :rtype: float
        """
        # square root => multiply by 10 instead of 20 for dBs
        mean_square = np.mean(self._samples**2)
        return 10 * np.log10(mean_square)

    def _convert_samples_to_float32(self, samples):
        """Convert sample type to float32.

        Audio sample type is usually integer or float-point.
        Integers will be scaled to [-1, 1] in float32.
        """
        float32_samples = samples.astype('float32')
        if samples.dtype in np.sctypes['int']:
            bits = np.iinfo(samples.dtype).bits
            float32_samples *= (1. / 2**(bits - 1))
        elif samples.dtype in np.sctypes['float']:
            pass
        else:
            raise TypeError("Unsupported sample type: %s." % samples.dtype)
        return float32_samples

    def _convert_samples_from_float32(self, samples, dtype):
        """Convert sample type from float32 to dtype.
        
        Audio sample type is usually integer or float-point. For integer
        type, float32 will be rescaled from [-1, 1] to the maximum range
        supported by the integer type.
chrisxu2014's avatar
chrisxu2014 已提交
580

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
        This is for writing a audio file.
        """
        dtype = np.dtype(dtype)
        output_samples = samples.copy()
        if dtype in np.sctypes['int']:
            bits = np.iinfo(dtype).bits
            output_samples *= (2**(bits - 1) / 1.)
            min_val = np.iinfo(dtype).min
            max_val = np.iinfo(dtype).max
            output_samples[output_samples > max_val] = max_val
            output_samples[output_samples < min_val] = min_val
        elif samples.dtype in np.sctypes['float']:
            min_val = np.finfo(dtype).min
            max_val = np.finfo(dtype).max
            output_samples[output_samples > max_val] = max_val
            output_samples[output_samples < min_val] = min_val
        else:
            raise TypeError("Unsupported sample type: %s." % samples.dtype)
        return output_samples.astype(dtype)