mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-05 00:02:29 -04:00
* added support ossaudiodev
git-svn-id: file:///home/shlomif/Backup/svn-dumps/PySolFC/svnsync-repos/pysolfc/PySolFC/trunk@61 efabe8c0-fbe8-4139-b769-b5e6d273206e
This commit is contained in:
parent
b39a6c690d
commit
9049e8fc5b
3 changed files with 85 additions and 4 deletions
|
@ -878,10 +878,10 @@ class Game:
|
|||
#
|
||||
|
||||
def playSample(self, name, priority=0, loop=0):
|
||||
##print "Game.playSample:", name, priority, loop
|
||||
if self.app.opt.sound_samples.has_key(name) and \
|
||||
not self.app.opt.sound_samples[name]:
|
||||
return 0
|
||||
##print "playSample:", name, priority, loop
|
||||
if self.app.audio:
|
||||
return self.app.audio.playSample(name, priority=priority, loop=loop)
|
||||
return 0
|
||||
|
|
|
@ -48,7 +48,7 @@ from resource import Tile
|
|||
from gamedb import GI
|
||||
from app import Application
|
||||
from pysolaudio import thread, pysolsoundserver
|
||||
from pysolaudio import AbstractAudioClient, PysolSoundServerModuleClient, Win32AudioClient
|
||||
from pysolaudio import AbstractAudioClient, PysolSoundServerModuleClient, Win32AudioClient, OSSAudioClient
|
||||
|
||||
# Toolkit imports
|
||||
from pysoltk import tkversion, wm_withdraw, wm_set_icon, loadImage
|
||||
|
@ -243,6 +243,8 @@ def pysol_init(app, args):
|
|||
app.audio = PysolSoundServerModuleClient()
|
||||
elif os.name == "nt":
|
||||
app.audio = Win32AudioClient()
|
||||
elif os.name == 'posix':
|
||||
app.audio = OSSAudioClient()
|
||||
if app.audio:
|
||||
app.audio.startServer()
|
||||
if app.audio.server is None:
|
||||
|
@ -418,7 +420,8 @@ Please check your %s installation.
|
|||
app.wm_withdraw()
|
||||
|
||||
# warn about audio problems
|
||||
if not opts["nosound"] and os.name == "posix" and pysolsoundserver is None:
|
||||
if (not opts["nosound"] and os.name == "posix" and
|
||||
not app.audio and pysolsoundserver is None):
|
||||
if 1 and app.opt.sound and re.search(r"linux", sys.platform, re.I):
|
||||
warn_pysolsoundserver = 1
|
||||
if thread is None:
|
||||
|
|
|
@ -39,6 +39,8 @@ import os, re, string, sys, time, types
|
|||
import traceback
|
||||
|
||||
import thread
|
||||
from threading import Thread
|
||||
|
||||
try:
|
||||
import pysolsoundserver
|
||||
except ImportError:
|
||||
|
@ -101,6 +103,7 @@ class AbstractAudioClient:
|
|||
self.stopMusic()
|
||||
|
||||
def playSample(self, name, priority=0, loop=0, volume=-1):
|
||||
##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:
|
||||
|
@ -301,7 +304,7 @@ class Win32AudioClient(AbstractAudioClient):
|
|||
def startServer(self):
|
||||
# use the built-in winsound module
|
||||
try:
|
||||
import winsound #keep#
|
||||
import winsound
|
||||
self.audiodev = winsound
|
||||
del winsound
|
||||
self.server = 0 # success - see also tk/menubar.py
|
||||
|
@ -329,3 +332,78 @@ class Win32AudioClient(AbstractAudioClient):
|
|||
a.PlaySound(None, flags)
|
||||
|
||||
|
||||
# /***********************************************************************
|
||||
# // OSS audio
|
||||
# ************************************************************************/
|
||||
|
||||
class OSSAudioClient(AbstractAudioClient):
|
||||
|
||||
def startServer(self):
|
||||
try:
|
||||
import ossaudiodev, wave
|
||||
self.server = 0 # success - see also tk/menubar.py
|
||||
self.audiodev = ossaudiodev #.open('w')
|
||||
except:
|
||||
if traceback: traceback.print_exc()
|
||||
self.server = None
|
||||
self.audiodev = None
|
||||
|
||||
|
||||
def _playSample(self, filename, priority, loop, volume):
|
||||
##print '_playSample:', filename, loop
|
||||
if loop:
|
||||
self._play_loop = True
|
||||
th = Thread(target=self._playLoop, args=(filename, priority))
|
||||
th.start()
|
||||
return 1
|
||||
th = Thread(target=self._play, args=(filename, priority))
|
||||
th.start()
|
||||
return 1
|
||||
|
||||
def _playLoop(self, filename, priority):
|
||||
##print '_playLoop:', filename
|
||||
try:
|
||||
import ossaudiodev, wave
|
||||
#audiodev = self.audiodev
|
||||
audiodev = ossaudiodev.open('w')
|
||||
w = wave.open(filename)
|
||||
fmt = ossaudiodev.AFMT_U8
|
||||
#fmt = ossaudiodev.AFMT_S8
|
||||
nch = w.getnchannels()
|
||||
rate = w.getframerate()
|
||||
frames = w.readframes(w.getnframes())
|
||||
#audiodev.nonblock()
|
||||
audiodev.setparameters(fmt, nch, rate)
|
||||
while self._play_loop:
|
||||
audiodev.write(frames)
|
||||
audiodev.reset()
|
||||
audiodev.close()
|
||||
#self.audiodev = ossaudiodev.open('w')
|
||||
return 1
|
||||
except:
|
||||
if traceback: traceback.print_exc()
|
||||
return 0
|
||||
|
||||
def _play(self, filename, priority):
|
||||
##print '_play:', filename
|
||||
try:
|
||||
import ossaudiodev, wave
|
||||
#audiodev = self.audiodev
|
||||
audiodev = ossaudiodev.open('w')
|
||||
audiodev.nonblock()
|
||||
w = wave.open(filename)
|
||||
fmt = ossaudiodev.AFMT_U8
|
||||
#fmt = ossaudiodev.AFMT_S8
|
||||
nch = w.getnchannels()
|
||||
rate = w.getframerate()
|
||||
frames = w.readframes(w.getnframes())
|
||||
audiodev.setparameters(fmt, nch, rate)
|
||||
audiodev.write(frames)
|
||||
return 1
|
||||
except:
|
||||
if traceback: traceback.print_exc()
|
||||
return 0
|
||||
|
||||
def _stopSamples(self):
|
||||
self._play_loop = False
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue