Skip to content

Tairitsu.BgmPlayer

Play background music.

BgmPlayer

Classes

BgmPlayer

BgmPlayer(path: str = GamePath.bgm['CITY'])

A class representing a background music player.

ATTRIBUTE DESCRIPTION
current_bgm

The currently playing background music.

METHOD DESCRIPTION
play

str, loop=-1): Plays the specified background music.

stop

Stops the currently playing background music.

resume

Resumes the paused background music.

update

Updates the BgmPlayer object.

Source code in Tairitsu/BgmPlayer.py
19
20
21
def __init__(self, path: str = GamePath.bgm["CITY"]):
    pygame.mixer.init()
    self.current_bgm = path
Attributes
bpm class-attribute instance-attribute
bpm = 122
current_bgm instance-attribute
current_bgm = path
offset class-attribute instance-attribute
offset = -2.648
Functions
play
play(name: str = '', loop=-1)

Play the specified background music.

PARAMETER DESCRIPTION
name

The name of the background music to play.

TYPE: str DEFAULT: ''

loop

The number of times the music should loop. Defaults to -1 (infinite loop).

TYPE: int DEFAULT: -1

RETURNS DESCRIPTION

None

Source code in Tairitsu/BgmPlayer.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def play(self, name: str = "", loop=-1):
    """
    Play the specified background music.

    Args:
        name (str): The name of the background music to play.
        loop (int, optional): The number of times the music should loop. Defaults to -1 (infinite loop).

    Returns:
        None
    """
    self.current_bgm = name if name != "" else self.current_bgm
    pygame.mixer.music.load(self.current_bgm)
    pygame.mixer.music.set_volume(0.5)
    self.start_time = time.time()
    pygame.mixer.music.play(loops=loop)
position
position() -> int

Returns the current position of the music playback in milliseconds.

RETURNS DESCRIPTION
int

The current position of the music playback in milliseconds.

TYPE: int

Source code in Tairitsu/BgmPlayer.py
49
50
51
52
53
54
55
56
def position(self) -> int:
        """
        Returns the current position of the music playback in milliseconds.

        Returns:
            int: The current position of the music playback in milliseconds.
        """
        return pygame.mixer.music.get_pos() + (self.offset * 1000)
position_beat
position_beat() -> float
Source code in Tairitsu/BgmPlayer.py
58
59
def position_beat(self) -> float:
    return (time.time() - self.start_time + self.offset) * self.bpm / 60
resume
resume()
Source code in Tairitsu/BgmPlayer.py
43
44
def resume(self):
    pygame.mixer.music.resume
stop
stop()
Source code in Tairitsu/BgmPlayer.py
40
41
def stop(self):
    pygame.mixer.music.stop()

SoundFX

SoundFX(path: str = GamePath.sound['MenuCursor'], volume: float = 0.5)
Source code in Tairitsu/BgmPlayer.py
63
64
65
66
def __init__(self, path: str = GamePath.sound["MenuCursor"], volume: float = 0.5):
   self.sound = pygame.mixer.Sound(path)
   self.sound.set_volume(volume)
   self.sound.play()
Attributes
sound instance-attribute
sound = pygame.mixer.Sound(path)