sounds.tin

sounds.tin
#python
 
from pyLib import sounds
sound = sounds.Sounds()
 
#gts
 
#action {^*%+{Orc|Troll|Man|Woman|Elf|Half-Elf|Dwarf|Hobbit|Bear|Numenorean|N\xFAmen\xF3rean}*%*{standing|sitting|resting|sleeping|fighting|riding|nocks a missile|begins some strange|arrived|leaves}%+{((north|south|east|west|up|down|above|below|hiding place).*)?}$} {
	#if {("%2" == "Numenorean") || ("%2" == "N\xFAmen\xF3rean") || ("%2" == "Woman")} {#var {tmpPlayer} {Man}};
	#else {#var {tmpPlayer} {%2}};
	#if {"%4" == "nocks a missile"} {#var {tmpAction} {shooting}};
	#elseif {"%4" == "begins some strange"} {
		#var {tmpAction} {casting};
		#line gag;
		#showme {*%1%2* is casting!}
	};
	#else {#var {tmpAction} {%4}};
	#var {tmpLocation} {%8};
	#if {("$tmpAction" == "leaves") && ("$tmpLocation" == "hiding place")} {
		#nop Maybe we do something with this later
	};
	#else {
		#nop {Ugly hack! We need this because TinTin isn't clearing %8.};
		#if {"$tmpLocation" == "hiding place"} {
			#var {tmpLocation} {}
		};
		playsound ${tmpPlayer}${tmpAction}${tmpLocation}\.wav
	};
	#unvar {tmpPlayer};
	#unvar {tmpAction};
	#unvar {tmpLocation}
}
 
#action {^You begin to feel {hungry|thirsty}.$} {
	playsound %1.wav
}
 
#action {^You gain a level!$} {
	playsound tada.wav
}
 
#action {^You {feel a hidden presence|smell blood}.$} {
	playsound hidden.wav
}
 
#alias {playsound} {#python sound.play("%0", "${volume}")}
#alias {stopsound} {#python sound.stop("%0")}
#alias {mutesound} {#python sound.mute()}
#alias {vol} {
	#if {"%1" == "up"} {
		#if {$volume < 81} {
			#math {volume} {$volume + 20};
			#echo {Increasing volume.}
		};
		#else {
			#var {volume} {100};
			#echo {Maximum volume!}
		}
	};
	#elseif {"%1" == "down"} {
		#if {$volume > 21} {
			#math {volume} {$volume - 20};
			#echo {Decreasing volume.}
		};
		#else {
			#var {volume} {1};
			#echo {Minimum volume!}
		}
	};
	#else {#showme Syntax: vol [up|down]}
}
 
#var {volume} {100}
sounds.py
import os
import random
 
from .tintin import TinTin
 
try:
	from pygame import mixer
except ImportError:
	TinTin.echo("Unable to import pyGame: please make sure it is installed.", "gts")
	mixer = None
 
SOUNDS_DIRECTORY = "sounds"
 
 
class Sounds(object):
	def __init__(self):
		self.soundObjects = {}
		self.muted = False
 
	def play(self, fileName="", volume="100"):
		fileName = fileName.strip()
		if self.muted or not fileName or mixer is None:
			return
		if not volume.isdigit():
			return TinTin.echo("Invalid volume: only numbers 1-100 are allowed.", "mume")
		volume = int(volume) / 100.0
		path = os.path.join(SOUNDS_DIRECTORY, fileName)
		if os.path.isdir(path):
			fileName = random.choice(os.listdir(path))
			path = os.path.join(path, fileName)
		if not fileName in self.soundObjects:
			if os.path.exists(path):
				self.soundObjects[fileName] = mixer.Sound(path)
			else:
				return TinTin.echo("No such sound", "mume")
		self.soundObjects[fileName].set_volume(volume)
		self.soundObjects[fileName].play()
 
	def stop(self, fileName=""):
		if mixer is None:
			return
		fileName = fileName.strip()
		if fileName:
			if fileName in self.soundObjects:
				self.soundObjects[fileName].stop()
				del self.soundObjects[fileName]
			else:
				TinTin.echo("that sound has not been loaded!", "mume")
		else:
			for soundObj in self.soundObjects:
				self.soundObjects[soundObj].stop()
			self.soundObjects.clear()
 
	def mute(self):
		if mixer is None:
			return
		self.muted = self.muted == False
		if self.muted:
			self.stop()
		TinTin.echo("sound {0}.".format("muted" if self.muted else "unmuted"), "mume")
 
	def load(self):
		if mixer is not None:
			mixer.init(frequency=44100, size=-16, channels=2, buffer=1024)
 
	def unload(self):
		if mixer is not None:
			mixer.quit()
  • tin/sounds.tin.txt
  • Last modified: 2022-09-12 15:51
  • by Admin