-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmeditation_timer.py
executable file
·218 lines (177 loc) · 6.72 KB
/
meditation_timer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
"""
Meditation timer
A command-line tool to assist in the practice of mindfulness
use the -h option for full help and options
(eg: meditation_timer.py -h)
Buddha sits, clouds pass
unending meditation
expression of bliss
"""
__VERSION__ = "0.4.0"
# Importing modules
from os.path import join, dirname
from time import time, sleep
import argparse
import pygame
import sys
import os
# Defining global variables
if dirname(__file__) == "/usr/local/bin":
DATA_PATH = "/usr/share/meditation-timer/data"
else:
DATA_PATH = dirname(os.path.realpath(__file__)) + "/data"
# Defining functions
def wait(duration=0, debug_time=False):
"""Wait a certain number of seconds
"""
t0 = time()
if duration <= 0:
duration = 0
time_end = time() + float(duration)
time_diff = time_end - time()
while time_diff > 0:
sleep(0.05)
time_diff = time_end - time()
if debug_time:
delta = time() - t0
print(" Waited: " + pretty_time(delta))
def play_chime():
"""Play a chime once
"""
pygame.mixer.init(frequency=44100)
pygame.mixer.music.load(DATA_PATH + "/bowl-short.ogg")
pygame.mixer.music.set_volume(args.volume)
pygame.mixer.music.play()
pygame.time.wait(8450)
pygame.mixer.quit()
def play_chimes(n=1, debug_time=False):
"""Play a chime n times
"""
for i in range(n):
t0 = time()
play_chime()
if debug_time:
delta = time() - t0
print(" Chime " + str(i + 1) + ": " + pretty_time(delta))
def pretty_time(t):
"""Format time in minutes and seconds
Format example: '2 min 23.9 sec'
"""
tmin = int(t) / 60
tsec = int(t - tmin * 60)
trest = int((t - tmin * 60 - tsec) * 100)
return "{} min {}.{} sec".format(tmin, tsec, trest)
def print_file(ascii_file, debug_time=False):
"""Print the content of a text file, for example ascii art
"""
if not debug_time:
ascii_data = open(ascii_file)
print("\n" * 69)
for line in ascii_data:
print(line.rstrip())
print("")
def meditation_timer():
"""Meditation timer
"""
t0 = time()
bell_duration = 8.45 # seconds
# Preparation
if not args.no_print:
print_file(join(DATA_PATH, "buddha0.txt"), args.debug_time)
wait(3, args.debug_time)
if not args.no_print:
print_file(join(DATA_PATH, "buddha1.txt"), args.debug_time)
wait(args.delay, args.debug_time)
if not args.no_print:
print_file(join(DATA_PATH, "buddha2.txt"), args.debug_time)
if args.quiet:
wait(bell_duration, args.debug_time)
else:
play_chimes(args.start_bells, args.debug_time)
if args.debug_time:
delta = time() - t0
print("--Preparation: " + pretty_time(delta))
# Meditation
if not args.no_print:
print_file(join(DATA_PATH, "buddha.txt"), args.debug_time)
t1 = time()
if args.interval:
num_intervals = int(args.period / args.interval_time)
remainder = args.period - num_intervals * args.interval_time
for i in range(num_intervals):
wait(args.interval_time * 60 - bell_duration * args.interval_bells, args.debug_time)
if args.quiet:
wait(bell_duration, args.debug_time)
else:
play_chimes(args.interval_bells, args.debug_time)
wait(remainder - bell_duration, args.debug_time)
meditation_time = time() - t1
if args.quiet:
wait(bell_duration, args.debug_time)
else:
play_chimes(args.end_bells, args.debug_time)
else:
wait(args.period * 60 - bell_duration, args.debug_time)
meditation_time = time() - t1
if args.quiet:
wait(bell_duration, args.debug_time)
else:
play_chimes(args.end_bells, args.debug_time)
# End of meditation
if not args.no_print:
print_file(join(DATA_PATH, "buddha3.txt"), args.debug_time)
tf = time()
program_time = time() - t0
if args.debug_time:
print("--Meditation:" + pretty_time(meditation_time))
print("--Program:" + pretty_time(program_time))
# Main loop
if __name__ == "__main__":
# Option parser
parser = argparse.ArgumentParser(description=
"""Meditation timer. Sound a bells at the start and end of the meditation period""")
parser.add_argument('-p', '--period', type=float, nargs='?', default=30,
help= 'meditation period duration, in minutes, default is 30')
parser.add_argument('-d', '--delay', type=int, nargs='?', default=30,
help='initial delay, in seconds, default is 30')
parser.add_argument('-s', '--start-bells', type=int, default=3,
help='number of bell chimes at the start of the meditation, default is 3')
parser.add_argument('-e', '--end-bells', type=int, default=3,
help='number of bell chimes at the end of the meditation, default is 3')
parser.add_argument('-i', '--interval', action="store_true",
help='whether bells should be played at interval during the meditation')
parser.add_argument('-I', '--interval-time', type=float, default=5,
help='interval, in minutes, at which to play bells during the meditation')
parser.add_argument('-b', '--interval-bells', type=int, default=1,
help='number of bells to play after each interval, default is 1')
parser.add_argument('-V', '--version', action="store_true",
help='show version number and quit')
parser.add_argument('-v', '--volume', type=float, default=0.3,
help='chime sound volume, from 0 to 1, default is 0.3')
parser.add_argument('-q', '--quiet', action="store_true",
help='run program without sound')
parser.add_argument('-n', '--no-print', action="store_true",
help='run program without printing text or the ascii art')
parser.add_argument('-D', '--debug-time', action="store_true",
help='print debug information about program and meditation times')
args = parser.parse_args()
# Insure that option values OK
assert args.period > 0, \
"Meditation period (-p) must be positive"
assert args.delay >= 0, \
"Meditation delay (-d) must be zero or positive"
assert args.start_bells >= 0, \
"Number of start bells (-s) must be null or positive"
assert args.end_bells >= 0, \
"Number of end bells (-e) must be null or positive"
assert args.interval_bells >= 0, \
"Number of interval bells (-b) must be null or positive"
assert args.interval_time > 0, \
"Interval duration (-I) must be positive"
# Show version or licence
if args.version:
print(__VERSION__)
sys.exit(0)
# Launch meditation period
meditation_timer()