-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnoisebox.py
86 lines (71 loc) · 2.36 KB
/
noisebox.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import json
import os.path
import sys
import time
import random
# created using - https://patorjk.com/software/taag/#p=display&f=Graffiti&t=NoiseBox
BANNER = r"""
_______ .__ __________
\ \ ____ |__| ______ ____\______ \ _______ ___
/ | \ / _ \| |/ ___// __ \| | _// _ \ \/ /
/ | ( <_> ) |\___ \\ ___/| | ( <_> > <
\____|__ /\____/|__/____ >\___ >______ /\____/__/\_ \
\/ \/ \/ \/ \/
NoiseBox - Background noise generator for concentration in your terminal.
"""
print(BANNER)
from pygame import mixer # noqa
DEBUG = False # Change this to `True` to print what's played
if getattr(sys, 'frozen', False):
APP_PATH = os.path.abspath(os.path.dirname(sys.executable))
else:
APP_PATH = os.path.dirname(os.path.abspath(__file__))
SOUNDS = {}
with open(os.path.join(APP_PATH, "mix.json")) as j:
MIX = json.load(j)
MIX_BANNER = MIX["banner"]
MIX_DESCRIPTION = MIX["description"]
SECONDS_MIN = MIX["random_pick_seconds_min"]
SECONDS_MAX = MIX["random_pick_seconds_max"]
START_ON = MIX["random_pick_start_on_seconds"]
CHANNELS = MIX["channels"]
RANDOM_SOUNDS = MIX["randomly_pick_and_play"]
GLOBAL_VOLUME = float(MIX.get("global_volume", 1.0))
def play(item, loops):
track = item["sound"]
volume = float(item["vol"]) * GLOBAL_VOLUME
if DEBUG:
if loops == -1:
print("Looping '{0}' at volume {1}".format(track, volume))
else:
print("Play '{0}' at volume {1}".format(track, volume))
SOUNDS[track].set_volume(volume)
SOUNDS[track].play(loops)
def main():
print("=" * 20)
print("Mix - ", MIX_DESCRIPTION)
print(MIX_BANNER)
print("=" * 20)
if DEBUG:
print("----")
mixer.init()
mixer.set_num_channels(CHANNELS)
for k, value in MIX["sounds"].items():
SOUNDS[k] = mixer.Sound(os.path.join(APP_PATH, "noise", value))
for track in MIX["loop"]:
play(track, loops=-1)
do_random_pick()
def do_random_pick():
time.sleep(START_ON)
play_random_pick()
while True:
next_play = random.randrange(SECONDS_MIN, SECONDS_MAX)
time.sleep(next_play)
play_random_pick()
def play_random_pick():
if not RANDOM_SOUNDS:
return
track = random.choice(RANDOM_SOUNDS)
play(track, loops=0)
if __name__ == "__main__":
main()