Created
May 27, 2017 02:29
-
-
Save AlexLamson/d807812d329c1c2fbd47d7df0e044dcb to your computer and use it in GitHub Desktop.
Python script to create tones every 5, 10 and 30 seconds. Useful for timing stretching exercises.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import math | |
''' | |
Note: | |
Requires the `sox` package, which can be installed with the following command: | |
sudo apt-get install sox | |
''' | |
def play_tone(duration, frequency): | |
os.system("play -nq synth {} sine {}".format(duration, frequency)) | |
'Uncomment to test what a frequency sounds like' | |
# play_tone(1, 1000) | |
# exit() | |
'Uncomment to wait 5 seconds before starting' | |
play_tone(0.2, 600) | |
play_tone(0.2, 800) | |
time.sleep(5-(0.2*2)) | |
def get_freq(t): | |
if t % 30 == 0: | |
return 1024 | |
if t % 10 == 0: | |
return 512 | |
if t % 5 == 0: | |
return 256 | |
return 0 | |
tone_duration = 0.8 | |
# Set this value to 0 to make the metronome continue forever | |
seconds_until_timeout = 10 | |
start_time = time.time() | |
t = 0 | |
while t < seconds_until_timeout or seconds_until_timeout <= 0: | |
#play tone | |
freq = get_freq(t) | |
if freq != 0: | |
play_tone(tone_duration, freq) | |
#start next loop next time second hand clicks | |
curr_time = time.time() - start_time | |
t = math.ceil(curr_time) | |
time.sleep(math.ceil(curr_time) - curr_time) | |
end_time = time.time() | |
duration = end_time - start_time | |
percent_error = 100*abs(duration-seconds_until_timeout)/seconds_until_timeout | |
# print("It took {:.4} seconds when the goal was {}, so {:.4}% error".format(duration, seconds_until_timeout, percent_error)) | |
print("Goal: {} sec | Actual: {:.6} sec | Error: {:.4}%".format(seconds_until_timeout, duration, percent_error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment