Skip to content

Instantly share code, notes, and snippets.

@Jonarzz Jonarzz/Alarm Clock
Created Nov 14, 2015

Embed
What would you like to do?
#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
You can’t perform that action at this time.