ya2 · news · projects · code · about

removed changelog
[pmachines.git] / audio / music.py
CommitLineData
e65a09cf 1from os.path import exists, basename
55efcb0a 2from platform import system
54a1397e 3from glob import glob
55efcb0a 4from pathlib import Path
54a1397e
FC
5from random import choice
6from logging import info
55efcb0a 7from panda3d.core import AudioSound, Filename
54a1397e
FC
8
9
10class MusicMgr:
11
9ba5488b 12 def __init__(self, volume):
55efcb0a
FC
13 files = self.curr_path + 'assets/audio/music/*.ogg'
14 self._start_music(glob(files))
9ba5488b
FC
15 base.musicManager.setVolume(.8 * volume)
16 base.sfxManagerList[0].setVolume(volume)
54a1397e
FC
17 taskMgr.add(self._on_frame, 'on frame music')
18
55efcb0a
FC
19 @property
20 def is_appimage(self):
21 par_path = str(Path(__file__).parent.absolute())
22 is_appimage = par_path.startswith('/tmp/.mount_Pmachi')
23 return is_appimage and par_path.endswith('/usr/bin')
24
25 @property
26 def curr_path(self):
27 if system() == 'Windows':
28 return ''
29 if exists('main.py'):
30 return ''
31 else:
32 par_path = str(Path(__file__).parent.absolute())
33 if self.is_appimage:
34 par_path = str(Path(par_path).absolute())
35 par_path += '/'
36 return par_path
37
54a1397e
FC
38 def _start_music(self, files):
39 self._music = loader.load_music(choice(files))
40 info('playing music ' + self._music.get_name())
41 self._music.play()
42
e1e44d5c
FC
43 def set_volume(self, volume):
44 base.musicManager.setVolume(.8 * volume)
45 base.sfxManagerList[0].setVolume(volume)
46
54a1397e
FC
47 def _on_frame(self, task):
48 if self._music.status() == AudioSound.READY:
e65a09cf
FC
49 oggs = Filename(
50 self.curr_path + 'assets/audio/music/*.ogg').to_os_specific()
5c7b5457 51 files = glob(oggs)
e65a09cf
FC
52 rm_music = Filename(
53 self.curr_path + 'assets/audio/music/' +
54 basename(self._music.get_name())).to_os_specific()
5c7b5457
FC
55 # basename is needed in windows
56 files.remove(rm_music)
54a1397e
FC
57 self._start_music(files)
58 return task.cont