Hi all...

I am new to this list so treat me gently... ;o)

I use Python almost totally differently to the vast majority of people. I like "banging the metal".

As I know of no other way to give my Python code away I thought I`d join here.

I only use STANDARD Python, no extras...

Here are a couple of snippets.

and PULSE- signals at the speakers/earphone socket(s).

The second is a very simple DEMO which I will use in an attempt as an AudioScope using
the external mic socket(s)...

These are Public Domain and I hope they will be of interest to anyone.


Enjoy finding simple solutions to often very difficult problems.


1) The Function Generator...

# A fun Python program to generate a Sine, Square, Triangle, Sawtooth,

# and Pulse waveforms using STANDARD Python.

# This is for (PC)Linux(OS), (ONLY?), and was done purely for fun.

# (It now works on Debian 6.0.0 and Knoppix 5.1.1!)


# Although it is fairly easy to alter the frequency within limits I

# have left it at approximately 1KHz to keep the code size down...


# It would be tricky to change the output level using STANDARD Python

# for (PC)Linux(OS) 2009 using this idea to generate waveforms, but it

# is possible within limits.


# Original idea copyright, (C)2009, B.Walker, G0LCU.




# It is assumed that /dev/audio exists; if NOT, then install oss-compat

# from the distro`s repository.


# Ensure the sound system is not already in use.


# Copy the file to the Lib folder/drawer/directory where Python resides.

# For a quick way to run just use at the ">>>" prompt:-


# >>> import afg[RETURN/ENTER]


# And away we go...


# The waveforms generated are unfiltered and therefore not pure,

# but hey, a function generator, for free, without external hardware,

# AND using standard Python, what more do you want... :)

# Using my notebook about 150mV p-p was generated per channel at the

# earphone socket(s).


# 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...


# You will need an oscilloscope connected to the earphone socket(s)

# to see the resultant waveform(s) generated, or listen to the

# harshness of the sound... ;o)


# It is EASILY possible to generate pseudo-random noise also but

# I'll leave that for you to work out... :)

# Import any modules, ~sys~ is not rquired but added nevertheless.

import sys

import os

# 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 sine

global square

global triangle

global sawtoothplus

global sawtoothminus

global pulseplus

global pulseminus

global waveform

global select

global count

# Allocate values to variables.

# Any discrepancy between random soundcards may require small changes

# in the numeric values inside each waveform mode...

# These all oscillate at around 1KHz.








# This is the INITIAL default waveform, the Square Wave.




# A continuous loop to change modes as required...

while 1:

# Set up a basic user window.

print os.system("clear"),chr(13)," ",chr(13),


print "Simple Function Generator using STANDARD Python 2.5.2"

print "for PCLinuxOS 2009, issued as Public Domain to LXF..."


print "Original idea copyright, (C)2009, B.Walker, G0LCU."


print "1) Sinewave."

print "2) Squarewave."

print "3) Triangle."

print "4) Positive going sawtooth."

print "5) Negative going sawtooth."

print "6) Positive going pulse."

print "7) Negative going pulse."

print "Q) or q) to quit..."


# Enter a number for the mode required.

select=raw_input("Select a number/letter and press RETURN/ENTER:- ")

if select=="": select="2"

if len(select)!=1: break

if select=="Q": break

if select=="q": break

if select=="1": waveform=sine

if select=="2": waveform=square

if select=="3": waveform=triangle

if select=="4": waveform=sawtoothplus

if select=="5": waveform=sawtoothminus

if select=="6": waveform=pulseplus

if select=="7": waveform=pulseminus

# Re-use the variable ~select~ again...

if select<=chr(48): select="Default OR last"

if select>=chr(56): select="Default OR last"

if select=="1": select="Sine wave"

if select=="2": select="Square wave"

if select=="3": select="Triangle wave"

if select=="4": select="Positive going sawtooth"

if select=="5": select="Negative going sawtooth"

if select=="6": select="Positive going pulse"

if select=="7": select="Negative going pulse"

print os.system("clear"),chr(13)," ",chr(13),


print select+" audio waveform generation..."


# Open up the audio channel(s) to write directly to.

audio=file('/dev/audio', 'wb')

# Make the tone generation time finite in milliseconds...

# A count of 10000 is 10 seconds of tone burst...


while count<10000:

# Write the waveform to the audio device.



# Close the audio device when finished.



# End of demo...


2) Record and Playback using STANDARD Python 2.5.2 and above...

# DEMO code for recording a few seconds of sound and playing back the same
# from inside a terminal running Python 2.x in a Linux distro...
# This assumes /dev/audio exists, if NOT, then install oss-compat
# from your distro`s repository.
# Original idea copyright, (C)2010, B.Walker, G0LCU.
# Issued as Public Domain to LXF.
# You may do as you like with this idea but some acknowledgement
# would be appreciated.
# Save in the Python/Lib drawer(/folder/directory) as
# Use "import arp<RETURN/ENTER>" without the quotes to test it.
# Tested on PCLinuxOS 2009, Knoppix 5.1.1, (and Debian 6.0.0 <- WITH
# "oss-compat" installed).
# Ensure the sound system is not already in use.
# (I think this is Python 3.x compatible too.)
# These two imports NOT needed for this quick demo.
# import sys
# import os

def main():
global record
# Record from my Laptop`s, Notebook`s and Netbook`s mic.
# Note sample rate unknown at the moment, (8KHz?).
# Shout into the internal mic` for test purposes.
audio=file('/dev/audio', 'rb')
# Playback from the sound card(s).
audio=file('/dev/audio', 'wb')

# End of record/playback DEMO.


Bazza, G0LCU...

Team AMIGA...



Martin P. Hellwig

Hi all...

I am new to this list so treat me gently... ;o)

I for one welcome you :)
I use Python almost totally differently to the vast majority of people. I like "banging the metal".

Well I can assure you that although you might be indeed in a minority,
it is absolutely unlikely you are alone in that.
As I know of no other way to give my Python code away I thought I`d join here.

I'd would suggest you put it on a free repository like googlecode
( or my personal favourite bit bucket
( but there are many other out there too.
Coincidentally both I have mentioned support Mercurial HG, which is
largely written in Python too.

And just post now and again a status update with links to the repository.

Thanks for sharing and happy sharing!



Steven D'Aprano

As I know of no other way to give my Python code away I thought I`d join

It would be far more appropriate to *ask* where to put your code *first*
rather than to just dump 350+ lines of double-spaced(!) code into
people's inboxes, where it will likely be deleted and rapidly forgotten.

The standard place for putting Python packages and modules is on the
Python Package Index, PyPI:

For *small* snippets, say, a single function, you can use the ActiveState

A few random comments about your code:

# Original idea copyright, (C)2009, B.Walker, G0LCU.

You can't copyright ideas.

# >>> import afg[RETURN/ENTER]

I thought you said you use only "STANDARD Python"? What's afg? It doesn't
seem very standard to me:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named afg

# Import any modules, ~sys~ is not rquired but added nevertheless.
import sys

Don't do that. If you don't need a module, don't import it. *Especially*
don't import it only to say "Hey, you don't need this code, I'm just
wasting your time by making you read this!!!"

# The program proper...
def main():
# Make all variables global, a quirk of mine... :)

It's not 1970 any more. People will avoid like the plague code that over-
uses globals.

Also, there's no need to put code inside a "main" function that exists
only to populate a few global values. Instead of this:

def main():
global sine
sine = 'blah blah blah'


Just do this:

sine = 'blah blah blah'

See how much simpler and clearer it is?


This is much more easily and efficiently written as:

sine = ''.join([chr(n) for n in (15, 45, 63, 45, 15, 3, 0, 3)])

or even shorter, as a string constant:

sine = '\x0f-?-\x0f\x03\x00\x03'

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question