Welcome, guest | Sign In | My Account | Store | Cart

This DEMO code is a simple metronome for the Linux platform. It is for newcomers to playing musical instruments as a timing unit for practicing with.

It is issued as Public Domain and you may do with it as you please...

The device /dev/dsp IS required for this to work. If your machine lacks this then install oss-compat from you distro's repository.

It exploits a flaw in the default /dev/dsp device inside linux...

The part of the code the uses Ctrl-C to exit a loop has a flaw. This is not a bug. I will let the big guns explain what is happening if they want to in the comments section.

Enjoy finding simple solutions to often very difficult pronblems...

Bazza, G0LCU...

Python, 73 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Metronome3x.py
#
# DEMO simple metronome that exploits a minor flaw in the /dev/audio and /dev/dsp devices inside Linux.
# It can tick at around 30 to 400 beats per minute. This minimal code can be improved upon to give
# greater accuracy, range and appearance on screen if required.
#
# Original copyright, (C)2007-2012, B.Walker, G0LCU. Now issued as Public Domain and you may do with
# it as you please.
#
# There is a small flaw that uses the Ctrl-C part of the code. I'll let the big guns tell you users
# that can't find it what it is. It is not a bug as such, but it is a flaw.
#
# Tested on an HP Notebook with Debian 6.0.0 and Python 3.1.3 and an Acer Aspire One Netbook with
# PCLinuxOS 2009 and Python 3.2.1.
# To run just type:-
#
# >>> exec(open("/absolute/path/to/Metronome3x.py").read())<RETURN/ENTER>
#
# And away you go...
#
# $VER: Metronome3x.py_Version_0.00.10_(C)2007-2012_B.Walker_G0LCU.
#
# Enjoy finding simple solutions to often very difficult problems...

# The only import(s) for this DEMO...
import time
import os

def main():
	while 1:
		# the _variable_ listing...
		# "n" is throw away integer number and purposely reused.
		# "beatstring" is the inputted string and is also reused.
		# "beat" is the floating point number from about 0.x to 1.x generated from the inputted data.
		#
		# The standard Linux clear screen cmmand.
		n=os.system("clear")
		# Set up a basic user screen/window.
		print("\nPython 3.x.x simple metronome for the Linux platform.\n")
		print("(C)2007-2012, B.Walker, G0LCU. Issued as Public Domain.\n")
		beatstring=input("Enter any whole number from 30 to 400 (bpm), (QUIT or EXIT to Quit):- ")
		# Allow a means of quitting the DEMO.
		if beatstring=="QUIT" or beatstring=="EXIT": break
		# Don't allow any errors...
		if len(beatstring)>=4: beatstring="100"
		if len(beatstring)<=1: beatstring="100"
		n=0
		while n<=(len(beatstring)-1):
			if beatstring[n]>=chr(48) and beatstring[n]<=chr(57): n=n+1
			else: beatstring="100"
		n=int(beatstring)
		if n<=30: n=30
		if n>=400: n=400
		# Convert this integer "n" back to the "beatstring" string...
		beatstring=str(n)
		# Now convert to the floating point value for the time.sleep() function.
		beat=((60/n)-0.125)
		print("\nApproximate beats per minute = "+beatstring+"...\n")
		print("Press Ctrl-C to enter another speed...")
		while 1:
			# Write directly to the /dev/dsp device.
			try:
				audio=open("/dev/dsp", "wb")
				audio.write(b"\x00\xFF")
				audio.close()
				time.sleep(beat)
			# There is a flaw here, I'll let you big guns find it... ;o)
			# Note it is NOT really a bug!
			except KeyboardInterrupt: break
main()

# End of the Metronome3x.py code.
# Enjoy finding simple solutions to often very difficult problems...

This has only been tested on two machines, both aging or old and only two versions of Python 3.x.x.

To re-iterate, /dev/dsp IS required for it to work...

Revision 2 was to correct a major cut'n'paste error! ;o/

Sorry to those that have already downloaded.

Bazza...