예제 #1
0
    def __generate(self, sound):
        """Generate samples for the tone.

        This generator yields samples corresponding to the tones properties.
        The sound object the tone is being applied to is supplied to ensure
        that the matching sampling rate is used.
        The previous sine wave phase is tracked in order to calculate the
        next value.

        Arguments:
        sound -- the sound.Sound object the tone is being generated for
        """

        sample_count = sound.convert_secs_to_samples(self.seconds)
        # No previous sample so start at 0
        previous_phase = 0
        for i in xrange(sample_count):
            sample, previous_phase = self._create_sample(sound.sampling_rate, i, previous_phase)
            yield sample
예제 #2
0
    def combine_tone(self, sound, start_position):
        """Combine the tone with a Sound object.

        This method combines the tone with a given sound object instance,
        starting at the given time. This can be used to add tones to a sound
        without having to create an  additional Sound object.
        If the tone is longer than the sound, it will extend past the end
        of the sound.

        Arguments:
        sound -- Sound object instance
        start_position -- start position in seconds
        """

        index = sound.convert_secs_to_samples(start_position)
        for sample in self.__generate(sound):
            if index < len(sound.samples):
                sound.combine_sample_at_index(sample, index)
            else:
                sound.add_sample(sample)
            index += 1