[CS:GO] Playing sounds

Discuss API design here.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

[CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 6:06 am

According to this AM article

Syntax: Select all

from core import GAME_NAME
from engines.sound import Sound
from events import Event
from players.entity import Player
from stringtables import string_tables
from stringtables.downloads import Downloadables


SOUND_PATH = "arcjail/welcome.mp3"


class GenericSound(Sound):
def precache(self):
"""Precache the sample."""
if GAME_NAME in ("csgo", ): # Probably more games: L4D2 etc
string_tables.soundprecache.add_string(self.sample, self.sample)
else:
engine_sound.precache_sound(self.sample)

@property
def sample(self):
"""Return the filename of the Sound instance."""
if GAME_NAME in ("csgo", ):
return "*/{}".format(self._sample)

return self._sample


my_sound = GenericSound(SOUND_PATH)
downloadables = Downloadables()
downloadables.add("sound/{}".format(SOUND_PATH))


@Event('player_spawn')
def on_player_spawn(game_event):
player = Player.from_userid(game_event['userid'])
my_sound.play(player.index)


You can't, though:
1. Use "download" kwarg in GenericSound constructor - it won't work
2. Properly get sound duration

SP really lacks this functionality. Simple file separation (generic fallback Sound class in engines/sound/__init__.py and an improved Sound class in engines/sound/csgo/__init__.py that inherits from it) would do the trick.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:GO] Playing sounds

Postby L'In20Cible » Sat Jul 09, 2016 8:03 am

Placing your custom sounds into the "../sound/music/" directory used to work just fine.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 9:05 am

Yeah, that's mentioned in the article. But what if I want to keep the same structure for all games?

So what you're saying is that I should move my files to the sound/music? I haven't thought of that. I'd rather use this Sound subclass till SP has its own implementation. It's a lack of feature anyways.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 9:41 am

Also, this thing
By adding your custom sounds under sound/music/, they will automatically be streamed from disk [...] This will have the side effect of your sounds volume being tied to the game's music volume, which many players turn down or off. For this reason, it's not recommended.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:GO] Playing sounds

Postby L'In20Cible » Sat Jul 09, 2016 9:45 am

I didn't read that article but, I would not go for the implementation you proposed. The sample attribute shouldn't be overriden. I would rather override "play" and makes sure the asterix is added to the path before being sent to the engine. I will make some testings to see if that is possible, and what other games are affected.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 9:52 am

"play", "stop", "is_precached" and "prepache" - but "precache" needs to be overwritten anyways.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:GO] Playing sounds

Postby L'In20Cible » Sat Jul 09, 2016 9:56 am

Yes, feel free to go ahead and propose a PR, as well. But my only concern here is that the sample attribute should reflect the value you passed on initialization and should be the same "scheme" for all engine/game.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 10:41 am

Another thing is that for samples starting with "music/", regular Sound class should be used. Because modified one won't work for wav-files.
Okay, I will think this over, and will do a PR to master.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:GO] Playing sounds

Postby L'In20Cible » Sat Jul 09, 2016 10:53 am

Not just those under the music directory, but also those embedded into VPKs (already cached client-side), I guess. This become a lot per-sound-engine-game checks to concider that I start to think a separate class would work better than trying to merge everything into the same. That way, we let user decide and he is the one that know better than what we can assume.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 10:55 am

That makes it complicated. What about separate class (like FakePrecacheSound or StreamSound) that anybody can import when they want?
Last edited by iPlayer on Sat Jul 09, 2016 10:56 am, edited 1 time in total.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:GO] Playing sounds

Postby L'In20Cible » Sat Jul 09, 2016 10:55 am

lol, check my edit I made right before reading your comment. :tongue:
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 10:59 am

Yeah... Right. That seems like a nice idea. How about I make it a custom package?

Syntax: Select all

from streamsound import StreamSound    # Still defaults to engines.sound.Sound if the game engine is not CS:GO


Or, if embedding in SP itself... Probably create a module engines.streamsound with StreamSound class in it. Another question is whether this class should inherit from engines.sound.Sound or just duplicate some code so that both classes seem equal. Maybe create SoundBase class that both Sound and StreamSound inherit from? But where do I place it? I'd make it this way:

engines.sound.base - BaseSound
engines.sound.sound - Sound
engines.sound.streamsound - StreamSound
engines.sound.__init__.py - forward imports for Sound, StreamSound and BaseSound.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:GO] Playing sounds

Postby L'In20Cible » Sat Jul 09, 2016 11:07 am

That class should definetly ineherits of Sound (isinstance checkings, etc). This can be directly into engines.sounds, in my opinion.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 11:09 am

Might just as well turn current Sound into a base class and do a PrecachedSound class that inherits from it.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: [CS:GO] Playing sounds

Postby iPlayer » Sat Jul 09, 2016 6:29 pm

Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 11 guests