Showing posts with label alarm-clock. Show all posts
Showing posts with label alarm-clock. Show all posts

Friday, December 30, 2016

Jal-Tarang, and a musical alarm clock in Python

By Vasudev Ram

Hi readers,

Season's greetings!

After you check out this video (Ranjana Pradhan playing the Jal Tarang in Sydney, 2006, read the rest of the post below ...



Here is a program that acts as a musical command-line alarm clock. It is an adaptation of the one I created here a while ago:

A simple alarm clock in Python (command-line)

This one plays a musical sound (using the playsound Python library) when the alarm time is reached, instead of beeping like the above one does. I had recently come across and used the playsound library in a Python course I conducted, so I thought of enhancing the earlier alarm clock app to use playsound. Playsound is a nice simple Python module with just one function, also called playsound, which can play either WAV or MP3 audio files on Windows.

The playsound library is described as "a pure Python, cross platform, single function module with no dependencies for playing sounds."

Excerpt from its PyPI page:

[ On Windows, uses windll.winmm. WAVE and MP3 have been tested and are known to work. Other file formats may work as well.

On OS X, uses AppKit.NSSound. WAVE and MP3 have been tested and are known to work. In general, anything QuickTime can play, playsound should be able to play, for OS X.

On Linux, uses ossaudiodev. I don’t have a machine with Linux, so this hasn’t been tested at all. Theoretically, it plays WAVE files. ]

Here is the code for the program, musical_alarm_clock.py:
from __future__ import print_function

'''
musical_alarm_clock.py

Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram

Description: A simple program to make the computer act like 
a musical alarm clock. Start it running from the command line 
with a command line argument specifying the number of minutes 
after which to give the alarm. It will wait for that long, and 
then play a musical sound a few times.
'''

import sys
import string
from playsound import playsound, PlaysoundException
from time import sleep, asctime

sa = sys.argv
lsa = len(sys.argv)
if lsa != 2:
    print("Usage: python {} duration_in_minutes.".format(sys.argv[0]))
    print("Example: python {} 10".format(sys.argv[0]))
    print("Use a value of 0 minutes for testing the alarm immediately.")
    print("The program plays a musical sound a few times after the duration is over.")
    sys.exit(1)

try:
    minutes = int(sa[1])
except ValueError:
    print("Invalid value {} for minutes.".format(sa[1]))
    print("Should be an integer >= 0.")
    sys.exit(1)

if minutes < 0:
    print("Invalid value {} for minutes.".format(minutes))
    print("Should be an integer >= 0.")
    sys.exit(1)

seconds = minutes * 60

if minutes == 1:
    unit_word = "minute"
else:
    unit_word = "minutes"

try:
    print("Current time is {}.".format(asctime()))
    if minutes > 0:
        print("Alarm set for {} {} later.".format(str(minutes), unit_word))
        sleep(seconds)
    else:
        print("Running in immediate test mode, with no delay.")
    print("Alarm time reached at {}.".format(asctime()))
    print("Wake up.")
    for i in range(5):
        playsound(r'c:\windows\media\chimes.wav')        
        #sleep(1.00)
        sleep(0.50)
        #sleep(0.25)
        #sleep(0.10)
except PlaysoundException as pe:
    print("Error: PlaysoundException: message: {}".format(pe))
    sys.exit(1)
except KeyboardInterrupt:
    print("Interrupted by user.")
    sys.exit(1)



Jal Tarang image attribution

The picture above is of a Jal Tarang.

The Jal Tarang is an ancient Indian melodic percussion instrument. Brief description adapted from the Wikipedia article: It consists of a set of bowls filled with water. The bowls can be of different sizes and contain different amounts of water. The instrument is tuned by adjusting the amount of water in each bowl. The music is played by striking the bowls with two sticks.

I've watched live jal-tarang performances only a few times as a kid (it was probably somewhat uncommon even then, and there are very few people who play it nowadays), so it was interesting to see this video and read the article.

Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Managed WordPress Hosting by FlyWheel



Thursday, November 7, 2013

A simple alarm clock in Python (command-line)

By Vasudev Ram



(Updated the post after a while, so it may show up twice in some feed readers (but with more info), sorry ...)

I had a need to set alarms for myself while working at the computer, to remind me to stop the current task and do some other task. Was using my smartphone's alarm clock app, but found that the volume was a bit too high, even at the lowest volume setting. So I thought of writing a simple alarm clock in Python as a command-line utility. Here it the code for it:
# alarm_clock.py

# Description: A simple Python program to make the computer act 
# like an alarm clock. Start it running from the command line 
# with a command line argument specifying the duration in minutes 
# after which to sound the alarm. It will sleep for that long, 
# and then beep a few times. Use a duration of 0 to test the 
# alarm immediiately, e.g. for checking that the volume is okay.

# Author: Vasudev Ram - http://www.dancingbison.com

import sys
import string
from time import sleep

sa = sys.argv
lsa = len(sys.argv)
if lsa != 2:
    print "Usage: [ python ] alarm_clock.py duration_in_minutes"
    print "Example: [ python ] alarm_clock.py 10"
    print "Use a value of 0 minutes for testing the alarm immediately."
    print "Beeps a few times after the duration is over."
    print "Press Ctrl-C to terminate the alarm clock early."
    sys.exit(1)

try:
    minutes = int(sa[1])
except ValueError:
    print "Invalid numeric value (%s) for minutes" % sa[1]
    print "Should be an integer >= 0"
    sys.exit(1)

if minutes < 0:
    print "Invalid value for minutes, should be >= 0"
    sys.exit(1)

seconds = minutes * 60

if minutes == 1:
    unit_word = " minute"
else:
    unit_word = " minutes"

try:
    if minutes > 0:
        print "Sleeping for " + str(minutes) + unit_word
        sleep(seconds)
    print "Wake up"
    for i in range(5):
        print chr(7),
        sleep(1)
except KeyboardInterrupt:
    print "Interrupted by user"
    sys.exit(1)

# EOF

Run it without any command line arguments to see how to use it:

C:> python alarm_clock.py

or

C:> alarm_clock.py

Use the first version of the command above, if your Python interpreter is not registered with the OS as the default application to run .py files, and the second version if it is registered. Usually the latter is the case, so you can omit the word python at the beginning of the command. The square brackets shown in the usage message (see the code) are not to be typed literally, they are there to indicate that the word within them is optional (a standard convention used on UNIX).

You can pass an argument of 0 for the duration, to test the alarm clock immediately, e.g. to see if the volume suits you or not. If not, increase or decrease the volume of your computer speakers.

I find this simple alarm clock program more convenient to use (when at my computer, of course) than either my smartphone's alarm clock app, or my traditional (physical) alarm clock like the one in the image above :-) (*)

I just have to keep one command window open, run that program in it, e.g.:

alarm_clock.py 30

to remind me to change tasks after 30 minutes. And when I want to set and run the alarm again, I just hit up arrow, change the time at the end of the command if needed, and hit Enter.

In fact, on my machine, I also created a batch file called alarm.bat, which accepts an argument and passes it to alarm_clock.py, so all I really have to type at the command line is:

alarm 30

(*) (Okay, I don't really have one of those old-fashioned alarm clocks; I have a modern plastic one. But I'd like to pick up one of those old-fashioned pocket watches like the one below, at some antique or second-hand shop some day. My grandfather had one of those ...

old-fashioned pocket watch

The tool uses the print chr(7) method to make the beeps. I did this to make it portable between Windows and Linux. It is possible to use the winsound module (only on Windows) for making the sound instead. See my earlier post about Python's winsound module: Try the Python WinSound API. It would also be possible to use other sound libraries instead, of course, on either Linux or Windows. For example, check out my earlier post about PyAudio (which also has a link to an earlier post about pyglet, another sound library option).

I'd also earlier written a digital clock (not an alarm clock, just a clock display) in 3 lines of Delphi code :-)

Of course, there are many variations and improvements possible on an alarm clock utility, such as having a GUI, making it look like a real alarm clock, and others. I may work on some of those at some point later. Meanwhile, this simple one works well enough for my current needs.


- Vasudev Ram - Dancing Bison Enterprises

Read other posts about Python on my blog.

Read other posts about utilities on my blog.

Contact me





O'Reilly 50% Ebook Deal of the Day