コード例 #1
0
ファイル: mixer.py プロジェクト: jggatc/pyjsdl-ts
 def __init__(self, sound_file, id=None):
     if id is None:
         self._id = Sound._id
         Sound._id += 1
     else:
         self._id = id
     if isinstance(sound_file, str):
         self._sound_object = Audio(sound_file.replace('\\','/'))
     else:
         self._sound_object = sound_file
     self._sound_objects = []
     self._sound_objects.append(self._sound_object)
     self._channel = None
     self._volume = 1.0
コード例 #2
0
ファイル: mixer.py プロジェクト: jggatc/pyjsdl
 def __init__(self, sound_file, id=None):
     if id is None:
         self._id = Sound._id
         Sound._id += 1
         self._mixer._register_sound(self)
     else:
         self._id = id
     if isinstance(sound_file, str):
         self._sound_object = Audio(sound_file.replace('\\','/'))
     else:
         self._sound_object = sound_file
     self._sound_objects = []
     self._channel = None
     self._ch = None
     self._volume = 1.0
     self._nonimplemented_methods()
コード例 #3
0
ファイル: mixer.py プロジェクト: jggatc/pyjsdl-ts
 def get_sound_object(self):
     if len(self._sound_objects) > 0:
         sound_object = self._sound_objects.pop()
     else:
         sound_object = Audio(self._sound_object.getSrc())
     return sound_object
コード例 #4
0
ファイル: mixer.py プロジェクト: jggatc/pyjsdl-ts
class Sound(object):
    """
    **pyjsdl.mixer.Sound**
    
    * Sound.play
    * Sound.stop
    * Sound.fadeout
    * Sound.set_volume
    * Sound.get_volume
    * Sound.get_num_channels
    * Sound.get_length
    """

    _id = 0
    _mixer = None

    def __init__(self, sound_file, id=None):
        if id is None:
            self._id = Sound._id
            Sound._id += 1
        else:
            self._id = id
        if isinstance(sound_file, str):
            self._sound_object = Audio(sound_file.replace('\\','/'))
        else:
            self._sound_object = sound_file
        self._sound_objects = []
        self._sound_objects.append(self._sound_object)
        self._channel = None
        self._volume = 1.0

    def play(self, loops=0, maxtime=0, fade_ms=0):
        """
        Play sound on mixer channel.
        Argument loops is repeat number or -1 for continuous,
        maxtime is maximum play time, and fade_ms is fade-in time.
        """
        self._channel = self._mixer._retrieve_channel()
        if self._channel:
            self._channel._play(self, loops, maxtime, fade_ms)
        return self._channel

    def stop(self):
        """
        Stop sound on active channels.
        """
        channels = self._mixer._channels
        for id in self._mixer._channel_active:
            if id > -1:
                try:
                    if channels[id]._sound._id == self._id:
                        channels[id].stop()
                except AttributeError:
                    continue
        return None

    def fadeout(self, time):
        """
        Fadeout sound on active channels in given time.
        """
        channels = self._mixer._channels
        for id in self._mixer._channel_active:
            if id > -1:
                try:
                    if channels[id]._sound._id == self._id:
                        channels[id].fadeout(time)
                except AttributeError:
                    continue
        return None

    def set_volume(self, volume):
        """
        Set sound volume.
        Argument volume of value 0.0 to 1.0.
        """
        if volume < 0.0:
            volume = 0.0
        elif volume > 1.0:
            volume = 1.0
        self._volume = volume
        return None

    def get_volume(self):
        """
        Get sound volume.
        """
        return self._volume

    def get_num_channels(self):
        """
        Get number of channels sound is active.
        """
        channels = self._mixer._channels
        channel = 0
        for id in self._mixer._channel_active:
            if id > -1:
                try:
                    if channels[id]._sound._id == self._id:
                        channel += 1
                except AttributeError:
                    continue
        return channel

    def get_length(self):
        """
        Get length of sound sample.
        """
        return self._sound_object.getDuration()

    def get_sound_object(self):
        if len(self._sound_objects) > 0:
            sound_object = self._sound_objects.pop()
        else:
            sound_object = Audio(self._sound_object.getSrc())
        return sound_object
コード例 #5
0
ファイル: mixer.py プロジェクト: jggatc/pyjsdl
class Sound:
    """
    **pyjsdl.mixer.Sound**
    
    * Sound.play
    * Sound.stop
    * Sound.set_volume
    * Sound.get_volume
    * Sound.get_num_channels
    * Sound.get_length
    """

    _id = 0
    _mixer = None

    def __init__(self, sound_file, id=None):
        if id is None:
            self._id = Sound._id
            Sound._id += 1
            self._mixer._register_sound(self)
        else:
            self._id = id
        if isinstance(sound_file, str):
            self._sound_object = Audio(sound_file.replace('\\','/'))
        else:
            self._sound_object = sound_file
        self._sound_objects = []
        self._channel = None
        self._ch = None
        self._volume = 1.0
        self._nonimplemented_methods()

    def play(self, loops=0, maxtime=0, fade_ms=0):
        """
        Play sound on mixer channel.
        Argument loops is number of repeats or -1 for continuous.
        """
        if not self._channel:
            self._channel = self._mixer.find_channel()
            if self._channel:
                self._channel._set_sound(self)
            else:
                return None
        if self._sound_object.isPaused():
            self._ch = self._channel
        else:
            self._ch = self._mixer.find_channel()
            if self._ch:
                sound = Sound(self._sound_object.getSrc(), self._id)
                self._ch._set_sound(sound)
            else:
                return None
        if not loops:
            self._ch._play()
        else:
            self._ch._play_repeat(loops)
        return self._ch

    def stop(self):
        """
        Stop sound on mixer channel.
        """
        if self._channel:
            self._channel.stop()

    def set_volume(self, volume):
        """
        Set sound volume.
        Argument volume of value 0.0 to 1.0.
        """
        if volume < 0.0:
            volume = 0.0
        elif volume > 1.0:
            volume = 1.0
        self._volume = volume
        self._sound_object.setVolume(self._volume)
        return None

    def get_volume(self):
        """
        Get sound volume.
        """
        return self._volume

    def get_num_channels(self):
        """
        Get number of channels sound is active.
        """
        channel = 0
        for id in self._mixer._channel_pool:
            try:
                if self._mixer._channels[id]._sound._id == self._id:
                    channel += 1
            except AttributeError:
                continue
        return channel

    def get_length(self):
        """
        Get length of sound sample.
        """
        return self._sound_object.getDuration()

    def _nonimplemented_methods(self):
        """
        Non-implemented methods.
        """
        self.fadeout = lambda *arg: None
        self.get_buffer = lambda *arg: None