A small OSX centric app' to help a newborn sleep...

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) A small OSX centric app' to help a newborn sleep...
# 1  
Old 01-11-2017
A small OSX centric app' to help a newborn sleep...

shush.sh

This is a noise generator for newborn babies to listen to sooth them to sleep.
See more about it here:-

White noise machine - Wikipedia

It is invoked as:-
Usage:- [./]shush.sh <time in seconds from 18 to 2700> [sensitivity [Hh|Mm|Ll]]<CR>
'Sensitivity' defaults to [M]edium if omitted.

It has been developed on this MBP, OSX 10.12.2 and works in 'bash', 'dash' and 'sh' modes.

It has been tested on this machine and CygWin(64). As it is OSX centric I have been unable to fully test on Linux. CygWin(64) is SSLLOOWW.

When run it cycles approx 9 second noise bursts until the allocated time is reached, then, it cycles a recording mode to detect if baby is crying or not.
If baby wakes up and starts crying then the recording is read to see if it in the limits of the sensitivity selected.
If it is it then cycles the playback of a different noise sample for the run time bursts allocated.

To quit just press Ctrl-C whilst any noise burst is playing.

There are NO extra dependencies required only a virgin install of Linux, CygWin or OSX is needed.

I won't hold my breathe with the reliability of QuickTime Player as coding for it could be broken by any future OS change, but fingers crossed.
Code:
#!/bin/sh
# !/usr/local/bin/dash
# shush.sh
# Usage:- [./]shush.sh <time in seconds from 18 to 2700> [sensitivity [Hh|Mm|Ll]] <CR>

: > /tmp/noise.raw
: > /tmp/noise.wav
: > /tmp/wakeupcall.raw
: > /tmp/wakeupcall.wav
: > /tmp/Untitled.m4a

clear

echo ""
echo " A white noise generator to calm baby to sleep."
echo " Public Domain, 2017, (CC0 Licence), for www.unix.com by Barry Walker."
echo ""
echo " It WILL pause after the timer is finished to detect if baby is awake to re-run."
echo ""
echo " Usage:- [./]shush.sh <time in seconds from 18 to 2700> [sensitivity [Hh|Mm|Ll]]"
echo " The default sensitivity in [M] for Medium."
echo ""
echo " For more informaton about this effect:-"
echo " https://en.wikipedia.org/wiki/White_noise_machine "
echo ""
printf " Press Ctrl-C, whilst the noise is sounding, to stop! "

COUNT=1
DECIMAL=0
NAN="Licence CCO, 2017, Barry Walker"
LIMIT=$1
HML=$2
U_NAME=$( uname | awk '{print substr ($0, 0, 5)}' )
HIGH=192
LOW=64
ARRAY=""

# Error check!
case $LIMIT in
	''|*[!0-9]*)	LIMIT=18 ;;
esac
if [ "$LIMIT" = "" ] || [ "$LIMIT" -le 18 ] || [ "$LIMIT" -ge 2700 ]
then
	LIMIT=18
fi
LIMIT=$(( $LIMIT / 9 ))

# Sensitivity level, default is [M]edium and can be omitted.
if [ "$HML" = "H" ] || [ "$HML" = "h" ]
then
	HIGH=144
	LOW=112
fi
if [ "$HML" = "L" ] || [ "$HML" = "l" ]
then
	HIGH=240
	LOW=16
fi

# Use QuickTime Player for OSX 10.12.x, this may FAIL at any time in the future due to OSX _upgrades_.
QuickTime_Player()
{
# Set /tmp/Untitled.m4a file to full R/W capability inside this function.
echo "" > /tmp/Untitled.m4a
chmod 666 /tmp/Untitled.m4a
# This takes about 5 seconds per sample total and is for OSX 10.12.x, (and greater?)...
osascript << AppleSampler
	tell application "QuickTime Player"
		activate
		set savePath to "Macintosh HD:tmp:Untitled.m4a"
		set recording to new audio recording
		set visible of front window to false
		delay 2
		start recording
		delay 2
		stop recording
		export document "Untitled" in file savePath using settings preset "Audio Only"
		close (every document whose name contains "Untitled") saving no
		tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
		delay 1
		quit
	end tell
AppleSampler
}

# Listen out for baby crying.
wakeupcall()
{
	printf "0" > /tmp/wakeupcall.raw
	# Use /dev/dsp for GYGWIN.
	if [ "$U_NAME" = "CYGWI" ]
	then
		dd if=/dev/dsp of=/tmp/wakeupcall.raw bs=1 count=12000 > /dev/null 2>&1
		sleep 5
	fi
	# Use QuickTime Player for Darwin. (Working as of OSX 10.12.2.)
	if [ "$U_NAME" = "Darwi" ]
	then
		# This is _SLOW_ but requires NO dependencies.
		QuickTime_Player > /dev/null 2>&1
		afconvert -f 'WAVE' -c 1 -d UI8@8000 /tmp/Untitled.m4a /tmp/wakeupcall.wav
		dd if=/tmp/wakeupcall.wav of=/tmp/wakeupcall.raw skip=4096 bs=1 count=8000 > /dev/null 2>&1
	fi
	# Use either /dev/dsp OR arecord for Linux flavours.
	if [ "$U_NAME" = "Linux" ]
	then
		dd if=/dev/dsp of=/tmp/wakeupcall.raw bs=1 count=12000 > /dev/null 2>&1
		arecord -d 1 -c 1 -f U8 -r 8000 -t raw /tmp/wakeupcall.raw > /dev/null 2>&1
		sleep 5
	fi
	# NOTE: This is NOT an ARRAY but a space delimited series of decimal integers.
	export ARRAY="$( od -An -tu1 /tmp/wakeupcall.raw )"
	for DECIMAL in $ARRAY
	do
		NAN=""
		if [ $DECIMAL -le $LOW ] || [ $DECIMAL -ge $HIGH ]
		then
			NAN=$DECIMAL
			sleep 1
			break
		fi
	done
}

# Main loop.
while :
do
	COUNT=1

	# WAV header code for a different noise sound per run.
	printf "%b" "\122\111\106\106\044\000\001\000\127\101\126\105\146\155\164\040\020\000\000\000\001\000\001\000\100\037\000\000\100\037\000\000\001\000\010\000\144\141\164\141\000\000\001\000" > /tmp/noise.wav
	# Create the noise binary.
	dd if=/dev/urandom of=/tmp/noise.raw bs=1 count=65536 > /dev/null 2>&1
	# Generate a RAW and WAV file.
	cat /tmp/noise.raw >> /tmp/noise.wav

	while [ "$COUNT" -le "$LIMIT" ]
	do
		# CygWin /dev/dsp, Linux OSS or PulseAudio.
		> /dev/null 2>&1 cat /tmp/noise.raw > /dev/dsp
		# Linux ALSA.
		aplay /tmp/noise.wav > /dev/null 2>&1
		# Apple OSX 10.12.x and greater.
		afplay /tmp/noise.wav > /dev/null 2>&1
		COUNT=$(( $COUNT + 1 ))
	done
	while :
	do
		wakeupcall > /dev/null 2>&1
		if [ "$NAN" != "" ]
		then
			break
		fi
	done
done

This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Wuhan Coronavirus Status App for China - Rapid Prototype using MQTT and the IoT OnOff IOS App

With a little bit of work, was able to build a nice "Wuhan Coronavirus Status" app using MQTT and the IoT-OnOff app. More on this technique here: ESP32 (ESP-WROOM-32) as an MQTT Client Subscribed to Linux Server Load Average Messages The result turned out nice, I think. I like the look and... (10 Replies)
Discussion started by: Neo
10 Replies

2. OS X (Apple)

Osx terminal

hi all, first off thesis my first post so if i am not in the right forum, i apologize. i'm an absolute newbie to unix. i've been reading my books and studying my crib sheets etc. but... :/ i want to accomplish two things. 1. search and remove duplicate files i.e.. audio, doc alias etc.... (1 Reply)
Discussion started by: monkeyhateclean
1 Replies

3. OS X (Apple)

OSX 10.7 and rsh

Hi Guys What do i need to change so that anyone can rsh into the machine, i am sure it is in the pam.d/rshd buti am hving and issue wiith it, at the moment only someone who has a pysical account on the machine can rsh in even with all the users in the passwd file it does not work ... (2 Replies)
Discussion started by: ab52
2 Replies

4. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

5. OS X (Apple)

Optimizing OSX

Hi forum, I'm administrating a workstation/server for my lab and I was wondering how to optimize OSX. I was wondering what unnecessary background tasks I could kick off the system so I free up as much memory and cpu power. Other optimization tips are also welcome (HD parameters, memory... (2 Replies)
Discussion started by: deiphon
2 Replies

6. Programming

multithreading on OSX

Hi all, I have a query about multithreading. What I would like to do is, at the start of my main update() function, start a couple of threads in parallel, once they are all complete carry on with my main update function. void update() { thread1->update(); // fluid solver ... (3 Replies)
Discussion started by: memoid
3 Replies

7. UNIX Desktop Questions & Answers

Mac Osx.2

I finally broke down and decided to buy a new piece of hardware. I think I made the right decision when I chose an Apple iBook - OSX is incredible! I haven't used a Mac since System7.5, and 10.2 is just blowing me away! Best of all, it's easy to use for people who are not used to Mac, but if I... (5 Replies)
Discussion started by: LivinFree
5 Replies
Login or Register to Ask a Question