2020-10-01 17:58:17 +02:00
|
|
|
import simpleaudio
|
|
|
|
import random
|
|
|
|
import os
|
|
|
|
import os.path
|
2021-06-12 21:57:16 +02:00
|
|
|
import logging
|
2020-10-01 17:58:17 +02:00
|
|
|
|
|
|
|
class ShuffledFolderPlayer:
|
|
|
|
|
|
|
|
def __init__(self, folder: str):
|
|
|
|
self._sounds = []
|
|
|
|
for folder_element in os.listdir(folder):
|
|
|
|
self._sounds.append(
|
|
|
|
simpleaudio.WaveObject.from_wave_file(os.path.join(folder, folder_element))
|
|
|
|
)
|
|
|
|
|
|
|
|
def play_one_random(self):
|
2021-06-12 21:57:16 +02:00
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
|
|
|
random.choice(self._sounds).play()
|
|
|
|
except Exception as e: # simpleaudio seems like it does not expose the exceptions it may raise. So we just log it and call it a day
|
|
|
|
logging.warning(f"Could not play sound file! {e}")
|
|
|
|
pass
|
2020-09-30 00:15:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BirbnetesIoTPlatformPlaybackDriver:
|
2020-10-01 17:58:17 +02:00
|
|
|
shuffled_folder_player = None
|
2020-09-30 00:15:17 +02:00
|
|
|
|
|
|
|
@classmethod
|
2020-10-01 17:58:17 +02:00
|
|
|
def init(cls, folder: str = '/var/lib/birbnetes/enemy_soudns'):
|
|
|
|
cls.shuffled_folder_player = ShuffledFolderPlayer(folder)
|
2020-09-30 00:15:17 +02:00
|
|
|
|
|
|
|
@classmethod
|
2020-10-01 17:58:17 +02:00
|
|
|
def play_one_random(cls):
|
|
|
|
cls.shuffled_folder_player.play_one_random()
|
2020-09-30 03:12:25 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def cleanup(cls):
|
2020-10-01 17:58:17 +02:00
|
|
|
simpleaudio.stop_all()
|