Sponsored Content
Full Discussion: A metronome...
Operating Systems OS X (Apple) A metronome... Post 302946022 by wisecracker on Friday 5th of June 2015 05:28:09 PM
Old 06-05-2015
A metronome...

As you all know I hit the hardware and do strange things with the HW using shell scripting.

This is yet another one...

Well computer audio systems have two flaws, this code exploits one of them and the other I found with AudioScope.sh.
There are others so minor I will not pursue them any further.
Ignoring the second as it would not matter to the absolute vast majority of users I will decsribe the first.
All sound systems suffer with a click if the sound card is accessed by other SW.

As this occurs on EVERY PC I have used I decided to exploit it.

So I decided to test out this anomoly by creating a metronome.
In the case of this MBP the maximum beats per minute I could obtain was 180 so this became the upper limit.

The code is a fun one as I decided to make the click a tick and a tock... ;o)

The code is error proofed as far as is possible and any errors fix the BPM, (beats per minute), to 90 or 120.

It is called from anywhere you put it after changing the permissions to your requirements, as, './tick <BPM>[RETURN/ENTER]' without the quotes.

I don't think this has been done before...

Enjoy...
Code:
#!/bin/bash
# tick <bpm from 30 to 180>
# A simple metronome for kids...
# Issued as Public Domain at www.unix.com by B.Walker, G0LCU.
# MBP 13 inch, circa August 2012, OSX 10.7.5, default bash terminal.
clear
echo "Simple fun metronome for kids usage."
echo "Press Ctrl C to stop."
TIME=0.166
TIMER=0.000
BPM=$1
case $BPM in
	*[!0-9]*)
		BPM=120
		echo "User error, set the metronome to $BPM beats per minute..."
		;;
	*)
		BPM=$1
		;;
esac
if [ $BPM -lt 30 ] || [ $BPM -gt 180 ]
then
	echo "Usage: tick <bpm from 30 to 180>"
	BPM=90
	echo "User error, set the metronome to $BPM beats per minute..."
fi
# Generate the tick.wav and tock.wav files.
> /tmp/tick.wav
> /tmp/tock.wav
printf "\x52\x49\x46\x46\x2C\x00\x00\x00\x57\x41\x56\x45\x66\x6D\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1F\x00\x00\x40\x1F\x00\x00\x01\x00\x08\x00\x64\x61\x74\x61\x40\x1F\x00\x00" >> /tmp/tick.wav
cp /tmp/tick.wav /tmp/tock.wav 
printf "\x00\xFF\x00\xFF\x00\xFF\x00\xFF" >> /tmp/tick.wav
printf "\x00\x00\xFF\xFF\x00\x00\xFF\xFF" >> /tmp/tock.wav
# Playback the tick tock pair to set up the TIMER variable.
echo "Testing 'tick'..."
afplay /tmp/tick.wav
echo "Testing 'tock'..."
TIMER=( $(time (afplay /tmp/tock.wav) 2>&1 1>/dev/null) )
TIMER="${TIMER[1]:2:5}"
TIME=$(echo "scale=3;((60/$BPM)-$TIMER)" | bc)
tock()
{
	afplay /tmp/tick.wav
	sleep $TIME
	afplay /tmp/tock.wav
	sleep $TIME
}
echo "Now running the metronome at $BPM beats per minute..."
while true
do
	tock
done

This User Gave Thanks to wisecracker For This Post:
 
DRUMSTICK-METRONOME(1)						   User Commands					    DRUMSTICK-METRONOME(1)

NAME
drumstick-metronome - A Drumstick command line utility for playing a metronome pattern. SYNOPSIS
drumstick-metronome [options] {PORT} [BPM] DESCRIPTION
This program is a Drumstick example and utility program. You can use it to play a MIDI metronome pattern. ARGUMENTS
The following arguments are required: PORT An ALSA client:port specification that will be subscribed in order to send MIDI events to it. The client portion can be a number or a name, like in "20:0" or "KMidimon:0". The following arguments are optional: BPM Tempo in beats per minute. -h|--help Prints a summary of the command-line options and exit. -V|--version Prints the program version number and exit. LICENSE
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 or any later version published by the Free Software Foundation, considering as source code any files used for the production of this manpage. SEE ALSO
drumstick-dumpmid(1), drumstick-playsmf(1) AUTHOR
Pedro Lopez-Cabanillas <plcl@users.sf.net> COPYRIGHT
Copyright (C) 2010 Pedro Lopez-Cabanillas drumstick 0.5.0 May 13, 2010 DRUMSTICK-METRONOME(1)
All times are GMT -4. The time now is 01:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy