Skip to content

Instantly share code, notes, and snippets.

@Jonarzz
Created November 14, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonarzz/40f1b139cb4b3990aa8c to your computer and use it in GitHub Desktop.
Save Jonarzz/40f1b139cb4b3990aa8c to your computer and use it in GitHub Desktop.
#coding=utf-8
import pyaudio
import wave
import time
from datetime import datetime
import sys
def main():
hour = input("Hour for the alarm to set: ")
while int(hour) < 0 or int(hour) > 23:
hour = input("Invalid hour.\nHour for the alarm to set: ")
minute = input("Minute for the alarm to set: ")
while int(minute) < 0 or int(minute) > 59:
minute = input("Invalid minute.\nMinute for the alarm to set: ")
alarm_time = str(hour) + ":" + str(minute)
while True:
if datetime.now().strftime('%H:%M') == datetime.strptime(alarm_time, '%H:%M').time().strftime('%H:%M'):
play_alarm_sound()
user_input = input("Do you want to set a new alarm? [Y/N]\n")
if str(user_input) in ['y', 'Y']:
hour = input("Hour for the alarm to set: ")
while int(hour) < 0 or int(hour) > 23:
hour = input("Invalid hour.\nHour for the alarm to set: ")
minute = input("Minute for the alarm to set: ")
while int(minute) < 0 or int(minute) > 59:
minute = input("Invalid minute.\nMinute for the alarm to set: ")
alarm_time = str(hour) + ":" + str(minute)
elif str(user_input) in ['n', 'N']:
sys.exit()
else:
while str(user_input) not in ['y','Y', 'n', 'N']:
user_input = input("Do you want to set a new alarm? [Y/N]\n")
if str(user_input) in ['y', 'Y']:
hour = input("Hour for the alarm to set: ")
while int(hour) < 0 or int(hour) > 23:
hour = input("Invalid hour.\nHour for the alarm to set: ")
minute = input("Minute for the alarm to set: ")
while int(minute) < 0 or int(minute) > 59:
minute = input("Invalid minute.\nMinute for the alarm to set: ")
alarm_time = str(hour) + ":" + str(minute)
else:
sys.exit()
time.sleep(1)
def play_alarm_sound():
chunk = 1024
f = wave.open("alarm.wav","rb")
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
data = f.readframes(chunk)
while data != b'':
stream.write(data)
data = f.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()
if __name__ == "__main__":
try:
main()
except ValueError:
print("Not a number! Run the program again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment