File: sound.py

package info (click to toggle)
lightyears 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,804 kB
  • sloc: python: 5,532; sh: 39; makefile: 6
file content (66 lines) | stat: -rw-r--r-- 1,739 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#
# 20,000 Light Years Into Space
# This game is licensed under GPL v2, and copyright (C) Jack Whitham 2006-07.
#


import pygame


from .primitives import *
from .game_types import *
from . import resource, config


VOL_SCALE = 0.7


def FX(name: Sounds) -> None:  # NO-COV
    s = resource.Load_Sound(name) # (comes from a cache)
    if ( s is not None ) and not config.cfg.mute:
        s.set_volume(VOL_SCALE)
        s.play()


class Persisting_Sound:
    def __init__(self, name: Sounds, secondary: Optional[Sounds] = None) -> None:
        self.sobj = resource.Load_Sound(name)
        if ( secondary is not None ):
            # A different, less annoying mode.
            self.sobj2 = resource.Load_Sound(secondary)
        else:
            self.sobj2 = self.sobj

        self.schan: Optional[pygame.Channel] = None

    def Set(self, volume: float) -> None:  # NO-COV
        if (( self.sobj is None )
        or ( self.sobj2 is None )):
            return

        if config.cfg.mute:
            volume = 0.0

        if ( volume <= 0.0 ):
            self.sobj.stop()
            self.sobj2.stop()
        else:
            self.sobj.set_volume(volume * VOL_SCALE)
            self.sobj2.set_volume(volume * VOL_SCALE)
            if (( self.schan is None )
            or ( not ( self.schan.get_sound()
                            in [ self.sobj , self.sobj2 ] ))):
                self.schan = self.sobj.play()
            if self.schan:
                self.schan.queue(self.sobj2)

    def Fade_Out(self) -> None:  # NO-COV
        if (( self.sobj is None )
        or ( self.sobj2 is None )
        or ( self.schan is None )):
            return

        self.schan.queue(self.sobj2)
        self.sobj2.fadeout(200)