Ejemplo n.º 1
0
    def max_play(self, value):
        if value is None:
            value = 0

        if self.__sound is not None:
            value = max(0, value)
            while len(self.__channels) < value:
                self.__channels.append(_get_channel())
            while len(self.__channels) > value:
                _release_channel(self.__channels.pop(-1))
Ejemplo n.º 2
0
 def parent(self, value):
     if value != self.__parent:
         self.__parent = value
         if value is None:
             self.rd["channels"] = []
             self.max_play = self.__max_play
         else:
             while self.rd["channels"]:
                 _release_channel(self.rd["channels"].pop(-1))
             self.rd["channels"] = value.rd["channels"]
Ejemplo n.º 3
0
    def max_play(self, value):
        if value is None:
            value = 0

        if self.__sound is not None:
            value = max(0, value)
            while len(self.__channels) < value:
                self.__channels.append(_get_channel())
            while len(self.__channels) > value:
                _release_channel(self.__channels.pop(-1))
Ejemplo n.º 4
0
    def max_play(self, value):
        self.__max_play = value

        if value is None:
            value = 0

        if self.__sound is not None and self.parent is None:
            value = max(0, value)
            while len(self.rd["channels"]) < value:
                self.rd["channels"].append(_get_channel())
            while len(self.rd["channels"]) > value:
                _release_channel(self.rd["channels"].pop(-1))
Ejemplo n.º 5
0
    def play(self, loops=1, volume=100, balance=0, maxtime=None,
             fade_time=None):
        """
        Play the sound.

        Arguments:

        - ``loops`` -- The number of times to play the sound; set to
          :const:`None` or ``0`` to loop indefinitely.
        - ``volume`` -- The volume to play the sound at as a percentage
          of :attr:`self.volume` from ``0`` to ``100`` (``0`` for no
          sound, ``100`` for :attr:`self.volume`).
        - ``balance`` -- The balance of the sound effect on stereo
          speakers as a float from ``-1`` to ``1``, where ``0`` is
          centered (full volume in both speakers), ``1`` is entirely in
          the right speaker, and ``-1`` is entirely in the left speaker.
        - ``maxtime`` -- The maximum amount of time to play the sound in
          milliseconds; set to :const:`None` for no limit.
        - ``fade_time`` -- The time in milliseconds over which to fade
          the sound in; set to :const:`None` or ``0`` to immediately
          play the sound at full volume.
        """
        if self.__sound is not None:
            if not loops:
                loops = 0
            if maxtime is None:
                maxtime = 0
            if fade_time is None:
                fade_time = 0

            # Adjust for the way Pygame does repeats
            loops -= 1

            # Calculate volume for each speaker
            left_volume = (self.volume / 100) * (volume / 100)
            right_volume = left_volume
            if balance < 0:
                right_volume *= 1 - abs(balance)
            elif balance > 0:
                left_volume *= 1 - abs(balance)

            if self.max_play:
                for channel in self.__channels:
                    if not channel.get_busy():
                        channel.play(self.__sound, loops, maxtime, fade_time)
                        channel.set_volume(left_volume, right_volume)
                        break
                else:
                    self.__channels[0].play(self.__sound, loops, maxtime,
                                            fade_time)
                    self.__channels[0].set_volume(left_volume, right_volume)
            else:
                channel = _get_channel()
                channel.play(self.__sound, loops, maxtime, fade_time)
                channel.set_volume(left_volume, right_volume)
                self.__temp_channels.append(channel)

            # Clean up old temporary channels
            while (self.__temp_channels and
                   not self.__temp_channels[0].get_busy()):
                _release_channel(self.__temp_channels.pop(0))
Ejemplo n.º 6
0
    def play(self, loops=1, volume=1, balance=0, maxtime=None,
             fade_time=None):
        """
        Play the sound.

        Arguments:

        - ``loops`` -- The number of times to play the sound; set to
          :const:`None` or ``0`` to loop indefinitely.
        - ``volume`` -- The volume to play the sound at as a factor
          of :attr:`self.volume` (``0`` for no sound, ``1`` for
          :attr:`self.volume`).
        - ``balance`` -- The balance of the sound effect on stereo
          speakers as a float from ``-1`` to ``1``, where ``0`` is
          centered (full volume in both speakers), ``1`` is entirely in
          the right speaker, and ``-1`` is entirely in the left speaker.
        - ``maxtime`` -- The maximum amount of time to play the sound in
          milliseconds; set to :const:`None` for no limit.
        - ``fade_time`` -- The time in milliseconds over which to fade
          the sound in; set to :const:`None` or ``0`` to immediately
          play the sound at full volume.
        """
        if self.__sound is not None:
            if not loops:
                loops = 0
            if maxtime is None:
                maxtime = 0
            if fade_time is None:
                fade_time = 0

            # Adjust for the way Pygame does repeats
            loops -= 1

            # Calculate volume for each speaker
            left_volume = self.volume * volume
            right_volume = left_volume
            if balance < 0:
                right_volume *= 1 - abs(balance)
            elif balance > 0:
                left_volume *= 1 - abs(balance)

            if self.max_play:
                for channel in self.__channels:
                    if not channel.get_busy():
                        channel.play(self.__sound, loops, maxtime, fade_time)
                        channel.set_volume(left_volume, right_volume)
                        break
                else:
                    self.__channels[0].play(self.__sound, loops, maxtime,
                                            fade_time)
                    self.__channels[0].set_volume(left_volume, right_volume)
            else:
                channel = _get_channel()
                channel.play(self.__sound, loops, maxtime, fade_time)
                channel.set_volume(left_volume, right_volume)
                self.__temp_channels.append(channel)

            # Clean up old temporary channels
            while (self.__temp_channels and
                   not self.__temp_channels[0].get_busy()):
                _release_channel(self.__temp_channels.pop(0))