def __init__(self): super(AudioSystem, self).__init__() self.resources = "C:\\Users\\Ben\\PycharmProjects\\TetrisDemo\\resources\\" SDL_Init(SDL_INIT_AUDIO) Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) soft_drop_file = self.resources + "soft-drop.wav" line_clear_file = self.resources + "line-clear.wav" self.soft_drop_sample = Mix_LoadWAV(byteify(soft_drop_file, "utf-8")) self.line_clear_sample = Mix_LoadWAV(byteify(line_clear_file, "utf-8")) self.componenttypes = MovementInput, AudioState
def __init__(self, config): """Loads audio files. """ self.volume = config.getint('Volume') # FIXME: audio file loading should happen using the resource manager # NOTE: right now these resources does not have the resource structure # and the mixer is feeded with every file into the directories: using # the resource manager will avoid problems and will permit to have a # more elastic way to interact with sounds. self.sounds = {} for filename in os.listdir(FX_ROOT): name, ext = os.path.splitext(filename) filepath = os.path.join(FX_ROOT, filename) event_sound_list = [] if os.path.isdir(filepath): # We have more sounds for the same name for subfile in os.listdir(filepath): subname, subext = os.path.splitext(subfile) if subext not in ('.aif', '.wav'): continue subfilepath = os.path.join(filepath, subfile) event_sound_list.append(subfilepath) else: if ext not in ('.aif', '.wav'): continue # We have only 1 sound for this name event_sound_list = [filepath] self.sounds[name] = [] for sound_filepath in event_sound_list: LOG.info('Loading sound {}'.format(sound_filepath)) self.sounds[name].append((sound_filepath, Mix_LoadWAV( byteify(sound_filepath, 'utf-8')))) self.musics = {} for filename in os.listdir(MUSIC_ROOT): name, ext = os.path.splitext(filename) filepath = os.path.join(MUSIC_ROOT, filename) LOG.info('Loading music {}'.format(filepath)) self.musics[name] = Mix_LoadMUS(byteify(filepath, 'utf-8')) if self.musics[name] is None: raise RuntimeError( 'Cannot open audio file: {}'.format(Mix_GetError())) # Map of the fx currently playing (indexed by entity id) self.sound_map = {}
def play_sample(filepath, quiet=False): sample_file = RESOURCES.get_path(filepath) sample = sdl2.sdlmixer.Mix_LoadWAV(byteify(sample_file, "utf-8")) if quiet: channel = sdl2.sdlmixer.Mix_PlayChannel(2, sample, -1) sdl2.sdlmixer.Mix_Volume(2, 5) # channel, volume:(0,128) else: channel = sdl2.sdlmixer.Mix_PlayChannel(1, sample, 0)
def test_BitmapFont_can_render(self): sf = surface.SDL_LoadBMP( byteify(RESOURCES.get_path("font.bmp"), "utf-8")) self.assertIsInstance(sf.contents, surface.SDL_Surface) font = sdl2ext.BitmapFont(sf, (32, 32), FONTMAP) self.assertIsInstance(font, sdl2ext.BitmapFont) self.assertTrue(font.can_render("text")) self.assertTrue(font.can_render("473285435hfsjadfhriuewtrhefd")) self.assertFalse(font.can_render("testä"))
def test_BitmapFont_contains(self): sf = surface.SDL_LoadBMP( byteify(RESOURCES.get_path("font.bmp"), "utf-8")) self.assertIsInstance(sf.contents, surface.SDL_Surface) font = sdl2ext.BitmapFont(sf, (32, 32), FONTMAP) self.assertIsInstance(font, sdl2ext.BitmapFont) for ch in "abcde12345.-,+": self.assertTrue(font.contains(ch)) for ch in "äöüß": self.assertFalse(font.contains(ch))
def test_BitmapFont(self): sf = surface.SDL_LoadBMP( byteify(RESOURCES.get_path("font.bmp"), "utf-8")) self.assertIsInstance(sf.contents, surface.SDL_Surface) font = sdl2ext.BitmapFont(sf, (32, 32), FONTMAP) self.assertIsInstance(font, sdl2ext.BitmapFont) sprite = sdl2ext.SoftwareSprite(sf.contents, True) self.assertIsInstance(sprite, sdl2ext.SoftwareSprite) font = sdl2ext.BitmapFont(sprite, (32, 32), FONTMAP) self.assertIsInstance(font, sdl2ext.BitmapFont)
def __init__(self, path): '''Load the sound file *Parameters:* - `path`: Path to the sound file ''' self.sample = mixer.Mix_LoadWAV(byteify(path, "utf-8")) if not self.sample: msg = "Cannot load file %s" % path logger.error(msg) raise SoundError(msg)
def initaudio(self): if SDL_Init(SDL_INIT_AUDIO) != 0: raise RuntimeError("Cannot initialize audio system: {}".format(SDL_GetError())) if Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024): raise RuntimeError("Cannot open mixed audio: {}".format(Mix_GetError())) self.sounds = {} for index in range(36, 101): try: sound_file = RESOURCES.get_path("{}.wav".format(index)) except KeyError: continue sample = Mix_LoadWAV(byteify(sound_file, "utf-8")) if sample is None: raise RuntimeError("Cannot open audio file: {}".format(Mix_GetError())) self.sounds[index] = sample print("Loaded {}".format(sound_file))
def _load_file(self, file): rw = SDL_RWFromFile(byteify(file, "utf-8"), b"rb") sp = SDL_LoadWAV_RW(rw, 1, ctypes.byref(self.want_spec), ctypes.byref(self._buf), ctypes.byref(self._length)) # print ("want.freq: %d" % self.want_spec.freq) # print ("want.is bytesize: %d" % SDL_AUDIO_BITSIZE(self.want_spec.format)) # print ("want.is is big endian: %d" % SDL_AUDIO_ISBIGENDIAN(self.want_spec.format)) # print ("want.is int: %d" % SDL_AUDIO_ISINT(self.want_spec.format)) # print ("want.is signed: %d" % SDL_AUDIO_ISSIGNED(self.want_spec.format)) # print ("want.aformat1: %d" % self.want_spec.format) # print ("want.aformat2: %d" % AUDIO_S16) # print ("want.channels: %d" % self.want_spec.channels) # print ("want.samples: %d" % self.want_spec.samples) if sp is None: raise RuntimeError("Could not open audio file: {}".format( SDL_GetError())) if self.want_spec.format != AUDIO_S16: raise RuntimeError("Unable to get Int16 audio format !")
from random import choice from sdl2 import * from sdl2.ext.compat import byteify from sdl2.sdlmixer import * import os # select the audio folder sounddir = 'CMStormTKBlue' sounds = os.listdir(sounddir) # initialization of the SDL audio component SDL_Init(SDL_INIT_AUDIO) Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 256) downs = [ Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'down.wav' in f ] ups = [ Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'up.wav' in f ] spacedowns = [ Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'down_space.wav' in f ] spaceups = [ Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'up_space.wav' in f ] returndowns = [
def load_audio(file_name): return Mix_LoadWAV(byteify(''.join((AUDIO_FILE_LOCATION, file_name, AUDIO_FILE_TYPE)), "utf-8"))
def _load_file(self, file): rw = SDL_RWFromFile(byteify(file, "utf-8"), b"rb") sp = SDL_LoadWAV_RW(rw, 1, byref(self.spec), byref(self._buf), byref(self._length)) if sp is None: raise RuntimeError("Could not open audio file: {}".format(SDL_GetError()))
from PyObjCTools import AppHelper from random import choice from sdl2 import * from sdl2.ext.compat import byteify from sdl2.sdlmixer import * import os # select the audio folder sounddir = 'CMStormTKBlue' sounds = os.listdir(sounddir) # initialization of the SDL audio component SDL_Init(SDL_INIT_AUDIO) Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 256) downs = [Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'down.wav' in f] ups = [Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'up.wav' in f] spacedowns = [Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'down_space.wav' in f] spaceups = [Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'up_space.wav' in f] returndowns = [Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'down_return.wav' in f] returnups = [Mix_LoadWAV(byteify(sounddir + os.sep + f, 'utf-8')) for f in sounds if 'up_return.wav' in f] # Keyboard Events, keyDown and keyUp events are catched here class AppDelegate(NSObject): def applicationDidFinishLaunching_(self, notification): downMask = NSKeyDownMask upMask = NSKeyUpMask NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(downMask, downHandler) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(upMask, upHandler) # play a random audiofile from a list
def _open(self, files): for file in files: sample = Mix_LoadWAV(byteify(str(file), 'utf-8')) if sample is None: raise RuntimeError('Cannot open audio file: ' + Mix_GetError()) self._samples.append(sample)
from sdl2.ext import Resources from sdl2.ext.compat import byteify from sdl2.sdlmixer import * except ImportError: import traceback traceback.print_exc() sys.exit(1) #sound_file = "/usr/share/sounds/sound-icons/electric-piano-3.wav" sound_file = "sample.mp3" if SDL_Init(SDL_INIT_AUDIO) != 0: raise RuntimeError("Cannot initialize audio system: {}".format( SDL_GetError())) if Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024): raise RuntimeError("Cannot open mixed audio: {}".format(Mix_GetError())) sample = Mix_LoadWAV(byteify(sound_file, "utf-8")) if sample is None: raise RuntimeError("Cannot open audio file: {}".format(Mix_GetError())) channel = Mix_PlayChannel(-1, sample, 0) if channel == -1: raise RuntimeError("Cannot play sample: {}".format(Mix_GetError())) while Mix_Playing(channel): SDL_Delay(100) Mix_CloseAudio() SDL_Quit(SDL_INIT_AUDIO)