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

Aha, but not what big guns were expecting...

LF Audio Sweep Generator.

Another kids level project to do for yourselves...

This is a Python DEMO to show the power of the sound card using Linux to generate an Audio Sweep Signal from 4KHz down to 100Hz and back again.

Written in such a way that anyone can understand how it works... This is for Linux and Python 2.x.x. Read the code for much more information, and...... A Python 3.x.x version is here:-

http://www.linuxformat.com/forums/viewtopic.php?t=14411

Enjoy finding simple solutions to often VERY difficult problems...

Bazza, G0LCU...

Python, 111 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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
# SweepGen2x.py
#
# A DEMO Audio Sweep Generator from 4KHz down to 100Hz and back up again
# using standard Text Mode Python. Another kids level piece of simple, FREE,
# Test Gear project code...
# This working idea is copyright, (C)2010, B.Walker, G0LCU.
# Written in such a way that anyone can understand how it works.
#
# Tested on PCLinuxOS 2009 and Debian 6.0.0 using Python 2.6.2, 2.6.6 and 2.7.2.
# It may well work on much earlier versions of Python but it is untested...
# "/dev/dsp" IS required for this to work; therefore if you haven't got it then
# install "oss-compat" from you distro's repository. Ensure the sound system is
# not already in use.
# It is easily possible to lengthen the higher frequency playing times and VERY
# easily alter the output level and to speed up or slow down the sweep speed.
# I'll let the big guns do that for you...
# IMPORTANT NOTE:- Every EVEN number of characters is a symmetrical "square" wave
# BUT every ODD number of characters has preference for the "space" by one character.
#
# To run this DEMO type at the Python prompt......
#
# >>> execfile("/full/path/to/SweepGen2x.py")<RETURN/ENTER>
#
# ......and away you go.
#
# Note:- NO import[s] required at all, good eh! ;o)

def main():
	# Set all "variables" as globals, my choice... ;o)
	global mark
	global space
	global freq
	global stringlength
	global n
	global sweep

	# Allocate initial values.
	mark="\xff"
	space="\x00"
	freq=mark+space
	# 8KHz is the default sample speed of the sound system.
	# Therefore this sets the lowest frequency, 8KHz/80=100Hz...
	stringlength=80
	n=0
	sweep=0

	# A simple screen clear and user screen for a default Python window...
	for n in range(0,40,1):
		print "\r\n"
	print "Sweep Generator DEMO from 4KHz down to 100HZ and back again...\n"
	print "This just gives 5 SIREN like sweeps but it is enough for this DEMO...\n"
	print "Copyright, (C)2010, B.Walker, G0LCU.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

	# Open the audio device, "/dev/dsp" for writing.
	audio=open("/dev/dsp", "wb")

	# Sweep for five times only for this DEMO...
	while sweep<=4:

		freq=mark+space
		stringlength=80
		n=0

		while 1:
			# Sweep down from 4KHz to 100Hz.
			# Add a trailing 0x00 character.
			audio.write(freq)
			freq=freq+space
			# Quit when length of "freq" string is 80 characters.
			if len(freq)>=stringlength: break
			audio.write(freq)
			# Add a leading 0xff character.
			freq=mark+freq
			# Quit when length of "freq" string is 80 characters.
			if len(freq)>=stringlength: break
		while 1:
			# Sweep back up again from 100Hz to 4KHz.
			# Start with an empty string.
			freq=""
			# Now create a new square wave string.
			for n in range(0,int((stringlength)/2),1):
				freq=freq+mark
			for n in range(0,int((stringlength)/2),1):
				freq=freq+space
			audio.write(freq)
			# Create a new string reduced by one character.
			# This removes one 0xff character.
			stringlength=stringlength-1
			# Quit when length of "freq" string is 2 characters.
			if len(freq)<=2: break
			# Start with an empty string.
			freq=""
			# Now create a new string reduced by one character.
			for n in range(0,int((stringlength)/2),1):
				freq=freq+mark
			for n in range(0,int(((stringlength)/2)+1),1):
				freq=freq+space
			audio.write(freq)
			# This removes one 0x00 character.
			stringlength=stringlength-1
			# Quit when length of "freq" string is 2 characters.
			if len(freq)<=2: break
		sweep=sweep+1
		# Ensure a complete exit from the loop.
		if sweep>=5: break
	# On exit ensure the audio device is closed.
	audio.close()
main()

# End of SweepGen2x.py DEMO...
# Enjoy finding simple solutions to often VERY difficult problems... ;o)

This adds to my already large portfolio on here on how to get the most out of your sound_card inside a Linux OS flavour.

IMPORTANT NOTE:- I have pointed to Python 2.6.x as the minimum requirement but this may well work on much earlier versions except that I have been unable to test it. Good luck...

I have more kids level stuff to come.

Enjoy finding simple solutions to often VERY difficult problems...

Bazza, G0LCU...

1 comment

Barry Walker (author) 12 years, 2 months ago  # | flag

Whoever it was that voted this up on 04-01-2012 many thanks from me...

Bazza, G0LCU...