1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00
This commit is contained in:
Shlomi Fish 2017-04-17 02:02:18 +03:00
parent d77ef531ad
commit fc90baffa7
3 changed files with 113 additions and 84 deletions

View file

@ -75,7 +75,8 @@ class AbstractAudioClient:
if self._connectServer():
self.connected = 1
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
self.destroy()
# disconnect and stop server
@ -92,7 +93,7 @@ class AbstractAudioClient:
#
def playSample(self, name, priority=0, loop=0, volume=-1):
##print 'AbstractAudioClient.playSample', name
# print 'AbstractAudioClient.playSample', name
if self.audiodev is None or not self.app or not self.app.opt.sound:
return 0
if priority <= self.sample_priority and self.sample_loop:
@ -106,7 +107,8 @@ class AbstractAudioClient:
self.sample_loop = loop
return 1
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
return 0
def stopSamples(self):
@ -115,7 +117,8 @@ class AbstractAudioClient:
try:
self._stopSamples()
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
self.sample_priority = -1
self.sample_loop = 0
@ -125,7 +128,8 @@ class AbstractAudioClient:
try:
self._stopSamplesLoop()
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
self.sample_priority = -1
self.sample_loop = 0
@ -176,16 +180,17 @@ class PysolSoundServerModuleClient(AbstractAudioClient):
def __init__(self):
AbstractAudioClient.__init__(self)
import pysolsoundserver
def startServer(self):
# use the module
try:
import pysolsoundserver
self.audiodev = pysolsoundserver
self.audiodev.init()
self.server = 1
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
self.server = None
self.audiodev = None
@ -197,7 +202,7 @@ class PysolSoundServerModuleClient(AbstractAudioClient):
r = self.cmd("protocol 6")
if r != 0:
return 0
##self.cmd("debug 1")
# self.cmd("debug 1")
return 1
# disconnect and stop server
@ -209,7 +214,8 @@ class PysolSoundServerModuleClient(AbstractAudioClient):
#
def _playSample(self, filename, priority, loop, volume):
self.cmd("playwav '%s' %d %d %d %d" % (filename, -1, priority, loop, volume))
self.cmd("playwav '%s' %d %d %d %d"
% (filename, -1, priority, loop, volume))
return 1
def _stopSamples(self):
@ -230,10 +236,13 @@ class PysolSoundServerModuleClient(AbstractAudioClient):
loop = 999999
for music in music_list:
if music.absname:
self.cmd("queuemus '%s' %d %d %d %d" % (music.absname, music.index, 0, loop, music.volume))
self.cmd("queuemus '%s' %d %d %d %d"
% (music.absname, music.index, 0, loop,
music.volume))
self.cmd("startqueue")
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
def playNextMusic(self):
self.cmd("nextmus")
@ -249,7 +258,8 @@ class PysolSoundServerModuleClient(AbstractAudioClient):
self.cmd("setwavvol %d" % s)
self.cmd("setmusvol %d" % m)
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
# ************************************************************************
@ -276,11 +286,12 @@ class Win32AudioClient(AbstractAudioClient):
flags = flags | a.SND_LOOP
if priority <= self.sample_priority:
flags = flags | a.SND_NOSTOP
###print filename, flags, priority
# print filename, flags, priority
try:
a.PlaySound(filename, flags)
return 1
except: pass
except:
pass
return 0
def _stopSamples(self):
@ -297,8 +308,8 @@ class OSSAudioServer:
def __init__(self, pipe):
self.pipe = pipe
#import ossaudiodev
#self.audiodev = ossaudiodev.open('w')
# import ossaudiodev
# self.audiodev = ossaudiodev.open('w')
self.sound_priority = -1
self._busy = False
@ -321,7 +332,8 @@ class OSSAudioServer:
self.play(filename, priority)
def _getParameters(self, filename):
import ossaudiodev, wave
import ossaudiodev
import wave
w = wave.open(filename)
fmt = ossaudiodev.AFMT_U8
nch = w.getnchannels()
@ -330,42 +342,44 @@ class OSSAudioServer:
return (frames, fmt, nch, rate)
def playLoop(self, filename, priority=None):
##print '_playLoop:', filename
import ossaudiodev, wave
# print '_playLoop:', filename
import ossaudiodev
try:
#audiodev = self.audiodev
# audiodev = self.audiodev
audiodev = ossaudiodev.open('w')
#audiodev.nonblock()
# audiodev.nonblock()
frames, fmt, nch, rate = self._getParameters(filename)
audiodev.setparameters(fmt, nch, rate)
while self._play_loop:
audiodev.write(frames)
audiodev.reset()
#audiodev.close()
#self.audiodev = ossaudiodev.open('w')
# audiodev.close()
# self.audiodev = ossaudiodev.open('w')
return 1
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
return 0
def play(self, filename, priority):
##print '_play:', filename
import ossaudiodev, wave
# print '_play:', filename
import ossaudiodev
try:
self._busy = True
#audiodev = self.audiodev
# audiodev = self.audiodev
audiodev = ossaudiodev.open('w')
#audiodev.nonblock()
# audiodev.nonblock()
frames, fmt, nch, rate = self._getParameters(filename)
audiodev.setparameters(fmt, nch, rate)
audiodev.write(frames)
#audiodev.close()
#self.audiodev = ossaudiodev.open('w')
# audiodev.close()
# self.audiodev = ossaudiodev.open('w')
self.sound_priority = priority
self._busy = False
return 1
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
self._busy = False
return 0
@ -377,7 +391,7 @@ class OSSAudioClient(AbstractAudioClient):
def __init__(self):
AbstractAudioClient.__init__(self)
import ossaudiodev, wave
import ossaudiodev
self.audiodev = ossaudiodev
def startServer(self):
@ -389,7 +403,7 @@ class OSSAudioClient(AbstractAudioClient):
server.mainLoop()
def _playSample(self, filename, priority, loop, volume):
##print '_playSample:', filename, loop
# print '_playSample:', filename, loop
os.write(self.pout, '%s\0%s\0%s\0' % (filename, priority, loop))
return 1
@ -413,10 +427,13 @@ class PyGameAudioClient(AbstractAudioClient):
def __init__(self):
AbstractAudioClient.__init__(self)
import pygame.mixer, pygame.time
import pygame.mixer
import pygame.time
if os.name == 'nt':
# for py2exe
import pygame.base, pygame.rwobject, pygame.mixer_music
import pygame.base
import pygame.rwobject
import pygame.mixer_music
self.mixer = pygame.mixer
self.time = pygame.time
self.music = self.mixer.music
@ -430,22 +447,22 @@ class PyGameAudioClient(AbstractAudioClient):
def connectServer(self, app):
AbstractAudioClient.connectServer(self, app)
## http://www.pygame.org/docs/ref/mixer.html
## NOTE: there is currently a bug on some windows machines which
## makes sound play back 'scratchy'. There is not enough cpu in
## the sound thread to feed the buffer to the sound api. To get
## around this you can increase the buffer size. However this
## means that there is more of a delay between the time you ask to
## play the sound and when it gets played. Try calling this before
## the pygame.init or pygame.mixer.init calls.
## pygame.mixer.pre_init(44100,-16,2, 1024 * 3)
#self.mixer.pre_init(44100, -16, 2, 1024 * 3)
# http://www.pygame.org/docs/ref/mixer.html
# NOTE: there is currently a bug on some windows machines which
# makes sound play back 'scratchy'. There is not enough cpu in
# the sound thread to feed the buffer to the sound api. To get
# around this you can increase the buffer size. However this
# means that there is more of a delay between the time you ask to
# play the sound and when it gets played. Try calling this before
# the pygame.init or pygame.mixer.init calls.
# pygame.mixer.pre_init(44100,-16,2, 1024 * 3)
# self.mixer.pre_init(44100, -16, 2, 1024 * 3)
buff_size = self.app.opt.sound_sample_buffer_size
self.mixer.pre_init(44100, -16, 2, 1024*buff_size)
self.mixer.init()
def _playSample(self, filename, priority, loop, volume):
##print '_playSample:', filename, priority, loop, volume
# print '_playSample:', filename, priority, loop, volume
if self.sound_channel and self.sound_channel.get_busy():
if self.sound_priority >= priority:
return 0
@ -457,7 +474,8 @@ class PyGameAudioClient(AbstractAudioClient):
self.sound.set_volume(vol)
self.sound_channel = self.sound.play(loop)
except:
if traceback: traceback.print_exc()
if traceback:
traceback.print_exc()
pass
self.sound_priority = priority
return 1
@ -469,7 +487,7 @@ class PyGameAudioClient(AbstractAudioClient):
self.sound_channel = None
def _playMusicLoop(self):
##print '_playMusicLoop'
# print '_playMusicLoop'
music_list = self.music_list
if not music_list:
return
@ -488,7 +506,7 @@ class PyGameAudioClient(AbstractAudioClient):
self._wait(200)
self._wait(300)
except:
##if traceback: traceback.print_exc()
# if traceback: traceback.print_exc()
self._wait(1000)
def _destroy(self):
@ -502,9 +520,9 @@ class PyGameAudioClient(AbstractAudioClient):
self.time.wait(s)
def playContinuousMusic(self, music_list):
##print 'playContinuousMusic'
# print 'playContinuousMusic'
self.music_list = music_list
#if self.audiodev is None or not self.app:
# if self.audiodev is None or not self.app:
# return
if not music_list:
return
@ -528,4 +546,3 @@ class PyGameAudioClient(AbstractAudioClient):
def playNextMusic(self):
if self.music:
self.music.stop()

View file

@ -23,26 +23,31 @@
# imports
import sys, re, time
import sys
import re
import time
import random
from pysollib.mygettext import _, n_
from pysollib.mfxutil import SubclassResponsibility
if sys.version_info > (3,):
long = int
# ************************************************************************
# * Abstract class for PySol Random number generator.
# *
# * We use a seed of type long in the range [0, MAX_SEED].
# ************************************************************************
class BasicRandom:
#MAX_SEED = 0L
#MAX_SEED = 0xffffffffffffffffL # 64 bits
MAX_SEED = 100000000000000000000L # 20 digits
ORIGIN_UNKNOWN = 0
ORIGIN_RANDOM = 1
ORIGIN_PREVIEW = 2 # random from preview
class BasicRandom:
# MAX_SEED = 0L
# MAX_SEED = 0xffffffffffffffffL # 64 bits
MAX_SEED = long('100000000000000000000') # 20 digits
ORIGIN_UNKNOWN = 0
ORIGIN_RANDOM = 1
ORIGIN_PREVIEW = 2 # random from preview
ORIGIN_SELECTED = 3 # manually entered
ORIGIN_NEXT_GAME = 4 # "Next game number"
@ -62,18 +67,18 @@ class BasicRandom:
raise SubclassResponsibility
def copy(self):
random = self.__class__(0L)
random = self.__class__(long(0))
random.__dict__.update(self.__dict__)
return random
def increaseSeed(self, seed):
if seed < self.MAX_SEED:
return seed + 1L
return 0L
return seed + long(1)
return long(0)
def _getRandomSeed(self):
t = long(time.time() * 256.0)
t = (t ^ (t >> 24)) % (self.MAX_SEED + 1L)
t = (t ^ (t >> 24)) % (self.MAX_SEED + long(1))
return t
def setSeedAsStr(self, new_s):
@ -129,6 +134,7 @@ class WHRandom(BasicRandom, random.WichmannHill):
# * Abstract class for LC Random number generators.
# ************************************************************************
class MFXRandom(BasicRandom):
def __init__(self, seed=None):
@ -146,8 +152,8 @@ class MFXRandom(BasicRandom):
def setSeed(self, seed):
seed = long(seed)
if not (0L <= seed <= self.MAX_SEED):
raise ValueError, "seed out of range"
if not (0 <= seed <= self.MAX_SEED):
raise ValueError("seed out of range")
self.seed = seed
return seed
@ -190,8 +196,8 @@ class MFXRandom(BasicRandom):
class LCRandom64(MFXRandom):
def random(self):
self.seed = (self.seed*6364136223846793005L + 1L) & self.MAX_SEED
return ((self.seed >> 21) & 0x7fffffffL) / 2147483648.0
self.seed = (self.seed*long('6364136223846793005') + 1) & self.MAX_SEED
return ((self.seed >> 21) & 0x7fffffff) / 2147483648.0
# ************************************************************************
@ -201,7 +207,7 @@ class LCRandom64(MFXRandom):
# ************************************************************************
class LCRandom31(MFXRandom):
MAX_SEED = 0x1ffffffffL # 33 bits
MAX_SEED = long('0x1ffffffff', 0) # 33 bits
def getSeedStr(self):
return "ms" + str(self.initial_seed)
@ -212,9 +218,10 @@ class LCRandom31(MFXRandom):
def setSeed(self, seed):
seed = long(seed)
self.seed = seed
if not (0L <= seed <= self.MAX_SEED):
raise ValueError, "seed out of range"
self.seedx = (seed if (seed < 0x100000000L) else (seed - 0x100000000L))
if not (0 <= seed <= self.MAX_SEED):
raise ValueError("seed out of range")
self.seedx = (seed if (seed < long('0x100000000', 0)) else
(seed - long('0x100000000', 0)))
return seed
def _rando(self):
@ -243,8 +250,8 @@ class LCRandom31(MFXRandom):
# select
#PysolRandom = LCRandom64
#PysolRandom = WHRandom
# PysolRandom = LCRandom64
# PysolRandom = WHRandom
PysolRandom = MTRandom
@ -256,6 +263,7 @@ def _match_ms(s):
"""match an ms based seed string."""
return re.match(r"ms([0-9]+)\n?\Z", s)
# construct Random from seed string
def constructRandom(s):
m = _match_ms(s)
@ -266,8 +274,9 @@ def constructRandom(s):
ret.setSeedAsStr(s)
return ret
else:
raise ValueError, "ms seed out of range"
s = re.sub(r"L$", "", str(s)) # cut off "L" from possible conversion to long
raise ValueError("ms seed out of range")
# cut off "L" from possible conversion to long
s = re.sub(r"L$", "", str(s))
s = re.sub(r"[\s\#\-\_\.\,]", "", s.lower())
if not s:
return None
@ -276,7 +285,9 @@ def constructRandom(s):
return LCRandom31(seed)
return PysolRandom(seed)
MS_LONG_BIT = (1L << 1000)
MS_LONG_BIT = (long(1) << 1000)
def random__str2long(s):
m = _match_ms(s)
@ -285,17 +296,17 @@ def random__str2long(s):
else:
return long(s)
def random__long2str(l):
if ((l & MS_LONG_BIT) != 0):
return "ms" + str(l & (~ MS_LONG_BIT))
else:
return str(l)
# test
if __name__ == '__main__':
r = constructRandom('12345')
print r.randint(0, 100)
print r.random()
print type(r)
print(r.randint(0, 100))
print(r.random())
print(type(r))

View file

@ -9,7 +9,8 @@ use Test::Differences qw( eq_or_diff );
use String::ShellQuote qw/ shell_quote /;
# my $cmd = shell_quote( 'flake8', '.' );
my $cmd = shell_quote( 'flake8', glob('./pysollib/[a-o]*.py') );
my $cmd = shell_quote( 'flake8',
grep { not($_ eq './pysollib/pysoltk.py') } glob('./pysollib/[a-p]*.py') );
# TEST
eq_or_diff( scalar(`$cmd`), '', "flake8 is happy with the code." );