<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> from cocos . audio . SDL import * <EOL> from cocos . audio . SDL . mixer import * <EOL> from cocos . audio . SDL . rwops import * <EOL> from cocos . audio . pygame import base <EOL> try : <EOL> from cocos . audio . pygame import music <EOL> _have_music = True <EOL> except ImportError : <EOL> _have_music = False <EOL> _request_frequency = MIX_DEFAULT_FREQUENCY <EOL> _request_size = MIX_DEFAULT_FORMAT <EOL> _request_stereo = MIX_DEFAULT_CHANNELS <EOL> _request_buffer = <NUM_LIT> <EOL> _channels = { } <EOL> try : <EOL> import queue <EOL> except ImportError : <EOL> import Queue as queue <EOL> event_queue = queue . Queue ( <NUM_LIT> ) <EOL> def __PYGAMEinit__ ( frequency = None , size = None , stereo = None , buffer = None ) : <EOL> if frequency is None : <EOL> frequency = _request_frequency <EOL> if size is None : <EOL> size = _request_size <EOL> if stereo is None : <EOL> stereo = _request_stereo <EOL> if buffer is None : <EOL> buffer = _request_buffer <EOL> stereo = min ( <NUM_LIT:2> , stereo ) <EOL> if size == <NUM_LIT:8> : <EOL> size = AUDIO_U8 <EOL> elif size == - <NUM_LIT:8> : <EOL> size = AUDIO_S8 <EOL> elif size == <NUM_LIT:16> : <EOL> size == AUDIO_U16SYS <EOL> elif size == - <NUM_LIT:16> : <EOL> size = AUDIO_S16SYS <EOL> i = <NUM_LIT> <EOL> while i < buffer : <EOL> i <<= <NUM_LIT:1> <EOL> buffer = i <EOL> global _endsound_callback <EOL> if not SDL_WasInit ( SDL_INIT_AUDIO ) : <EOL> base . register_quit ( _autoquit ) <EOL> SDL_InitSubSystem ( SDL_INIT_AUDIO ) <EOL> Mix_OpenAudio ( frequency , size , stereo , buffer ) <EOL> if Mix_Linked_Version ( ) . is_since ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) : <EOL> Mix_ChannelFinished ( _endsound_callback ) <EOL> Mix_VolumeMusic ( <NUM_LIT> ) <EOL> return <NUM_LIT:1> <EOL> def _autoquit ( ) : <EOL> global _channels <EOL> if SDL_WasInit ( SDL_INIT_AUDIO ) : <EOL> Mix_HaltMusic ( ) <EOL> _channels = { } <EOL> if _have_music : <EOL> music . _free_loaded ( ) <EOL> Mix_CloseAudio ( ) <EOL> SDL_QuitSubSystem ( SDL_INIT_AUDIO ) <EOL> def _endsound_callback ( channel ) : <EOL> channel = _channels . get ( channel , None ) <EOL> if not channel : <EOL> return <EOL> if channel . _endevent != SDL_NOEVENT : <EOL> event_queue . put ( channel ) <EOL> if channel . _queue : <EOL> channel . _sound = channel . _queue <EOL> channel . _queue = None <EOL> channelnum = Mix_PlayChannelTimed ( channel . _id , channel . _sound . _chunk , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> if channelnum != - <NUM_LIT:1> : <EOL> Mix_GroupChannel ( channelnum , id ( channel . _sound ) ) <EOL> else : <EOL> channel . _sound = None <EOL> def init ( frequency = None , size = None , stereo = None , buffer = None ) : <EOL> """<STR_LIT>""" <EOL> __PYGAMEinit__ ( frequency , size , stereo , buffer ) <EOL> def pre_init ( frequency = <NUM_LIT:0> , size = <NUM_LIT:0> , stereo = <NUM_LIT:0> , buffer = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> global _request_frequency <EOL> global _request_size <EOL> global _request_stereo <EOL> global _request_buffer <EOL> if frequency : <EOL> _request_frequency = frequency <EOL> if size : <EOL> _request_size = size <EOL> if stereo : <EOL> _request_stereo = stereo <EOL> if buffer : <EOL> _request_buffer = buffer <EOL> def quit ( ) : <EOL> """<STR_LIT>""" <EOL> _autoquit ( ) <EOL> def get_init ( ) : <EOL> """<STR_LIT>""" <EOL> if not SDL_WasInit ( SDL_INIT_AUDIO ) : <EOL> return <EOL> opened , frequency , format , channels = Mix_QuerySpec ( ) <EOL> if format & ~ <NUM_LIT> : <EOL> format = - ( format & <NUM_LIT> ) <EOL> return frequency , format , channels > <NUM_LIT:1> <EOL> def _mixer_init_check ( ) : <EOL> if not SDL_WasInit ( SDL_INIT_AUDIO ) : <EOL> raise base . error ( '<STR_LIT>' ) <EOL> def stop ( ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_HaltChannel ( - <NUM_LIT:1> ) <EOL> def pause ( ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_Pause ( - <NUM_LIT:1> ) <EOL> def unpause ( ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_Resume ( - <NUM_LIT:1> ) <EOL> def fadeout ( time ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_FadOutChannel ( - <NUM_LIT:1> , time ) <EOL> def set_num_channels ( channels ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_AllocateChannels ( channels ) <EOL> for i in list ( _channels . keys ( ) [ : ] ) : <EOL> if i >= channels : <EOL> del channels [ i ] <EOL> def get_num_channels ( ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> return Mix_GroupCount ( - <NUM_LIT:1> ) <EOL> def set_reserved ( channels ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_ReserveChannels ( channels ) <EOL> def find_channel ( force = False ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> chan = Mix_GroupAvailable ( - <NUM_LIT:1> ) <EOL> if chan == - <NUM_LIT:1> : <EOL> if not force : <EOL> return <EOL> chan = Mix_GroupOldest ( - <NUM_LIT:1> ) <EOL> return Channel ( chan ) <EOL> def get_busy ( ) : <EOL> """<STR_LIT>""" <EOL> if not SDL_WasInit ( SDL_INIT_AUDIO ) : <EOL> return False <EOL> return bool ( Mix_Playing ( - <NUM_LIT:1> ) ) <EOL> class Sound ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' ] <EOL> def __init__ ( self , file , _chunk = None ) : <EOL> """<STR_LIT>""" <EOL> if _chunk : <EOL> self . _chunk = _chunk <EOL> return <EOL> _mixer_init_check ( ) <EOL> if hasattr ( file , '<STR_LIT>' ) : <EOL> rw = SDL_RWFromObject ( file ) <EOL> self . _chunk = Mix_LoadWAV_RW ( rw , <NUM_LIT:0> ) <EOL> else : <EOL> self . _chunk = Mix_LoadWAV ( file ) <EOL> def __del__ ( self ) : <EOL> if self . _chunk : <EOL> Mix_FreeChunk ( self . _chunk ) <EOL> def play ( self , loops = <NUM_LIT:0> , maxtime = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> channelnum = Mix_PlayChannelTimed ( - <NUM_LIT:1> , self . _chunk , loops , maxtime ) <EOL> if channelnum == - <NUM_LIT:1> : <EOL> return <EOL> Mix_Volume ( channelnum , <NUM_LIT> ) <EOL> Mix_GroupChannel ( channelnum , id ( self ) ) <EOL> channel = Channel ( channelnum ) <EOL> channel . _queue = None <EOL> channel . _sound = self <EOL> return channel <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_HaltGroup ( id ( self ) ) <EOL> def fadeout ( self , time ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_FadeOutGroup ( id ( self ) , time ) <EOL> def set_volume ( self , volume ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_VolumeChunk ( self . _chunk , int ( volume * <NUM_LIT> ) ) <EOL> def get_volume ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> return Mix_VolumeChunk ( self . _chunk , - <NUM_LIT:1> ) / <NUM_LIT> <EOL> def get_num_channels ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> return Mix_GroupCount ( id ( self ) ) <EOL> def get_length ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> opened , freq , format , channels = Mix_QuerySpec ( ) <EOL> if format == AUDIO_S8 or format == AUDIO_U8 : <EOL> mixerbytes = <NUM_LIT:1> <EOL> else : <EOL> mixerbytes = <NUM_LIT:2> <EOL> numsamples = self . _chunk . alen / mixerbytes / channels <EOL> return numsamples / float ( freq ) <EOL> class Channel ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __new__ ( cls , id ) : <EOL> _mixer_init_check ( ) <EOL> if id < <NUM_LIT:0> or id >= Mix_GroupCount ( - <NUM_LIT:1> ) : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> if id in _channels : <EOL> return _channels [ id ] <EOL> inst = super ( Channel , cls ) . __new__ ( cls ) <EOL> return inst <EOL> def __init__ ( self , id ) : <EOL> """<STR_LIT>""" <EOL> self . _id = id <EOL> if id not in _channels : <EOL> self . _sound = None <EOL> self . _queue = None <EOL> self . _endevent = SDL_NOEVENT <EOL> _channels [ id ] = self <EOL> def play ( self , sound , loops = <NUM_LIT:0> , time = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> channelnum = Mix_PlayChannelTimed ( self . _id , sound . _chunk , loops , time ) <EOL> if channelnum != - <NUM_LIT:1> : <EOL> Mix_GroupChannel ( channelnum , id ( sound ) ) <EOL> self . _sound = sound <EOL> self . _queue = None <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_HaltChannel ( self . _id ) <EOL> def pause ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_Pause ( self . _id ) <EOL> def unpause ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_Resume ( self . _id ) <EOL> def fadeout ( self , time ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> Mix_FadeOutChannel ( self . _id , time ) <EOL> def set_volume ( self , left , right = None ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> if Mix_Linked_Version ( ) . is_since ( ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) ) : <EOL> if right is None : <EOL> Mix_SetPanning ( self . _id , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> else : <EOL> Mix_SetPanning ( self . _id , int ( left * <NUM_LIT:255> ) , int ( right * <NUM_LIT:255> ) ) <EOL> left = <NUM_LIT:1.0> <EOL> else : <EOL> if right is not None : <EOL> left = ( left + right ) / <NUM_LIT:2> <EOL> Mix_Volume ( self . _id , int ( left * <NUM_LIT> ) ) <EOL> def get_volume ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> return Mix_Volume ( self . _id , - <NUM_LIT:1> ) / <NUM_LIT> <EOL> def get_busy ( self ) : <EOL> """<STR_LIT>""" <EOL> _mixer_init_check ( ) <EOL> return Mix_Playing ( self . _id ) <EOL> def get_sound ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sound <EOL> def queue ( self , sound ) : <EOL> """<STR_LIT>""" <EOL> if not self . _sound : <EOL> channelnum = Mix_PlayChannelTimed ( self . _id , sound . _chunk , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> if channelnum != - <NUM_LIT:1> : <EOL> Mix_GroupChannel ( channelnum , id ( sound ) ) <EOL> self . _sound = sound <EOL> else : <EOL> self . _queue = sound <EOL> def get_queue ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _queue <EOL> def set_endevent ( self , id = None ) : <EOL> """<STR_LIT>""" <EOL> if id is None : <EOL> id = SDL_NOEVENT <EOL> self . _endevent = id <EOL> def get_endevent ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _endevent </s>
