Esempio n. 1
0
    def allocated_channels(self):
        """
        The number of channels currently allocated by SDL_mixer.

        Seems to default to 8.
        """
        return mix_call(Mix_AllocateChannels, -1)
Esempio n. 2
0
 def background_parse(self, data):
     file = rw_from_object(io.BytesIO(data))
     # ^^^^ is a pure-python emulation, does not need cleanup.
     return mix_call(Mix_LoadWAV_RW,
                     file,
                     False,
                     _check_error=lambda rv: not rv)
Esempio n. 3
0
 def background_parse(self, data):
     # Band-aid over some synchronization issues
     # https://github.com/ppb/pursuedpybear/issues/619
     while not any(query_spec()):
         time.sleep(0)
     file = rw_from_object(io.BytesIO(data))
     # ^^^^ is a pure-python emulation, does not need cleanup.
     return mix_call(Mix_LoadWAV_RW,
                     file,
                     False,
                     _check_error=lambda rv: not rv)
Esempio n. 4
0
 def __exit__(self, *exc):
     # Unregister callback and release reference
     mix_call(Mix_ChannelFinished, _filler_channel_finished)
     self._finished_callback = None
     # Cleanup SDL_mixer
     mix_call(Mix_CloseAudio)
     mix_call(Mix_Quit)
     super().__exit__(*exc)
Esempio n. 5
0
def query_spec():
    """
    Helpful wrapper around Mix_QuerySpec()
    """
    frequency = ctypes.c_int()
    format = ctypes.c_uint16()
    channels = ctypes.c_int()
    count = mix_call(
        Mix_QuerySpec,
        ctypes.byref(frequency),
        ctypes.byref(format),
        ctypes.byref(channels),
        _check_error=lambda rv: rv == 0 and Mix_GetError(),
    )
    return count, frequency, format, channels
Esempio n. 6
0
    def on_play_sound(self, event, signal):
        sound = event.sound
        chunk = event.sound.load()

        try:
            channel = mix_call(
                Mix_PlayChannel,
                -1,  # Auto-pick channel
                chunk,
                0,  # Do not repeat
                _check_error=lambda rv: rv == -1)
        except SdlMixerError as e:
            if not str(e).endswith("No free channels available"):
                raise
            self.logger.warn(
                "Attempted to play sound, but there were no available channels."
            )
        else:
            self._currently_playing[
                channel] = sound  # Keep reference of playing asset
Esempio n. 7
0
    def __enter__(self):
        super().__enter__()
        mix_call(
            Mix_OpenAudio,
            44100,  # Sample frequency, 44.1 kHz is CD quality
            AUDIO_S16SYS,  # Audio, 16-bit, system byte order. IDK is signed makes a difference
            2,  # Number of output channels, 2=stereo
            4096,  # Chunk size. TBH, this is a magic knob number.
            # ^^^^ Smaller is more CPU, larger is less responsive.
            # A lot of the performance-related recommendations are so dated I'm
            # not sure how much difference it makes.
            _check_error=lambda rv: rv == -1)
        mix_call(Mix_Init,
                 MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG)
        self.allocated_channels = 16

        # Register callback, keeping reference for later cleanup
        self._finished_callback = channel_finished(self._on_channel_finished)
        mix_call(Mix_ChannelFinished, self._finished_callback)
Esempio n. 8
0
 def allocated_channels(self, value):
     mix_call(Mix_AllocateChannels, value)
Esempio n. 9
0
 def volume(self, value):
     mix_call(Mix_VolumeChunk, self.load(), int(value * MIX_MAX_VOLUME))
Esempio n. 10
0
 def volume(self):
     """
     The volume setting of this chunk, from 0.0 to 1.0
     """
     return mix_call(Mix_VolumeChunk, self.load(), -1) / MIX_MAX_VOLUME