def __init__(self, wave_file='djembe_2.wav'): self.wave_path = '%s%s' % (WAVE_DIR, wave_file) try: # try to open the wave file through pygame.mixer.Sound self.wave = wave.open(self.wave_path, 'rb') #self.wave = mixer.Sound(self.wave_path) except IOError: print('file %s not found: this Sound() will actually not play' \ % self.wave_path) else: # get wave file parameters red = -8 if _samplefmt < 0 else 8 if self.wave.getparams()[0:3] != (_channels, _samplefmt/red, _samplerate): print('sound file has not the correct settings') raise(Exception) # read the wave into memory # if hasattr(self, 'wave'): # get a channel from pygame.mixer self._chan = mixer.find_channel() if self._chan is None: print('could not get a channel from pygame.mixer') # #self._buf = self.wave.readframes(self.wave.getnframes()) #self._snd = mixer.Sound(self._buf) self._snd = mixer.Sound(self.wave_path)
def init_backend(self): pygame.init() mixer.init( frequency=self.sample_rate, size=self.bit_depth * -1, channels=1, buffer=1024, ) self.sounds_in_queue = 0 self.channel = mixer.find_channel() self.end_event_type = pygame.USEREVENT self.channel.set_endevent(self.end_event_type)
def play(self, name, volume=[1.0, 1.0], wait = 0, loop = 0): """ plays the sound with the given name. name - of the sound. volume - left and right. Ranges 0.0 - 1.0 wait - used to control what happens if sound is allready playing: 0 - will not wait if sound playing. play anyway. 1 - if there is a sound of this type playing wait for it. 2 - if there is a sound of this type playing do not play again. loop - number of times to loop. -1 means forever. """ vol_l, vol_r = volume sound = self.get_sound(name) if sound: if wait in [1,2]: if self.chans.has_key(name) and self.chans[name].get_busy(): if wait == 1: # sound is allready playing we wait for it to finish. self.queued_sounds.append((name, volume, wait)) return elif wait == 2: # not going to play sound if playing. return self.chans[name] = sound.play(loop) if not self.chans[name]: if loop == 1: # forces a channel to return. we fade that out, # and enqueue our one. self.chans[name] = mixer.find_channel(1) #TODO: does this fadeout block? self.chans[name].fadeout(100) self.chans[name].queue(sound) else: # the pygame api doesn't allow you to queue a sound and # tell it to loop. So we hope for the best, and queue # the sound. self.queued_sounds.append((name, volume, wait)) # delete the None channel here. del self.chans[name] elif self.chans[name]: self.chans[name].set_volume(vol_l, vol_r) else: raise 'not found'
def get_channel(self): """ Get the current channel. :return: Channel :rtype: pygame.mixer.Channel """ channel = _mixer.find_channel() if self._uniquechannel: # If the channel is unique if self._channel is None: # If the channel has not been setted self._channel = channel else: self._channel = channel # Store the avaiable channel return self._channel
def get_channel(self) -> 'mixer.Channel': """ Return the channel of the sound engine. :return: Sound engine channel """ # noinspection PyArgumentList channel = mixer.find_channel() # force only available on pygame v2 if self._uniquechannel: # If the channel is unique if self._channel is None: # If the channel has not been set self._channel = channel else: self._channel = channel # Store the available channel return self._channel
def get_channel(self): """ Return the channel of the sound engine. :return: Channel :rtype: :py:class:`pygame.mixer.Channel` """ channel = _mixer.find_channel() if self._uniquechannel: # If the channel is unique if self._channel is None: # If the channel has not been set self._channel = channel else: self._channel = channel # Store the available channel return self._channel
def play(sound_name, is_music=False): if is_music: mixer.music.load(MUS[sound_name]["file"]) mixer.music.set_volume(0.5) if options['debug']: barf.Barf("SAV", "AUDIO: Loaded Music file: (%s)." % (MUS[sound_name]["name"])) mixer.music.play(-1, 0.0) if options['debug']: barf.Barf("PLG", "AUDIO: Playing Music.") if not is_music: if not is_playing(AUD[sound_name]["file"]): sfx = mixer.find_channel() try: sfx.play(AUD[sound_name]["file"]) if options['debug']: barf.Barf("PLG", "AUDIO: Playing '%s'." % (AUD[sound_name]["name"])) except: pass
def __init__(self): try: #Inicilializo el modulo para ejecutar sonidos if sys.platform.find('win') != -1: mixer.pre_init(44100,16,1,4096) else: mixer.pre_init(44100) mixer.init() self.__canal = mixer.find_channel() self.__cola = [] self.__ejecutando_sonidos = False self.__end_sound_event = False self.__new_sound_event = False self.__escuchar = True self.__sonido_actual = "" self.SILENCE_CHANNEL = USEREVENT + 4 self.nombre_grupo_sonido = "" except pygame.error, e: raise Exception("ERROR!: " + str(e) + ", al inicilizar el video. La aplicacion se cerrara")
def fire_bullet(): # Direction to mouse mouseX, mouseY = pygame.mouse.get_pos() mousePos = Vector2(mouseX + camera.boundingBox.x, mouseY+camera.boundingBox.y) dirToMouse = Vector2.get_normal(mousePos - player.position) # Create a bullet bullet = Particle() bullet.tag = "player bullet" bullet.position.x = player.position.x bullet.position.y = player.position.y bullet.velocity = dirToMouse * 800.0 bullet.boundingBox = Rect(player.position.to_tuple(), (5, 5)) bullet.color = (255, 150, 255) bullet.set_life(1.3) all_game_objects.append(bullet) channel = mixer.find_channel(True) channel.queue(laser_shot_sfx)
def play(self, key, loops=0, max_time=0, fade_ms=0, channel=None): """ plays the sound with the given *key* (as previously registered with register_sound) loops: number of _additional_ times it will play. so 1 actually plays twice. -1 is endless max_time: playback will stop after max_time millis fade_ms: playback fades in (from 0 volume) over fade_ms millis channel: the channel to play back on. Specifying CH_VOICE will automatically call play_voice and if other voice samples are currently playing, playback will be queued. If channel==None, CH_SFX is assumed _unless_ the sound file's .is_voice==True (was registered as a voice) To force a voice to play as a sound effect, pass CH_SFX as the channel arg """ if not self.enabled: return if key in self.sounds: if len(self.sounds[key]['sound_list']) > 0: random.shuffle(self.sounds[key]['sound_list']) # print channel # print channel.__class__.__name__ if channel is not None and channel.__class__.__name__ == 'Channel': channel.set_volume(self.volume) channel.play(self.sounds[key]['sound_list'][0], loops, max_time, fade_ms) return channel elif (channel == CH_VOICE) or (self.sounds[key]['is_voice'] == True and channel == None): # call play_voice on behalf of the caller self.logger.info("playing [%s] as a voice!" % key) self.play_voice(key) elif channel is not None and channel > 0: mixer.Channel(channel).set_volume(self.volume) return mixer.Channel(channel).play( self.sounds[key]['sound_list'][0], loops, max_time, fade_ms) else: c = mixer.find_channel(True) # True means force c.set_volume(self.volume) c.play(self.sounds[key]['sound_list'][0], loops, max_time, fade_ms) return c else: self.logger.error("ERROR SOUND KEY NOT FOUND: %s", key) return 0
def play(self, loops=0, maxtime=0, fadein=0): """Starts playback of this Sound. @param loops: The number of times this Sound will be repeated, not including the first playback. Therefore, the default of 0 will play the sound once, while higher numbers will play the sound once, then repeat it the given number of times. Using -1 as the argument will cause the playback to repeat indefinitely. @param maxtime: The maximum length of time (in milliseconds) to play this Sound. The default of 0 plays the Sound in its entirety. @param fadein: The amount of time to fade in the Sound. The playback will start at 0 volume, and ramp up to full volume over this many milliseconds. """ self.__channel = mixer.find_channel() if self.__channel is not None: self.__channel.play(self.__sound, loops, maxtime, fadein) if self.panning: self.__channel.set_volume(*self.__doPan(self.panning)) if self.endevent is not None: self.__channel.set_endevent(self.endevent)
def play(self, loops=0, maxtime=0, fadein=0): """Starts playback of this Sound. @param loops: The number of times this Sound will be repeated, not including the first playback. Therefore, the default of 0 will play the sound once, while higher numbers will play the sound once, then repeat it the given number of times. Using -1 as the argument will cause the playback to repeat indefinitely. @param maxtime: The maximum length of time (in milliseconds) to play this Sound. The default of 0 plays the Sound in its entirety. @param fadein: The amount of time to fade in the Sound. The playback will start at 0 volume, and ramp up to full volume over this many milliseconds. """ self.__channel = mixer.find_channel() if self.__channel is not None: self.__channel.play(self.__sound, loops, maxtime, fadein) if self.panning: self.__channel.set_volume(*self.__doPan(self.panning)) if self.endevent is not None: self.__channel.set_endevent(self.endevent)
def __init__(self): try: #Inicilializo el modulo para ejecutar sonidos if sys.platform.find('win') != -1: mixer.pre_init(44100, 16, 1, 4096) else: mixer.pre_init(44100) mixer.init() self.__canal = mixer.find_channel() self.__cola = [] self.__ejecutando_sonidos = False self.__end_sound_event = False self.__new_sound_event = False self.__escuchar = True self.__sonido_actual = "" self.SILENCE_CHANNEL = USEREVENT + 4 self.nombre_grupo_sonido = "" except pygame.error, e: raise Exception( "ERROR!: " + str(e) + ", al inicilizar el video. La aplicacion se cerrara")
def play(self,key, loops=0, max_time=0, fade_ms=0, channel=None): """ plays the sound with the given *key* (as previously registered with register_sound) loops: number of _additional_ times it will play. so 1 actually plays twice. -1 is endless max_time: playback will stop after max_time millis fade_ms: playback fades in (from 0 volume) over fade_ms millis channel: the channel to play back on. Specifying CH_VOICE will automatically call play_voice and if other voice samples are currently playing, playback will be queued. If channel==None, CH_SFX is assumed _unless_ the sound file's .is_voice==True (was registered as a voice) To force a voice to play as a sound effect, pass CH_SFX as the channel arg """ if not self.enabled: return if key in self.sounds: if len(self.sounds[key]['sound_list']) > 0: random.shuffle(self.sounds[key]['sound_list']) # print channel # print channel.__class__.__name__ if channel is not None and channel.__class__.__name__== 'Channel': channel.set_volume(self.volume) channel.play(self.sounds[key]['sound_list'][0],loops,max_time,fade_ms) return channel elif(channel == CH_VOICE) or (self.sounds[key]['is_voice']==True and channel==None): # call play_voice on behalf of the caller self.logger.info("playing [%s] as a voice!" % key) self.play_voice(key) elif channel is not None and channel > 0: mixer.Channel(channel).set_volume(self.volume) return mixer.Channel(channel).play(self.sounds[key]['sound_list'][0],loops,max_time,fade_ms) else: c = mixer.find_channel(True) # True means force c.set_volume(self.volume) c.play(self.sounds[key]['sound_list'][0],loops,max_time,fade_ms) return c else: self.logger.error("ERROR SOUND KEY NOT FOUND: %s", key) return 0
def play(sound, volume=0.4, loop=True): queuedSound = mixer.Sound(sound) channel = mixer.find_channel() channel.set_volume(volume) channel.play(queuedSound, loops=-1 if loop else 0, fade_ms=1000)
def wall(self): if self.x < 0: self.v *= -1 free_channel = mixer.find_channel() free_channel.play(self.sound)
def play_disabled_key_sound(self): canal_aux = mixer.find_channel() #self.__canal.stop() sonido = mixer.Sound(self.__sounds_files.get_sound_file_name("otros","fx","disabled_key")) canal_aux.play(sonido)
def play(sound: AudioClip): channel = mixer.find_channel() channel.play(sound.getClip())
import os.path, sys, socket import pygame.mixer, pygame.time mixer = pygame.mixer time = pygame.time GROUP_IP = '192.168.0.22' PORT = 8002 BUFFER_SIZE = 38000 PIXEL_ID = 2 mixer.init(44100, -16, 1, 1024) channel = mixer.find_channel() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((GROUP_IP, PORT)) while True: data, addr = sock.recvfrom(BUFFER_SIZE) s = mixer.Sound(data) channel.queue(s)
def force_sound(self, sound): """Forcefully play a sound""" channel = mixer.find_channel(force=True) if channel is not None: channel.play(sound)
def play_sound(self, sound): """Non forcefully play a sound""" channel = mixer.find_channel() if channel is not None: channel.play(sound)