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

Simple White Noise Generator Using Standard Python In Linux - noise.py

This code is a stand alone program to generate a signal, at the earphone sockets, of white noise.

It needs /dev/dsp to work; if you haven't got it then install oss-compat from your distro's repository. (NOTE:- /dev/audio could also be used but I decided to use /dev/dsp to show that this was within easy reach of standard Python too.)

Ensure the audio system is NOT in use for this to work and all the levels are set up for your normal requirements. In my case root level WAS NOT required but that does not mean that root level IS NOT required - so be aware.

All that is required to make this a piece of audio test equipment is a cable plugged into to the earphone socket. The output level is fully controllable inside the code and the noise is generated in about 10 second bursts

Assuming it is copied into the module(s) drawer just type:-

>>> import noise[RETURN/ENTER]

And away you go...

This is Public Domain and you may do with it as you like.

Read the program for more information. (There will be more to come in the future... :)

Enjoy finding simple solutions to often very difficult problems... ;o)

73...

Bazza, G0LCU...

Team AMIGA...

Python, 108 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
# A stereo "White Noise" generator using STANDARD Python 2.5.2 or higher.
# This is for (PC)Linux(OS), (ONLY?), and was done purely for fun.
#
# It is another simple piece of testgear for the amateur electronics
# enthusiast and uses /dev/dsp instead of /dev/audio. Enjoy... ;o)
#
# (Original copyright, (C)2010, B.Walker, G0LCU.)
#
# DONATED TO LXF AS PUBLIC DOMAIN...
#
# Ensure the sound is enabled and the volume is turned up.
#
# Copy the file to the Lib folder/drawer/directory where Python resides,
# or where the modules reside, as "noise.py" without the quotes.
#
# Start the Python interpreter from a console/terminal window.
#
# For a quick way to run the noise generator just use at the ">>>" prompt:-
#
# >>> import noise[RETURN/ENTER]
#
# And away we go...
#
# This code is now Public Domain and you may do with it as you please...
#
# Coded on a(n) HP dual core notebook running PCLinuxOS 2009 and
# Python 2.5.2 for Linux; also tested on Knoppix 5.1.1 and Python 2.5.2
# and Debian 6.0.0 and Python 2.6.6...
#
# Connect an oscilloscope to the earphone socket(s) to see the noise
# waveform(s) being generated.

# Import any modules...
import os
import random

# Clear a terminal window ready to run this program.
print os.system("clear"),chr(13),"  ",chr(13),

# The program proper...
def main():
	# Make all variables global, a quirk of mine... :)
	global noise
	global value
	global select
	global count
	global amplitudestring
	global amplitude
	
	# The INITIAL default values.
	select="G0LCU."
	value=0
	noise=chr(value)
	count=0
	amplitudestring="64"
	amplitude=64
	
	# A continuous loop to re-generate noise as required...
	while 1:
		# Set up a basic user window.
		print os.system("clear"),chr(13),"  ",chr(13),
		print
		print "Simple Noise Generator using STANDARD Python 2.5.2"
		print "for PCLinuxOS 2009, issued as Public Domain to LXF."
		print
		print "(Original copyright, (C)2010, B.Walker, G0LCU.)"
		print
		# Set amplitude level from 0 to 64 unclusive.
		amplitudestring=raw_input("Enter amplitude level, 1 to 64:- ")
		# Don`t allow any typo error at all within limits...
		# On any typo error set amplitude to maximum.
		if amplitudestring=="": amplitudestring="64"
		if amplitudestring.isdigit()==0: amplitudestring="64"
		if len(amplitudestring)>=3: amplitudestring="64"
		# Now allocate the numerical value once the error chacking has been done.
		amplitude=int(amplitudestring)
		if amplitude<=1: amplitude=1
		if amplitude>=64: amplitude=64
		print
		# Select RETURN/ENTER for "White Noise", OR, any other key then RETURN/ENTER to Quit.
		select=raw_input("Press RETURN/ENTER for noise or any other key then RETURN/ENTER to Quit:- ")
		if select!="": break
		print os.system("clear"),chr(13),"  ",chr(13),
		print
		print "A 10 second white noise audio burst..."
		print
		print "Amplitude level",amplitude,"\b..."
		print
		# Change the random seed value per run.
		random.seed(None)
		# Open up the audio channel(s) to write directly to.
		# Note this DEMO uses /dev/dsp and NOT /dev/audo... ;o)
		audio=file('/dev/dsp','wb')
		# A count of 70000 is about 10 seconds of noise burst...
		count=0
		while count<70000:
			# Generate a random byte value.
			value=random.random()*amplitude
			noise=chr(int(value))
			# Write the character, (byte), "value" to the audio device.
			audio.write(noise)
			count=count+1
		# Close the audio device when finished.
		audio.close()
main()

# End of demo...
# Enjoy finding simple solutions to often very difficult problems...