pyxora.wrapper.music

  1from ..utils import engine
  2import pygame
  3
  4class Music:
  5    _volume = 1.0
  6    _active = None
  7    def __init__(self, path:str, volume:float=1.0) -> None:
  8        """
  9        Initialize a Music object with a path and volume.
 10
 11        Args:
 12            path (str): The path to the music file.
 13            volume (float): The volume level of the music.
 14        """
 15        self._path = path
 16        self._local_volume = volume
 17
 18    @property
 19    def playing(self) -> bool:
 20        """The playing status of the music."""
 21        if self is not Music._active:
 22            return False
 23        return pygame.mixer.music.get_busy()
 24
 25    @property
 26    def time(self) -> int:
 27        """The current time position of the music."""
 28        if self is not Music._active:
 29            return 0
 30        return pygame.mixer.music.get_pos()
 31
 32    @property
 33    def volume(self) -> float:
 34        """
 35        The volume of the music (volume * local_volume)."""
 36        return self._volume * self._local_volume
 37
 38    @property
 39    def metadata(self) -> dict:
 40        """The metadata of the music."""
 41        return pygame.mixer.music.get_metadata(self._path)
 42
 43    @classmethod
 44    def change_volume(cls,value: float) -> None:
 45        """Change the volume of all music."""
 46        cls._volume = value
 47        pygame.mixer.music.set_volume(cls.volume)
 48
 49    def change_local_volume(self,value: float) -> None:
 50        """Change the volume of the music."""
 51        self._local_volume = value
 52        pygame.mixer.music.set_volume(self.volume)
 53
 54    def play(self,loops:int=-1, start:float=0.0, fade_ms:int =0) -> None:
 55        """
 56        Starts the music.
 57
 58        Parameters:
 59            loops (int): Number of times to repeat the music after the first play.
 60                        -1 means the music will play once.
 61                        0 means indefinitely.
 62                        >=1 means play n times.
 63
 64            start (float): Position (in seconds) to start the music from.
 65
 66            fade_ms (int): Milliseconds to fade in the music.
 67        """
 68        pygame.mixer.music.load(self._path)
 69        pygame.mixer.music.set_volume(self.volume)
 70        pygame.mixer.music.play(loops+1,start,fade_ms)
 71        Music._active = self
 72
 73    def resume(self) -> None:
 74        """
 75        Resume the music.
 76        """
 77        if self is not Music._active:
 78            engine.warning(f"Music object {self} is not active.")
 79            return
 80        pygame.mixer.music.unpause()
 81
 82    def pause(self) -> None:
 83        """
 84        Pause the music.
 85        """
 86        if self is not Music._active:
 87            engine.warning(f"Music object {self} is not active.")
 88            return
 89        pygame.mixer.music.pause()
 90
 91    def stop(self) -> None:
 92        """
 93        Stop the music.
 94        """
 95        if self is not Music._active:
 96            engine.warning(f"Music object {self} is not active.")
 97            return
 98        pygame.mixer.music.stop()
 99        pygame.mixer.music.unload()
100        Music._active = None
class Music:
  5class Music:
  6    _volume = 1.0
  7    _active = None
  8    def __init__(self, path:str, volume:float=1.0) -> None:
  9        """
 10        Initialize a Music object with a path and volume.
 11
 12        Args:
 13            path (str): The path to the music file.
 14            volume (float): The volume level of the music.
 15        """
 16        self._path = path
 17        self._local_volume = volume
 18
 19    @property
 20    def playing(self) -> bool:
 21        """The playing status of the music."""
 22        if self is not Music._active:
 23            return False
 24        return pygame.mixer.music.get_busy()
 25
 26    @property
 27    def time(self) -> int:
 28        """The current time position of the music."""
 29        if self is not Music._active:
 30            return 0
 31        return pygame.mixer.music.get_pos()
 32
 33    @property
 34    def volume(self) -> float:
 35        """
 36        The volume of the music (volume * local_volume)."""
 37        return self._volume * self._local_volume
 38
 39    @property
 40    def metadata(self) -> dict:
 41        """The metadata of the music."""
 42        return pygame.mixer.music.get_metadata(self._path)
 43
 44    @classmethod
 45    def change_volume(cls,value: float) -> None:
 46        """Change the volume of all music."""
 47        cls._volume = value
 48        pygame.mixer.music.set_volume(cls.volume)
 49
 50    def change_local_volume(self,value: float) -> None:
 51        """Change the volume of the music."""
 52        self._local_volume = value
 53        pygame.mixer.music.set_volume(self.volume)
 54
 55    def play(self,loops:int=-1, start:float=0.0, fade_ms:int =0) -> None:
 56        """
 57        Starts the music.
 58
 59        Parameters:
 60            loops (int): Number of times to repeat the music after the first play.
 61                        -1 means the music will play once.
 62                        0 means indefinitely.
 63                        >=1 means play n times.
 64
 65            start (float): Position (in seconds) to start the music from.
 66
 67            fade_ms (int): Milliseconds to fade in the music.
 68        """
 69        pygame.mixer.music.load(self._path)
 70        pygame.mixer.music.set_volume(self.volume)
 71        pygame.mixer.music.play(loops+1,start,fade_ms)
 72        Music._active = self
 73
 74    def resume(self) -> None:
 75        """
 76        Resume the music.
 77        """
 78        if self is not Music._active:
 79            engine.warning(f"Music object {self} is not active.")
 80            return
 81        pygame.mixer.music.unpause()
 82
 83    def pause(self) -> None:
 84        """
 85        Pause the music.
 86        """
 87        if self is not Music._active:
 88            engine.warning(f"Music object {self} is not active.")
 89            return
 90        pygame.mixer.music.pause()
 91
 92    def stop(self) -> None:
 93        """
 94        Stop the music.
 95        """
 96        if self is not Music._active:
 97            engine.warning(f"Music object {self} is not active.")
 98            return
 99        pygame.mixer.music.stop()
100        pygame.mixer.music.unload()
101        Music._active = None
Music(path: str, volume: float = 1.0)
 8    def __init__(self, path:str, volume:float=1.0) -> None:
 9        """
10        Initialize a Music object with a path and volume.
11
12        Args:
13            path (str): The path to the music file.
14            volume (float): The volume level of the music.
15        """
16        self._path = path
17        self._local_volume = volume

Initialize a Music object with a path and volume.

Arguments:
  • path (str): The path to the music file.
  • volume (float): The volume level of the music.
playing: bool
19    @property
20    def playing(self) -> bool:
21        """The playing status of the music."""
22        if self is not Music._active:
23            return False
24        return pygame.mixer.music.get_busy()

The playing status of the music.

time: int
26    @property
27    def time(self) -> int:
28        """The current time position of the music."""
29        if self is not Music._active:
30            return 0
31        return pygame.mixer.music.get_pos()

The current time position of the music.

volume: float
33    @property
34    def volume(self) -> float:
35        """
36        The volume of the music (volume * local_volume)."""
37        return self._volume * self._local_volume

The volume of the music (volume * local_volume).

metadata: dict
39    @property
40    def metadata(self) -> dict:
41        """The metadata of the music."""
42        return pygame.mixer.music.get_metadata(self._path)

The metadata of the music.

@classmethod
def change_volume(cls, value: float) -> None:
44    @classmethod
45    def change_volume(cls,value: float) -> None:
46        """Change the volume of all music."""
47        cls._volume = value
48        pygame.mixer.music.set_volume(cls.volume)

Change the volume of all music.

def change_local_volume(self, value: float) -> None:
50    def change_local_volume(self,value: float) -> None:
51        """Change the volume of the music."""
52        self._local_volume = value
53        pygame.mixer.music.set_volume(self.volume)

Change the volume of the music.

def play(self, loops: int = -1, start: float = 0.0, fade_ms: int = 0) -> None:
55    def play(self,loops:int=-1, start:float=0.0, fade_ms:int =0) -> None:
56        """
57        Starts the music.
58
59        Parameters:
60            loops (int): Number of times to repeat the music after the first play.
61                        -1 means the music will play once.
62                        0 means indefinitely.
63                        >=1 means play n times.
64
65            start (float): Position (in seconds) to start the music from.
66
67            fade_ms (int): Milliseconds to fade in the music.
68        """
69        pygame.mixer.music.load(self._path)
70        pygame.mixer.music.set_volume(self.volume)
71        pygame.mixer.music.play(loops+1,start,fade_ms)
72        Music._active = self

Starts the music.

Arguments:
  • loops (int): Number of times to repeat the music after the first play. -1 means the music will play once. 0 means indefinitely. >=1 means play n times.
  • start (float): Position (in seconds) to start the music from.
  • fade_ms (int): Milliseconds to fade in the music.
def resume(self) -> None:
74    def resume(self) -> None:
75        """
76        Resume the music.
77        """
78        if self is not Music._active:
79            engine.warning(f"Music object {self} is not active.")
80            return
81        pygame.mixer.music.unpause()

Resume the music.

def pause(self) -> None:
83    def pause(self) -> None:
84        """
85        Pause the music.
86        """
87        if self is not Music._active:
88            engine.warning(f"Music object {self} is not active.")
89            return
90        pygame.mixer.music.pause()

Pause the music.

def stop(self) -> None:
 92    def stop(self) -> None:
 93        """
 94        Stop the music.
 95        """
 96        if self is not Music._active:
 97            engine.warning(f"Music object {self} is not active.")
 98            return
 99        pygame.mixer.music.stop()
100        pygame.mixer.music.unload()
101        Music._active = None

Stop the music.