def __enter__(self): LOG.debug('Initializing SDL context') sdl.SDL_Init(sdl.SDL_INIT_EVERYTHING) LOG.debug('Initializing SDL mixer') if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024): raise RuntimeError( 'Cannot open mixed audio: {}'.format(sdlmixer.Mix_GetError())) if sdlmixer.Mix_Init(0) == -1: raise RuntimeError( 'Cannot initialize mixer: {}'.format(sdlmixer.Mix_GetError())) return self
def __init__(self, game): super(SoundMixer, self).__init__() self.mute = False self.sounds = [] if sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) != 0: raise RuntimeError("Cannot initialize audio system: {}".format( sdl2.SDL_GetError())) if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024): raise RuntimeError("Cannot open mixed audio: {}".format( sdlmixer.Mix_GetError())) if sdlmixer.Mix_AllocateChannels(64) != 64: raise RuntimeError("Cannot allocate audio channels: {}".format( sdlmixer.Mix_GetError()))
def play(self, loops): channel = sdlmixer.Mix_PlayChannel(channel=-1, chunk=self.sample, loops=loops) if channel == -1: raise RuntimeError("Cannot play sample: {}".format( sdlmixer.Mix_GetError())) return channel
def loadSound(self, file): # find if this file has been loaded before and return that if so filepath = os.path.abspath(file) for index, sound in enumerate(self.sounds): if sound.path == filepath: return index # haven't seen this file before so add it and return new index index = self._getNextEmptySlot() try: sample = sdlmixer.Mix_LoadWAV( sdl2.ext.compat.byteify(filepath, "utf-8")) if sample is None: raise RuntimeError("Cannot open audio file: {}".format( sdlmixer.Mix_GetError())) self.sounds[index] = Sample(filepath, sample) except Exception as e: log(f"Problem loading sound: {str(e)} file: {filepath}") return len(self.sounds) - 1
def play(self, volume=1.0, repeat=1): ''' Play the sound *Parameters:* - `volume`: Sound volume 0.0 to 1.0 - `repeat`: Number of time to repeat the sound, 0=infinity *Returns:* Channel id of the sound ''' channel = mixer.Mix_PlayChannel(-1, self.sample, repeat-1) if channel == -1: msg = "Cannot play the sound: %s" % mixer.Mix_GetError() logger.error(msg) raise SoundError(msg) mixer.Mix_Volume(channel, int(mixer.MIX_MAX_VOLUME * volume)) return channel
def open(self, configuration): '''Open and configure audio system *Parameters:* - `configuration`: Configuration parameters from Application ''' if sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_AUDIO) != 0: msg = "Cannot initialize audio system: %s" % sdl2.SDL_GetError() logger.critical(msg) raise SDL2Error(msg) if mixer.Mix_OpenAudio(mixer.MIX_DEFAULT_FREQUENCY, mixer.MIX_DEFAULT_FORMAT, 2, 1024): msg = "Cannot open mixed audio: %s" % mixer.Mix_GetError() logger.critical(msg) raise SDL2Error(msg) mixer.Mix_AllocateChannels(configuration.audio_channel) logger.info("Audio initialized")
def play(self, volume=1.0, repeat=1): ''' Play the music *Parameters:* - `volume`: Sound volume 0.0 to 1.0 - `repeat`: Number of time to repeat the sound, 0=infinity *Returns:* Channel id of the sound ''' if not repeat: repeat = -1 result = mixer.Mix_PlayMusic(self.music, repeat) if result == -1: msg = "Cannot play the music: %s" % mixer.Mix_GetError() logger.error(msg) raise SoundError(msg) mixer.Mix_VolumeMusic(int(mixer.MIX_MAX_VOLUME * volume))
def check_audio_codecs(): sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) try: libs = { 'FLAC': sdlmixer.MIX_INIT_FLAC, 'MOD': sdlmixer.MIX_INIT_MOD, 'MP3': sdlmixer.MIX_INIT_MP3, 'OGG': sdlmixer.MIX_INIT_OGG, 'MIDI': sdlmixer.MIX_INIT_MID, 'OPUS': sdlmixer.MIX_INIT_OPUS } for lib, flags in libs.items(): sdlmixer.Mix_SetError(b"") ret = sdlmixer.Mix_Init(flags) err = sdlmixer.Mix_GetError() if err: yield lib, err.decode('utf-8') if ret & flags == flags: yield lib, None else: yield lib, True sdlmixer.Mix_Quit() finally: sdl2.SDL_Quit()
import traceback traceback.print_exc() sys.exit(1) if __name__ == "__main__": RESOURCES = sdl2.ext.Resources(__file__, "") # Audio if sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) != 0: raise RuntimeError("Cannot initialize audio system: {}".format( sdl2.SDL_GetError())) # int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize) if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024): raise RuntimeError("Cannot open mixed audio: {}".format( sdlmixer.Mix_GetError())) sdlmixer.Mix_AllocateChannels(64) sound_file = RESOURCES.get_path("Cat.wav") sample = sdlmixer.Mix_LoadWAV(sdl2.ext.compat.byteify(sound_file, "utf-8")) if sample is None: raise RuntimeError("Cannot open audio file: {}".format( sdlmixer.Mix_GetError())) count = 5 while count > 0: channel = sdlmixer.Mix_PlayChannel(channel=-1, chunk=sample, loops=0) sdlmixer.Mix_SetPosition(channel, 270, 100) print(channel) if channel == -1: raise RuntimeError("Cannot play sample: {}".format(