Page 1 of 1

[CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 6:06 am
by iPlayer
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 8:03 am
by L'In20Cible
Placing your custom sounds into the "../sound/music/" directory used to work just fine.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 9:05 am
by iPlayer
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 9:41 am
by iPlayer
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 9:45 am
by L'In20Cible
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 9:52 am
by iPlayer
"play", "stop", "is_precached" and "prepache" - but "precache" needs to be overwritten anyways.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 9:56 am
by L'In20Cible
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 10:41 am
by iPlayer
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 10:53 am
by L'In20Cible
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 10:55 am
by iPlayer
That makes it complicated. What about separate class (like FakePrecacheSound or StreamSound) that anybody can import when they want?

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 10:55 am
by L'In20Cible
lol, check my edit I made right before reading your comment. :tongue:

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 10:59 am
by iPlayer
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.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 11:07 am
by L'In20Cible
That class should definetly ineherits of Sound (isinstance checkings, etc). This can be directly into engines.sounds, in my opinion.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 11:09 am
by iPlayer
Might just as well turn current Sound into a base class and do a PrecachedSound class that inherits from it.

Re: [CS:GO] Playing sounds

Posted: Sat Jul 09, 2016 6:29 pm
by iPlayer