New Sound class!!

Official Announcements about Source.Python.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

New Sound class!!

Postby satoon101 » Wed Jun 18, 2014 2:19 am

Just committed some changes that add a new Sound class in engines.sound to help interact with sounds. Each Sound instance should be specific to the sample (wav or mp3 file) that is passed on instantiation. All attributes, with the exception of the sample itself and a _downloads attribute which stores a Downloadables instance if the sound is to be downloadable, are read/write values for the instance. Only the recipients, index, and sample values are required on instatiation. If you wish to use a value for any of the others that are not the default values, you need to pass them in as well:

Syntax: Select all

from engines.sound import Sound

# Just pass in the required values
# You can use an empty tuple to denote all users for recipients
# To emit from the "world", you can use 0 or SOUND_FROM_WORLD
# If from the world, you can use the origin to set where in the world the sound should emit from
mysound = Sound((), SOUND_FROM_WORLD, 'source-python/gungame/knife_level.mp3')

# To make the sound downloadable
mysound = Sound((), SOUND_FROM_WORLD, 'source-python/gungame/knife_level.mp3', download=True)

Notice also that unlike other packages, the sounds are not required to be in the sound/source-python/ directory. There are plenty of sounds that come with the server that people might want to play, and even those from other plugins like SM or Mani. We would still prefer that sounds included with SP plugins are located within the sound/source-python directory, and that might even be a requirement for scripts hosted on our online plugin manager (once we have one of those).

The other optional values that can be set on instantiation or even by using the same name for the instance attribute to change the value are as follows (with their default values):

  • volume - VOL_NORM
  • attenuation - Attenuations.NONE
  • channel - Channels.AUTO
  • flags - 0
  • pitch - PitchTypes.HIGH
  • origin - NULL_VECTOR
  • direction - NULL_VECTOR
  • origins - ()
  • update_positions - True
  • sound_time - 0.0
  • speaker_entity - -1

To play the sound, you can pass in recipients or it will use the recipients attribute of the instance:

Syntax: Select all

from filters.recipients import RecipientFilter

# Use the instance's recipients
mysound.play()

# Use the given recipients
# Assuming the index 1 is a player on the server
recipients = RecipientFilter(1)
mysound.play(recipients)


To stop the sound, you can pass in an index and/channel. The index is the emitting index, not a member of the recipient filter. If you don't pass an index or channel, each will use the instance's current values of those attributes:

Syntax: Select all

mysound.index = 1

mysound.play(RecipientFilter(2))
mysound.stop(1)


When play is called, we check to see if the sound is precached and if not, precache the sound. Those methods are provided within the class, but most of the time, they will only be called internally.

There is also a "duration" property of the class that returns the length (in seconds) of the sound:

Syntax: Select all

length = mysound.duration
qazuar
Junior Member
Posts: 16
Joined: Fri Jun 20, 2014 9:05 am

Postby qazuar » Wed Jul 09, 2014 8:25 pm

This crashes on CS:GO, any chance it will be solved?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Jul 10, 2014 12:48 am

I don't think I have tested sounds on CS:GO. I will do that when I get the chance to see what I can find out.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Thu Jul 10, 2014 2:27 am

I patched orangebox but not csgo, check out the comments of the follwing commit:https://github.com/Source-Python-Dev-Team/Source.Python/commit/03d9e12a35b065b64c3dc121237ce5a00c6edbb1

Return to “News & Announcements”

Who is online

Users browsing this forum: No registered users and 18 guests