# clsbeep.py
#
# Clear-screen and error beep module for various platforms.
# ---------------------------------------------------------
#
# File saved as "clsbeep.py" and placed into the Python - Lib drawer or
# where-ever the modules are located.
#
# Setting up a basic error beep and clear screen for Python 1.4 and greater.
# Original idea copyright, (C)2002-2014, B.Walker, G0LCU.
#
# Issued under the MIT licence.
#
# Tested on Python 1.4.x for a stock AMIGA 1200 and Python 2.0.x for WinUAE and E-UAE.
# Tested on Windows Vista, (Windows 7), from Python 2.0.1 to 3.3.2.
# Tested on Python 2.5.2 for PCLinuxOS 2009, Debian 6.0.0 from Python 2.5.6 to 3.2.2.
# Now added Apple OSX 10.7.x and higher from Python 2.5.6 to 3.4.1.
# All platforms in CLI/Command-Prompt/Terminal mode.
#
# ----------------------
# Usage in other files:-
# >>> import clsbeep[RETURN/ENTER]
# ----------------------
# Called as:-
# clsbeep.beep()
# clsbeep.cls()
# clsbeep.both()
#
# OR, as a generic format:-
# clsbeep.generic_beep()
# clsbeep.generic_cls()
# clsbeep.generic_both()
# ----------------------
# The ~if~ statement selects the correct format for the platform in use.
# ----------------------

# Import necessary modules for this to work.
import os
import sys

# This is the generic bell character.
def generic_beep():
	print(chr(7))

# A generic clear screen for ANY platform.
def generic_cls():
	# This moves the cursor 64 newlines to give a pseudo-clear screen
	# on terminal or CLI windows.
	# Note this is always done, irrespective of platform detection.
	for n in range(0,64,1):
		print("\r\n")

# Do both a generic beep and then clear the screen.
def generic_both():
	generic_beep()
	generic_cls()

# Generate a beep when called.
def beep():
	# A stock AMIGA 1200 using Python 1.4 or greater.
	# This assumes that the sound is enabled in the PREFS: drawer.
	# AND/OR the screen flash is enabled also.
	if sys.platform=="amiga":
		print("\a\v")

	# MS Windows (TM), from Windows ME upwards. Used in Command
	# Prompt mode for best effect.
	# The *.WAV file can be anything of your choice.
	# CHORD.WAV was the default.
	# SNDREC32.EXE no longer exists in WIndows Vista, and higher?
	if sys.platform=="win32":
		# os.system('SNDREC32.EXE "C:\WINDOWS\MEDIA\CHORD.WAV" /EMBEDDING /PLAY /CLOSE')
		# This uses the _internal_speaker_ as the sounder.
		print(chr(7))

	# A version for all Linux platforms.
	# For general console Python usage.
	if sys.platform=="linux2":
		error=os.system("which aplay > /dev/null 2>&1")
		if error==0:
			os.system("aplay beep.wav > /dev/null 2>&1")
		else:
			print(chr(7))

	# A version for Apple OSX 10.7.5 and above.
	# For general console Python usage.
	if sys.platform=="darwin":
		error=os.system("which afplay > /dev/null 2>&1")
		if error==0:
			os.system("afplay beep.wav > /dev/null 2>&1")
		else:
			print(chr(7))

	# Add here for other OSs.
	# Add here any peculiarities.
	# if sys.platform=="some-platform":
		# Do some sound error beep.

# Do a clear screen, with the limitations as shown.
def cls():
	# A stock AMIGA 1200 using Python 1.4 or greater.
	if sys.platform=="amiga":
		print("\f")

	# MS Windows (TM), from Windows ME upwards.
	# This is for the Command Prompt version ONLY both windowed AND/OR
	# screen modes.
	if sys.platform=="win32":
		n=os.system("CLS")

	# A generic version for Linux platforms.
	# For general console Python usage.
	if sys.platform=="linux2":
		n=os.system("clear")

	# A generic version for Apple OSX 10.7.x and above.
	# For general console Python usage.
	if sys.platform=="darwin":
		n=os.system("clear")

	# Add here for other OSs.
	# Peculiarities here.
	# if sys.platform=="some-platform":
		# Do some clear screen action...

# Do both if required.
def both():
	beep()
	cls()

# Generate a sinewave WAVE file for playback.
# The file 'beep.wav' is saved to the CURRENT drawer/folder/directory.
def sinewave_beep():
	header=[ 82, 73, 70, 70, 100, 31, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0, 64, 31, 0, 0, 64, 31, 0, 0, 1, 0, 8, 0, 100, 97, 116, 97, 64, 31, 0, 0 ]
	waveform=[ 79, 45, 32, 45, 79, 113, 126, 113 ]
	wavefile=open("beep.wav", "w+")
	for hdr in range(0, 44, 1):
		wavefile.write(chr(header[hdr]))
	for sample in range(0, 1000, 1):
		for wf in range(0, 8, 1):
			wavefile.write(chr(waveform[wf]))
	wavefile.close()
sinewave_beep()

# Module end...
# Enjoy finding simple solutions to often very difficult problems.