Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Vasudev Ram: Jal-Tarang, and a musical alarm clock in Python

$
0
0
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




Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>