Another Python freebie...

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Another Python freebie...

Postby Bazza » Fri Oct 01, 2010 5:02 pm

Hi all...

Another piece of test gear for the electronics enthusiast.

Well to add to the Audio Function Generator Python code
I gave to LXF some time ago here is another Python freebie
for LXF.

A "`White` Noise Generator" written in STANDARD Python 2.5.2
for PCLinuxOS 2009. It Works in Knoopix 5.1.1 and Python too.

Now experimenting with "Pink Noise", "Brown Noise" and
"Bandpass Noise" filtering using Python.

Enjoy finding simple solutions to often very difficult problems.

Watch for wordwrapping etc...

Code: Select all
# 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.
#
# Original idea 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
# 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 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...
#
# Connect an oscilloscope to the earphone socket(s) to see the noise
# waveform being generated.
#
# It's not perfect, but hey, a noise generator for free can't be bad eh!.

# Import any modules, ~sys~ is not rquired but added nevertheless.
import sys
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
    # The INITIAL default values.
    select="G0LCU."
    value=0
    noise=chr(value)
    count=0

    # 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 idea copyright, (C)2010, B.Walker, G0LCU."
        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=="": select="1"
        # Don`t allow an error and crash out with one.
        if len(select)!=1: break
        if select<=chr(48): break
        if select>=chr(50): break
        print os.system("clear"),chr(13),"  ",chr(13),
        print
        print "White noise audio generation..."
        print
        # Set the random seed value per run.
        random.seed(None)
        # Open up the audio channel(s) to write directly to.
        audio=file('/dev/audio', 'wb')
        # A count of 70000 is about 10 seconds of noise burst...
        count=0
        while count<70000:
            # Generate a random byte value 0 to 63.
            value=random.random()*64
            value=int(value)
            noise=chr(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...


hmmm, I wonder how easy it is to create a real time audio
scope using STANDARD Python for Linux...
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1483
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests